Loading a model with altered shape

username: Ryan_Zheng
cooked: <p>Hey guys!<br>
I have finetuned a <code>ssdlite320_mobilenet_v3_large</code> model in pytorch, however, I altered the shape to only classify to 6 classes instead of the default amount it came with. After training it, I saved my model to a pth file, but now I’m having trouble loading the model using the pth file using pytorch’s <code>load_state_dict</code> method because of incompatible shapes:</p>
<pre><code class="lang-auto">size mismatch for backbone.features.1.0.3.0.weight: copying a param with shape torch.Size([80, 672, 1, 1]) from checkpoint, the shape in current model is torch.Size([160, 672, 1, 1]).
        size mismatch for backbone.features.1.0.3.1.weight: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.0.3.1.bias: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.0.3.1.running_mean: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.0.3.1.running_var: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.1.block.0.0.weight: copying a param with shape torch.Size([480, 80, 1, 1]) from checkpoint, the shape in current model is torch.Size([960, 160, 1, 1]).
        size mismatch for backbone.features.1.1.block.0.1.weight: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.0.1.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.0.1.running_mean: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.0.1.running_var: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.1.0.weight: copying a param with shape torch.Size([480, 1, 5, 5]) from checkpoint, the shape in current model is torch.Size([960, 1, 5, 5]).
        size mismatch for backbone.features.1.1.block.1.1.weight: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.1.1.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.1.1.running_mean: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.1.1.running_var: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.2.fc1.weight: copying a param with shape torch.Size([120, 480, 1, 1]) from checkpoint, the shape in current model is torch.Size([240, 960, 1, 1]).
        size mismatch for backbone.features.1.1.block.2.fc1.bias: copying a param with shape torch.Size([120]) from checkpoint, the shape in current model is torch.Size([240]).
        size mismatch for backbone.features.1.1.block.2.fc2.weight: copying a param with shape torch.Size([480, 120, 1, 1]) from checkpoint, the shape in current model is torch.Size([960, 240, 1, 1]).
        size mismatch for backbone.features.1.1.block.2.fc2.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.1.block.3.0.weight: copying a param with shape torch.Size([80, 480, 1, 1]) from checkpoint, the shape in current model is torch.Size([160, 960, 1, 1]).
        size mismatch for backbone.features.1.1.block.3.1.weight: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.1.block.3.1.bias: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.1.block.3.1.running_mean: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.1.block.3.1.running_var: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.2.block.0.0.weight: copying a param with shape torch.Size([480, 80, 1, 1]) from checkpoint, the shape in current model is torch.Size([960, 160, 1, 1]).
        size mismatch for backbone.features.1.2.block.0.1.weight: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.0.1.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.0.1.running_mean: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.0.1.running_var: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.1.0.weight: copying a param with shape torch.Size([480, 1, 5, 5]) from checkpoint, the shape in current model is torch.Size([960, 1, 5, 5]).
        size mismatch for backbone.features.1.2.block.1.1.weight: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.1.1.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.1.1.running_mean: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.1.1.running_var: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.2.fc1.weight: copying a param with shape torch.Size([120, 480, 1, 1]) from checkpoint, the shape in current model is torch.Size([240, 960, 1, 1]).
        size mismatch for backbone.features.1.2.block.2.fc1.bias: copying a param with shape torch.Size([120]) from checkpoint, the shape in current model is torch.Size([240]).
        size mismatch for backbone.features.1.2.block.2.fc2.weight: copying a param with shape torch.Size([480, 120, 1, 1]) from checkpoint, the shape in current model is torch.Size([960, 240, 1, 1]).
        size mismatch for backbone.features.1.2.block.2.fc2.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.2.block.3.0.weight: copying a param with shape torch.Size([80, 480, 1, 1]) from checkpoint, the shape in current model is torch.Size([160, 960, 1, 1]).
        size mismatch for backbone.features.1.2.block.3.1.weight: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.2.block.3.1.bias: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.2.block.3.1.running_mean: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.2.block.3.1.running_var: copying a param with shape torch.Size([80]) from checkpoint, the shape in current model is torch.Size([160]).
        size mismatch for backbone.features.1.3.0.weight: copying a param with shape torch.Size([480, 80, 1, 1]) from checkpoint, the shape in current model is torch.Size([960, 160, 1, 1]).
        size mismatch for backbone.features.1.3.1.weight: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.3.1.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.3.1.running_mean: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.features.1.3.1.running_var: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for backbone.extra.0.0.0.weight: copying a param with shape torch.Size([256, 480, 1, 1]) from checkpoint, the shape in current model is torch.Size([256, 960, 1, 1]).
        size mismatch for head.classification_head.module_list.0.1.weight: copying a param with shape torch.Size([12, 672, 1, 1]) from checkpoint, the shape in current model is torch.Size([42, 672, 1, 1]).
        size mismatch for head.classification_head.module_list.0.1.bias: copying a param with shape torch.Size([12]) from checkpoint, the shape in current model is torch.Size([42]).
        size mismatch for head.classification_head.module_list.1.0.0.weight: copying a param with shape torch.Size([480, 1, 3, 3]) from checkpoint, the shape in current model is torch.Size([960, 1, 3, 3]).
        size mismatch for head.classification_head.module_list.1.0.1.weight: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for head.classification_head.module_list.1.0.1.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for head.classification_head.module_list.1.0.1.running_mean: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for head.classification_head.module_list.1.0.1.running_var: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for head.classification_head.module_list.1.1.weight: copying a param with shape torch.Size([12, 480, 1, 1]) from checkpoint, the shape in current model is torch.Size([42, 960, 1, 1]).
        size mismatch for head.classification_head.module_list.1.1.bias: copying a param with shape torch.Size([12]) from checkpoint, the shape in current model is torch.Size([42]).
        size mismatch for head.classification_head.module_list.2.1.weight: copying a param with shape torch.Size([12, 512, 1, 1]) from checkpoint, the shape in current model is torch.Size([42, 512, 1, 1]).
        size mismatch for head.classification_head.module_list.2.1.bias: copying a param with shape torch.Size([12]) from checkpoint, the shape in current model is torch.Size([42]).
        size mismatch for head.classification_head.module_list.3.1.weight: copying a param with shape torch.Size([12, 256, 1, 1]) from checkpoint, the shape in current model is torch.Size([42, 256, 1, 1]).
        size mismatch for head.classification_head.module_list.3.1.bias: copying a param with shape torch.Size([12]) from checkpoint, the shape in current model is torch.Size([42]).
        size mismatch for head.classification_head.module_list.4.1.weight: copying a param with shape torch.Size([12, 256, 1, 1]) from checkpoint, the shape in current model is torch.Size([42, 256, 1, 1]).
        size mismatch for head.classification_head.module_list.4.1.bias: copying a param with shape torch.Size([12]) from checkpoint, the shape in current model is torch.Size([42]).
        size mismatch for head.classification_head.module_list.5.1.weight: copying a param with shape torch.Size([12, 128, 1, 1]) from checkpoint, the shape in current model is torch.Size([42, 128, 1, 1]).
        size mismatch for head.classification_head.module_list.5.1.bias: copying a param with shape torch.Size([12]) from checkpoint, the shape in current model is torch.Size([42]).
        size mismatch for head.regression_head.module_list.1.0.0.weight: copying a param with shape torch.Size([480, 1, 3, 3]) from checkpoint, the shape in current model is torch.Size([960, 1, 3, 3]).
        size mismatch for head.regression_head.module_list.1.0.1.weight: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for head.regression_head.module_list.1.0.1.bias: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for head.regression_head.module_list.1.0.1.running_mean: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for head.regression_head.module_list.1.0.1.running_var: copying a param with shape torch.Size([480]) from checkpoint, the shape in current model is torch.Size([960]).
        size mismatch for head.regression_head.module_list.1.1.weight: copying a param with shape torch.Size([24, 480, 1, 1]) from checkpoint, the shape in current model is torch.Size([24, 960, 1, 1]).
</code></pre>
<p>I’m not really sure how to approach this as I didn’t find anything online to go off of, and I was wondering if anyone could help.<br>
Thank you!</p>

username: Soumya_Kundu
cooked: <p>You could try removing the classification head and load only those, but I’m not sure how changing the final classification makes changes to the entire model. That shouldn’t be the case.</p>
<p>Something similar to what’s done in this tutorial: <a href="https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html" class="inline-onebox" rel="noopener nofollow ugc">Transfer Learning for Computer Vision Tutorial — PyTorch Tutorials 2.4.0+cu124 documentation</a></p>

username: kutayozdur
cooked: <p>This is a pretty late answer but it can help other people.<br>
Try to load the model like this:</p>
<pre><code class="lang-auto">from torchvision.models.detection import SSDLite320_MobileNet_V3_Large_Weights

model = ssdlite320_mobilenet_v3_large(weights=SSDLite320_MobileNet_V3_Large_Weights.COCO_V1)
</code></pre>
<p>And then you can load your weights using <code>load_state_dict</code> method.</p>