This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
img_laptop_url = "https://upload.wikimedia.org/wikipedia/commons/9/90/ThinkPad_X220.jpg" | |
img_laptop = PIL.Image.open(BytesIO(requests.get(img_laptop_url).content)) | |
imshow(np.asarray(img_laptop)) | |
img_golden_url = "https://upload.wikimedia.org/wikipedia/commons/9/93/Golden_Retriever_Carlos_%2810581910556%29.jpg" | |
img_golden = PIL.Image.open(BytesIO(requests.get(img_golden_url).content)) | |
imshow(np.asarray(img_golden)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
%matplotlib inline #will be useful for images preview | |
from matplotlib.pyplot import imshow | |
import tensorflow as tf | |
import coremltools | |
#For easier images processing | |
import numpy as np | |
import PIL | |
import requests |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Core ML model generated. Saved at location: mobilenet_v2_1.0_224.mlmodel | |
Core ML input(s): | |
[name: "input__0" | |
type { | |
imageType { | |
width: 224 | |
height: 224 | |
colorSpace: RGB | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Convert TensorFlow model to Core ML format | |
# Input model definition | |
IMAGE_INPUT_NAME = ["input:0"] | |
IMAGE_INPUT_NAME_SHAPE = {'input:0':[1,224,224,3]} | |
IMAGE_INPUT_SCALE = 1.0/255.0 | |
OUTPUT_NAME = ['MobilenetV2/Predictions/Reshape_1:0'] | |
MODEL_LABELS = 'ImageNetLabels.txt' | |
# Output model |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!cat mobilenet_v2_1.0_224_info.txt | |
# Output | |
# >> Model: mobilenet_v2_1.0_224 | |
# >> Input: input | |
# >> Output: MobilenetV2/Predictions/Reshape_1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Download and unpack MobileNet v2 model from https://www.tensorflow.org/lite/guide/hosted_models | |
!curl -LO http://download.tensorflow.org/models/tflite_11_05_08/mobilenet_v2_1.0_224.tgz | |
!curl -LO https://storage.googleapis.com/download.tensorflow.org/data/ImageNetLabels.txt | |
!tar -xvzf mobilenet_v2_1.0_224.tgz | |
# List of unpacked files | |
# >> mobilenet_v2_1.0_224.ckpt.data-00000-of-00001 | |
# >> mobilenet_v2_1.0_224.ckpt.index | |
# >> mobilenet_v2_1.0_224.ckpt.meta | |
# >> mobilenet_v2_1.0_224_eval.pbtxt |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
!pip install tfcoreml | |
import tensorflow as tf | |
import tfcoreml | |
# TensorFlow 2.0 isn't yet supported. Make sure you use 1.x | |
print("TensorFlow version {}".format(tf.__version__)) | |
print("Eager mode: ", tf.executing_eagerly()) | |
print("Is GPU available: ", tf.test.is_gpu_available()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Concatenation of argmax and max value for each row | |
def max_values_only(data): | |
argmax_col = np.argmax(data, axis=1).reshape(-1, 1) | |
max_col = np.max(data, axis=1).reshape(-1, 1) | |
return np.concatenate([argmax_col, max_col], axis=1) | |
# Build simplified prediction tables | |
tf_model_pred_simplified = max_values_only(tf_model_predictions) | |
tflite_model_pred_simplified = max_values_only(tflite_model_predictions) | |
tflite_q_model_pred_simplified = max_values_only(tflite_q_model_predictions) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Concatenate results from all models | |
all_models_dataframe = pd.concat([tf_pred_dataframe, | |
tflite_pred_dataframe, | |
tflite_q_pred_dataframe], | |
keys=['TF Model', 'TFLite', 'TFLite quantized'], | |
axis='columns') | |
# Swap columns to hava side by side comparison | |
all_models_dataframe = all_models_dataframe.swaplevel(axis='columns')[tflite_pred_dataframe.columns] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Set batch of images into input tensor | |
tflite_interpreter.set_tensor(input_details[0]['index'], val_image_batch) | |
# Run inference | |
tflite_interpreter.invoke() | |
# Get prediction results | |
tflite_model_predictions = tflite_interpreter.get_tensor(output_details[0]['index']) | |
print("Prediction results shape:", tflite_model_predictions.shape) | |
# >> Prediction results shape: (32, 5) |