read uff file in matlab

To read a UFF (Universal Framework Format) file in MATLAB, you need to use the TensorRT library, which is a part of NVIDIA Deep Learning SDK. Here is the basic process:

  1. Convert the TensorFlow model to UFF format using the TensorFlow-to-TensorRT converter. Here is an example code snippet:
main.m
import tensorflow as tf
from tensorflow.python.compiler.tensorrt import trt_convert as trt

graph_def = tf.GraphDef()
with tf.gfile.GFile('your_model.pb', 'rb') as f:
    graph_def.ParseFromString(f.read())

input_names = ['input1', 'input2']
output_names = ['output1', 'output2']

converter = trt.TrtGraphConverter(
    nodes_blacklist=output_names, 
    input_saved_model_dir=None,
    input_graph_def=graph_def,
    input_saved_model_tags=None,
    begins_at_zero=True,
    max_workspace_size_bytes=1<<30,
    precision_mode='FP32',
    minimum_segment_size=3)
converter.convert()
converter.save('your_model.uff')
617 chars
22 lines

This code reads a TensorFlow model saved in a .pb file, converts it to UFF format, and saves it to a .uff file.

  1. Load the UFF file in MATLAB using the nvinfer interface of TensorRT. Here is an example code snippet:
main.m
engine = nvinfer('your_model.uff');
36 chars
2 lines

This code creates a TensorRT engine object from the UFF file.

  1. Allocate memory for input and output data, and run inference. Here is an example code snippet:
main.m
input_data = randn([input_shape batch_size]);
output_data = zeros([output_shape batch_size], 'single');
d_input_data = gpuArray(input_data);
d_output_data = gpuArray(output_data);
[out, ~] = engine.infer('input_name', d_input_data, 'output_name', d_output_data);
output_data = gather(out);
290 chars
7 lines

This code generates random input data, allocates memory on GPU for input and output data, runs inference on the input data using the TensorRT engine, and finally copies the output data back to host memory.

Note that this is a basic example, and you may need to modify the code to match your specific use case.

gistlibby LogSnag