<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic change pre-trained Model for fine-tuning does not work in Developers Questions</title>
    <link>https://community.esri.com/t5/developers-questions/change-pre-trained-model-for-fine-tuning-does-not/m-p/1327627#M6734</link>
    <description>&lt;P&gt;Hello,&lt;BR /&gt;I would like to use the following pre-trained model for a fine-tuning.&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.esri.com/arcgis-blog/products/arcgis-pro/imagery/deep-learning-with-arcgis-pro-tips-tricks-part-2/" target="_blank" rel="noopener"&gt;https://www.esri.com/arcgis-blog/products/arcgis-pro/imagery/deep-learning-with-arcgis-pro-tips-tricks-part-2/&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the model was trained on RGB aerial images and I have RGBI aerial images.&lt;BR /&gt;Trying to use this model as a basis causes the following error:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;RuntimeError&lt;/SPAN&gt;: Error(s) in loading state_dict for DynamicUnet:
	size mismatch for layers.0.0.weight: copying a param with shape torch.Size([64, 3, 7, 7]) from checkpoint, the shape in current model is torch.Size([64, 4, 7, 7]).
	size mismatch for layers.10.layers.0.0.weight: copying a param with shape torch.Size([49, 99, 3, 3]) from checkpoint, the shape in current model is torch.Size([50, 100, 3, 3]).
	size mismatch for layers.10.layers.0.0.bias: copying a param with shape torch.Size([49]) from checkpoint, the shape in current model is torch.Size([50]).
	size mismatch for layers.10.layers.1.0.weight: copying a param with shape torch.Size([99, 49, 3, 3]) from checkpoint, the shape in current model is torch.Size([100, 50, 3, 3]).
	size mismatch for layers.10.layers.1.0.bias: copying a param with shape torch.Size([99]) from checkpoint, the shape in current model is torch.Size([100]).
	size mismatch for layers.11.0.weight: copying a param with shape torch.Size([9, 99, 1, 1]) from checkpoint, the shape in current model is torch.Size([3, 100, 1, 1]).
	size mismatch for layers.11.0.bias: copying a param with shape torch.Size([9]) from checkpoint, the shape in current model is torch.Size([3]).&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only way to solve this problem was to modify the first layer as follows:&lt;/P&gt;&lt;P&gt;class CustomUNet(nn.Module):&lt;BR /&gt;def __init__(self):&lt;BR /&gt;super(CustomUNet, self).__init__()&lt;BR /&gt;# Change first Layer from three to four input channels&lt;BR /&gt;self.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)&lt;BR /&gt;# add the other layers from the pre-trained model&lt;BR /&gt;self.pretrained = unet_pretrained&lt;/P&gt;&lt;P&gt;def forward(self, x):&lt;BR /&gt;x = self.conv1(x)&lt;BR /&gt;x = self.pretrained(x)&lt;BR /&gt;return x&lt;/P&gt;&lt;P&gt;custom_UNet = CustomUNet()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I have not been able to convert the model back to a UnetClassifier afterwards.&lt;/P&gt;&lt;P&gt;Can anyone help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Tue, 12 Sep 2023 09:20:02 GMT</pubDate>
    <dc:creator>ManuelNiemeyer</dc:creator>
    <dc:date>2023-09-12T09:20:02Z</dc:date>
    <item>
      <title>change pre-trained Model for fine-tuning does not work</title>
      <link>https://community.esri.com/t5/developers-questions/change-pre-trained-model-for-fine-tuning-does-not/m-p/1327627#M6734</link>
      <description>&lt;P&gt;Hello,&lt;BR /&gt;I would like to use the following pre-trained model for a fine-tuning.&lt;/P&gt;&lt;P&gt;&lt;A href="https://www.esri.com/arcgis-blog/products/arcgis-pro/imagery/deep-learning-with-arcgis-pro-tips-tricks-part-2/" target="_blank" rel="noopener"&gt;https://www.esri.com/arcgis-blog/products/arcgis-pro/imagery/deep-learning-with-arcgis-pro-tips-tricks-part-2/&lt;/A&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, the model was trained on RGB aerial images and I have RGBI aerial images.&lt;BR /&gt;Trying to use this model as a basis causes the following error:&lt;/P&gt;&lt;DIV class=""&gt;&amp;nbsp;&lt;/DIV&gt;&lt;PRE&gt;&lt;SPAN class=""&gt;RuntimeError&lt;/SPAN&gt;: Error(s) in loading state_dict for DynamicUnet:
	size mismatch for layers.0.0.weight: copying a param with shape torch.Size([64, 3, 7, 7]) from checkpoint, the shape in current model is torch.Size([64, 4, 7, 7]).
	size mismatch for layers.10.layers.0.0.weight: copying a param with shape torch.Size([49, 99, 3, 3]) from checkpoint, the shape in current model is torch.Size([50, 100, 3, 3]).
	size mismatch for layers.10.layers.0.0.bias: copying a param with shape torch.Size([49]) from checkpoint, the shape in current model is torch.Size([50]).
	size mismatch for layers.10.layers.1.0.weight: copying a param with shape torch.Size([99, 49, 3, 3]) from checkpoint, the shape in current model is torch.Size([100, 50, 3, 3]).
	size mismatch for layers.10.layers.1.0.bias: copying a param with shape torch.Size([99]) from checkpoint, the shape in current model is torch.Size([100]).
	size mismatch for layers.11.0.weight: copying a param with shape torch.Size([9, 99, 1, 1]) from checkpoint, the shape in current model is torch.Size([3, 100, 1, 1]).
	size mismatch for layers.11.0.bias: copying a param with shape torch.Size([9]) from checkpoint, the shape in current model is torch.Size([3]).&lt;/PRE&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;The only way to solve this problem was to modify the first layer as follows:&lt;/P&gt;&lt;P&gt;class CustomUNet(nn.Module):&lt;BR /&gt;def __init__(self):&lt;BR /&gt;super(CustomUNet, self).__init__()&lt;BR /&gt;# Change first Layer from three to four input channels&lt;BR /&gt;self.conv1 = nn.Conv2d(4, 64, kernel_size=7, stride=2, padding=3, bias=False)&lt;BR /&gt;# add the other layers from the pre-trained model&lt;BR /&gt;self.pretrained = unet_pretrained&lt;/P&gt;&lt;P&gt;def forward(self, x):&lt;BR /&gt;x = self.conv1(x)&lt;BR /&gt;x = self.pretrained(x)&lt;BR /&gt;return x&lt;/P&gt;&lt;P&gt;custom_UNet = CustomUNet()&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;However, I have not been able to convert the model back to a UnetClassifier afterwards.&lt;/P&gt;&lt;P&gt;Can anyone help me?&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Tue, 12 Sep 2023 09:20:02 GMT</pubDate>
      <guid>https://community.esri.com/t5/developers-questions/change-pre-trained-model-for-fine-tuning-does-not/m-p/1327627#M6734</guid>
      <dc:creator>ManuelNiemeyer</dc:creator>
      <dc:date>2023-09-12T09:20:02Z</dc:date>
    </item>
  </channel>
</rss>

