I created a very simple proof of concept to test TF-TRT conversion but it does not work.
First I created the model:
import tensorflow as tf
import tensorflow_datasets as tfds
(ds_train, ds_test), ds_info = tfds.load(
'mnist',
split=['train', 'test'],
shuffle_files=True,
as_supervised=True,
with_info=True,
)
def normalize_img(image, label):
"""Normalizes images: `uint8` -> `float32`."""
return tf.cast(image, tf.float32) / 255., label
ds_train = ds_train.map(
normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
ds_train = ds_train.cache()
ds_train = ds_train.shuffle(ds_info.splits['train'].num_examples)
ds_train = ds_train.batch(128)
ds_train = ds_train.prefetch(tf.data.AUTOTUNE)
ds_test = ds_test.map(
normalize_img, num_parallel_calls=tf.data.AUTOTUNE)
ds_test = ds_test.batch(128)
ds_test = ds_test.cache()
ds_test = ds_test.prefetch(tf.data.AUTOTUNE)
model = tf.keras.models.Sequential([
tf.keras.layers.Flatten(input_shape=(28, 28)),
tf.keras.layers.Dense(512, activation='relu'),
tf.keras.layers.Dense(10)
])
model.compile(
optimizer=tf.keras.optimizers.Adam(0.001),
loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
metrics=[tf.keras.metrics.SparseCategoricalAccuracy()],
)
model.fit(
ds_train,
epochs=6,
validation_data=ds_test,
)
model.save("saved_model")
Then I tried to convert to trt:
from tensorflow.python.compiler.tensorrt import trt_convert as trt
import tensorflow as tf
converter = trt.TrtGraphConverterV2(input_saved_model_dir="saved_model")
converter.convert()
converter.save("output")
But it crashes:
Traceback (most recent call last):
File "save_trt.py", line 5, in <module>
converter.convert()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/compiler/tensorrt/trt_convert.py", line 1196, in convert
self._input_saved_model_tags)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py", line 864, in load
result = load_internal(export_dir, tags, options)["root"]
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py", line 903, in load_internal
ckpt_options, options, filters)
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py", line 162, in __init__
self._load_all()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py", line 259, in _load_all
self._load_nodes()
File "/usr/local/lib/python3.6/dist-packages/tensorflow/python/saved_model/load.py", line 448, in _load_nodes
slot_variable = optimizer_object.add_slot(
AttributeError: '_UserObject' object has no attribute 'add_slot'
I am on a Xavier AGX with tensorflow 2.6.2 in jetpack 4.6.1.
Thanks!