question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`AttributeError: 'JpegImageFile' object has no attribute 'shape'`

See original GitHub issue

I was trying the code with the random image but I need to print the image I am passing nottouch.rand

my_image = load_img("127074.jpg")
v = Vit()
img = torch.randn(1, 3, 256, 256)
preds = v(img)
print(preds)
p=v(my_image)
print(p)

the code worked with using random but when i pass my_image i got AttributeError: 'JpegImageFile' object has no attribute 'shape'

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:14

github_iconTop GitHub Comments

1reaction
lessw2020commented, Sep 26, 2021

Hi @ersamo Yes exactly. In the above, from the error…your image is on gpu, your model is on cpu. To fix model.to(pred_device)

Then both are on gpu and you should be cruising.

1reaction
lessw2020commented, Sep 26, 2021

dinner was delayed…so here you go:

import torch
import torchvision
from PIL import Image
from torch import Tensor
import torchvision.transforms as T


inference_transform = T.Compose(
    [
        T.Resize(256),
        T.ToTensor(),
        T.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225]),  # Imagenet
    ]
)

pred_device=torch.device('cuda' if torch.cuda.is_available() else 'cpu')

# load image, prep for inference and run it
orig_img = Image.open("myimage.jpg")

img_tensor = inference_transform(orig_img)

img_tensor = img_tensor.unsqueeze(0)

tensor_on_device = img_tensor.to(pred_device)

output = model(tensor_on_device)
Read more comments on GitHub >

github_iconTop Results From Across the Web

AttributeError: 'JpegImageFile' object has no attribute 'shape' #2
I run code in Python 3.6, I haven't 3.7 available, When I run python optimize_anchors.py annotations.csv I get the error: Using TensorFlow ...
Read more >
AttributeError: 'JpegImageFile' object has no attribute 'read'
This is the error that I need to fix now. AttributeError: 'JpegImageFile' object has no attribute 'read'. Code: # Imports here import pandas ......
Read more >
“AttributeError: 'JpegImageFile' object has no attribute 'shape ...
The problem is that pimg is in PIL image format. While imutils.resize function expects the image in Numpy array format.
Read more >
AttributeError: 'JpegImageFile' object has no attribute 'read'
I am a beginner and I am learning to code an image classifier. My goal is to create a predict function. In this...
Read more >
AttributeError: 'JpegImageFile' object has no attribute 'read'
Coding example for the question AttributeError: 'JpegImageFile' object has no attribute 'read'-Opencv.
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found