Chapter 111
Convert Keras to TF-Lite
NotebookPython 3 (ipykernel)36 cells
In [1]python · cell 1
python
%autosave 0Output
Autosave disabled
In [2]python · cell 2
python
!wget https://github.com/DataTalksClub/machine-learning-zoomcamp/releases/download/chapter7-model/xception_v4_large_08_0.894.h5 -O clothing-model.h5Output
--2021-11-20 22:29:34-- https://github.com/alexeygrigorev/mlbookcamp-code/releases/download/chapter7-model/xception_v4_large_08_0.894.h5 Resolving github.com (github.com)... 140.82.121.4 Connecting to github.com (github.com)|140.82.121.4|:443... connected. HTTP request sent, awaiting response... 302 Found Location: https://objects.githubusercontent.com/github-production-release-asset-2e65be/256401220/0156a400-0049-11eb-8490-c0d01b48ea8c?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20211120%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20211120T222934Z&X-Amz-Expires=300&X-Amz-Signature=2b0ca2655863a50c3ce11098e1da1b35da810a1b4c0cef1cc509ab75688512d1&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=256401220&response-content-disposition=attachment%3B%20filename%3Dxception_v4_large_08_0.894.h5&response-content-type=application%2Foctet-stream [following] --2021-11-20 22:29:34-- https://objects.githubusercontent.com/github-production-release-asset-2e65be/256401220/0156a400-0049-11eb-8490-c0d01b48ea8c?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAIWNJYAX4CSVEH53A%2F20211120%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20211120T222934Z&X-Amz-Expires=300&X-Amz-Signature=2b0ca2655863a50c3ce11098e1da1b35da810a1b4c0cef1cc509ab75688512d1&X-Amz-SignedHeaders=host&actor_id=0&key_id=0&repo_id=256401220&response-content-disposition=attachment%3B%20filename%3Dxception_v4_large_08_0.894.h5&response-content-type=application%2Foctet-stream Resolving objects.githubusercontent.com (objects.githubusercontent.com)... 185.199.111.133, 185.199.108.133, 185.199.109.133, ... Connecting to objects.githubusercontent.com (objects.githubusercontent.com)|185.199.111.133|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 86185888 (82M) [application/octet-stream] Saving to: ‘clothing-model.h5’ clothing-model.h5 100%[===================>] 82.19M 75.8MB/s in 1.1s 2021-11-20 22:29:38 (75.8 MB/s) - ‘clothing-model.h5’ saved [86185888/86185888]
In [1]python · cell 3
python
!python -VOutput
Python 3.9.7
In [2]python · cell 4
python
import numpy as np
import tensorflow as tf
from tensorflow import keras
tf.__version__Output
'2.7.0'
In [3]python · cell 5
python
!wget http://bit.ly/mlbookcamp-pants -O pants.jpgOutput
--2022-05-31 16:28:06-- http://bit.ly/mlbookcamp-pants
Resolving bit.ly (bit.ly)... 67.199.248.10, 67.199.248.11
Connecting to bit.ly (bit.ly)|67.199.248.10|:80... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://raw.githubusercontent.com/alexeygrigorev/clothing-dataset-small/master/test/pants/4aabd82c-82e1-4181-a84d-d0c6e550d26d.jpg [following]
--2022-05-31 16:28:06-- https://raw.githubusercontent.com/alexeygrigorev/clothing-dataset-small/master/test/pants/4aabd82c-82e1-4181-a84d-d0c6e550d26d.jpg
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 185.199.108.133, 185.199.109.133, 185.199.110.133, ...
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|185.199.108.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 23048 (23K) [image/jpeg]
Saving to: 'pants.jpg'
0K .......... .......... .. 100% 2.07M=0.01s
2022-05-31 16:28:06 (2.07 MB/s) - 'pants.jpg' saved [23048/23048]
In [4]python · cell 6
python
from tensorflow.keras.preprocessing.image import load_img
from tensorflow.keras.applications.xception import preprocess_inputIn [5]python · cell 7
python
model = keras.models.load_model('clothing-model.h5')In [6]python · cell 8
python
img = load_img('pants.jpg', target_size=(299, 299))
x = np.array(img)
X = np.array([x])
X = preprocess_input(X)In [7]python · cell 9
python
preds = model.predict(X)In [8]python · cell 10
python
predsOutput
array([[-1.8682919, -4.761245 , -2.3169832, -1.0625719, 9.887158 ,
-2.812432 , -3.6662836, 3.200363 , -2.6023405, -4.835046 ]],
dtype=float32)In [9]python · cell 11
python
classes = [
'dress',
'hat',
'longsleeve',
'outwear',
'pants',
'shirt',
'shoes',
'shorts',
'skirt',
't-shirt'
]In [10]python · cell 12
python
dict(zip(classes, preds[0]))Output
{'dress': -1.8682919,
'hat': -4.761245,
'longsleeve': -2.3169832,
'outwear': -1.0625719,
'pants': 9.887158,
'shirt': -2.812432,
'shoes': -3.6662836,
'shorts': 3.200363,
'skirt': -2.6023405,
't-shirt': -4.835046}Convert Keras to TF-Lite
In [20]python · cell 14
python
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model = converter.convert()
with open('clothing-model.tflite', 'wb') as f_out:
f_out.write(tflite_model)Output
2021-11-20 22:42:03.897442: W tensorflow/python/util/util.cc:368] Sets are not currently considered sequences, but this may change in the future, so consider avoiding using them.
INFO:tensorflow:Assets written to: /tmp/tmp1igj75zx/assets
2021-11-20 22:42:32.793557: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:363] Ignored output_format.
2021-11-20 22:42:32.793604: W tensorflow/compiler/mlir/lite/python/tf_tfl_flatbuffer_helpers.cc:366] Ignored drop_control_dependency.
2021-11-20 22:42:32.794886: I tensorflow/cc/saved_model/reader.cc:43] Reading SavedModel from: /tmp/tmp1igj75zx
2021-11-20 22:42:32.859041: I tensorflow/cc/saved_model/reader.cc:107] Reading meta graph with tags { serve }
2021-11-20 22:42:32.859087: I tensorflow/cc/saved_model/reader.cc:148] Reading SavedModel debug info (if present) from: /tmp/tmp1igj75zx
2021-11-20 22:42:33.089582: I tensorflow/cc/saved_model/loader.cc:210] Restoring SavedModel bundle.
2021-11-20 22:42:33.859358: I tensorflow/cc/saved_model/loader.cc:194] Running initialization op on SavedModel bundle at path: /tmp/tmp1igj75zx
2021-11-20 22:42:34.170920: I tensorflow/cc/saved_model/loader.cc:283] SavedModel load for tags { serve }; Status: success: OK. Took 1376037 microseconds.
2021-11-20 22:42:34.864702: I tensorflow/compiler/mlir/tensorflow/utils/dump_mlir_util.cc:237] disabling MLIR crash reproducer, set env var `MLIR_CRASH_REPRODUCER_DIRECTORY` to enable.
WARNING:absl:Buffer deduplication procedure will be skipped when flatbuffer library is not properly loaded
In [21]python · cell 15
python
!ls -lhOutput
total 163M -rw-rw-r-- 1 ubuntu ubuntu 83M Aug 17 10:47 clothing-model.h5 -rw-rw-r-- 1 ubuntu ubuntu 81M Nov 20 22:42 clothing-model.tflite -rw-rw-r-- 1 ubuntu ubuntu 23K Nov 20 22:33 pants.jpg -rw-rw-r-- 1 ubuntu ubuntu 947 Nov 20 22:12 plan.md -rw-rw-r-- 1 ubuntu ubuntu 6.1K Nov 20 22:21 tensorflow-model.ipynb
In [22]python · cell 16
python
import tensorflow.lite as tfliteIn [23]python · cell 17
python
interpreter = tflite.Interpreter(model_path='clothing-model.tflite')
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]['index']
output_index = interpreter.get_output_details()[0]['index']In [30]python · cell 18
python
interpreter.set_tensor(input_index, X)
interpreter.invoke()
preds = interpreter.get_tensor(output_index)In [34]python · cell 19
python
classes = [
'dress',
'hat',
'longsleeve',
'outwear',
'pants',
'shirt',
'shoes',
'shorts',
'skirt',
't-shirt'
]
dict(zip(classes, preds[0]))Output
{'dress': -1.8682897,
'hat': -4.7612453,
'longsleeve': -2.316984,
'outwear': -1.0625705,
'pants': 9.887156,
'shirt': -2.8124316,
'shoes': -3.6662838,
'shorts': 3.2003622,
'skirt': -2.6023388,
't-shirt': -4.8350453}Removing TF dependency
In [36]python · cell 21
python
from PIL import ImageIn [38]python · cell 22
python
with Image.open('pants.jpg') as img:
img = img.resize((299, 299), Image.NEAREST)In [45]python · cell 23
python
def preprocess_input(x):
x /= 127.5
x -= 1.
return xIn [47]python · cell 24
python
x = np.array(img, dtype='float32')
X = np.array([x])
X = preprocess_input(X)In [49]python · cell 25
python
interpreter.set_tensor(input_index, X)
interpreter.invoke()
preds = interpreter.get_tensor(output_index)In [50]python · cell 26
python
classes = [
'dress',
'hat',
'longsleeve',
'outwear',
'pants',
'shirt',
'shoes',
'shorts',
'skirt',
't-shirt'
]
dict(zip(classes, preds[0]))Output
{'dress': -1.8682897,
'hat': -4.7612453,
'longsleeve': -2.316984,
'outwear': -1.0625705,
'pants': 9.887156,
'shirt': -2.8124316,
'shoes': -3.6662838,
'shorts': 3.2003622,
'skirt': -2.6023388,
't-shirt': -4.8350453}Simpler way of doing it
In [52]python · cell 28
python
!pip install keras-image-helperOutput
Collecting keras-image-helper Using cached keras_image_helper-0.0.1-py3-none-any.whl (4.6 kB) Requirement already satisfied: numpy in /home/ubuntu/miniconda3/lib/python3.8/site-packages (from keras-image-helper) (1.21.4) Requirement already satisfied: pillow in /home/ubuntu/miniconda3/lib/python3.8/site-packages (from keras-image-helper) (8.4.0) Installing collected packages: keras-image-helper Successfully installed keras-image-helper-0.0.1
In [7]python · cell 29
python
!pip install --extra-index-url https://google-coral.github.io/py-repo/ tflite_runtimeOutput
Looking in indexes: https://pypi.org/simple, https://google-coral.github.io/py-repo/ Collecting tflite_runtime Downloading https://github.com/google-coral/pycoral/releases/download/v2.0.0/tflite_runtime-2.5.0.post1-cp38-cp38-linux_x86_64.whl (1.5 MB) [K |████████████████████████████████| 1.5 MB 4.6 MB/s eta 0:00:01 [?25hRequirement already satisfied: numpy>=1.16.0 in /home/ubuntu/miniconda3/lib/python3.8/site-packages (from tflite_runtime) (1.21.4) Installing collected packages: tflite-runtime Successfully installed tflite-runtime-2.5.0.post1
In [3]python · cell 30
python
#import tensorflow.lite as tflite
import tflite_runtime.interpreter as tflite
from keras_image_helper import create_preprocessorIn [4]python · cell 31
python
interpreter = tflite.Interpreter(model_path='clothing-model.tflite')
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]['index']
output_index = interpreter.get_output_details()[0]['index']In [5]python · cell 32
python
preprocessor = create_preprocessor('xception', target_size=(299, 299))In [6]python · cell 33
python
url = 'http://bit.ly/mlbookcamp-pants'
X = preprocessor.from_url(url)In [7]python · cell 34
python
interpreter.set_tensor(input_index, X)
interpreter.invoke()
preds = interpreter.get_tensor(output_index)In [8]python · cell 35
python
classes = [
'dress',
'hat',
'longsleeve',
'outwear',
'pants',
'shirt',
'shoes',
'shorts',
'skirt',
't-shirt'
]
dict(zip(classes, preds[0]))Output
{'dress': -1.8682901,
'hat': -4.7612457,
'longsleeve': -2.3169823,
'outwear': -1.0625706,
'pants': 9.8871565,
'shirt': -2.8124304,
'shoes': -3.6662836,
'shorts': 3.200361,
'skirt': -2.6023388,
't-shirt': -4.835045}In [ ]python · cell 36
python
