Run custom CNN with python imageNet class

Hello, I have train a custom cnn model with keras and then I have obtained the .uff file and the .engine TensorRT model. Is it possible to load my model with the line net = jetson.inference.imageNet(opt.network) ? Thanks

Hi,

YES. Please check this page for more information.

You will need to update the input/output layer, label and model path.
Thanks.

1 Like

Thanks, I will try that

I have successfully compile and run the the example of the link, then I have loading my .uff model with:

imageNet* net = imageNet::Create(NULL, “models/my_cnn_model.uff”, NULL, “models/labels.txt”,
“input_1”, “dense_1/Softmax”, 1, TYPE_FP16, DEVICE_GPU, true);

It compiles without error but when run it I have this error:

$ ./run_tensor_rt_engine
[image] loaded ‘imgs/no_defectuosa_1.jpg’ (1995 x 1420, 3 channels)

imageNet – loading classification network model from:
– prototxt (null)
– model models/my_cnn_model.uff
– class_labels models/labels.txt
– input_blob ‘input_1’
– output_blob ‘dense_1/Softmax’
– batch_size 1

[TRT] TensorRT version 5.1.6
[TRT] loading NVIDIA plugins…
[TRT] Plugin Creator registration succeeded - GridAnchor_TRT
[TRT] Plugin Creator registration succeeded - NMS_TRT
[TRT] Plugin Creator registration succeeded - Reorg_TRT
[TRT] Plugin Creator registration succeeded - Region_TRT
[TRT] Plugin Creator registration succeeded - Clip_TRT
[TRT] Plugin Creator registration succeeded - LReLU_TRT
[TRT] Plugin Creator registration succeeded - PriorBox_TRT
[TRT] Plugin Creator registration succeeded - Normalize_TRT
[TRT] Plugin Creator registration succeeded - RPROI_TRT
[TRT] Plugin Creator registration succeeded - BatchedNMS_TRT
[TRT] completed loading NVIDIA plugins.
[TRT] detected model format - UFF (extension ‘.uff’)
[TRT] desired precision specified for GPU: FP16
[TRT] native precisions detected for GPU: FP32, FP16
[TRT] attempting to open engine cache file models/my_cnn_model.uff.1.1.GPU.FP16.engine
[TRT] loading network profile from engine cache… models/my_cnn_model.uff.1.1.GPU.FP16.engine
[TRT] device GPU, models/my_cnn_model.uff loaded
[TRT] Glob Size is 4755780 bytes.
[TRT] Added linear block of size 3686400
[TRT] Added linear block of size 1843200
[TRT] Added linear block of size 230400
[TRT] Added linear block of size 205312
[TRT] Added linear block of size 57856
[TRT] Deserialize required 2944367 microseconds.
[TRT] device GPU, CUDA engine context initialized with 2 bindings
[TRT] binding – index 0
– name ‘input_1’
– type FP32
– in/out INPUT
– # dims 3
– dim #0 3 (SPATIAL)
– dim #1 480 (SPATIAL)
– dim #2 480 (SPATIAL)
[TRT] binding – index 1
– name ‘dense_1/Softmax’
– type FP32
– in/out OUTPUT
– # dims 3
– dim #0 1 (SPATIAL)
– dim #1 1 (SPATIAL)
– dim #2 2 (SPATIAL)
[TRT] binding to input 0 input_1 binding index: 0
[TRT] binding to input 0 input_1 dims (b=1 c=3 h=480 w=480) size=2764800
[TRT] binding to output 0 dense_1/Softmax binding index: 1
[TRT] binding to output 0 dense_1/Softmax dims (b=1 c=1 h=1 w=2) size=8
device GPU, models/my_cnn_model.uff initialized.
[TRT] models/my_cnn_model.uff loaded
imageNet – loaded 2 class info entries
imageNet – didn’t load expected number of class descriptions (2 of 1)
imageNet – failed to load synset class descriptions (2 / 2 of 1)
[TRT] imageNet – failed to initialize.
failed to load image recognition network

My model only has two outputs in a softmax layer so I have created this labels.txt file:
1 pass
2 failed

So I don’t understand why TensorRT return this error:

imageNet – loaded 2 class info entries
imageNet – didn’t load expected number of class descriptions (2 of 1)
imageNet – failed to load synset class descriptions (2 / 2 of 1)

Comparing the output of the GoogleNet model with my custom model, I have realice where is the problem. GoogleNet output’s is in NCHW format with (b=1 c=1 h=1000 w=1) shape, where 1000 is the number of units in the softmax layer, but my model has (b=1 c=1 h=1 w=2) shape (since it only has 2 units in the output softmax layer).
So, how can I reshape the output to (b=1 c=1 h=2 w=1) to be compatible with Tensor RT?