Testing accuracy gap when training a resnet50 on ImageNet from scratch

inzouzouwetrust: <p>Hi all,</p>
<p>I’m currently interested in reproducing some baseline image classification results using PyTorch.<br>
My goal is to get a <code>resnet50</code> model to have a test accuracy as close as the one reported in torchvision: <a href="https://pytorch.org/vision/0.8/models.html" class="inline-onebox" rel="noopener nofollow ugc">torchvision.models — Torchvision 0.8.1 documentation</a> (i.e. <strong>76.15</strong> top 1 accuracy)<br>
In order to do that, I closely follow the setup from the official PyTorch examples repository: <a href="https://github.com/pytorch/examples/blob/master/imagenet/main.py" class="inline-onebox" rel="noopener nofollow ugc">examples/main.py at master · pytorch/examples · GitHub</a>.<br>
Namely, I set:</p>
<ul>
<li><code>seed=19</code></li>
<li><code>batch_size=256</code></li>
<li><code>lr=0.1</code></li>
<li><code>weight_decay=1e-4</code></li>
<li>SGD is using <code>momentum=0.9</code>
</li>
<li>LR scheduler is the StepLR that decays the learning rate by 10 every 30 epochs</li>
<li>I train for 100 epochs (as opposed to 90 in the code above)</li>
<li>I use exactly the same data augmentation as the code above</li>
</ul>
<p>The only difference is that I’m leveraging PyTorch Lightning to seamlessly use 4 GPUs in Distributed Data Parallel mode on a single node.<br>
However, I am only able to achieve <strong>73.12</strong> top 1 accuracy. I don’t want to draw conclusions on my other experiments given this gap on the standard baseline.</p>
<p>My question: has anyone tried and reproduced the torchvision numbers using the setup I described above?<br>
From my reading in the <code>resnet</code> models source code, the pretrained weights could have been obtained by following this setup: <a href="https://ngc.nvidia.com/catalog/resources/nvidia:resnet_50_v1_5_for_pytorch" class="inline-onebox" rel="noopener nofollow ugc">NVIDIA NGC</a> where all the hyperparameters have been thoroughly tuned. Can someone confirm this? In that case, what is the top 1 accuracy I should expect on the val set when using a simpler setup (the one described above)?</p>
<p>Have a good day!</p>

inzouzouwetrust: <p>I put together a “Minimal Working Example” to reproduce my results here: <a href="https://github.com/inzouzouwetrust/resnet50-imagenet-baseline" class="inline-onebox" rel="noopener nofollow ugc">GitHub - inzouzouwetrust/resnet50-imagenet-baseline: Image classification baseline using ResNet50 on ImageNet</a>.<br>
Let me know if that helps!</p>
<p><strong>UPDATE</strong>:</p>
<p>I may have found the root cause for the test performance discrepancy.</p>
<p>In my implementation, I happened to use a total batch size equal to 1024 as each process used a batch size of 256 and 4 processes were spawned. In the official PyTorch example, each process use <code>bs=256/N</code> where <code>N</code> is the number of processes (4 here). It means that I had to either adjust the batch size (i.e. set it to 64 per process) or tune the learning rate accordingly (i.e. set it higher initially, e.g. 0.4 when using 256 images per process).</p>
<p>I will keep this post updated once I get the final results.</p>

ashish007git: <p>Hi,</p>
<p>I am trying to figure out the accuracy gaps between the pytorch model accuracies compared to resnet50 paper .The published accuracies are slightly better that pytorch ones. I was wondering if that was due to changes in ImageNet datasets used for training. Did you use ImageNet 2012 training and validation datasets for your runs?</p>
<p>Most of the hyperparameters you posted are similar to that of resnet paper. I am curious to know if your final results are closer to either pytorch implementation or the resnet paper and if you were able to close the accuracy gap? Thanks!</p>

inzouzouwetrust: <p>Hey <a class="mention" href="/u/ashish007git">@ashish007git</a>,</p>
<p>Sorry for the late reply, I was pretty busy <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"><br>
If you take a look directly in <code>torchvision.models</code> source code (<a href="https://pytorch.org/vision/0.8/_modules/torchvision/models/resnet.html#resnet18" rel="noopener nofollow ugc">here</a>), you can see some comments that explain that some implementation details lead to a better accuracy.<br>
Beside, if you take a look at <a href="https://ngc.nvidia.com/catalog/resources/nvidia:resnet_50_v1_5_for_pytorch" rel="noopener nofollow ugc">NVIDIA NGC</a> you will see that their training routine slightly differs from the one published in the original paper. This way you can squeeze out those last .1%<br>
My understanding is that the pretrained weights from <code>torchvision.models</code> are obtained by performing the training procedure linked above.</p>
<p>Have a nice day!<br>
Alex</p>