def predict_classes(img: np.ndarray, classifier: MobileNetV2) -> dict: """Predict classification of an image. Args: img (np.ndarray): Image, preprocessed for MobileNetv2 classifier (MobileNetV2): Instance of classifier. Returns: dict: Predicted classes and their assigned probability. """ predictions = classifier.predict(img) prediction = decode_predictions(predictions) classes = dict([(i[1], float(i[2])) for i in prediction[0]]) return classes
import tensorflow as tf from tensorflow.keras.applications.mobilenet_v2 import MobileNetV2 as Net from tensorflow.keras.preprocessing import image from tensorflow.keras.applications.mobilenet import preprocess_input, decode_predictions import numpy as np import time model = Net(weights='imagenet') img_path = 'images/grace_hopper.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]) times = [] for i in range(20): start_time = time.time() preds = model.predict(x) delta = (time.time() - start_time) times.append(delta) mean_delta = np.array(times).mean() fps = 1 / mean_delta print('average(sec):{:.2f},fps:{:.2f}'.format(mean_delta, fps))