JetPack 4.6
python 3.6.9
tensorrt 8.0.1.6
torch 1.9.0 在jetson_zoo下载符合JetPack对应的版本
opencv 4.1.1
ros2安装在docker中
max_batch_size: 1
onnx_file_path: onnx文件路径
engine_file_path: engine文件路径
save_engine: 是否保存engine文件
def get_engine(max_batch_size,
onnx_file_path,
engine_file_path,
save_engine=True):
TRT_LOGGER = trt.Logger()
assert not os.path.exists(engine_file_path), "Engine file alrealdy exist"
explicit_batch = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
with trt.Builder(TRT_LOGGER) as builder, \
builder.create_network(explicit_batch) as network, \
trt.OnnxParser(network, TRT_LOGGER) as parser, \
builder.create_builder_config() as config:
profile = builder.create_optimization_profile()
config.max_workspace_size = 1<<28
builder.max_batch_size = max_batch_size
if builder.platform_has_fast_fp16:
config.set_flag(trt.BuilderFlag.FP16)
if not os.path.exists(onnx_file_path):
quit("ONNX file {} not found!".format(onnx_file_path))
print('loading onnx file from path {} ...'.format(onnx_file_path))
parser.parse_from_file(onnx_file_path)
print("Completed parsing of onnx file")
print("Building an engine from file{}' this may take a while...".format(onnx_file_path))
print(network.get_layer(network.num_layers-1).get_output(0).shape)
engine = builder.build_engine(network, config)
print("Completed creating Engine")
if save_engine:
with open(engine_file_path, 'wb') as f:
f.write(engine.serialize())
return engine
class EntropyCalibrator(trt.IInt8EntropyCalibrator2):
def __init__(self, training_data, cache_file, batch_size=128):
trt.IInt8EntropyCalibrator2.__init__(self)
self.cache_file = cache_file
t1 = time.time()
self.data = self.load_data(training_data)
t2 = time.time()
print('load_data:', 1000*(t2-t1), ' ms')
self.batch_size = batch_size
self.current_index = 0
self.device_input = cuda.mem_alloc(self.data[0].nbytes * self.batch_size)
def load_data(self, datapath):
print("loading image data")
imgs = os.listdir(datapath)
dataset = []
for order, data in enumerate(imgs):
image_path = os.path.join(datapath, data)
img = cv2.imread(image_path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img = Image.fromarray(img)
img = img_transforms(img).numpy()
dataset.append(img)
print('calibration image order:', order)
return np.array(dataset)
def get_batch_size(self):
return self.batch_size
def get_batch(self, names):
if self.current_index + self.batch_size > self.data.shape[0]:
return None
current_batch = int(self.current_index / self.batch_size)
if current_batch % 10 == 0:
print("Calibrating batch {:}, containing {:} images".format(current_batch, self.batch_size))
batch = self.data[self.current_index:self.current_index + self.batch_size].ravel()
cuda.memcpy_htod(self.device_input, batch)
self.current_index += self.batch_size
return [self.device_input]
def read_calibration_cache(self):
if os.path.exists(self.cache_file):
with open(self.cache_file, "rb") as f:
return f.read()
def write_calibration_cache(self, cache):
with open(self.cache_file, "wb") as f:
f.write(cache)
def get_engine(max_batch_size=1, onnx_file_path="", engine_file_path="", mode="fp16", save_engine=True):
TRT_LOGGER = trt.Logger()
assert not os.path.exists(engine_file_path), "Engine file alrealdy exist"
explicit_batch = 1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)
with trt.Builder(TRT_LOGGER) as builder, \
builder.create_network(explicit_batch) as network, \
trt.OnnxParser(network, TRT_LOGGER) as parser, \
builder.create_builder_config() as config:
profile = builder.create_optimization_profile()
config.max_workspace_size = 1<<28
builder.max_batch_size = max_batch_size
half = True
assert (builder.platform_has_fast_int8 == True), 'not support int8'
config.set_flag(trt.BuilderFlag.INT8)
config.int8_calibrator = Int8_calibrator
if not os.path.exists(onnx_file_path):
quit("ONNX file {} not found!".format(onnx_file_path))
print('loading onnx file from path {} ...'.format(onnx_file_path))
parser.parse_from_file(onnx_file_path)
print("Completed parsing of onnx file")
print("Building an engine from file {}' this may take a while...".format(onnx_file_path))
print(network.get_layer(network.num_layers-1).get_output(0).shape)
engine = builder.build_engine(network, config)
print("Completed creating Engine")
if save_engine:
with open(engine_file_path, 'wb') as f:
f.write(engine.serialize())
return engine