ImageFolder return

Lamb_Sauce: <p>Hi everyone second post here!<br>
I got this error</p>
<pre><code class="lang-auto">TypeError: linear(): argument 'input' (position 1) must be Tensor, not int
</code></pre>
<p>This is my code - it runs pretty slow</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

import numpy as np
from PIL import Image
import time

start = time.time()
transform = transforms.Compose([transforms.Resize(255), transforms.CenterCrop(224), transforms.ToTensor(),]) 
# This gives us some transforms to apply later on

training = torchvision.datasets.ImageFolder(root = "training_set", transform = transform)
print(training)

train_dataloader = DataLoader(dataset = training, shuffle = True, batch_size = 32)
print(train_dataloader)




print("Before NN creation",start-time.time())
# Creating the model
class NeuralNetwork (nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.Linear_Stack = nn.Sequential(
            nn.Linear(224*224, 40000), 
            nn.ReLU(),
            nn.Linear(40000, 10000),
            nn.ReLU(),
            nn.Linear(10000, 75),
            nn.ReLU(),
            nn.Linear(75, 5),
            nn.ReLU(),
            nn.Linear(5, 1),
            nn.LogSoftmax(dim=1)
        )
    def forward (self, x):
        logits = self.Linear_Stack(x)
        return logits

model = NeuralNetwork()
print(model)

epochs = 2
lr = 0.001
loss = nn.CrossEntropyLoss()
optim = torch.optim.SGD(model.parameters(), lr = lr)
print(loss, optim)  

#creating the main loop
def main(loader, model, loss, optim):
    size = len(loader.dataset)
    for (images, labels) in enumerate(loader):
        # Prediction
        pred = model.forward(images)
        loss = loss(pred, target)
        #backprop
        optim.zero_grad()
        loss.backward()
        optim.step()
        print(loss)

for x in range(epochs):
    main(train_dataloader, model, loss, optim)
print(start-time.time())
</code></pre>
<p>I guess main question is: Am I using Image Folder correctly? (I know it works because I get the correct amount of datapoints when I print it) Is my code even succinct and I couldn’t find anything on what the variable names for the outputs are (like X, y or Sample, Target). I think that answering these questions will help me solve my question, reply if you have anything else that you think will be helpful and I will take it onboard.</p>

eqy: <p><code>enumerate(loader)</code>  will give you something like <code>batch_idx, (data, target)</code> back so it is likely you are passing the <code>batch_idx</code> in place of the image. <a href="https://github.com/pytorch/examples/blob/01539f9eada34aef67ae7b3d674f30634c35f468/mnist/main.py#L39" class="inline-onebox" rel="noopener nofollow ugc">examples/main.py at 01539f9eada34aef67ae7b3d674f30634c35f468 · pytorch/examples · GitHub</a></p>

Lamb_Sauce: <p>Thanks. What is <code>batch_idx</code> ?</p>

eqy: <p><code>enumerate</code> will give you an index for whatever you are iterating over so it is simply the batch number out of the total number of batches in an epoch</p>

Lamb_Sauce: <p>Thank you. How can I go about implementing these into my code. Could you edit it for me to review? I am a little confused about batch_idx.</p>

m3tobom_M: <p>Here is an example of enumerate. You need to add another variable to your loop which is batch index in this case.</p>
<p><div class="lightbox-wrapper"><a class="lightbox" href="https://discuss.pytorch.org/uploads/default/original/3X/5/b/5b531d702dcff211fcfc1f3801e8b34802783f22.jpeg" data-download-href="https://discuss.pytorch.org/uploads/default/5b531d702dcff211fcfc1f3801e8b34802783f22" title="Screenshot_20210511-040141_Dcoder"><img src="https://discuss.pytorch.org/uploads/default/optimized/3X/5/b/5b531d702dcff211fcfc1f3801e8b34802783f22_2_562x500.jpeg" alt="Screenshot_20210511-040141_Dcoder" data-base62-sha1="d1TwpSEZKhX7ud2FaF2RPpnz62m" width="562" height="500" srcset="https://discuss.pytorch.org/uploads/default/optimized/3X/5/b/5b531d702dcff211fcfc1f3801e8b34802783f22_2_562x500.jpeg, https://discuss.pytorch.org/uploads/default/original/3X/5/b/5b531d702dcff211fcfc1f3801e8b34802783f22.jpeg 1.5x, https://discuss.pytorch.org/uploads/default/original/3X/5/b/5b531d702dcff211fcfc1f3801e8b34802783f22.jpeg 2x" data-dominant-color="262826"><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_20210511-040141_Dcoder</span><span class="informations">720×640 125 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>

Lamb_Sauce: <p>Thank you everyone (<a class="mention" href="/u/eqy">@eqy</a>, <a class="mention" href="/u/m3tobom_m">@m3tobom_M</a> ). At the moment, my i5 hangs (it is only just a laptop) with this code. I have ironed out the errors but it just hangs and my computer stops. I ran the FashionMNIST in a reasonable time but this is super slow. Can anyone help me out?</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

import numpy as np
from PIL import Image
import time

start = time.time()
transform = transforms.Compose([transforms.Resize(200*200), transforms.CenterCrop(224), transforms.ToTensor(),]) 
# This gives us some transforms to apply later on

training = torchvision.datasets.ImageFolder(root = "training_set", transform = transform)
print(training)

train_dataloader = DataLoader(dataset = training, shuffle = True, batch_size = 32)
print(train_dataloader)




print("Before NN creation",start-time.time())
# Creating the model
class NeuralNetwork (nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.Linear_Stack = nn.Sequential(
            nn.Linear(200*200, 40000), 
            nn.ReLU(),
            nn.Linear(40000, 10000),
            nn.ReLU(),
            nn.Linear(10000, 75),
            nn.ReLU(),
            nn.Linear(75, 5),
            nn.ReLU(),
            nn.Linear(5, 1),
            nn.LogSoftmax(dim=1)
        )
    def forward (self, x):
        logits = self.Linear_Stack(x)
        return logits

model = NeuralNetwork()
print(model)

epochs = 1
check = 0
lr = 0.001
loss = nn.CrossEntropyLoss()
optim = torch.optim.SGD(model.parameters(), lr = lr)
print(loss, optim)  

#creating the main loop
def main(loader, model, loss, optim):
    size = len(loader.dataset)
    print(size)
    for batch_idx, (data, target) in enumerate(loader):
        # Prediction
        check = check + 1
        print(check)
        pred = model.forward(data)
        loss = loss(pred, target)
        #backprop
        optim.zero_grad()
        loss.backward()
        optim.step()
        print(loss)

for x in range(epochs):
    main(train_dataloader, model, loss, optim)
    print(start-time.time())
</code></pre>
<p>Upon adding some print commands, I get stuck after <code>print(check)</code> and before creating the model. The main one is after the <code>print(check)</code> though. That’s where everything just hangs.</p>

ptrmcl: <p>I think it’s because your model has wayyyy too many trainable parameters.</p>

Lamb_Sauce: <p>Like Layers? How many would be the best to start off?</p>

ptrmcl: <p>Number of layers is reasonable, maybe try 2-3. The number of units in the first 2 layers is way too much. It should be in the tens to hundreds at max. You have to try out different values. This is called hyperparameter tuning, which, in essence, is tuning the parameters not learned by the model from the data. Other examples include learning rate, number of layers, etc.</p>
<p>I also suggest maybe going for a smaller dataset so you can train faster <img src="https://discuss.pytorch.org/images/emoji/apple/slight_smile.png?v=9" title=":slight_smile:" class="emoji" alt=":slight_smile:"></p>
<p>After that, I suggest looking at an architecture called convolutional neural networks. These have been the goto for computer vision for quite a while, although recent research suggests that may change soon…</p>
<p>Lastly, I suggest using a platform called <a href="https://www.google.com/url?sa=t&amp;rct=j&amp;q=&amp;esrc=s&amp;source=web&amp;cd=&amp;cad=rja&amp;uact=8&amp;ved=2ahUKEwiRqr3R7MDwAhXL3J4KHX0mCvAQFjAAegQIBRAE&amp;url=https%3A%2F%2Fcolab.research.google.com%2F&amp;usg=AOvVaw3A5aPK2kLFzKOzb6sOckVw" rel="noopener nofollow ugc">Google Colab</a>. This is resource that gives you a free GPU to play around with, which will substantially speed up your experiments. It’s in a notebook format, where you can type and run your code piece-by-piece.</p>

Lamb_Sauce: <p>Okie. I have tried Colab, its a pain to upload all my data though but I will try it again. Secondly, with this code:</p>
<pre><code class="lang-auto">
start = time.time()
transform = transforms.Compose([transforms.Resize(200*200), transforms.CenterCrop(224), transforms.ToTensor(),]) 
# This gives us some transforms to apply later on

training = torchvision.datasets.ImageFolder(root = "training_set", transform = transform)
print(training)

train_dataloader = DataLoader(dataset = training, shuffle = True, batch_size = 32)
print(train_dataloader)




print("Before NN creation",start-time.time())
# Creating the model
class NeuralNetwork (nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.Linear_Stack = nn.Sequential(
            nn.Linear(200*200, 40000), 
            nn.ReLU(),
            nn.Linear(40000, 5),
            nn.ReLU(),
            nn.Linear(5, 1),
            nn.LogSoftmax(dim=1)
        )
    def forward (self, x):
        logits = self.Linear_Stack(x)
        return logits

model = NeuralNetwork()
print(model)

epochs = 1
check = 0
lr = 0.001
loss = nn.CrossEntropyLoss()
optim = torch.optim.SGD(model.parameters(), lr = lr)
print(loss, optim)  

#creating the main loop
def main(loader, model, loss, optim):
    size = len(loader.dataset)
    print(size)
    for batch_idx, (data, target) in enumerate(loader):
        # Prediction
        check = check + 1
        print(check)
        pred = model.forward(data)
        loss = loss(pred, target)
        #backprop
        optim.zero_grad()
        loss.backward()
        optim.step()
        print(loss)
</code></pre>
<p>How would I go about changing the values. Am I right in changing the <code>transforms.Resize()</code> value to exactly what I put into the Model, in this case 200*200. Admittedly, I blindly put that transform in thinking it did what I think it would do.</p>

ptrmcl: <p>General advice, look at the docs! This will answer most of your questions. For example, <a href="https://pytorch.org/vision/stable/transforms.html#torchvision.transforms.Resize" rel="noopener nofollow ugc">here</a>, it says that if you pass in a single parameter, it will resize the shortest side of the image to that (and maintain the aspect ratio).</p>
<p>First thing I would do though is to make sure you understand the basics of what you’re trying to do. Do you know what a fully connected neural net is? I see that your bio says you’re in high school and you’re just starting. That’s when I started! A common starting point for many (including myself) is <a href="https://www.coursera.org/learn/machine-learning" rel="noopener nofollow ugc">Andrew Ng’s free machine learning course</a>.</p>

Lamb_Sauce: <p>Yes I am. Yeet. Haha, 69. I’m in high school. Really childish and foolish.<br>
Thanks <a class="mention" href="/u/ptrmcl">@ptrmcl</a> . How do I change the parameters of the full image to be 224 by 224?<br>
BTW, does Andrew Ng’s course require Calulus??</p>

Lamb_Sauce: <p>Also, after doing some work after school,<br>
This is m new code:</p>
<pre><code class="lang-auto">import torch
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

import numpy as np
from PIL import Image
import time

start = time.time()
transform = transforms.Compose([transforms.resize((225,225)),transforms.CenterCrop(224),transforms.ToTensor()]) 
# This gives us some transforms to apply later on
# transforms.Resize(225), 

training = torchvision.datasets.ImageFolder(root = "training_set", transform = transform)
print(training)

train_dataloader = DataLoader(dataset = training, shuffle = True, batch_size = 32)
print(train_dataloader)




print("Before NN creation",start-time.time())
# Creating the model
class NeuralNetwork (nn.Module):
    def __init__(self):
        super(NeuralNetwork, self).__init__()
        self.Linear_Stack = nn.Sequential(
            nn.Linear(50176, 1000), 
            nn.ReLU(),
            nn.Linear(1000, 5),
            nn.ReLU(),
            nn.Linear(5, 1),
            nn.LogSoftmax(dim=1)
        )
    def forward (self, x):
        logits = self.Linear_Stack(x)
        return logits

model = NeuralNetwork()
print(model)

epochs = 1

lr = 0.001
loss = nn.CrossEntropyLoss()
optim = torch.optim.SGD(model.parameters(), lr = lr)
print(loss, optim)  

#creating the main loop
def main(loader, model, loss, optim):
    size = len(loader.dataset)
    print(size)
    for batch_idx, (data, target) in enumerate(loader):
        # Prediction
        
        pred = model.forward(data)
        loss = loss(pred, target)
        #backprop
        optim.zero_grad()
        loss.backward()
        optim.step()
        print(loss)

for x in range(epochs):
    main(train_dataloader, model, loss, optim)
    print(start-time.time())
</code></pre>
<p>I get this error</p>
<pre><code class="lang-auto">RuntimeError: mat1 and mat2 shapes cannot be multiplied (21504x224 and 50176x1000)
</code></pre>
<p>Can anyone point me out on what I’m doing wrong?</p>

ptrmcl: <p>Resize((224,224))</p>
<p>Yeah you need some basic calculus + linear algebra. I think he gives a refresher?? If not there are lots of resources to learn that.</p>
<p>I just remembered <a href="https://www.youtube.com/watch?v=aircAruvnKk&amp;list=PLZHQObOWTQDNU6R1_67000Dx_ZCJB-3pi" rel="noopener nofollow ugc">this series</a> which introduces the neural nets you’re using on images from a famous youtuber called 3 blue 1 brown. He has very nice visualizations.</p>

ptrmcl: <p>you wanted to resize to 224x224 right? look what you’re passing in to resize</p>

Lamb_Sauce: <p>I get this</p>
<pre><code class="lang-auto">RuntimeError: mat1 and mat2 shapes cannot be multiplied (21504x224 and 50176x1000)
</code></pre>
<p>What does this message mean and can you help me fix it?</p>

m3tobom_M: <p>First, you need to resize all your inputs to a fixed size, then flatten your inputs. Flatten input size and first linear layer’s input sizes must match. Here is a sample code. Also as I recommend you to read docs as <a class="mention" href="/u/ptrmcl">@ptrmcl</a> suggested.</p>
<p>Also here is a sample for you. You can read what view do from docs. It is a simple function</p>
<pre><code class="lang-auto">&gt;&gt;&gt; import torch
&gt;&gt;&gt; input_tensor = torch.rand((1, 3, 224, 224)) # Input images resized to 224x224 in transform
&gt;&gt;&gt; _, c, h, w = input_tensor.shape
&gt;&gt;&gt; input_size_of_first_linear_layer = c * h * w
&gt;&gt;&gt; flat_input_tensor = input_tensor.view(-1, input_size_of_first_linear_layer)
&gt;&gt;&gt; input_tensor.shape
torch.Size([1, 3, 224, 224])
&gt;&gt;&gt; flat_input_tensor.shape
torch.Size([1, 150528])
&gt;&gt;&gt; layers = torch.nn.Sequential(
...     torch.nn.Linear(input_size_of_first_linear_layer, 1000),
...     torch.nn.Linear(1000, 2))
&gt;&gt;&gt;
&gt;&gt;&gt; layers
Sequential(
  (0): Linear(in_features=150528, out_features=1000, bias=True)
  (1): Linear(in_features=1000, out_features=2, bias=True)
)
&gt;&gt;&gt; layers(flat_input_tensor)
tensor([[-0.0779,  0.3057]], grad_fn=&lt;AddmmBac
</code></pre>

Lamb_Sauce: <p>Thank you <a class="mention" href="/u/m3tobom_m">@m3tobom_M</a> . After flattening, I get this tensor size - <code>torch.Size([64, 19200])</code><br>
Is that ok? Can I change it to be 1, 19200?<br>
Secondly, I also get this weird error</p>
<pre><code class="lang-auto">RuntimeError: element 0 of tensors does not require grad and does not have a grad_fn
</code></pre>
<p>For reference, this is my current code:</p>
<pre><code class="lang-auto">import torch
from torch import tensor
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from torch.utils.data import DataLoader

import numpy as np
from PIL import Image
import time

start = time.time()
transform = transforms.Compose([transforms.Resize((200,200)),transforms.CenterCrop(80),transforms.ToTensor()]) 
training = torchvision.datasets.ImageFolder(root = "training_set", transform = transform)
print(training)

train_dataloader = DataLoader(dataset = training, shuffle = True, batch_size = 64)
print(train_dataloader)




print("Before NN creation",start-time.time())
# Creating the model
class NeuralNetwork (nn.Module):
    def __init__(self,input_dims):
        super(NeuralNetwork, self).__init__()
        self.Linear_Stack = nn.Sequential(
            nn.Linear(input_dims, 1000), 
            nn.ReLU(),
            nn.Linear(1000, 5),
            nn.ReLU(),
            nn.Linear(5, 1),
            nn.LogSoftmax(dim=1)
        )
    def forward (self, x):
        logits = self.Linear_Stack(x)
        return logits

model = NeuralNetwork(19200)
print(model)


epochs = 1

lr = 0.001
loss = nn.CrossEntropyLoss()
optim = torch.optim.SGD(model.parameters(), lr = lr)
print(loss, optim)  

out = 0

def process(tensorin):
    global out
    _, c,h,w = tensorin.shape
    first_layer = c * h * w
    out = tensorin.view(-1, first_layer)
    return out
#creating the main loop
def main(loader, model, loss, optim):
    size = len(loader.dataset)
    print(size)
    for batch_idx, (data, target) in enumerate(loader):
        print(data.shape)
        process(data)
        print(target)
        print(out.shape)
        pred = model(out)
        loss = loss(out, target)
        #backprop
        optim.zero_grad()
        loss.backward()
        optim.step()
        print(loss)

for x in range(epochs):
    main(train_dataloader, model, loss, optim)
    print(start-time.time())
</code></pre>
<p>Thank you for any help (and help given - <a class="mention" href="/u/m3tobom_m">@m3tobom_M</a> , <a class="mention" href="/u/eqy">@eqy</a>, <a class="mention" href="/u/ptrblck">@ptrblck</a>  and <a class="mention" href="/u/ptrmcl">@ptrmcl</a>)</p>

ptrblck: <p>It seems you are defining <code>out = 0</code> in the global scope, pass this integer then to the model in:</p>
<pre><code class="lang-python">print(out.shape)
pred = model(out)
</code></pre>
<p>which I would assume should yield errors, since neither <code>out.shape</code> is defined nor would the model accept an <code>int</code> as the input, and later calculate the <code>loss</code> using <code>out</code>.<br>
Besides that you are also overriding <code>loss</code> (the criterion) with the loss value, so wou might also want to fix this.</p>