I’ve made a small demo example that showcases TF-TRT optimization of a small toy model in Keras TF2. You can run it on JetPack 4.4 using three simple commands:
git clone https://github.com/pascal-hwky/tftrt-jetson.git
cd tftrt-jetson
./arm.sh
Please find the repository here. Currently, I’m still receiving a strange error in the optimization stage:
Assertion Error in fromSymbolic: 0 (x must be build-time constant)
Unfortunately, this prevents the conversion of this segment to a fully optimized TRT engine operation. If anybody has seen this error before and knows what’s causing it, I would greatly appreciate any help!
This issue is caused from the restrictions of dynamic shapes.
Based on the document below, you will need to specify the constant channel input:
This indicates that you will need to have at least 4 dimension (including batch) input tensor on NHWC format.
After checking, your sample can work correctly when expand the input tensor to the 2nd or 4th dimension.
inputs = keras.Input(shape=(1,1,32,))
...
def my_input_fn():
for _ in range(100):
inp1 = np.random.normal(size=(8,1,1,32)).astype(np.float32)
yield inp1,
Or
inputs = keras.Input(shape=(32,1,1,))
...
def my_input_fn():
for _ in range(100):
inp1 = np.random.normal(size=(8,32,1,1)).astype(np.float32)
yield inp1,
In order to have a network segment eligible for TensorRT optimization, I additionally had to change the network architecture to use convolutional layers.