Best way to use torchvision Resnet

mhnazeri: <p>Hi,<br>
I’m using torch ‘2.2.1+cu121’. I wanted to use Resnet50 for feature extraction. I tried two approaches. First:</p>
<pre><code class="lang-auto">image_encoder = models.get_model(
                'resnet50', weights=None
            ).to(self.device)
image_encoder.fc = torch.nn.Identity()
</code></pre>
<p>And second:</p>
<pre><code class="lang-auto">class Model(nn.Module):
    def __init__(self):
        super().__init__()

        self.f = []
        for name, module in resnet50().named_children():
            if name == 'conv1':
                module = nn.Conv2d(3, 64, kernel_size=3, stride=1, padding=1, bias=False)

            if not isinstance(module, nn.Linear) and not isinstance(module, nn.MaxPool2d):
                self.f.append(module)


    def forward(self, x):
        x = self.f(x)
        feature = torch.flatten(x, start_dim=1)
        return F.normalize(feature, dim=-1)
</code></pre>
<p>However, the second approach converges with the loss I defined while the first approach does not. My question is what’s the difference and why does the second one work? What am I missing here?</p>

ptrblck: <p>Did you print both models and compared which submodules are registered as I would expect to see differences based on your filtering in the second approach.<br>
Also, I don’t think the original reset implementation uses a normalization at the end of the forward pass, so this would be a difference, too.</p>

mhnazeri: <p>Thanks for the reply <a class="mention" href="/u/ptrblck">@ptrblck</a>.<br>
Yes, I compared the models and as you mentioned, in the filtering, we don’t have Linear (only last layer has) and MaxPool2d (only first layer has) anymore, and also the kernel size in the first layer has been changed from 7 to 3. And you are right, the original resnet does not have a normalization, however, in the training loop, I took care of that and I applied the normalization in the forward of the bigger model.</p>
<p>The thing is, the second approach is taken from a public repo (an SSL paper from Meta), and the first one was my implementation. Here are the L2 and cov loss between two models for the first approach (it’s an SSL model):<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/4/d/4d205650052391ce84498f0ba7d39c91b154e4b7.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/4d205650052391ce84498f0ba7d39c91b154e4b7" title="train_repr_loss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/4/d/4d205650052391ce84498f0ba7d39c91b154e4b7_2_690x345.jpeg" alt="train_repr_loss" data-base62-sha1="b0i2VH5ptAbEi3QOIlxdNfRzE8f" width="690" height="345" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/4/d/4d205650052391ce84498f0ba7d39c91b154e4b7_2_690x345.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/4/d/4d205650052391ce84498f0ba7d39c91b154e4b7_2_1035x517.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/4/d/4d205650052391ce84498f0ba7d39c91b154e4b7.jpeg 2x" data-dominant-color="FEFEFE"><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">train_repr_loss</span><span class="informations">1200×600 37.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>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/0/e047b304040cd9a2854ae4e5835e24a598836a43.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/e047b304040cd9a2854ae4e5835e24a598836a43" title="train_cov_loss"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e047b304040cd9a2854ae4e5835e24a598836a43_2_690x345.jpeg" alt="train_cov_loss" data-base62-sha1="w04zeQLEgqLda347jg6aSWu1zj5" width="690" height="345" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e047b304040cd9a2854ae4e5835e24a598836a43_2_690x345.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/e/0/e047b304040cd9a2854ae4e5835e24a598836a43_2_1035x517.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/e/0/e047b304040cd9a2854ae4e5835e24a598836a43.jpeg 2x" data-dominant-color="FEFEFE"><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">train_cov_loss</span><span class="informations">1200×600 42.3 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>However, if I use the second approach (the Meta one), here are the L2 and cov:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/c/d/cdc1fe57309c677ac34de3dbf5400af688748606.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/cdc1fe57309c677ac34de3dbf5400af688748606" title="train_repr_loss(1)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cdc1fe57309c677ac34de3dbf5400af688748606_2_690x345.jpeg" alt="train_repr_loss(1)" data-base62-sha1="tmdvETaDcsJ9heMgX0J9pVmUCOi" width="690" height="345" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cdc1fe57309c677ac34de3dbf5400af688748606_2_690x345.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/c/d/cdc1fe57309c677ac34de3dbf5400af688748606_2_1035x517.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/c/d/cdc1fe57309c677ac34de3dbf5400af688748606.jpeg 2x" data-dominant-color="FDFEFE"><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">train_repr_loss(1)</span><span class="informations">1200×600 40 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>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c26d45d120c999c57daf27fb015cda77f853f05.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/2c26d45d120c999c57daf27fb015cda77f853f05" title="train_cov_loss(1)"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c26d45d120c999c57daf27fb015cda77f853f05_2_690x345.jpeg" alt="train_cov_loss(1)" data-base62-sha1="6iAcraxyFNktNcDPSZW9FLaB2rr" width="690" height="345" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c26d45d120c999c57daf27fb015cda77f853f05_2_690x345.jpeg, https://discuss.pytorch.org/uploads/default/optimized/3X/2/c/2c26d45d120c999c57daf27fb015cda77f853f05_2_1035x517.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/2/c/2c26d45d120c999c57daf27fb015cda77f853f05.jpeg 2x" data-dominant-color="FEFEFE"><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">train_cov_loss(1)</span><span class="informations">1200×600 37.4 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>I don’t understand why such a huge difference in training. Is it because of removing Linear and Maxpooling? There was no mention of the effect of pooling in the paper. And what is the best approach to use Resnet? The first approach seems easier and I it was actually suggested <a href="https://discuss.pytorch.org/t/what-is-nn-identity-used-for/125457/14">here</a>. Does Identity() breaks gradient backpropagation? It doesn’t seem to me to do so according to it’s <a href="https://pytorch.org/docs/stable/_modules/torch/nn/modules/linear.html#Identity" rel="noopener nofollow ugc">source</a>.</p>

ptrblck: <p>No, <code>nn.Identity</code> modules do not break the computation graph.<br>
Based on your description it seems you are changing a lot and it might make sense to verify smaller submodules for their parity first.</p>

mhnazeri: <p>Thanks for the guidance <a class="mention" href="/u/ptrblck">@ptrblck</a>.</p>
<p>I checked the changed modules (First conv kernel size, and removing the MaxPool2d) by commenting out the filters in the for loop. They didn’t affect the model’s convergence. Still, the model converged while the first approach diverged.</p>
<p>I couldn’t figure out the reason for the divergence in the first approach and convergence in the second approach. I’m moving forward with the second approach for now. Thanks for the help.</p>