Answers.Body: <p>Each machine learning model should be trained by constant input image shape, the bigger shape the more information that the model can extract but it also needs a heavier model.</p>
  <p>A model's parameters will adapt with the datasets it learns on, which means it will perform well with the input shape that it learned. Therefore, to answer your question <strong>&quot;What I wanna understand is can I change input shape of those models so that I don't decrease my models training performance?&quot;</strong>, the answer is no, it will decrease the performance.</p>
  <p><strong>&quot;I know that I can pass any shape to those (pytorch) pretrained models and it works.&quot;</strong></p>
  <p><span class="math-container">$\Rightarrow$</span> this happened because Pytorch team replace all Pooling layer with <strong>Adaptive Average Pooling 2d</strong> so you can pass any shape of the image into the model without any bugs.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-31T20:10:23.320Z
Answers.Text: Thanks. As a side question, do you why tensorflow allows to have any input shape? I mean if it is pretrained model shouldn't it as well take only the shape which it was pretrained on?
Answers.UserDisplayName: artas2357
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-01T00:42:45.363Z
Answers.Text: I don't have much experience with tensorflow, you can check it by load the model, then print the architecture with `model.summary()` if you used Keras. The layer before Dense / Fully connected layer should be [Adaptive pooling](https://www.tensorflow.org/addons/api_docs/python/tfa/layers/AdaptiveAveragePooling1D) which help you to assign the size of the output feature map no matter what is the input shape
Answers.UserDisplayName: CuCaRot
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-04-01T07:06:02.533Z
Answers.Text: Ok, I'll try it, anyways, thanks for taking time to explain these question.
Answers.UserDisplayName: artas2357
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-29T04:56:02.497Z
Answers.LastActivityDate: 2021-03-29T04:56:02.497Z
Answers.UserDisplayName: CuCaRot
Answers.Body: <p>In all pre-trained models, the input image has to be the same shape; the <code>transform</code> object resizes the image when you add it as a parameter to your dataset object</p>
  <pre><code>transform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor()])
  dataset = datasets.ImageNet(&quot;.&quot;, split=&quot;train&quot;, transform=transform)
  </code></pre>
  <p><code>T.Resize(256)</code> changes the image shape then <code>T.CenterCrop(224)</code> takes a random crop to help with overfitting in the training data.</p>
  <p>The <code>mean</code> and <code>std</code> are applied to each image before they are used as an input to the model. They were determined when the model was trained, I assume they were hyperparamters.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2021-03-29T04:15:22.913Z
Answers.LastActivityDate: 2021-03-29T04:15:22.913Z
Answers.UserDisplayName: Joseph Santarcangelo
Body: <p>In this <a href="https://pytorch.org/vision/stable/models.html" rel="nofollow noreferrer">https://pytorch.org/vision/stable/models.html</a> tutorial it clearly states:</p>
  <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 at least 224. 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>
  </blockquote>
  <p>Does that mean that for example if I want my model to have input size 128x128 it is or if I calculate mean and std which is unique to my dataset that it is gonna perform worse or won't work at all? I know that with tensorflow if you are loading pretrained models there is a specific argument input_shape which you can set according to your needs just like here:</p>
  <pre><code>tf.keras.applications.ResNet101(
  include_top=True, weights='imagenet', input_tensor=None,
  input_shape=None, pooling=None, classes=1000, **kwargs)
  </code></pre>
  <p>I know that I can pass any shape to those (pytorch) pretrained models and it works. What I wanna understand is can I change input shape of those models so that I don't decrease my models training performance?</p>
  
Tags: <deep-learning><pytorch>
Title: Does anybody know what would happen if I changed input shape of pytorch models?
UserDisplayName: artas2357
ThreadText: # Does anybody know what would happen if I changed input shape of pytorch models?
  
  (Asked by: username_0 on 2021-03-28)
  
  In this https://pytorch.org/vision/stable/models.html tutorial it clearly states:
  
  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 at least 224. 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].
  
  Does that mean that for example if I want my model to have input size 128x128 it is or if I calculate mean and std which is unique to my dataset that it is gonna perform worse or won't work at all? I know that with tensorflow if you are loading pretrained models there is a specific argument input_shape which you can set according to your needs just like here:
  tf.keras.applications.ResNet101(
  include_top=True, weights='imagenet', input_tensor=None,
  input_shape=None, pooling=None, classes=1000, **kwargs)
  
  I know that I can pass any shape to those (pytorch) pretrained models and it works. What I wanna understand is can I change input shape of those models so that I don't decrease my models training performance?
  
  **Tags:** deep-learning, pytorch
  
  ---
  
  ## 2 answers
  
  ### Answer by username_1 on 2021-03-29. Score: 0 | Accepted answer
  
  Each machine learning model should be trained by constant input image shape, the bigger shape the more information that the model can extract but it also needs a heavier model.
  
  A model's parameters will adapt with the datasets it learns on, which means it will perform well with the input shape that it learned. Therefore, to answer your question **"What I wanna understand is can I change input shape of those models so that I don't decrease my models training performance?"**, the answer is no, it will decrease the performance.
  
  **"I know that I can pass any shape to those (pytorch) pretrained models and it works."**
  
  $\Rightarrow$ this happened because Pytorch team replace all Pooling layer with **Adaptive Average Pooling 2d** so you can pass any shape of the image into the model without any bugs.
  
  > **Comments:**
  > **username_0**: Thanks. As a side question, do you why tensorflow allows to have any input shape? I mean if it is pretrained model shouldn't it as well take only the shape which it was pretrained on?  
  > **username_1**: I don't have much experience with tensorflow, you can check it by load the model, then print the architecture with `model.summary()` if you used Keras. The layer before Dense / Fully connected layer should be [Adaptive pooling](https://www.tensorflow.org/addons/api_docs/python/tfa/layers/AdaptiveAveragePooling1D) which help you to assign the size of the output feature map no matter what is the input shape  
  > **username_0**: Ok, I'll try it, anyways, thanks for taking time to explain these question.  
  
  ---
  
  ### Answer by username_2 on 2021-03-29. Score: 0
  
  In all pre-trained models, the input image has to be the same shape; the transform object resizes the image when you add it as a parameter to your dataset object
  transform = T.Compose([T.Resize(256), T.CenterCrop(224), T.ToTensor()])
  dataset = datasets.ImageNet(".", split="train", transform=transform)
  
  T.Resize(256) changes the image shape then T.CenterCrop(224) takes a random crop to help with overfitting in the training data.
  The mean and std are applied to each image before they are used as an input to the model. They were determined when the model was trained, I assume they were hyperparamters.
  
  
  ---
  
  