<jupyter_start><jupyter_text>TensorFlow Lite軽量化  事前準備<jupyter_code>%tensorflow_version 2.x
import tensorflow as tf
print(tf.__version__)
from tensorflow.keras.preprocessing.image import img_to_array, load_img
import numpy as np
import time<jupyter_output><empty_output><jupyter_text>テスト画像をダウンロードします。<jupyter_code>!wget https://raw.githubusercontent.com/karaage0703/janken_dataset/master/choki/choki_01.jpg
from IPython.display import Image as IPImage
from IPython.display import display_jpeg
display_jpeg(IPImage('choki_01.jpg'))<jupyter_output><empty_output><jupyter_text>モデルとラベルをGoogle Driveからコピーします。<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
!cp '/content/drive/My Drive/my_model.h5' './my_model.h5'
!cp '/content/drive/My Drive/efficientnet_model.h5' './efficientnet_model.h5'
!cp '/content/drive/My Drive/labels.txt' './labels.txt'
!ls -lh<jupyter_output>total 124M
-rw-r--r-- 1 root root  28K Nov 22 14:33 choki_01.jpg
drwx------ 5 root root 4.0K Nov 22 14:33 drive
-rw------- 1 root root  34M Nov 22 14:33 efficientnet_model.h5
-rw------- 1 root root   12 Nov 22 14:33 labels.txt
-rw------- 1 root root  91M Nov 22 14:33 my_model.h5
drwxr-xr-x 1 root root 4.0K Nov 13 17:33 sample_data<jupyter_text>ラベルを読み込みます<jupyter_code>labels = []
with open('./labels.txt','r') as f:
  for line in f:
    labels.append(line.rstrip())
print(labels)

NUM_CLASSES = len(labels)<jupyter_output>['choki', 'gu', 'pa']<jupyter_text>モデルの変換 モデルを読み込みます MNISTベースの自作モデルを読み込みます。<jupyter_code>keras_mnist_model = tf.keras.models.load_model("my_model.h5")<jupyter_output><empty_output><jupyter_text>TFLiteモデルに変換します。<jupyter_code>converter = tf.lite.TFLiteConverter.from_keras_model(keras_mnist_model)

tflite_model = converter.convert()
open("mnist_model.tflite", "wb").write(tflite_model)<jupyter_output>WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/tracking.py:111: Model.state_updates (from tensorflow.python.keras.engine.training) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
WARNING:tensorflow:From /usr/local/lib/python3.6/dist-packages/tensorflow/python/training/tracking/tracking.py:111: Layer.updates (from tensorflow.python.keras.engine.base_layer) is deprecated and will be removed in a future version.
Instructions for updating:
This property should not be used in TensorFlow 2.0, as updates are applied automatically.
INFO:tensorflow:Assets written to: /tmp/tmpw9dr7w4c/assets<jupyter_text>EfficientNetモデルを読み込みます。<jupyter_code># keras_model = tf.keras.models.load_model("efficientnet_model.h5")

import tensorflow_hub as hub
keras_efficientnet_model = tf.keras.models.load_model('efficientnet_model.h5', custom_objects={'KerasLayer':hub.KerasLayer})<jupyter_output>WARNING:tensorflow:Error in loading the saved optimizer state. As a result, your model is starting with a freshly initialized optimizer.<jupyter_text>https://github.com/tensorflow/tensorflow/issues/26835 モデルを変換します。<jupyter_code>converter = tf.lite.TFLiteConverter.from_keras_model(keras_efficientnet_model)

tflite_model = converter.convert()
open("efficientnet_model.tflite", "wb").write(tflite_model)<jupyter_output>INFO:tensorflow:Assets written to: /tmp/tmpx2bx2m0_/assets<jupyter_text>モデルの性能（推論時間）を比較  軽量化前のモデル データの変換<jupyter_code>img = img_to_array(load_img('choki_01.jpg', target_size=(64,64)))
img = img / 255.0
img = img[np.newaxis]
img = np.asarray(img)<jupyter_output><empty_output><jupyter_text>MNISTベースモデルの推論<jupyter_code>%%time
preds = keras_mnist_model.predict(img)
result = np.argmax(preds[0])
print(labels[result])<jupyter_output>choki
CPU times: user 48.4 ms, sys: 0 ns, total: 48.4 ms
Wall time: 51.1 ms<jupyter_text>EfficientNetモデルの推論<jupyter_code>%%time
preds = keras_efficientnet_model.predict(img)
result = np.argmax(preds[0])
print(labels[result])<jupyter_output>choki
CPU times: user 50.1 ms, sys: 6.03 ms, total: 56.2 ms
Wall time: 55.2 ms<jupyter_text>軽量化したモデル MNISTモデルのTFLiteファイルを読み込みます。<jupyter_code>interpreter = tf.lite.Interpreter(model_path="mnist_model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
print(input_details)
print(output_details)<jupyter_output>[{'name': 'Identity', 'index': 16, 'shape': array([1, 3], dtype=int32), 'shape_signature': array([-1,  3], dtype=int32), 'dtype': <class 'numpy.float32'>, 'quantization': (0.0, 0), 'quantization_parameters': {'scales': array([], dtype=float32), 'zero_points': array([], dtype=int32), 'quantized_dimension': 0}, 'sparsity_parameters': {}}]<jupyter_text>データの変換<jupyter_code>img = img_to_array(load_img('choki_01.jpg', target_size=(64,64)))
img = img / 255.0
img = img[np.newaxis]
img = np.asarray(img)
input_tensor = tf.convert_to_tensor(img)<jupyter_output><empty_output><jupyter_text>推論<jupyter_code>%%time
# set input tensor
interpreter.set_tensor(input_details[0]['index'], input_tensor)

# Inference
interpreter.invoke()

scores = interpreter.get_tensor(output_details[0]['index'])
result = np.argmax(scores[0])
print(labels[result])<jupyter_output>choki
CPU times: user 10.9 ms, sys: 79 µs, total: 11 ms
Wall time: 11.8 ms<jupyter_text>EfficientNetモデルのTFLiteファイルを読み込みます。<jupyter_code>interpreter = tf.lite.Interpreter(model_path="efficientnet_model.tflite")
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()<jupyter_output><empty_output><jupyter_text>推論<jupyter_code>%%time
# set input tensor
interpreter.set_tensor(input_details[0]['index'], input_tensor)

# Inference
interpreter.invoke()

scores = interpreter.get_tensor(output_details[0]['index'])
result = np.argmax(scores[0])
print(labels[result])<jupyter_output>choki
CPU times: user 12.6 ms, sys: 0 ns, total: 12.6 ms
Wall time: 10.9 ms
{'fraction_non_alphanumeric': 0.13020745488277957, 'fraction_numerical': 0.03238320121437004, 'mean_word_length': 7.733431516936672, 'pattern_counts': {'":': 0, '<': 56, '<?xml version=': 0, '>': 56, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '16', 'dir': 'jupyter-structured-clean-dedup', 'id': '329977', 'license': 'MIT', 'n_tokens_mistral': 2449, 'n_tokens_neox': 2316, 'n_words': 390, 'path': 'ch06/06_karaage_ai_book_tflite_convert.ipynb', 'repo_name': 'karaage0703/.github', 'size': '5929'}