I created a model that works fine. It’s a model of clothes classification, unfortunately, I don’t know how to use it with pictures that I download from the internet and they don’t share the same size. Do you know how to do it?
This is what I tried and it does not work.
from PIL import Image
import numpy as np
image = Image.open(r’C:\Users\tripa\OneDrive\desktop\remera.jpg’)
new_image = image.resize((28, 28))
pred_probs=model_14.predict(new_image)
print(pred_probs)
There are the following six steps to determine what object does the image contains?
- Load an image
- Resize it to a predefined size since pre-trained models expect the input to be of a specific size
- Scale the value of the pixels to the range [0, 255]
- Select a pre-trained model
- Run the pre-trained model
- Display the results
Complete working code to predict new image using resnet-50
pre-trained model
import tensorflow as tf
from tensorflow.keras.applications.resnet50 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
import numpy as np
import matplotlib.pyplot as plt
print(tf.__version__)
def classify_image(img_path):
img = image.load_img(img_path, target_size=(224, 224))
img_array = image.img_to_array(img)
img_batch = np.expand_dims(img_array, axis=0)
img_preprocessed = preprocess_input(img_batch)
model = tf.keras.applications.resnet50.ResNet50()
prediction = model.predict(img_preprocessed)
print(decode_predictions(prediction, top=3)[0])
classify("/content/jersey.jpeg")
Output:
2.8.2
Downloading data from https://storage.googleapis.com/tensorflow/keras-applications/resnet/resnet50_weights_tf_dim_ordering_tf_kernels.h5
102973440/102967424 [==============================] - 1s 0us/step
102981632/102967424 [==============================] - 1s 0us/step
Downloading data from https://storage.googleapis.com/download.tensorflow.org/data/imagenet_class_index.json
40960/35363 [==================================] - 0s 0us/step
49152/35363 [=========================================] - 0s 0us/step
[('n03595614', 'jersey', 0.99856997), ('n03710637', 'maillot', 0.00064364297), ('n04370456', 'sweatshirt', 0.00043315586)]