简体   繁体   English

如何将 Tensorflow 模型转换为 TFLite 模型

[英]How to convert a Tensorflow model into a TFLite model

I have trained a DNN using tensorflow back end and I want to host it in firebase.我已经使用 tensorflow 后端训练了一个 DNN,我想将它托管在 firebase 中。 The trained model is saved as a .meta file and I have tried to convert the model into tflite using following code, but I have got some errors.经过训练的模型保存为.meta文件,我尝试使用以下代码将模型转换为tflite ,但出现了一些错误。 So how can I convert this model into Tensorflow Lite?那么如何将这个模型转换成 Tensorflow Lite 呢?

Error :错误

  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}

Code:代码:

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)

Since the error message says "OSError: SavedModel file does not exist at: snakes-0.001-2conv-basic.model/{saved_model.pbtxt|saved_model.pb}"由于错误消息显示“OSError:SavedModel 文件不存在于:snakes-0.001-2conv-basic.model/{saved_model.pbtxt|saved_model.pb}”

Then why don't you try to print out MODEL_NAME and also take a look at your local directory to see if a model file is in there.那么为什么不尝试打印出 MODEL_NAME 并查看您的本地目录,看看那里是否有模型文件。

I am also learning this part.我也在学习这部分。 But as fAr as I tried, lite conversion can accept frozen graph or SavedModel .但正如我所尝试的那样,精简版转换可以接受冻结图或SavedModel For frozen graph, you can see the example in https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tutorials/post_training_quant.ipynb .对于冻结图,您可以在https://github.com/tensorflow/tensorflow/blob/master/tensorflow/lite/tutorials/post_training_quant.ipynb 中查看示例。 In the bottom Optimizing an existing model can provide some ideas.在底部优化现有模型可以提供一些想法。

For tf.contrib.lite.TFLiteConverter.from_saved_model , you should pass the directory contains one .pb file and one variables folder.对于tf.contrib.lite.TFLiteConverter.from_saved_model ,您应该传递包含一个 .pb 文件和一个 variables 文件夹的目录。 In my own case, when I save the model, I got the directory structure, which is like在我自己的情况下,当我保存模型时,我得到了目录结构,就像

SAVED_MODEL_FOLDER
     |---TIMESTEMP_FOLDER
             |---VARIABLES_FOLDER
             |---SAVED_MODEL.pb

Then the error will be gone if you convert by calling tf.contrib.lite.TFLiteConverter.from_saved_model({TIMESTAMP_FOLDER})如果您通过调用tf.contrib.lite.TFLiteConverter.from_saved_model({TIMESTAMP_FOLDER})转换,则错误将消失

To convert only the model.仅转换模型。 You need to convert it to TFLite graph and then use TOCO coverter.您需要将其转换为 TFLite 图,然后使用 TOCO 转换程序。

I have done it personally and the steps are here ( https://apiai-aws-heroku-nodejs-bots.blogspot.com/2020/04/convert-tensorflow-object-detection.html ).我已经亲自完成了,步骤在这里( https://apiai-aws-heroku-nodejs-bots.blogspot.com/2020/04/convert-tensorflow-object-detection.html )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM