Using Inception v3 for finetuning the classification of cats and dogs previously pretrained on ImageNet and also getting the predictions on test set

username: Mona_Jalal
cooked: <p>I am using this notebook which is combination of 3 notesbooks (linked in the second cell) for finetuning the Inception V3 for training the last layers on a cats and dogs dataset (from Kaggle).</p>
<pre><code class="lang-auto">import torchvision.models as models

model_ft = models.inception_v3(pretrained=True)
for param in model_ft.parameters():
    param.requires_grad = False

# Parameters of newly constructed modules have requires_grad=True by default
# Handle the auxilary net
num_ftrs = model_ft.AuxLogits.fc.in_features
model_ft.AuxLogits.fc = nn.Linear(num_ftrs, 2)
# Handle the primary net
num_ftrs = model_ft.fc.in_features
model_ft.fc = nn.Linear(num_ftrs, 2)
</code></pre>
<p>I am trying to understand if I have done this correctly, so I decided to use the visualization module from PyTorch trainings. However, I get some problem in visualization in cell [57].</p>
<p><a href="https://github.com/monajalal/Inception_V3-cats_and_dogs/commit/c398071ef85ba379c5cbe8d82f98157743b4cd8c" class="onebox" target="_blank" rel="noopener nofollow ugc">https://github.com/monajalal/Inception_V3-cats_and_dogs/commit/c398071ef85ba379c5cbe8d82f98157743b4cd8c</a></p>
<p>Error is:</p>
<pre><code class="lang-auto">ValueError                                Traceback (most recent call last)
Input In [57], in &lt;module&gt;
----&gt; 1 visualize_model(model_ft)

Input In [56], in visualize_model(model, num_images)
     15 for j in range(inputs.size()[0]):
     16     images_so_far += 1
---&gt; 17     ax = plt.subplot(num_images//2, 2, images_so_far)
     18     ax.axis('off')
     19     ax.set_title('predicted: {}'.format(class_names[preds[j]]))

File ~/research/venv/dpcc/lib/python3.8/site-packages/matplotlib/pyplot.py:1268, in subplot(*args, **kwargs)
   1265 fig = gcf()
   1267 # First, search for an existing subplot with a matching spec.
-&gt; 1268 key = SubplotSpec._from_subplot_args(fig, args)
   1270 for ax in fig.axes:
   1271     # if we found an axes at the position sort out if we can re-use it
   1272     if hasattr(ax, 'get_subplotspec') and ax.get_subplotspec() == key:
   1273         # if the user passed no kwargs, re-use

File ~/research/venv/dpcc/lib/python3.8/site-packages/matplotlib/gridspec.py:608, in SubplotSpec._from_subplot_args(figure, args)
    606 else:
    607     if not isinstance(num, Integral) or num &lt; 1 or num &gt; rows*cols:
--&gt; 608         raise ValueError(
    609             f"num must be 1 &lt;= num &lt;= {rows*cols}, not {num!r}")
    610     i = j = num
    611 return gs[i-1:j]

ValueError: num must be 1 &lt;= num &lt;= 8, not 9
</code></pre>
<p><img src="https://discuss.pytorch.org/uploads/default/original/3X/b/7/b746d4f6d4e8cd624c69c54534740d2e057f4793.png" alt="Screenshot from 2022-03-03 12-47-53" data-base62-sha1="q9l8i16l7MWsKVNvYv98NCB9gfF" width="307" height="255"></p>
<p>Also, based on the output I get in cell 38, is it fair to say that since the first element in output tensor is larger and I only have two classes, so it is a cat? I actually am testing on a random cat image from Web for this cell.<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ec2f02b1bb0b7c8625c5d1843802fd76d64a16ae.png" data-download-href="https://discuss.pytorch.org/uploads/default/ec2f02b1bb0b7c8625c5d1843802fd76d64a16ae" title="Screenshot from 2022-03-03 12-48-29"><img src="https://discuss.pytorch.org/uploads/default/original/3X/e/c/ec2f02b1bb0b7c8625c5d1843802fd76d64a16ae.png" alt="Screenshot from 2022-03-03 12-48-29" data-base62-sha1="xHnoXDgcSCVC5VH3fBUqTd7Q8rk" width="690" height="251" data-dominant-color="F3F3F3"><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">Screenshot from 2022-03-03 12-48-29</span><span class="informations">704×257 18.8 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>

username: ptrblck
cooked: <p>The error is raised by <code>matplotlib</code> as it seems you are trying to plot <code>9</code> figures in a <code>subplot</code> of <code>8</code>.</p>