Changing the first Conv layer of a resnet34

username: Harsimar_Singh
cooked: <p>Hi experts and community folks, here I am trying to do some hack using list.children()</p>
<p>import fastai<br>
from torchsummary import summary<br>
from fastai.vision.models.unet import *<br>
from fastai.callbacks import *<br>
from fastai.utils.mem import *</p>
<p>arch = torchvision.models.resnet34(pretrained=False)</p>
<p>li = list(model.children()) <span class="hashtag">#list</span> of the children</p>
<p>print( li[0][0][0] ) -&gt; gives Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)</p>
<p>So I do is li[0][0][0] = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)</p>
<p>So here my input will be one channel which I want as the input to the resnet.</p>
<p>summary(model.to(‘cuda’),input_size=(1, 320, 320)) -&gt; Here I have this error</p>
<hr>
<p>NotImplementedError                       Traceback (most recent call last)<br>
 in <br>
----&gt; 1 summary(model.to(‘cuda’),input_size=(1, 320, 320))</p>
<p>~/anaconda3/envs/harsimar/lib/python3.6/site-packages/torchsummary/torchsummary.py in summary(model, input_size, batch_size, device)<br>
70     # make a forward pass<br>
71     # print(x.shape)<br>
—&gt; 72     model(*x)<br>
73<br>
74     # remove these hooks</p>
<p>~/anaconda3/envs/harsimar/lib/python3.6/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>~/anaconda3/envs/harsimar/lib/python3.6/site-packages/torch/nn/modules/container.py in forward(self, input)<br>
98     def forward(self, input):<br>
99         for module in self:<br>
–&gt; 100             input = module(input)<br>
101         return input<br>
102</p>
<p>~/anaconda3/envs/harsimar/lib/python3.6/site-packages/torch/nn/modules/module.py in <strong>call</strong>(self, *input, **kwargs)<br>
530             result = self._slow_forward(*input, **kwargs)<br>
531         else:<br>
–&gt; 532             result = self.forward(*input, **kwargs)<br>
533         for hook in self._forward_hooks.values():<br>
534             hook_result = hook(self, input, result)</p>
<p>~/anaconda3/envs/harsimar/lib/python3.6/site-packages/torch/nn/modules/module.py in forward(self, *input)<br>
94             registered hooks while the latter silently ignores them.<br>
95         “”"<br>
—&gt; 96         raise NotImplementedError<br>
97<br>
98     def register_buffer(self, name, tensor):</p>
<p>NotImplementedError:</p>
<p>Any views on this?</p>

username: ptrblck
cooked: <p>If you want to change the first layer, you can directly replace it via:</p>
<pre><code class="lang-python">model = torchvision.models.resnet34(pretrained=False)
model.conv1 = nn.Conv2d(1, 64, (7, 7), (2, 2), (3, 3), bias=False)

x = torch.randn(1, 1, 224, 224)
out = model(x)
</code></pre>
<p>I’m not sure what might go wrong in your code, as it’s a bit hard to read.<br>
You can post code snippets by wrapping them into three backticks ```, which makes debugging a bit easier. <img src="https://discuss.pytorch.org/images/emoji/apple/wink.png?v=9" title=":wink:" class="emoji" alt=":wink:"></p>

username: Harsimar_Singh
cooked: <p>[quote=“Harsimar_Singh, post:1, topic:84215”]<br>
So I did this first which resulted in error -</p>
<pre><code class="lang-auto">import fastai
from torchsummary import summary
from fastai.vision.models.unet import *
from fastai.callbacks import *
from fastai.utils.mem import *

arch = torchvision.models.resnet34(pretrained=False)

li = list(model.children()) #list of the children

print( li[0][0][0] ) -&gt; gives Conv2d(3, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)

So I do is li[0][0][0] = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)

So here my input will be one channel which I want as the input to the resnet.

summary(model.to(‘cuda’),input_size=(1, 320, 320)) -&gt; Here I have this error which is NotImplemented Error
</code></pre>
<p>After your suggestions I did this</p>
<pre><code class="lang-auto">def build_model(args):
    arch = torchvision.models.resnet34(pretrained=False)
    arch.conv1 = nn.Conv2d(1, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
    li = list(arch.children())
    arch = nn.Sequential(*li[:8])
    model = DynamicUnet(encoder=arch,n_classes = 1,img_size = (320,320)) #fastai unet converter class using a pertained network as encoder 
    model.to(args.device)
    return model
</code></pre>
<p>And this worked beautifully !! Thank you so much <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"> <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>