Giter Club home page Giter Club logo

Comments (4)

jlecomte avatar jlecomte commented on May 27, 2024 2

So, I just ran into the issue and spent a bit of time figuring out what was going on. Turns out, it's relatively simple, but it became simple only after banging my head against the keyboard a hundred times LOL! Basically, the key thing to understand is that the model is designed to work only with BATCHES! So, if you feed it a batch, basically a tensor of shape [<batch_size>, <color_channels>, <height>, <width>], e.g., [32, 1, 28, 28], everything just works, and the output of the model is a tensor of shape [<batch_size>, <number_of_classes>], e.g., [32, 10] in this notebook. Now, if you want to do a prediction on a single image, i.e. a tensor of shape [<color_channels>, <height>, <width>], it's not going to work. So, the trick is to artificially add a dimension to our image tensor before feeding it to our model. Basically, we just turn our image tensor into a batch of 1 image, i.e. a tensor of shape [1, 1, 28, 28]. We do that with:

image.unsqueeze(0)

Now, the output of the model will be a batch containing 1 prediction, i.e. a tensor of shape [1, 10]. In inference mode, use squeeze() to remove the first dimension. So, here is the code:

model_X.to(device)
model_X.eval()
with torch.inference_mode():
  prediction = model_X(image.unsqueeze(0).to(device)).squeeze()
prediction

And the output will be a tensor of shape [10], and you can use argmax(dim=1) to get the index of the class with the highest probability.

Hopefully, this clarifies things. I think this issue can be closed (unless I missed something obvious, of course) but I do think that the course material may need to be updated to clarify this a bit.

from pytorch-deep-learning.

aronvandepol avatar aronvandepol commented on May 27, 2024

I've run into something similar in 03. Excersises - Running single dummy tensors torch.rand(size=(1, 28, 28)).unsqueeze(dim=0) as you did in the solutions caused shape errors for me (again 49 vs 490) but did work in the training/test loops. Unsure where this comes from.. Could also be me misunderstanding something with shapes? still learning after all.

Note: model architecture was completely the same, even when running your code it prompted shape error on single tensors.

from pytorch-deep-learning.

cm-awais avatar cm-awais commented on May 27, 2024

Hello,
I ran into same issue, then I gave model.unsqueeze(dim=0) as suggested by aronvandepol, can someone explain what is the reason of such a problem?
Thanks.

from pytorch-deep-learning.

Kashish-1426 avatar Kashish-1426 commented on May 27, 2024

So, I just ran into the issue and spent a bit of time figuring out what was going on. Turns out, it's relatively simple, but it became simple only after banging my head against the keyboard a hundred times LOL! Basically, the key thing to understand is that the model is designed to work only with BATCHES! So, if you feed it a batch, basically a tensor of shape [<batch_size>, <color_channels>, <height>, <width>], e.g., [32, 1, 28, 28], everything just works, and the output of the model is a tensor of shape [<batch_size>, <number_of_classes>], e.g., [32, 10] in this notebook. Now, if you want to do a prediction on a single image, i.e. a tensor of shape [<color_channels>, <height>, <width>], it's not going to work. So, the trick is to artificially add a dimension to our image tensor before feeding it to our model. Basically, we just turn our image tensor into a batch of 1 image, i.e. a tensor of shape [1, 1, 28, 28]. We do that with:

image.unsqueeze(0)

Now, the output of the model will be a batch containing 1 prediction, i.e. a tensor of shape [1, 10]. In inference mode, use squeeze() to remove the first dimension. So, here is the code:

model_X.to(device)
model_X.eval()
with torch.inference_mode():
  prediction = model_X(image.unsqueeze(0).to(device)).squeeze()
prediction

And the output will be a tensor of shape [10], and you can use argmax(dim=1) to get the index of the class with the highest probability.

Hopefully, this clarifies things. I think this issue can be closed (unless I missed something obvious, of course) but I do think that the course material may need to be updated to clarify this a bit.

Thank you for making things simple as i was about to post the same question.

from pytorch-deep-learning.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.