Hi there, thanks for getting back to me. Keras inference with this model is with:
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
from tensorflow.keras.preprocessing import image
import os
import numpy as np
from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 as Net
model = Net(weights='imagenet')
img_path = '/home/user/keras_tensorrt_inference/mobilenetv2/tabby_tiger_cat.jpg'
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
preds = model.predict(x)
# decode the results into a list of tuples (class, description, probability)
# (one such list for each sample in the batch)
print('Predicted:', decode_predictions(preds, top=3)[0])
I hadn’t actually considered that the preprocessing steps could have impacted things like this, that fixes it, thanks!
I’ve ammended the function you reference, and the output is correct!
from tensorflow.keras.applications.mobilenet_v2 import preprocess_input, decode_predictions
def load_normalized_test_case(test_image, pagelocked_buffer):
# Converts the input image to a CHW Numpy array
def normalize_image(image):
# Resize, antialias and transpose the image to CHW.
c, h, w = ModelData.INPUT_SHAPE
x = np.asarray(image.resize((w, h), Image.ANTIALIAS)).transpose([2, 0, 1])
x = preprocess_input(x)
return x.astype(trt.nptype(ModelData.DTYPE)).ravel()
# Normalize the image and copy to pagelocked memory.
np.copyto(pagelocked_buffer, normalize_image(Image.open(test_image)))
return test_image