What's the range of the input value desired to use pretrained resnet152 and vgg19?

yunjey: <p>I try to use pretrained <code>resnet152</code> and <code>vggnet19</code>. In the case of resnet, there are batch normalization layers which are likely to invariant to input normalization (e.g. <code>torchvision.transforms.Normalize</code>). However, in the case of vggnet, I think that if the input range is different, it will cause different results.</p>
<p>What is the <b>range of the input value</b>  when you trained the CNNs in <a href="http://pytorch.org/docs/torchvision/models.html" rel="nofollow noopener">torchvision</a>?</p>
<p>Is it the range of [-1.0, 1.0] or [0, 1] ?</p>

smth: <p>input image is first loaded to range [0, 1] and then this normalization is applied to RGB image as described here:</p>
<aside class="onebox allowlistedgeneric" data-onebox-src="https://github.com/pytorch/vision#models">
  <header class="source">
      <img src="https://github.githubassets.com/favicons/favicon.svg" class="site-icon" width="32" height="32">

      <a href="https://github.com/pytorch/vision#models" target="_blank" rel="noopener">GitHub</a>
  </header>

  <article class="onebox-body">
    <div class="aspect-image" style="--aspect-ratio:690/344;"><img src="https://opengraph.githubassets.com/a357964202c86d34aed1954b6a7bd29c1b06dc6234d6b9ffe26e25f7cac6676f/pytorch/vision" class="thumbnail" width="690" height="345"></div>

<h3><a href="https://github.com/pytorch/vision#models" target="_blank" rel="noopener">GitHub - pytorch/vision: Datasets, Transforms and Models specific to Computer...</a></h3>

  <p>Datasets, Transforms and Models specific to Computer Vision - GitHub - pytorch/vision: Datasets, Transforms and Models specific to Computer Vision</p>


  </article>

  <div class="onebox-metadata">
    
    
  </div>

  <div style="clear: both"></div>
</aside>

<blockquote>
<p>All pre-trained models expect input images normalized in the same way, i.e. mini-batches of 3-channel RGB images of shape (3 x H x W), where H and W are expected to be atleast 224.</p>
<p>The images have to be loaded in to a range of [0, 1] and then normalized using mean=[0.485, 0.456, 0.406] and std=[0.229, 0.224, 0.225]</p>
<p>An example of such normalization can be found in the imagenet example here <a href="https://github.com/pytorch/examples/blob/42e5b996718797e45c46a25c55b031e6768f8440/imagenet/main.py#L89-L101">https://github.com/pytorch/examples/blob/42e5b996718797e45c46a25c55b031e6768f8440/imagenet/main.py#L89-L101</a></p>
</blockquote>

yunjey: <p>Wow, this is very nice information. Thanks!</p>

brisker: <p>If I am trying to train my own model from scratch (not vgg or resnet), do I still need to normalize the rgb pixels to [0,1]? Or just [0,255] like in torch</p>

shicai: <p>either is ok. by substracting proper mean values,  these two methods can get similar performance…</p>

unnir: <p>why are you using these numbers for normalization [0.485, 0.456, 0.406]?</p>

cyberjoac: <p>Did you find out ? I’m wondering the same <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=5" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>

ClementPinard: <p>It’s just a mean computed from a random subset of imagenet pictures. See here for a mean/std computation that was done in torch.</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/soumith/imagenet-multiGPU.torch/blob/master/donkey.lua#L154" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/soumith/imagenet-multiGPU.torch/blob/master/donkey.lua#L154" target="_blank" rel="nofollow noopener">soumith/imagenet-multiGPU.torch/blob/master/donkey.lua#L154</a></h4>
<pre class="onebox"><code class="lang-lua"><ol class="start lines" start="144" style="counter-reset: li-counter 143 ;">
<li>   split = 0,</li>
<li>   verbose = true,</li>
<li>   forceClasses = trainLoader.classes -- force consistent class indices between trainLoader and testLoader</li>
<li>}</li>
<li>torch.save(testCache, testLoader)</li>
<li>testLoader.sampleHookTest = testHook</li>
<li>end</li>
<li>collectgarbage()</li>
<li>-- End of test loader section</li>
<li>
</li>
<li class="selected">-- Estimate the per-channel mean/std (so that the loaders can normalize appropriately)</li>
<li>if paths.filep(meanstdCache) then</li>
<li>local meanstd = torch.load(meanstdCache)</li>
<li>mean = meanstd.mean</li>
<li>std = meanstd.std</li>
<li>print('Loaded mean and std from cache.')</li>
<li>else</li>
<li>local tm = torch.Timer()</li>
<li>local nSamples = 10000</li>
<li>print('Estimating the mean (per-channel, shared for all pixels) over ' .. nSamples .. ' randomly sampled training images')</li>
<li>local meanEstimate = {0,0,0}</li>
</ol></code></pre>


  </article>
  <div class="onebox-metadata">
    
    
  </div>
  <div style="clear: both"></div>
</aside>

<p>They just kept the magic numbers without keeping the tedious mean/std computation part when porting to pytorch.</p>

Sara176: <p>hi<br>
i am a beginner , my inputs are videos processed and stored in  tensors of shape<br>
[batch size, number of channels , number of frames , height , width]<br>
in this case how can i resize the height and width and perform the required normalization to use resnet? , i saw a lot of tutorials but in all of them they are using images</p>