Skip to content

Instantly share code, notes, and snippets.

@jkjung-avt
Created October 12, 2021 09:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jkjung-avt/88d6d2578835b167dfb07be12827eb46 to your computer and use it in GitHub Desktop.
Save jkjung-avt/88d6d2578835b167dfb07be12827eb46 to your computer and use it in GitHub Desktop.
Example code for handling TensorRT INT8 calibration file
from pathlib import Path
import struct
lines = Path('calib_yolov4-int8-608.bin').read_text().splitlines()
for line in lines:
pair = line.split(':')
if len(pair) != 2:
continue
assert len(pair[1]) == 9
bstr = bytes.fromhex(pair[1][1:]) # convert to byte string
unit = struct.unpack('>f', bstr)[0] # convert to float32
print('%s:\t%f' % (pair[0], unit))
import tensorrt as trt
TRT_LOGGER = trt.Logger(trt.Logger.VERBOSE)
EXPLICIT_BATCH = [1 << (int)(trt.NetworkDefinitionCreationFlag.EXPLICIT_BATCH)]
builder = trt.Builder(TRT_LOGGER)
network = builder.create_network(*EXPLICIT_BATCH)
parser = trt.OnnxParser(network, TRT_LOGGER)
with open('yolov4-int8-608.onnx', 'rb') as f:
parser.parse(f.read())
shape = list(network.get_input(0).shape)
shape[0] = 1
network.get_input(0).shape = shape
for layer in network:
if layer.type == trt.LayerType.CONCATENATION:
inputs = [layer.get_input(i).name for i in range(layer.num_inputs)]
outputs = [layer.get_output(i).name for i in range(layer.num_outputs)]
print('Layer %s' % layer.name)
print(' inputs: %s' % str(inputs))
print(' outputs: %s' % str(outputs))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment