Traktor/app/loadFromSavedModel.py
2021-06-01 18:01:11 +02:00

31 lines
1.1 KiB
Python

from tensorflow.keras.models import load_model
from tensorflow import keras as k
import numpy as np
from config import RESOURCE_DIR
import os
# Load the model
model = load_model(
os.path.join(RESOURCE_DIR, "saved_model"),
custom_objects=None,
compile=True
)
# Data fror predictions
img_width, img_height, img_num_channels = 25, 25, 3
labels = ["cabbage", "carrot", "corn", "lettuce", "paprika", "potato", "tomato"]
# Predictions
filename = 'Image_1.jpg'
loaded_image = k.preprocessing.image.load_img(path=RESOURCE_DIR + '/smaller_test/potato/' + filename,
target_size=(img_width, img_height, img_num_channels))
# convert to array and resample dividing by 255
img_array = k.preprocessing.image.img_to_array(loaded_image) / 255.
# add sample dimension. the predictor is expecting (1, CHANNELS, IMG_WIDTH, IMG_HEIGHT)
img_np_array = np.expand_dims(img_array, axis=0)
predictions = model.predict(img_np_array)
prediction = np.argmax(predictions[0])
print(f'Ground truth: {filename} - Prediction: {labels[prediction]}')