如何将 TensorFlow 模型转换为 TFLite 模型
Posted
技术标签:
【中文标题】如何将 TensorFlow 模型转换为 TFLite 模型【英文标题】:How to convert a Tensorflow model into a TFLite model 【发布时间】:2019-05-06 05:21:09 【问题描述】:我已经使用 tensorflow 后端训练了一个 DNN,我想将它托管在 firebase 中。训练后的模型保存为 .meta 文件,我尝试使用以下代码将模型转换为 tflite,但出现了一些错误。那么如何将这个模型转换成 Tensorflow Lite 呢?
错误:
File "<ipython-input-3-feb5263f2a51>", line 1, in <module>
runfile('D:/My Projects/FinalProject_Vr_02/cnn.py', wdir='D:/My Projects/FinalProject_Vr_02')
File "C:\Users\Asus\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 704, in runfile
execfile(filename, namespace)
File "C:\Users\Asus\Anaconda3\lib\site-packages\spyder_kernels\customize\spydercustomize.py", line 108, in execfile
exec(compile(f.read(), filename, 'exec'), namespace)
File "D:/My Projects/FinalProject_Vr_02/cnn.py", line 124, in <module>
converter = tf.contrib.lite.TFLiteConverter.from_saved_model(MODEL_NAME)
File "C:\Users\Asus\Anaconda3\lib\site-packages\tensorflow\contrib\lite\python\lite.py", line 340, in from_saved_model
output_arrays, tag_set, signature_key)
File "C:\Users\Asus\Anaconda3\lib\site-packages\tensorflow\contrib\lite\python\convert_saved_model.py", line 239, in freeze_saved_model
meta_graph = get_meta_graph_def(saved_model_dir, tag_set)
File "C:\Users\Asus\Anaconda3\lib\site-packages\tensorflow\contrib\lite\python\convert_saved_model.py", line 61, in get_meta_graph_def
return loader.load(sess, tag_set, saved_model_dir)
File "C:\Users\Asus\Anaconda3\lib\site-packages\tensorflow\python\saved_model\loader_impl.py", line 196, in load
loader = SavedModelLoader(export_dir)
File "C:\Users\Asus\Anaconda3\lib\site-packages\tensorflow\python\saved_model\loader_impl.py", line 212, in __init__
self._saved_model = _parse_saved_model(export_dir)
File "C:\Users\Asus\Anaconda3\lib\site-packages\tensorflow\python\saved_model\loader_impl.py", line 82, in _parse_saved_model
constants.SAVED_MODEL_FILENAME_PB))
OSError: SavedModel file does not exist at: snakes-0.001-2conv-basic.model/saved_model.pbtxt|saved_model.pb
代码:
import cv2
import numpy as np
import os
from random import shuffle
from tqdm import tqdm
TRAIN_DIR = 'D:\\My Projects\\Dataset\\dataset5_for_testing\\train'
TEST_DIR = 'D:\\My Projects\\Dataset\\dataset5_for_testing\\test'
IMG_SIZE = 50
LR = 1e-3
MODEL_NAME = 'snakes--.model'.format(LR, '2conv-basic')
def label_img(img):
print("\nimg inside label_img",img)
print("\n",img.split('.')[-2])
temp_name= img.split('.')[-2]
print("\n",temp_name[:1])
temp_name=temp_name[:1]
word_label = temp_name
if word_label == 'A': return [0,0,0,0,1] #A_c
elif word_label == 'B': return [0,0,0,1,0] #B_h
elif word_label == 'C': return [0,0,1,0,0] #C_i
elif word_label == 'D': return [0,1,0,0,0] #D_r
elif word_label == 'E' : return [1,0,0,0,0] #E_s
def create_train_data():
training_data = []
for img in tqdm(os.listdir(TRAIN_DIR)):
label = label_img(img)
path = os.path.join(TRAIN_DIR,img)
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
training_data.append([np.array(img),np.array(label)])
shuffle(training_data)
np.save('train_data.npy', training_data)
return training_data
def process_test_data():
testing_data = []
for img in tqdm(os.listdir(TEST_DIR)):
path = os.path.join(TEST_DIR,img)
img_num = img.split('.')[0]
img = cv2.imread(path,cv2.IMREAD_GRAYSCALE)
img = cv2.resize(img, (IMG_SIZE,IMG_SIZE))
testing_data.append([np.array(img), img_num])
shuffle(testing_data)
np.save('test_data.npy', testing_data)
return testing_data
train_data = create_train_data()
import tflearn
from tflearn.layers.conv import conv_2d, max_pool_2d
from tflearn.layers.core import input_data, dropout, fully_connected
from tflearn.layers.estimator import regression
'''
from tflearn.data_preprocessing import ImagePreprocessing
from tflearn.data_augmentation import ImageAugmentation
# normalisation of images
img_prep = ImagePreprocessing()
img_prep.add_featurewise_zero_center()
img_prep.add_featurewise_stdnorm()
# Create extra synthetic training data by flipping & rotating images
img_aug = ImageAugmentation()
img_aug.add_random_flip_leftright()
img_aug.add_random_rotation(max_angle=25.)
'''
import tensorflow as tf
tf.reset_default_graph()
#convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input',data_preprocessing=img_prep, data_augmentation=img_aug)
convnet = input_data(shape=[None, IMG_SIZE, IMG_SIZE, 1], name='input')
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 128, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 64, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = conv_2d(convnet, 32, 5, activation='relu')
convnet = max_pool_2d(convnet, 5)
convnet = fully_connected(convnet, 1024, activation='relu')
convnet = dropout(convnet, 0.8)
convnet = fully_connected(convnet, 5, activation='softmax')
convnet = regression(convnet, optimizer='adam', learning_rate=LR, loss='categorical_crossentropy', name='targets')
model = tflearn.DNN(convnet, tensorboard_dir='log')
if os.path.exists('.meta'.format(MODEL_NAME)):
model.load(MODEL_NAME)
print('model loaded!')
#train = train_data[:-500]
#test = train_data[-500:]
train = train_data[:-200]
test = train_data[-200:]
X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]
test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]
model.fit('input': X, 'targets': Y, n_epoch=3, validation_set=('input': test_x, 'targets': test_y),
snapshot_step=500, show_metric=True, run_id=MODEL_NAME)
model.save(MODEL_NAME)
converter = tf.contrib.lite.TFLiteConverter.from_saved_model(MODEL_NAME)
tflite_model = converter.convert()
open("converted_model.tflite", "wb").write(tflite_model)
【问题讨论】:
【参考方案1】:由于错误消息显示“OSError:SavedModel 文件不存在于:snakes-0.001-2conv-basic.model/saved_model.pbtxt|saved_model.pb”
那你为什么不尝试打印出 MODEL_NAME 并查看你的本地目录,看看是否有模型文件。
【讨论】:
【参考方案2】:我也在学习这部分。但正如我尝试的那样,精简版转换可以接受冻结图或SavedModel。对于冻结图,您可以查看https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tutorials/post_training_quant.ipynb 中的示例。在底部优化现有模型可以提供一些想法。
对于tf.contrib.lite.TFLiteConverter.from_saved_model
,您应该传递包含一个.pb 文件和一个变量文件夹的目录。在我自己的情况下,当我保存模型时,我得到了目录结构,就像
SAVED_MODEL_FOLDER
|---TIMESTEMP_FOLDER
|---VARIABLES_FOLDER
|---SAVED_MODEL.pb
然后调用tf.contrib.lite.TFLiteConverter.from_saved_model(TIMESTAMP_FOLDER)
转换错误就会消失
【讨论】:
【参考方案3】:仅转换模型。您需要将其转换为 TFLite 图,然后使用 TOCO 转换。
我已经亲自完成了,步骤在这里(https://apiai-aws-heroku-nodejs-bots.blogspot.com/2020/04/convert-tensorflow-object-detection.html)。
【讨论】:
以上是关于如何将 TensorFlow 模型转换为 TFLite 模型的主要内容,如果未能解决你的问题,请参考以下文章
如何将训练有素的 Tensorflow 模型转换为 Keras?
如何将 HED 模型转换为 Tensorflow Lite 模型
如何创建可轻松转换为 TensorFlow Lite 的模型?