1
votes

I have trained a DNN using tensorflow back end and I want to host it in 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. So how can I convert this model into 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)
3

3 Answers

1
votes

Since the error message says "OSError: SavedModel file does not exist at: 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.

1
votes

I am also learning this part. But as fAr as I tried, lite conversion can accept frozen graph or SavedModel. For frozen graph, you can see the example in 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. 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})

0
votes

To convert only the model. You need to convert it to TFLite graph and then use TOCO coverter.

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 ).