JKL0909 commited on
Commit
72527e8
·
1 Parent(s): 5938607

commit first

Browse files
Code.ipynb CHANGED
@@ -1736,11 +1736,123 @@
1736
  },
1737
  {
1738
  "cell_type": "code",
1739
- "execution_count": null,
1740
  "id": "a5f51335",
1741
  "metadata": {},
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1742
  "outputs": [],
1743
- "source": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1744
  }
1745
  ],
1746
  "metadata": {
 
1736
  },
1737
  {
1738
  "cell_type": "code",
1739
+ "execution_count": 1,
1740
  "id": "a5f51335",
1741
  "metadata": {},
1742
+ "outputs": [
1743
+ {
1744
+ "name": "stderr",
1745
+ "output_type": "stream",
1746
+ "text": [
1747
+ "/home/ubuntu/.local/lib/python3.12/site-packages/torchvision/models/_utils.py:208: UserWarning: The parameter 'pretrained' is deprecated since 0.13 and may be removed in the future, please use 'weights' instead.\n",
1748
+ " warnings.warn(\n",
1749
+ "/home/ubuntu/.local/lib/python3.12/site-packages/torchvision/models/_utils.py:223: UserWarning: Arguments other than a weight enum or `None` for 'weights' are deprecated since 0.13 and may be removed in the future. The current behavior is equivalent to passing `weights=DenseNet169_Weights.IMAGENET1K_V1`. You can also use `weights=DenseNet169_Weights.DEFAULT` to get the most up-to-date weights.\n",
1750
+ " warnings.warn(msg)\n"
1751
+ ]
1752
+ },
1753
+ {
1754
+ "name": "stdout",
1755
+ "output_type": "stream",
1756
+ "text": [
1757
+ "Mô hình đã được chuyển đổi thành công sang ONNX và lưu tại: /home/ubuntu/vnet/TaoST/Model3/melanoma_classifier_densenet169.onnx\n",
1758
+ "Mô hình ONNX đã được kiểm tra và xác nhận hợp lệ.\n"
1759
+ ]
1760
+ }
1761
+ ],
1762
+ "source": [
1763
+ "import torch\n",
1764
+ "import torch.nn as nn\n",
1765
+ "from torchvision import models\n",
1766
+ "\n",
1767
+ "# 1. Định nghĩa lại kiến trúc mô hình (giống hệt khi training)\n",
1768
+ "def create_model():\n",
1769
+ " # Tải mô hình pretrained DenseNet-169\n",
1770
+ " model = models.densenet169(pretrained=True)\n",
1771
+ " \n",
1772
+ " # Đóng băng tất cả các lớp để bắt đầu\n",
1773
+ " for param in model.parameters():\n",
1774
+ " param.requires_grad = False\n",
1775
+ " \n",
1776
+ " # Thay thế lớp phân loại\n",
1777
+ " # DenseNet-169 có 1664 đặc trưng đầu ra ở lớp cuối cùng\n",
1778
+ " num_ftrs = model.classifier.in_features\n",
1779
+ " \n",
1780
+ " # Thay thế lớp phân loại của DenseNet với một lớp mới có 1 đầu ra\n",
1781
+ " # Sigmoid sẽ được áp dụng trong BCEWithLogitsLoss\n",
1782
+ " model.classifier = nn.Linear(num_ftrs, 1)\n",
1783
+ " \n",
1784
+ " return model\n",
1785
+ "\n",
1786
+ "# 2. Khởi tạo mô hình và tải trọng số đã lưu\n",
1787
+ "device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n",
1788
+ "model = create_model()\n",
1789
+ "\n",
1790
+ "# Tải trọng số từ file .pth\n",
1791
+ "model_path = \"/home/ubuntu/vnet/TaoST/Model3/melanoma_classifier_densenet169.pth\"\n",
1792
+ "model.load_state_dict(torch.load(model_path, map_location=device))\n",
1793
+ "model.to(device)\n",
1794
+ "model.eval() # Chuyển sang chế độ inference\n",
1795
+ "\n",
1796
+ "# 3. Tạo dummy input với kích thước phù hợp (batch_size, channels, height, width)\n",
1797
+ "dummy_input = torch.randn(1, 3, 224, 224).to(device)\n",
1798
+ "\n",
1799
+ "# 4. Xuất sang ONNX\n",
1800
+ "onnx_path = \"/home/ubuntu/vnet/TaoST/Model3/melanoma_classifier_densenet169.onnx\"\n",
1801
+ "torch.onnx.export(\n",
1802
+ " model, # Mô hình cần xuất\n",
1803
+ " dummy_input, # Input đầu vào mẫu\n",
1804
+ " onnx_path, # Đường dẫn tới file ONNX đầu ra\n",
1805
+ " export_params=True, # Lưu các tham số của mô hình\n",
1806
+ " opset_version=12, # Phiên bản ONNX opset\n",
1807
+ " do_constant_folding=True, # Tối ưu hóa các hằng số\n",
1808
+ " input_names=['input'], # Tên của input\n",
1809
+ " output_names=['output'], # Tên của output\n",
1810
+ " dynamic_axes={ # Kích thước động (batch size có thể thay đổi)\n",
1811
+ " 'input': {0: 'batch_size'},\n",
1812
+ " 'output': {0: 'batch_size'}\n",
1813
+ " }\n",
1814
+ ")\n",
1815
+ "\n",
1816
+ "print(f\"Mô hình đã được chuyển đổi thành công sang ONNX và lưu tại: {onnx_path}\")\n",
1817
+ "\n",
1818
+ "# 5. Kiểm tra xem mô hình ONNX có thể được tải và sử dụng không (tùy chọn)\n",
1819
+ "try:\n",
1820
+ " import onnx\n",
1821
+ " \n",
1822
+ " # Tải mô hình ONNX\n",
1823
+ " onnx_model = onnx.load(onnx_path)\n",
1824
+ " \n",
1825
+ " # Kiểm tra mô hình ONNX\n",
1826
+ " onnx.checker.check_model(onnx_model)\n",
1827
+ " \n",
1828
+ " print(\"Mô hình ONNX đã được kiểm tra và xác nhận hợp lệ.\")\n",
1829
+ "except ImportError:\n",
1830
+ " print(\"Thư viện ONNX không được cài đặt. Bỏ qua bước kiểm tra.\")\n",
1831
+ "except Exception as e:\n",
1832
+ " print(f\"Lỗi khi kiểm tra mô hình ONNX: {e}\")"
1833
+ ]
1834
+ },
1835
+ {
1836
+ "cell_type": "code",
1837
+ "execution_count": null,
1838
+ "id": "fed15a92",
1839
+ "metadata": {},
1840
  "outputs": [],
1841
+ "source": [
1842
+ "import zipfile\n",
1843
+ "\n",
1844
+ "# Đường dẫn đến file ipynb\n",
1845
+ "ipynb_file = \"/home/ubuntu/vnet/TaoST/Model3/Code.ipynb\"\n",
1846
+ "\n",
1847
+ "# Đường dẫn file zip đầu ra\n",
1848
+ "zip_file = \"/home/ubuntu/vnet/TaoST/Model3/Code.zip\"\n",
1849
+ "\n",
1850
+ "# Tạo file zip\n",
1851
+ "with zipfile.ZipFile(zip_file, 'w') as zipf:\n",
1852
+ " zipf.write(ipynb_file, arcname=\"Code.ipynb\") # arcname để đặt tên file trong zip\n",
1853
+ "\n",
1854
+ "print(f\"Đã nén file {ipynb_file} thành {zip_file}\")"
1855
+ ]
1856
  }
1857
  ],
1858
  "metadata": {
melanoma_classifier_densenet169.onnx → Model2.onnx RENAMED
File without changes
melanoma_classifier_densenet169.pth → Model2.pth RENAMED
File without changes