Removing the last layers loses the name of the others layers

jpainam: <p>Hi<br>
I have this model<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/9/941340e0d1e80adad1c58dbd239094a123814a10.PNG" data-download-href="https://discuss.pytorch.org/uploads/default/941340e0d1e80adad1c58dbd239094a123814a10" title="getred"><img src="https://discuss.pytorch.org/uploads/default/original/2X/9/941340e0d1e80adad1c58dbd239094a123814a10.PNG" alt="getred" data-base62-sha1="l7VUMLeXWpSs8FvIjwLlICvh6jS" width="690" height="368" data-dominant-color="343232"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">getred</span><span class="informations">765×408 15.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div><br>
And I got rid of the layers in red.<br>
like this</p>
<pre data-code-wrap="python"><code class="lang-python">removed = list(model.children())[:-2]
model = nn.Sequential(*removed)
print(model)
</code></pre>
<p>But ends up losing the name of my layer<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/7/72b4b12ee3ef8f9d2902d4a80911c53420e94711.PNG" data-download-href="https://discuss.pytorch.org/uploads/default/72b4b12ee3ef8f9d2902d4a80911c53420e94711" title="number"><img src="https://discuss.pytorch.org/uploads/default/original/2X/7/72b4b12ee3ef8f9d2902d4a80911c53420e94711.PNG" alt="number" data-base62-sha1="gmJyT57qnq5h1i5lO6VZS7AfSq5" width="690" height="452" data-dominant-color="353535"><div class="meta"><svg class="fa d-icon d-icon-far-image svg-icon" aria-hidden="true"><use href="#far-image"></use></svg><span class="filename">number</span><span class="informations">792×519 18.2 KB</span><svg class="fa d-icon d-icon-discourse-expand svg-icon" aria-hidden="true"><use href="#discourse-expand"></use></svg></div></a></div></p>
<p>Is there any thing I can do to keep the name of the layers. Because I might refers to them latters</p>

HansBambel: <p>I have the same issue. Is there a way to keep the names of the layers? I am removing the first layer of ResNet and substituting it with another one, but then all the other layer’s names are numbered.</p>
<pre><code class="lang-auto">self.backbone = models.resnet50(pretrained=False)
first_layer = torch.nn.Conv2d(12, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
self.backbone = torch.nn.Sequential(first_layer, *list(self.backbone.named_children())[1:])
</code></pre>

HansBambel: <p>I managed to get it working with the following:</p>
<pre><code class="lang-auto">self.backbone = models.resnet50(pretrained=False)
first_layer = torch.nn.Conv2d(12, 64, kernel_size=(7, 7), stride=(2, 2), padding=(3, 3), bias=False)
self.backbone2 = nn.Sequential()
self.backbone2.add_module(name="conv1", module=first_layer)
[self.backbone2.add_module(name, child) for name, child in self.backbone.named_children() if name != "conv1"]
self.backbone = self.backbone2
</code></pre>
<p><code>nn.Sequential()</code> has a <code>add_module()</code> function that takes a name and the module as parameter which we can use to get the same names as the original architecture.</p>
<p>Hope that helps (after 2 years) <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>

ggaemo: <p>Thank you so much! I got my problem fixed very fast!</p>