TensorRT로 변환할 때 .pt → .onnx → .trt or .engine을 거쳐서 변환됩니다.
🔨 실행환경: Jetson Orin Nano 8GB
아래 링크 클론 해주세요!
https://github.com/KopiSoftware/TRT_Ultra_Fast_Lane_Detect?tab=readme-ov-file
여기서 하라는대로 실행하면 에러가 꽤 떴어서 안뜨도록 코드 정리해뒀습니당
아래 따라서 쭉 실행해주세요!
Pretrained model 다운로드
Dataset - Tusimple, CULane
저는 Tusimple로 train된 모델 다운받았습니당 (혹시 몰라서 CULane도 링크 넣어놨어요)
https://drive.google.com/file/d/1WCYyur5ZaWczH15ecmeDowrW30xcLrCn/view?usp=sharing
https://drive.google.com/file/d/1zXBRTw50WOzvUp6XKsi8Zrk3MUC3uFuq/view?usp=sharing
다운받은 tusimple_18.pth 파일명을 model.pth로 바꿔주세요
TRT_Ultra_Fast_Lane_Detect
│
├── configs/
├── model.pth <-- 여기에 파일 배치
├── onnx_to_tensorrt.py
├── torch2onnx.py
├── tensorrt_run.py
├── requirement.txt
...
model.pth 그냥 TRT_Ultra_Fast_Lane_Detect 아래에 두면 됩니당
Install
라이브러리 설치해주세요
pip3 install -r requirement.txt
ONNX 모델로 변환 (.pth → .onnx)
pytorch 모델(model.pth)를 ONNX 형식으로 변환해야 합니다. torch2onnx.py 스크립트 사용해주세요
python3 configs/${config_file}.py # 일반적인? 코드
${config_file}는 PyTorch 모델의 설정 파일인데, tusimple_4.py 사용하려면 아래 코드 실행하면 됩니다.
<# 이거 실행하시면 돼요
python3 [torch2onnx.py](http://torch2onnx.py/) configs/tusimple_4.py
∴ 변환된 ONNX 파일(model.onnx)이 생성됩니당
def build_engine 수정!
onnx_to_tensorrt.py에서 def build_engine 아래 코드로 통째로 바꿔주세요
def build_engine(onnx_file_path, mode, verbose=False):
"""Build a TensorRT engine from an ONNX file."""
TRT_LOGGER = trt.Logger(trt.Logger.VERBOSE) if verbose else trt.Logger()
with trt.Builder(TRT_LOGGER) as builder, builder.create_network(*EXPLICIT_BATCH) as network, trt.OnnxParser(network, TRT_LOGGER) as parser:
# 워크스페이스 및 엔진 설정
config = builder.create_builder_config()
config.max_workspace_size = 1 << 30 # 1GB
if mode == 'fp16':
config.set_flag(trt.BuilderFlag.FP16)
# ONNX 모델 로드
print('Loading ONNX file from path {}...'.format(onnx_file_path))
with open(onnx_file_path, 'rb') as model:
if not parser.parse(model.read()):
print('ERROR: Failed to parse the ONNX file.')
for error in range(parser.num_errors):
print(parser.get_error(error))
return None
# 네트워크 입력 배치 크기 설정
if trt.__version__[0] >= '7':
shape = list(network.get_input(0).shape)
shape[0] = 1 # 배치 크기 1로 설정
network.get_input(0).shape = shape
# TensorRT 엔진 빌드
print('Building an engine. This may take a while...')
engine = builder.build_engine(network, config)
print('Completed creating engine.')
return engine
ircv@ubuntu:~/projects/deep_daiv/TRT_Ultra_Fast_Lane_Detect$ python3 onnx_to_tensorrt.py -p fp16 --model model.onnx
Traceback (most recent call last):
File "onnx_to_tensorrt.py", line 84, in <module>
main()
File "onnx_to_tensorrt.py", line 76, in main
engine = build_engine(onnx_file_path, mode,args.verbose)
File "onnx_to_tensorrt.py", line 22, in build_engine
builder.max_workspace_size = 1 << 30
AttributeError: 'tensorrt.tensorrt.Builder' object has no attribute 'max_workspace_size' AttributeError: 'tensorrt.tensorrt.Builder' object has no attribute 'max_workspace_size' 에러는 NVIDIA TensorRT의 최신 버전에서 max_workspace_size 속성이 제거되거나, 새로운 방식으로 대체되었기 때문에 발생한 것입니다. TensorRT의 API가 업데이트되면서 이러한 변경사항이 발생했을 것 같은데 그래서 코드 수정해야합니다.수정사항
max_batch_size → config 객체를 통해 설정하거나 직접 네트워크 입력에서 배치 크기를 조정할 수 있도록 수정했습니다.builder.build_cuda_engine(network) builder.build_engine(network, config)로 대체TensorRT 모델로 변환 (.onnx → .trt)
onnx_to_tensorrt.py 사용해주세요.
python3 onnx_to_tensorrt.py -p ${mode_in_fp16_or_fp32} --model ${model_name}
${mode_in_fp16_or_fp32}: FP16 또는 FP32를 지정합니다.${model_name}: 변환할 ONNX 모델 이름입니다(예: model.onnx).FP16으로 변환
python3 onnx_to_tensorrt.py -p fp16 --model model.onnx
실행하면 아래처럼 뜹니당
ircv@ubuntu:~/projects/deep_daiv/TRT_Ultra_Fast_Lane_Detect$ python3 onnx_to_tensorrt.py -p fp16 --model model.onnx
onnx_to_tensorrt.py:24: DeprecationWarning: Use set_memory_pool_limit instead.
config.max_workspace_size = 1 << 30 # 1GB
Loading ONNX file from path model.onnx...
[12/18/2024-16:54:57] [TRT] [W] onnx2trt_utils.cpp:375: Your ONNX model has been generated with INT64 weights, while TensorRT does not natively support INT64. Attempting to cast down to INT32.
Building an engine. This may take a while...
onnx_to_tensorrt.py:45: DeprecationWarning: Use build_serialized_network instead.
engine = builder.build_engine(network, config)
[12/18/2024-16:57:08] [TRT] [W] TensorRT encountered issues when converting weights between types and that could affect accuracy.
[12/18/2024-16:57:08] [TRT] [W] If this is not the desired behavior, please modify the weights or retrain with regularization to adjust the magnitude of the weights.
[12/18/2024-16:57:08] [TRT] [W] Check verbose logs for the list of affected weights.
[12/18/2024-16:57:08] [TRT] [W] - 29 weights are affected by this issue: Detected subnormal FP16 values.
[12/18/2024-16:57:08] [TRT] [W] - 17 weights are affected by this issue: Detected values less than smallest positive FP16 subnormal value and converted them to the FP16 minimum subnormalized value.
Completed creating engine.
Serialized the TensorRT engine to file: model_fp16.trt
완료!
FP32로 변환
python3 onnx_to_tensorrt.py -p fp32 --model model.onnx
결과적으로 이렇게 다 생성됐습니다 !

TensorRT 모델 실행
python3 tensorrt_run.py --model ${model_name}
${model_name}: TensorRT 모델 이름
→ model_fp16.trt이지만 아래처럼 model_fp16까지만 써주세요
(.trt까지 쓰면 .trt.trt가 실행되려고 해서 에러나더라구요)
python3 tensorrt_run.py --model model_fp16