I have a problem with

I have a code as below. However ti gives me an error
which is not fixed by any advice I’ve seen on many sites including overflow*[Moderated by Moderator]*, tensorflow.org,jupyter etc.

import numpy as np
import tensorflow as tf
from tensorflow.keras.applications import MobileNetV2
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image

# Load the pre-trained MobileNetV2 model
model = MobileNetV2(weights='imagenet', include_top=True)

# Load and preprocess an image
# Replace 'path/to/your/image.jpg' with the actual file path
img_path = 'path/to/your/image.jpg'
img = image.load_img(img_path, target_size=(224, 224))  # MobileNetV2 expects images of size 224x224
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)

# Make a prediction
predictions = model.predict(x)

# Decode and print the top-3 predicted classes
for _, pred_class, prob in decode_predictions(predictions, top=3)[0]:
    print(f"Predicted: {pred_class} with probability {prob}")

Hi @Jan_Pax, I have tried to execute the above code in colab to get the top 3 predictions of the model. I did not face any error. Please refer to this gist for working code example. Thank You.

Of course that you didn’t face any errors in Colab. I do not see any errors in Jupyter Notebook 7.0 and last release tensorflow in my environment either. But I do see errors in TF 2.10 and JN <7.0 ! Even with this I don’t see a correct output which should be much larger than I’m obtaining:


['__doc__', '__file__', '__loader__', '__name__', '__package__', '__path__', '__spec__']

I need to use TF 2.10 because this is the last release that works for GPU and not just CPU.