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.

How can I get the prediction of a single image?

See original GitHub issue

I want just to see the prediction of a single image using the following code:

`

Model reconstruction from JSON file

with open(‘model_struct.json’, ‘r’) as f: model = model_from_json(f.read())

Load weights into the new model

model.load_weights(‘best_weights.h5’)

Load the image file

parser = argparse.ArgumentParser(description=‘Image to test’) parser.add_argument(‘image’, type=str, help=‘The image image file to check’) args = parser.parse_args()

img = load_img(args.image)

Predict

prediction = model.predict(img) `

But it raises the following error: ValueError: Error when checking : expected input_1 to have 4 dimensions, but got array with shape (720, 960, 3)

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:6

github_iconTop GitHub Comments

1reaction
eridgdcommented, Oct 2, 2018

You need to have an additional dimension for batch, even with just one image:

img = load_img(args.image)
assert img.ndim == 3
img = np.expand_dims(img, axis=0) # Alternatively could do: img[None, ...]
assert img.ndim == 4
0reactions
msohaildanishcommented, Nov 2, 2019

I found the solution for this. I was using utils. load_img() to get image as np.array but in Dronet utils.callback_img() there is extra step before returning image as np array which is return np.asarray(img, dtype=np.float32) * np.float32(1.0/255.0) drone_control/dronet/dronet_perception/src/Dronet/utils.py

Read more comments on GitHub >

github_iconTop Results From Across the Web

Keras: model.predict for a single image - Stack Overflow
I'd like to make a prediction for a single image with Keras. I've trained my model so I'm just loading the weights.
Read more >
How to predict an image using CNN with Keras?
How to predict an image's type? · Load an image. · Resize it to a predefined size such as 224 x 224 pixels....
Read more >
Depth prediction from a single image with conditional ...
In this paper, we formulate this problem from single images as a deep adversarial learning framework. A two-stage convolutional network is designed as...
Read more >
Depth Map Prediction from a Single Image using a Multi-Scale ...
Predicting depth is an essential component in understanding the 3D geometry of a scene. While for stereo images local correspondence suffices for estimation ......
Read more >
Predicting on single image using Fastai Model - Beginner (2018)
You have to pass the image through the transformations before you feed it into the classifier. This was discussed at length here: How...
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