Binary classifier Cats & Dogs questions

XxaemaethxX: <p>Hi all, I’m currently looking at Pytorch tutorials online &amp; going thru a PacktPublishing book called “Deep Learning with PyTorch” by Vishnu Subramanian and I had some questions I hope some of the more experienced ML/data science comrades could help me with.</p>
<p>1)The book stated the cat and dog images were 256x256<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/8/8085d1edc0a8b40416c7e6cf86426dca226de161.png" alt="28f9ab3bcbf1363c8d822a338737c020" data-base62-sha1="ikXP6G6qVdUnor7j7JB5tOUfD9f" width="639" height="116"><br>
but it dosn’t make sense to me because later on the line of code was used:</p>
<blockquote>
<p>simple_transform = transforms.Compose([transforms.Resize((224,224))<br>
,transforms.ToTensor()<br>
,transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])])</p>
</blockquote>
<p>If the images were rescaled to 224x224, then why say there were 256x256? I checked the dogs-vs-cats dataset from kaggle in the folder and all the images are of different a various sizes much bigger or smaller than 256x256 (essentially all are randomly sized).</p>
<ol start="2">
<li>
<p>This is a screen shot of the Neural Network class I used for the Cat &amp; Dog binary classifier CNN:<br>
<img src="https://discuss.pytorch.org/uploads/default/original/2X/8/8ea677c93c8357158ee8e420ce7b750a3cc54735.png" alt="013d920f72571b70c7553f1e1ce6448e" data-base62-sha1="klWsjq9R0b9qNjU75eCl87PbEFL" width="524" height="348"><br>
<em><em>If</em> the input 3 channel RGB cat/dog images are 224x224</em>, I understand how Convo2d with a 5x5 kernel outputs 10 @ 220x220 and then max_pool2d with a 2x2 kernel to 10 @ 110x110, then Convo2d again to a 10 @ 106x106 and then max_pool2d to 20 channels of 53x53 size which equals 56180, which is the input to the <strong>first</strong> nn.Linear layer (self.fc1) in my network, <strong>BUT</strong> how or why is the output of the first linear layer calculated to be 500? i.e. self.fc1 = nn.Linear(56180, 500) ? furthermore, for the ouput of the second linear layer 50? (<em>I know 2 is the output of the third/last linear layer because its a binary classifier and either chooses dog or cat</em>).</p>
</li>
<li>
<p>When downloading the dogs-vs-cats dataset from kaggle, <a href="https://www.kaggle.com/c/dogs-vs-cats/data" rel="noopener nofollow ugc">https://www.kaggle.com/c/dogs-vs-cats/data</a> it seems the “train” folder which held 25000 images of cats and dogs inside of it was used to create a “validation” dataset the book called it.</p>
</li>
</ol>
<p>I used these lines of code to , which created a new “valid” folder that didn’t exist inside the main folder dogs-vs-cats (which held the “train” folder that had the 25,000 images).</p>
<p>Then, the lines of code were used to create “cat” and “dog” sub-folders in BOTH “train” and “valid” folders inside the “dogs-vs-cats” folder that held them both:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/f/f76b5a9591017e0ab3fc6c7111d8e614c509b5ec.png" data-download-href="https://discuss.pytorch.org/uploads/default/f76b5a9591017e0ab3fc6c7111d8e614c509b5ec" title="14c2131efd82c288f14324595be92990"><img src="https://discuss.pytorch.org/uploads/default/original/2X/f/f76b5a9591017e0ab3fc6c7111d8e614c509b5ec.png" alt="14c2131efd82c288f14324595be92990" data-base62-sha1="ziLWgpjxw4mcoU5UriPJHUYknlq" width="690" height="420" data-dominant-color="F2F2F2"><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">14c2131efd82c288f14324595be92990</span><span class="informations">1031×628 28.7 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>
<p>I was looking at the folder as I ran the lines of code above in Jupyter notebook and could see the files getting copied when I ran the first for loop i.e.  <strong>"for i in shuffle[ : 2000 ]: "</strong> I had to use <strong>shutil.copy</strong> instead of <strong>shutil.copyfile</strong> that they used in the book because I was getting a permission error for reading and writing files ( I tried running Conda prompt as adminstrator and then launching Jupyter notebook off it to no avail).</p>
<p>I saw that the 2,000 unique images of cats and dogs were <strong>MOVED</strong> (not copied) from the “train” folder which meant 25,000 - 2,000 = 23,000. So now the “train” folder held 23,000 images BUT now the “valid” folder held 4,000 images because it looked like it moved the cat and dogs images to the main “valid” folder and then <strong>COPIED</strong> them again to the “cat” and “dog” subfolders within which was weird to me (essentially creating duplicate images). Is this correct? Could it be because I used shutil.copy instead of shutil.copyfile?:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/e/eb6fc108b38c9d6388ac01c222a2cf88ecab89d0.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/eb6fc108b38c9d6388ac01c222a2cf88ecab89d0" title="d904214e12f36fd8efedc866f8773fbd"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/e/eb6fc108b38c9d6388ac01c222a2cf88ecab89d0_2_584x500.jpeg" alt="d904214e12f36fd8efedc866f8773fbd" data-base62-sha1="xALDwtiXUv9gs0Vs8bj9Zfb5O6I" width="584" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/e/eb6fc108b38c9d6388ac01c222a2cf88ecab89d0_2_584x500.jpeg, https://discuss.pytorch.org/uploads/default/optimized/2X/e/eb6fc108b38c9d6388ac01c222a2cf88ecab89d0_2_876x750.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/e/eb6fc108b38c9d6388ac01c222a2cf88ecab89d0.jpeg 2x" data-dominant-color="C0BCB8"><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">d904214e12f36fd8efedc866f8773fbd</span><span class="informations">1067×913 560 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>
<p>After running the second copy for loop i.e. <strong>"for i in shuffle[2000: ]: "</strong>, it seemed like the remaining 23,000 images in the “train” folder where placed in their corresponding “cat” and “dog” subfolders, but also <strong>COPIED</strong> over the main “valid” folder, which already held 2,000 images and not totaled 25,000 once again (i.e. 23,000 + 2,000). So then “train” folder looked like this after:<br>
<div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/2X/4/456e9c0126abc878ac1918aadd629a872b35b3a6.png" data-download-href="https://discuss.pytorch.org/uploads/default/456e9c0126abc878ac1918aadd629a872b35b3a6" title="32184c58afa5433ee9e6806c113770da"><img src="https://discuss.pytorch.org/uploads/default/optimized/2X/4/456e9c0126abc878ac1918aadd629a872b35b3a6_2_690x252.png" alt="32184c58afa5433ee9e6806c113770da" data-base62-sha1="9UdVlFoTuQiU4rZPpIG4Ext5yrc" width="690" height="252" srcset="https://discuss.pytorch.org/uploads/default/optimized/2X/4/456e9c0126abc878ac1918aadd629a872b35b3a6_2_690x252.png, https://discuss.pytorch.org/uploads/default/optimized/2X/4/456e9c0126abc878ac1918aadd629a872b35b3a6_2_1035x378.png 1.5x, https://discuss.pytorch.org/uploads/default/original/2X/4/456e9c0126abc878ac1918aadd629a872b35b3a6.png 2x" data-dominant-color="FDFCFC"><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">32184c58afa5433ee9e6806c113770da</span><span class="informations">1065×389 23.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>
<p>I was confused why the images were getting swapped over like that? Or I feel like some of these images are getting COPIED when they should be simply getting moved, because I essentially have a duplicate images files in my “valid” folder now.</p>
<p>Also, how does one go about selecting the batch size? I used 32 here per the tutorial, but is it totally arbitrary? and the For loops for the epoch training? Does the amount of loops through your data depend on the batch size or amount of images you have?</p>
<p>Sorry about all the questions and hopefully I was not confusing but I would appreciate any help provided.</p>

JakeAndFinn: <p>Hi, there I will try to answer your questions here.<br>
1: If you resize the image using transforms.Resize(244,244) all images will be of the size 244 so if the book says the input to the first layer is 256 it is wrong it will be [batch_size, 3,244, 244]<br>
If you use the Kaggle dataset (I found out first hand) you should resize them so they are all the same size or you will get errors.<br>
you can do this if you are not 100% sure about the shape of the tensor when running the code; in your Forward method do this</p>
<pre><code class="lang-auto">   def forward(self, x):
       print(x.shape)
        x = self.pool(torch.relu(self.conv1(x)))
        x = self.pool(torch.relu(self.conv2(x)))
        x = self.pool(torch.relu(self.conv3(x)))
        print(x.shape)
        # Flatten image
        x = x.view(x.size(0), -1)
        x = self.dropout(x)
        x = torch.relu(self.fc1(x))
        x = self.dropout(x)
        x = self.fc2(x)
        return x
</code></pre>
<p>So using the print(x.shape) will give you the tensors shape so you are 100% sure what is going in and out of the model.</p>
<p>For 2: This I think is called downsampling so you are making the data smaller by removing what is not ‘needed’ or ‘noise’ in the image.  I have seen them go both down and up in size in the FC layers.</p>
<p>3: For this what you should have in your dataset is two main folders a train and a validation folder. These two folders will then have both a cat and a dog folder.<br>
So the structure would be like this<br>
dataset/train/<br>
…/cat/<br>
… /dog/</p>
<p>dataset/valid/<br>
…/cat/<br>
… /dog/<br>
Also, there should be no duplicate images in the folders. The code does not look like it is checking if the image exists or not. But you are right when thinking that the images should be moved rather than duplicated/copied. You do not want to have the same image in the train/valid/test folders as this will cause a sample bias in your dataset. So what you have looked ok but you will have to add the train path carefully like this<br>
path = …/train/train/dog/</p>
<p>I hope that helps out a bit.</p>

JakeAndFinn: <p>Oh wow I realized that this is a year old post.</p>