TensoroFlow Lite简介
TensoroFlow Lite功能特性
加速方式
TensoroFlow Lite模型转换
模型转换流程:
基于savedmodel转换为TFLite
import tensorflow as tf
import pathlib
#创建数据
x=[-1,0,1,2,3,4]
y=[-3,-1,1,3,5,7]
#创建模型
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(units=1,input_shape=[1])
])
model.compile(optimizer='sgd',loss='mean_squared_error')
model.fit(x,y,epochs=500)
#export the savedModel 将模型导出到该目录下
export_dir='/tmp/saved_model'
tf.saved_model.save(model,export_dir)
#convert the model 转换模型
converter=tf.lite.TFLiteConverter.from_saved_model(export_dir)
tflite_model = converter.convert()
#save the model 保存模型
tflite_model_file=pathlib.Path('/tmp/foo.tflite')
tflite_model_file.write_bytes(tflite_model)
基于keras转换为TFLite,基于图像处理
import tensorflow as tf
import pathlib
#创建数据
x=[-1,0,1,2,3,4]
y=[-3,-1,1,3,5,7]
#创建模型
model = tf.keras.applications.MobileNetV2(weight="imagenet",input_shape=(224,224,3))
model.save('model.h5')
converter = tf.lite.TFLiteConverter.from_keras_model(model)
tflite_model=converter.convert()
#save the model
tflite_model_file=pathlib.Path('/tmp/foo.tflite')
tflite_model_file.write_bytes(tflite_model)
import tensorflow as tf
import pathlib
#校准方式
def generator():
data=tfds.load()
for _ in range(num_calibration_steps):
image,=data.take(1)
yield [image]
converter=tf.lite.TFLiteConverter.from_saved_model(saved_model_dir)
#优化方式
converter.optimizations=[tf.lite.Optimize.DEFAULT]
converter.representative_dataset=tf.lite.RepresentativeDataset(generator)
import tensorflow as tf
import pathlib
import numpy as np
import matplotlib.pyplot as plt
#创建数据
x=[-1,0,1,2,3,4]
y=[-3,-1,1,3,5,7]
#创建模型
model = tf.keras.models.Sequential([
tf.keras.layers.Dense(units=1,input_shape=[1])
])
model.compile(optimizer='sgd',loss='mean_squared_error')
model.fit(x,y,epochs=500)
#export the savedModel 将模型导出到该目录下
export_dir='/tmp/saved_model'
tf.saved_model.save(model,export_dir)
#convert the model 转换模型
converter=tf.lite.TFLiteConverter.from_saved_model(export_dir)
tflite_model = converter.convert()
#save the model 保存模型
tflite_model_file=pathlib.Path('/tmp/foo.tflite')
tflite_model_file.write_bytes(tflite_model)
interpreter = tf.lite.Interpreter(model_content=tflite_model)
interpreter.allocate_tensors()
input_details = interpreter.get_input_details()
output_details = interpreter.get_output_details()
input_shape = input_details[0]['shape']
inputs,outputs = [],[]
for _ in range(100):
input_data = np.array(np.random.random_sample(input_shape),dtype=np.float32)
interpreter.set_tensor(input_details[0]['index'],input_data)
interpreter.invoke()
tflite_results = interpreter.get_tensor(output_details[0]['index'])
tflite_results=model(tf.constant(input_data))
output_data = np.array(tflite_results)
inputs.append(input_data[0][0])
outputs.append(output_data[0][0])
plt.plot(inputs,outputs,'r')
plt.show()
TensoroFlow Lite模型运行
迁移学习
import numpy as np
import matplotlib.pyplot as plt
import tensorflow as tf
import tensorflow_hub as hub
import tensorflow_datasets as tfds
tfds.disable_progress_bar()
from tqdm import tqdm
module_selection=("mobilenet_v2",224,1280)
handle_base,pixels,FV_SIZE = module_selection
MODUKE_HANDLE = ""
IMAGE_SIZE=(pixels,pixels)
splits = ['train[:80%]','train[80%:90%]','train:[90%]']
splits,info = tfds.load('cats_vs_dogs',with_info=True,as_supervised=True,split=splits)
(train_examples,validation_examples,test_examplts)=splits
num_examples = info.splits['train'].num_examples
num_classes = info.features['label'].num_classes
#格式化输入数据
def format_image(image,label):
image = tf.image.resize(image,IMAGE_SIZE)/255.0
return image,label
BATCH_SIZE=32
#多线程操作,加速预处理的过程
train_batches = train_examples.shuffle(num_examples//4).map(format_image).batch(BATCH_SIZE).prefetch(1)
validation_batches = validation_examples.map(format_image).batch(BATCH_SIZE).prefetch(1)
test_batches = test_examplts.map(format_image).batch(1)
for image_batch,label_batch in train_batches.take(1):
pass
print(image_batch.shape)
do_fine_tuning=False
feature_extractor=hub.KerasLayer(
MODUKE_HANDLE,
input_shape=IMAGE_SIZE+(3,),
output_shape=[FV_SIZE],
trainable=do_fine_tuning
)
model = tf.keras.Sequential([
feature_extractor,
tf.keras.layers.Dense(num_classes,activation='softmax')
])
model.summary()
#提高准确率
NUM_LAYERS=10
if do_fine_tuning:
feature_extractor.trainable=True
for layer in model.layers[-NUM_LAYERS:]:
layer.trainable=True
else:
feature_extractor.trainable=False
#Training the Model
if do_fine_tuning:
model.compile(optimizer=tf.keras.optimizers.SGD(lr=0.002,momentum=0.9),
loss=tf.keras.losses.SparseCategoricalCrossentropy(),
metrics=['accuracy'])
else:
model.compile(optimizer='adam',
loss='sqarse_categorical_crossentropy',
metrics=['accuracy'])
EPOCHS=2
his = model.fit(train_batches,
epochs=EPOCHS,
validation_data=validation_batches
)
#Export the Model
CATS_VS_DOGS_SAVED_MODEL='exp_saved_model'
tf.saved_model.save(model,CATS_VS_DOGS_SAVED_MODEL)
loaded = tf.saved_model.load(CATS_VS_DOGS_SAVED_MODEL)
print(list(loaded.signatures.keys()))
infer = loaded.signatures["serving_defaule"]
print(infer.structured_input_signature)
print(infer.structured_outputs)
#生成一个转换器
converter = tf.lite.TFLiteConverter.from_saved_model(CATS_VS_DOGS_SAVED_MODEL)
#确定优化方法
converter.optimizations=[tf.lite.Optimize.DEFAULT]
def representative_data_gen():
for input_value,_ in test_batches.take(100):
yield [input_value]
converter.representative_dataset = representative_data_gen
converter.target_spec.supported_ops=[tf.lite.OpsSet.TFLITE_BUILTINS_INT8]
tflite_model=converter.convert()
tflite_model_file='converted_model.tflite'
with open(tflite_model_file,"wb") as f:
f.write(tflite_model)
interpreter = tf.lite.Interpreter(model_path=tflite_model_file)
interpreter.allocate_tensors()
input_index = interpreter.get_input_details()[0]["index"]
output_index = interpreter.get_output_details()[0]["index"]
predictions = []
test_labels,test_imgs = [],[]
for img,label in tqdm(test_batches.take(10)):
interpreter.set_tensor(input_index,img)
interpreter.invoke()
predictions.append(interpreter.get_tensor(output_index))
test_labels.append(label.numpy()[0])
test_imgs.append(img)
class_name = ['cat','dog']
def plot_image(i,predictions_array,true_label,img):
predictions_array,true_label,img=predictions_array[i],true_label[i],img[i]
plt.grid(False)
plt.xticks([])
plt.yticks([])
img = np.squeeze(img)
plt.imshow(img,cmap=plt.cm.binary)
predicted_label=np.argmax(predictions_array)
if predicted_label==true_label:
color='green'
else:
color='red'
plt.xlabel("{} {:2.0f}%({})".format(class_name[predicted_label],
100*np.max(predictions_array),
class_name[true_label]),color=color)
index = 0
plt.figure(figsize=(6,3))
plt.subplot(1,2,1)
plot_image(index,predictions,test_labels,test_imgs)
plt.show()
labels = ['cat','dog']
with open('labels.txt','w') as f:
f.write('\n'.join(labels))
try:
from google.colab import files
files.download('converted_model.tflite')
files.download('labels.txt')
except:
pass
from PIL import Image
for index,(image,label) in enumerate(test_batches.take(50)):
image=tf.cast(image*255.0,tf.unit8)
image=tf.squeeze(image).numpy()
pil_image=Image.fromarray(image)
pil_image.save('test_images/{}_{}.jpg'.format(class_name[label[0]],index))
try:
files.download('cats_vs_test_images.zip')
except:
pass