Spaces:
Running
Running
File size: 9,334 Bytes
1ae016f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 | {
"cells": [
{
"cell_type": "markdown",
"id": "671818be",
"metadata": {},
"source": [
"# Model Conversion or Compression \n",
"**This notebook demonstrates how to convert a PyTorch model to FP16 precision, which can reduce the model size and potentially speed up inference on compatible hardware. We will use the `FusionClassifier` as an example, but the same approach can be applied to other models as well.**\n",
"\n",
"**From FP32 to FP16**"
]
},
{
"cell_type": "code",
"execution_count": 7,
"id": "b1715593",
"metadata": {},
"outputs": [
{
"name": "stderr",
"output_type": "stream",
"text": [
"Loading weights: 100%|ββββββββββ| 342/342 [00:00<00:00, 2845.51it/s]\n",
"[transformers] \u001b[1mConvNextModel LOAD REPORT\u001b[0m from: facebook/convnext-small-224\n",
"Key | Status | | \n",
"------------------+------------+--+-\n",
"classifier.bias | UNEXPECTED | | \n",
"classifier.weight | UNEXPECTED | | \n",
"\n",
"Notes:\n",
"- UNEXPECTED:\tcan be ignored when loading from different task/architecture; not ok if you expect identical arch.\n"
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"============================================================\n",
"Initializing model...\n",
"============================================================\n",
"Model weights loaded successfully.\n",
"Model converted to FP16.\n",
"============================================================\n",
"FP16 model saved successfully.\n",
"Saved Path : D:\\DamageLens\\checkpoints\\best_fusion_model_fp16.pth\n",
"FP16 Model Size : 135.77 MB\n",
"============================================================\n"
]
}
],
"source": [
"import os\n",
"import torch\n",
"import torch.nn as nn\n",
"import torchvision.models as models\n",
"from transformers import ConvNextModel\n",
"\n",
"\n",
"# =========================================================\n",
"# FUSION MODEL\n",
"# =========================================================\n",
"\n",
"class FusionClassifier(nn.Module):\n",
" def __init__(self, num_classes, convnext_model_name=\"facebook/convnext-small-224\"):\n",
" super().__init__()\n",
"\n",
" # -------------------------------------------------\n",
" # EfficientNet-V2-S\n",
" # -------------------------------------------------\n",
" eff = models.efficientnet_v2_s(\n",
" weights=models.EfficientNet_V2_S_Weights.IMAGENET1K_V1\n",
" )\n",
"\n",
" # Freeze all\n",
" for param in eff.parameters():\n",
" param.requires_grad = False\n",
"\n",
" # Unfreeze last stages\n",
" for param in eff.features[5].parameters():\n",
" param.requires_grad = True\n",
"\n",
" for param in eff.features[6].parameters():\n",
" param.requires_grad = True\n",
"\n",
" for param in eff.features[7].parameters():\n",
" param.requires_grad = True\n",
"\n",
" self.eff_features = eff.features\n",
" self.eff_avgpool = eff.avgpool\n",
" self.eff_out_dim = eff.classifier[1].in_features # 1280\n",
"\n",
" # -------------------------------------------------\n",
" # ConvNeXt Small\n",
" # -------------------------------------------------\n",
" cnx = ConvNextModel.from_pretrained(convnext_model_name)\n",
"\n",
" # Freeze all\n",
" for param in cnx.parameters():\n",
" param.requires_grad = False\n",
"\n",
" # Unfreeze stages\n",
" for param in cnx.encoder.stages[2].parameters():\n",
" param.requires_grad = True\n",
"\n",
" for param in cnx.encoder.stages[3].parameters():\n",
" param.requires_grad = True\n",
"\n",
" for param in cnx.layernorm.parameters():\n",
" param.requires_grad = True\n",
"\n",
" self.cnx_backbone = cnx\n",
" self.cnx_out_dim = 768\n",
"\n",
" # -------------------------------------------------\n",
" # Fusion Head\n",
" # -------------------------------------------------\n",
" fused_dim = self.eff_out_dim + self.cnx_out_dim\n",
"\n",
" self.fusion_head = nn.Sequential(\n",
" nn.Dropout(0.4),\n",
"\n",
" nn.Linear(fused_dim, 512),\n",
" nn.LayerNorm(512),\n",
" nn.GELU(),\n",
"\n",
" nn.Dropout(0.3),\n",
"\n",
" nn.Linear(512, 256),\n",
" nn.LayerNorm(256),\n",
" nn.GELU(),\n",
"\n",
" nn.Dropout(0.2),\n",
"\n",
" nn.Linear(256, num_classes)\n",
" )\n",
"\n",
" def forward(self, pixel_values_eff, pixel_values_cnx):\n",
"\n",
" # EfficientNet branch\n",
" x_eff = self.eff_features(pixel_values_eff)\n",
" x_eff = self.eff_avgpool(x_eff)\n",
" x_eff = torch.flatten(x_eff, 1)\n",
"\n",
" # ConvNeXt branch\n",
" cnx_out = self.cnx_backbone(\n",
" pixel_values=pixel_values_cnx,\n",
" return_dict=True\n",
" )\n",
"\n",
" x_cnx = cnx_out.pooler_output\n",
"\n",
" # Fusion\n",
" fused = torch.cat([x_eff, x_cnx], dim=1)\n",
"\n",
" logits = self.fusion_head(fused)\n",
"\n",
" return logits\n",
"\n",
"\n",
"# =========================================================\n",
"# CONFIG\n",
"# =========================================================\n",
"\n",
"class_map = {\n",
" 0: \"Front Breakage\",\n",
" 1: \"Front Crushed\",\n",
" 2: \"Front Normal\",\n",
" 3: \"Rear Breakage\",\n",
" 4: \"Rear Crushed\",\n",
" 5: \"Rear Normal\"\n",
"}\n",
"\n",
"device = torch.device(\"cpu\")\n",
"\n",
"CHECKPOINT_PATH = r\"D:\\DamageLens\\checkpoints\\best_fusion_model.pt\"\n",
"\n",
"SAVE_FP16_PATH = r\"D:\\DamageLens\\checkpoints\\best_fusion_model_fp16.pth\"\n",
"\n",
"NUM_CLASSES = len(class_map)\n",
"\n",
"CONVNEXT_MODEL_NAME = \"facebook/convnext-small-224\"\n",
"\n",
"\n",
"# =========================================================\n",
"# INITIALIZE MODEL\n",
"# =========================================================\n",
"\n",
"model = FusionClassifier(\n",
" num_classes=NUM_CLASSES,\n",
" convnext_model_name=CONVNEXT_MODEL_NAME\n",
")\n",
"\n",
"print(\"=\" * 60)\n",
"print(\"Initializing model...\")\n",
"print(\"=\" * 60)\n",
"\n",
"\n",
"# =========================================================\n",
"# LOAD TRAINED WEIGHTS\n",
"# =========================================================\n",
"\n",
"checkpoint = torch.load(\n",
" CHECKPOINT_PATH,\n",
" map_location=device\n",
")\n",
"\n",
"# If checkpoint contains state_dict\n",
"if \"model_state_dict\" in checkpoint:\n",
" model.load_state_dict(checkpoint[\"model_state_dict\"])\n",
"\n",
"# If checkpoint is directly state_dict\n",
"else:\n",
" model.load_state_dict(checkpoint)\n",
"\n",
"print(\"Model weights loaded successfully.\")\n",
"\n",
"\n",
"# =========================================================\n",
"# CONVERT TO FP16\n",
"# =========================================================\n",
"\n",
"model = model.half()\n",
"\n",
"print(\"Model converted to FP16.\")\n",
"\n",
"\n",
"# =========================================================\n",
"# CREATE CHECKPOINT DIRECTORY\n",
"# =========================================================\n",
"\n",
"os.makedirs(\"checkpoints\", exist_ok=True)\n",
"\n",
"\n",
"# =========================================================\n",
"# SAVE FP16 MODEL\n",
"# =========================================================\n",
"\n",
"torch.save(\n",
" model.state_dict(),\n",
" SAVE_FP16_PATH\n",
")\n",
"\n",
"print(\"=\" * 60)\n",
"print(\"FP16 model saved successfully.\")\n",
"print(f\"Saved Path : {SAVE_FP16_PATH}\")\n",
"\n",
"size_mb = os.path.getsize(SAVE_FP16_PATH) / (1024 * 1024)\n",
"\n",
"print(f\"FP16 Model Size : {size_mb:.2f} MB\")\n",
"print(\"=\" * 60)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "myvenv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.11.0"
}
},
"nbformat": 4,
"nbformat_minor": 5
}
|