moein-shariatnia / Deep-Learning

In-depth tutorials on deep learning. The first one is about image colorization using GANs (Generative Adversarial Nets).
MIT License
152 stars 51 forks source link

How to prediction on single image #1

Closed masudrehman closed 3 years ago

masudrehman commented 3 years ago

sir, How to do prediction on single image?

moein-shariatnia commented 3 years ago

sir, How to do prediction on single image?

Hey, If you are following along the Colab notebook, you can use the following code snippet to do prediction on a single image and visualize the final result. Note that you need to define the model (as noted in the notebook) and load the pretrained weights that I've provided there:

img = PIL.Image.open("black_and_white.jpg")
img = img.resize((256, 256))
# to make it between -1 and 1
img = transforms.ToTensor()(img)[:1] * 2. - 1.
model.eval()
with torch.no_grad():
    preds = model.net_G(img.unsqueeze(0).to(device))
plt.imshow(lab_to_rgb(img.unsqueeze(0), preds.cpu())[0])

I hope this helps you out. Please let me know if there is any problem. Good Luck!

masudrehman commented 3 years ago

thanks it worked

masudrehman commented 3 years ago

Thank you sir

On Sun, Dec 27, 2020, 10:05 PM moein-shariatnia notifications@github.com wrote:

sir, How to do prediction on single image?

Hey, If you are following along the Colab notebook, you can use the following code snippet to do prediction on a single image and visualize the final result. Note that you need to define the model (as noted in the notebook) and load the pretrained weights that I've provided there:

img = PIL.Image.open("black_and_white.jpg") img = img.resize((256, 256))

to make it between -1 and 1

img = transforms.ToTensor()(img)[:1] * 2. - 1. model.eval() with torch.no_grad(): preds = model.net_G(img.unsqueeze(0).to(device)) plt.imshow(lab_to_rgb(img.unsqueeze(0), preds.cpu())[0])

I hope this helps you out. Please let me know if there is any problem. Good Luck!

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/moein-shariatnia/Deep-Learning/issues/1#issuecomment-751491923, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOUBCEOSFMJZATYA42OXGQDSW5SOXANCNFSM4VK2HBJQ .

moein-shariatnia commented 3 years ago

Hey, you should be actually saving "model.state_dict()" instead of "model" itself. I think this would resolve your error.

On Sun, Dec 27, 2020 at 9:01 PM masudbarki notifications@github.com wrote:

also sir if you help me with this error AttributeError: Can't get attribute 'MainModel' on <module 'main'> this error happens when i saved the model with torch.save(model," fullmodel.pt') when i was using the same colab in which i have done the training path, the torch.load("fullmodel.pt") worked perfectly.. but in another colab where i uploaded the fullmodel.pt it gives the error AttributeError: Can't get attribute 'MainModel' on <module 'main'> when loading the model with torch.load("fullmodel.pth")..

— You are receiving this because you commented. Reply to this email directly, view it on GitHub https://github.com/moein-shariatnia/Deep-Learning/issues/1#issuecomment-751494569, or unsubscribe https://github.com/notifications/unsubscribe-auth/ARXTC26FYROP5TKF2PRY2UTSW5VPJANCNFSM4VK2HBJQ .

masudrehman commented 3 years ago

Thanks..it works now perfectly...

On Tue, Dec 29, 2020, 3:54 PM moein-shariatnia notifications@github.com wrote:

Hey, you should be actually saving "model.state_dict()" instead of "model" itself. I think this would resolve your error.

On Sun, Dec 27, 2020 at 9:01 PM masudbarki notifications@github.com wrote:

also sir if you help me with this error AttributeError: Can't get attribute 'MainModel' on <module 'main'> this error happens when i saved the model with torch.save(model," fullmodel.pt') when i was using the same colab in which i have done the training path, the torch.load("fullmodel.pt") worked perfectly.. but in another colab where i uploaded the fullmodel.pt it gives the error AttributeError: Can't get attribute 'MainModel' on <module 'main'> when loading the model with torch.load("fullmodel.pth")..

— You are receiving this because you commented. Reply to this email directly, view it on GitHub < https://github.com/moein-shariatnia/Deep-Learning/issues/1#issuecomment-751494569 , or unsubscribe < https://github.com/notifications/unsubscribe-auth/ARXTC26FYROP5TKF2PRY2UTSW5VPJANCNFSM4VK2HBJQ

.

— You are receiving this because you modified the open/close state. Reply to this email directly, view it on GitHub https://github.com/moein-shariatnia/Deep-Learning/issues/1#issuecomment-752034171, or unsubscribe https://github.com/notifications/unsubscribe-auth/AOUBCEK4ZFMHXP5GTTOCXN3SXGYP7ANCNFSM4VK2HBJQ .

radudiaconu0 commented 2 years ago

sir, How to do prediction on single image?

Hey,

If you are following along the Colab notebook, you can use the following code snippet to do prediction on a single image and visualize the final result. Note that you need to define the model (as noted in the notebook) and load the pretrained weights that I've provided there:


img = PIL.Image.open("black_and_white.jpg")

img = img.resize((256, 256))

# to make it between -1 and 1

img = transforms.ToTensor()(img)[:1] * 2. - 1.

model.eval()

with torch.no_grad():

    preds = model.net_G(img.unsqueeze(0).to(device))

plt.imshow(lab_to_rgb(img.unsqueeze(0), preds.cpu())[0])

I hope this helps you out. Please let me know if there is any problem. Good Luck!

Why we transforms it between -1 and 1 and not 0 1

moein-shariatnia commented 2 years ago

Why we transforms it between -1 and 1 and not 0 1 @radudiaconu0

The model has been trained in this way so the evaluation must be done in the same way. The generator architecture contains a Tanh function in the last layer, so, the generated image is between -1 and 1. So, the real images coming to discriminator should be in the same range. That's the reason why we scale between -1 and 1 in the dataset.