Wrong input size Inception_v3

James_Robertson: <p>I am attempting to plot the data of my neural network based on Inception_v3 using t-SNE. My issue is that my input size is wrong when passing images to the final layer of my model.</p>
<pre><code class="lang-auto">myModel = models.inception_v3(pretrained=True)


# get the number of inputs for the final layer (fc) of the network
num_ftrs = myModel.fc.in_features

myModel.fc = nn.Linear(num_ftrs, 15)

# load previously trained model

myModel.load_state_dict(torch.load('/content/gdrive/MyDrive/CNN_model.pt'))


myModel = nn.Sequential(*list(myModel.children())[:-1])

# move tensors to GPU if CUDA is available
if train_on_gpu:
    myModel.cuda()

print(myModel)
</code></pre>
<p>This code works fine and the final layer becomes a sequential layer.</p>
<pre><code class="lang-auto"># Visualize Sample Test Results
# define batch size
batch_size = 20

# define transforms
# training (with random flips and rotation)

transform = transforms.Compose([transforms.Resize(resizeto), # resize to 224x?
                                transforms.CenterCrop(size), # take a square (224x224) crop from the centre
                                transforms.ToTensor(), # convert data to torch.FloatTensor
                                transforms.Normalize([0.485, 0.456, 0.406],
                                                     [0.229, 0.224, 0.225])])
# choose the training, validation and test datasets
test_data = datasets.ImageFolder("output" + '/test', transform=transform)
test_loader = torch.utils.data.DataLoader(test_data, batch_size, shuffle=True,drop_last = True)
# obtain one batch of test images
dataiter = iter(test_loader)
images, labels = next(dataiter)
print(images.shape)

images.numpy()
# move model inputs to cuda, if GPU available
if train_on_gpu:
   images = images.cuda()

print(images.shape)
# get sample outputs
mushroom_feats = myModel(images)
imageNet_feats = model(images)

# put on cpu, convert to numpy array and squeeze to batchsize x 512
imageNet_feats = np.squeeze(imageNet_feats.cpu().detach().numpy())
mushroom_feats = np.squeeze(mushroom_feats.cpu().detach().numpy())

labels = labels.numpy()

print(imageNet_feats.shape)
print(mushroom_feats.shape)
print(labels)
</code></pre>
<p>Here when images is passed to myModel I get the error:<br>
RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [20, 1000]</p>

katharsis: <p>Your model expects its input to be [batch_size, channels, height, width] but your image input size is [20, 1000]. You have to reshape your images to the correct format prior to feeding it to your network. Try using torch.reshape().</p>

James_Robertson: <p>thank you for your prompt response, my issue still remains as the input is already the correct size</p>
<pre><code class="lang-auto">print(images.shape)
# get sample outputs
mushroom_feats = myModel(images)
</code></pre>
<p>In the code above the printed images.shape gives a 4d tensor of (batch size, channels, height, width] yet when I try to extract the features images is read as a 2d input. This issue may be specific to inception_v3.</p>
<p>Here is the exact error with printed tensor shape</p>
<pre><code class="lang-auto">torch.Size([20, 3, 299, 299])

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

&lt;ipython-input-21-35bed4452875&gt; in &lt;cell line: 28&gt;()
     26 print(images.shape)
     27 # get sample outputs
---&gt; 28 mushroom_feats = myModel(images)
     29 imageNet_feats = model(images)
     30 

9 frames

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    457                             weight, bias, self.stride,
    458                             _pair(0), self.dilation, self.groups)
--&gt; 459         return F.conv2d(input, weight, bias, self.stride,
    460                         self.padding, self.dilation, self.groups)
    461 

RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [20, 1000]
</code></pre>

katharsis: <p>What happens if you create a random tensor via <code>x = torch.rand(20, 3, 299, 299)</code> and model it by <code>myModel(x)</code>?</p>
<p>In addition to that, can you please show a print statement of your model’s last few layers?</p>

James_Robertson: <p>Thanks for your feedback again, I have tried using a random tensor and modelling it by myModel(x) as suggested. Attached below is also a screen shot of the print statement, please let me know if more information is required to help! <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=12" title=":slight_smile:" class="emoji" alt=":slight_smile:" loading="lazy" width="20" height="20"></p>
<pre><code class="lang-auto">x = torch.rand(20, 3, 299, 299)
x.numpy()
print(x.shape)
# move model inputs to cuda, if GPU available
if train_on_gpu:
   images = images.cuda()
   x = x.cuda()

print(x.shape)
# get sample outputs
mushroom_feats = myModel(x)
</code></pre>
<p>However this outputs the same error with both print statements shown outputting the same tensor shape</p>
<pre><code class="lang-auto">torch.Size([20, 3, 299, 299])
torch.Size([20, 3, 299, 299])

---------------------------------------------------------------------------

RuntimeError                              Traceback (most recent call last)

&lt;ipython-input-15-0a6e59df7c48&gt; in &lt;cell line: 33&gt;()
     31 print(x.shape)
     32 # get sample outputs
---&gt; 33 mushroom_feats = myModel(x)
     34 imageNet_feats = model(x)
     35 

9 frames

/usr/local/lib/python3.10/dist-packages/torch/nn/modules/conv.py in _conv_forward(self, input, weight, bias)
    457                             weight, bias, self.stride,
    458                             _pair(0), self.dilation, self.groups)
--&gt; 459         return F.conv2d(input, weight, bias, self.stride,
    460                         self.padding, self.dilation, self.groups)
    461 

RuntimeError: Expected 3D (unbatched) or 4D (batched) input to conv2d, but got input of size: [20, 1000]
</code></pre>
<p>Below is final layers of my model<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/7/c/7c63d7a69ee2ce88a659aa4e89f4192f035d0989.png" data-download-href="https://discuss.pytorch.org/uploads/default/7c63d7a69ee2ce88a659aa4e89f4192f035d0989" title="InceptionE"><img src="https://discuss.pytorch.org/uploads/default/original/3X/7/c/7c63d7a69ee2ce88a659aa4e89f4192f035d0989.png" alt="InceptionE" data-base62-sha1="hKp7ii2czld3AOqUgG4feinKzy1" width="690" height="454" data-dominant-color="313535"><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">InceptionE</span><span class="informations">1108×730 27.1 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>

ptrblck: <p>Rewrapping the model into an <code>nn.Sequential</code> container:</p>
<pre><code class="lang-python">myModel = nn.Sequential(*list(myModel.children())[:-1])
</code></pre>
<p>can easily break it since all functional API calls will be missing.<br>
Could you explain why you are recreating the model?</p>

James_Robertson: <p>I am trying to use TSNE to visualize how the data is clustered together with the goal of identifying patterns, this method worked for resnet18 but I can’t figure out how to visualize my data for inception_v3</p>

ptrblck: <p>If you want to remove the last linear layer from the model, replace it with an <code>nn.Identity</code> instead of rewrapping the model. As mentioned before, if you are creating an <code>nn.Sequential</code> block all functional API calls from the original model’s <code>forward</code> method will be missing and the model can easily break.</p>