Size mismatch between inputs and the Linear layer

username: abhinavankur
cooked: <p>I’ll keep this brief. These are the shapes for the input I am passing to my network.</p>
<p>print(images.shape)<br>
print(images.view(images.shape[0], -1).shape)<br>
print([len(image_datasets[key]) for key in image_datasets.keys()])</p>
<p>torch.Size([32, 3, 224, 224])<br>
torch.Size([32, 150528])<br>
[6552, 818, 819]</p>
<p>The first layer in my classifier, within VGG16 network, expects 150528 inputs for this very reason. However, when I pass the same after reshaping it, I get the following error in the forward pass:</p>
<p>RuntimeError: size mismatch, m1: [32 x 25088], m2: [150528 x 7168]</p>
<p>I really don’t understand where 25088 comes from. Can someone please help?</p>

username: ptrblck
cooked: <p>Usually you are using pooling layers inside these standard models and <code>VGG16</code> uses them as well.<br>
You can check if by printing the architecture via <code>print(model)</code>.<br>
These layers will decrease the spatial size of the activations (which can also be done in conv layers).<br>
Also the activations will have a different number of channels, which is defined by the number of filters in each convolution.</p>
<p>The <code>25088</code> is the number of input features, as the last activation, which is passed to the first linear layer has the shape <code>[batch_size, 512, 7, 7]</code>.</p>
<p>For more information about convolutions etc. I would recommend to take a look at <a href="https://cs231n.github.io/convolutional-networks/">CS231n - CNN</a>.</p>

username: abhinavankur
cooked: <p>Thanks <a class="mention" href="/u/ptrblck">@ptrblck</a>. I realized this after posting the question. Anyway, I have a new issue now on the very same lines. When I try to do a prediction on a single image, after preprocessing it the way PyTorch Transforms are doing, I get a size mismatch error. Exemplifying:<br>
This is the shape of one batch from the dataloader: torch.Size([32, 3, 224, 224])<br>
This is the shape of the image after preprocessing it: torch.Size([3, 224, 224])</p>
<p>When I try to predict this, I get the following error:<br>
<code>RuntimeError: expected stride to be a single integer value or a list of 1 values to match the convolution dimensions, but got stride=[1, 1]</code></p>
<p>Looking at your answer on other threads, I did add unsqueeze_(0) which leads to this error:<br>
<code>ValueError: Expected input batch_size (1) to match target batch_size (32)</code></p>
<p>Please help me with this.</p>
<p>P.S. I understand if one solution is to increase the size in 0th dimension to 32 (Please show me how to do it) but I am not able to understand why PyTorch cannot recognize the batch size to be independent of the input. Why does it care about the batch size?</p>

username: ptrblck
cooked: <aside class="quote no-group" data-username="abhinavankur" data-post="3" data-topic="78972">
<div class="title">
<div class="quote-controls"></div>
<img loading="lazy" alt="" width="24" height="24" src="https://discuss.pytorch.org/user_avatar/discuss.pytorch.org/abhinavankur/48/23535_2.png" class="avatar"> abhinavankur:</div>
<blockquote>
<p>This is the shape of one batch from the dataloader: torch.Size([32, 3, 224, 224])<br>
This is the shape of the image after preprocessing it: torch.Size([3, 224, 224])</p>
</blockquote>
</aside>
<p>Could you post the preprocessing code, please?<br>
Somehow the batch dimension was reduced to a single example, which should be wrong.</p>

username: abhinavankur
cooked: <p><a class="mention" href="/u/ptrblck">@ptrblck</a>, I am taking the liberty of sharing the notebook itself so that I don’t miss anything. Obviously, I am doing a blunder which I am not able to comprehend. It is well documented and I hope this wouldn’t waste much of your time. The error is in the cell numbered 21.</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/abhinavankur/udacity-ml-nd/blob/master/image_classifier/Image%20Classifier%20Project.ipynb" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/abhinavankur/udacity-ml-nd/blob/master/image_classifier/Image%20Classifier%20Project.ipynb" target="_blank" rel="nofollow noopener">abhinavankur/udacity-ml-nd/blob/master/image_classifier/Image Classifier Project.ipynb</a></h4>
<pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Developing an AI application\n",
    "\n",
    "Going forward, AI algorithms will be incorporated into more and more everyday applications. For example, you might want to include an image classifier in a smart phone app. To do this, you'd use a deep learning model trained on hundreds of thousands of images as part of the overall application architecture. A large part of software development in the future will be using these types of models as common parts of applications. \n",
    "\n",
    "In this project, you'll train an image classifier to recognize different species of flowers. You can imagine using something like this in a phone app that tells you the name of the flower your camera is looking at. In practice you'd train this classifier, then export it for use in your application. We'll be using [this dataset](http://www.robots.ox.ac.uk/~vgg/data/flowers/102/index.html) of 102 flower categories, you can see a few examples below. \n",
    "\n",
    "&lt;img src='assets/Flowers.png' width=500px&gt;\n",
    "\n",
    "The project is broken down into multiple steps:\n",
    "\n",
    "* Load and preprocess the image dataset\n",
    "* Train the image classifier on your dataset\n",
    "* Use the trained classifier to predict image content\n",
    "\n",
</code></pre>

  This file has been truncated. <a href="https://github.com/abhinavankur/udacity-ml-nd/blob/master/image_classifier/Image%20Classifier%20Project.ipynb" target="_blank" rel="nofollow noopener">show original</a>

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


username: abhinavankur
cooked: <p>Hi <a class="mention" href="/u/ptrblck">@ptrblck</a>, there’s one more thing I need to point out. The error <code>ValueError: Expected input batch_size (1) to match target batch_size (32) </code> occurs when I am loading the checkpointed model. However, when I try to train the model and then use that model object for inference I get the same error but with different batch size. <code>ValueError: Expected input batch_size (1) to match target batch_size (51).</code>. I am linking the notebook again with this error in the state.</p>
<aside class="onebox githubblob">
  <header class="source">
      <a href="https://github.com/abhinavankur/udacity-ml-nd/blob/master/image_classifier/Image%20Classifier%20Project_Training_Error.ipynb" target="_blank" rel="nofollow noopener">github.com</a>
  </header>
  <article class="onebox-body">
    <h4><a href="https://github.com/abhinavankur/udacity-ml-nd/blob/master/image_classifier/Image%20Classifier%20Project_Training_Error.ipynb" target="_blank" rel="nofollow noopener">abhinavankur/udacity-ml-nd/blob/master/image_classifier/Image Classifier Project_Training_Error.ipynb</a></h4>
<pre><code class="lang-ipynb">{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# Developing an AI application\n",
    "\n",
    "Going forward, AI algorithms will be incorporated into more and more everyday applications. For example, you might want to include an image classifier in a smart phone app. To do this, you'd use a deep learning model trained on hundreds of thousands of images as part of the overall application architecture. A large part of software development in the future will be using these types of models as common parts of applications. \n",
    "\n",
    "In this project, you'll train an image classifier to recognize different species of flowers. You can imagine using something like this in a phone app that tells you the name of the flower your camera is looking at. In practice you'd train this classifier, then export it for use in your application. We'll be using [this dataset](http://www.robots.ox.ac.uk/~vgg/data/flowers/102/index.html) of 102 flower categories, you can see a few examples below. \n",
    "\n",
    "&lt;img src='assets/Flowers.png' width=500px&gt;\n",
    "\n",
    "The project is broken down into multiple steps:\n",
    "\n",
    "* Load and preprocess the image dataset\n",
    "* Train the image classifier on your dataset\n",
    "* Use the trained classifier to predict image content\n",
    "\n",
</code></pre>

  This file has been truncated. <a href="https://github.com/abhinavankur/udacity-ml-nd/blob/master/image_classifier/Image%20Classifier%20Project_Training_Error.ipynb" target="_blank" rel="nofollow noopener">show original</a>

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


username: ptrblck
cooked: <p>The error in cell20/21 is created by the wrong usage of the <code>labels</code> tensor:</p>
<pre><code class="lang-python">img = img.to(device)
log_ps = model(img.unsqueeze(0))
test_loss = criterion(log_ps, labels)
</code></pre>
<p>While you are loading a single image and unsqueeze the batch dimension, which is fine, you are reusing <code>labels</code> from previous cells (most likely from the test <code>DataLoader</code>).<br>
Instead of reusing <code>labels</code> you should either load the target for the current image you would like to classify or skip the loss calculation, if the target is unknown.</p>
<p>I think the second error might be related.<br>
Let me know, if that helps or if you need more information.</p>

username: abhinavankur
cooked: <p>Thanks a lot <a class="mention" href="/u/ptrblck">@ptrblck</a>. Frankly, I am disappointed in myself to see doing such errors and on top of it unable to find them as well.</p>
<p>I think this solves the problem for now. If need be, I’ll engage you again. Thanks once again!</p>

username: ptrblck
cooked: <p>You shouldn’t be disappointed for making such errors.<br>
You can’t imagine how often I’m still running into these things. <img src="https://discuss.pytorch.org/images/emoji/apple/smiley.png?v=9" title=":smiley:" class="emoji" alt=":smiley:"></p>