I have successfully gotten the examples (inception_v1, v2 etc) to run in TensorRT on the TX2 however I am having trouble getting my own model to run.
Here is a simplified keras model
#!/usr/bin/env python
from keras.layers import Lambda, Input, Dense, \
Flatten, Reshape, Subtract, Multiply
from keras.models import Model
from keras.losses import mse, binary_crossentropy
from keras.utils import plot_model
from keras import backend as K
import tensorflow as tf
import numpy as np
import matplotlib.pyplot as plt
import argparse
import os
import sys
import h5py
import datetime
tf_sess = tf.Session()
K.set_session(tf_sess)
# network parameters
x_len = 64
y_len = 64
linear_len = x_len*y_len
#input_shape = (linear_len,)
input_shape = (x_len, y_len, 1)
intermediate_dim = 1024
batch_size = 128
latent_dim = 200
epochs = 100
# build model
inputs = Input(shape=input_shape, name='input')
interm = Flatten()(inputs)
outputs = Dense(latent_dim, name='latent')(interm)
# instantiate model
model = Model(inputs, outputs, name='model')
model.summary()
# save the model
base_filename = datetime.datetime.now().strftime("%Y%m%d_%H%M%S") + '_'
model.save(base_filename + 'toy_model.h5')
then freezing with:
#!/usr/bin/env python
import sys
import datetime
from keras.models import load_model
import keras.backend as K
from tensorflow.python.framework import graph_io
from tensorflow.python.tools import freeze_graph
from tensorflow.core.protobuf import saver_pb2
from tensorflow.python.training import saver as saver_lib
def convert_keras_to_pb(keras_model, out_names, models_dir, model_filename):
model = load_model(keras_model)
K.set_learning_phase(0)
sess = K.get_session()
saver = saver_lib.Saver(write_version=saver_pb2.SaverDef.V2)
checkpoint_path = saver.save(sess, './saved_ckpt', global_step=0,
latest_filename='checkpoint_state')
graph_io.write_graph(sess.graph, '.', 'tmp.pb')
freeze_graph.freeze_graph('./tmp.pb', '',
False, checkpoint_path, out_names,
"save/restore_all", "save/Const:0",
models_dir+model_filename, False, "")
if __name__ == "__main__":
keras_model = sys.argv[1]
out_names = sys.argv[2]
models_dir = sys.argv[3]
model_filename = keras_model.replace('.h5','.pb')
convert_keras_to_pb(keras_model, out_names, models_dir, model_filename)
then attempting to compile with:
~/tf_to_trt_image_classification$ python scripts/convert_plan.py ./20180802_094247_toy_model.pb ./20180802_094247_toy_model.plan input 64 64 latent/BiasAdd 1 0 float
gives:
Using output node latent/BiasAdd
Converting to UFF graph
DEBUG: convert reshape to flatten node
No. nodes: 7
UFF Output written to data/tmp.uff
UFFParser: parsing latent/bias
UFFParser: parsing input
UFFParser: parsing flatten_1/Reshape
UFFParser: parsing latent/kernel
UFFParser: parsing latent/MatMul
UFFParser: parsing latent/BiasAdd
UFFParser: parsing MarkOutput_0
latent/BiasAdd: kernel weights has count 819200 but 2457600 was expected
I have tried several configurations of the model including one that has 3 channels (as opposed to this one that has a single channel) and I get the same results -
Any help would be greatly appreciated -