YOLO v3 tiny 모델을 사용하여 모델을 학습하고 자이카에서 사용하기 위해 onnx, tensorRT 변환을 수행해야 한다.
https://github.com/2damin/yolov3-pytorch 에서 clone 받아온다
$ git clone https://github.com/2damin/yolov3-pytorch.git
$ docker pull nvcr.io/nvidia/pytorch:21.12-py3
$ docker run -it -v ${PWD}/deep_learning_project:/yolov3-pytorch --name yolo_torch nvcr.io/nvidia/pytorch:21.12-py3 /bin/bash
# pip install -r requirements.txt
ONNX_EXPORT = False
# python main.py --mode train --cfg yolov3-tiny_tstl_352.cfg --pretrained yolov3-tiny.weights
AttributeError: partially initialized module 'cv2' has no attribute '_registerMatType' (most likely due to a circular import)
pip install "opencv-python-headless<4.3"
Traceback (most recent call last):
File "main.py", line 20, in <module>
import onnx,onnxruntime
File "/opt/conda/lib/python3.8/site-packages/onnx/__init__.py", line 11, in <module>
from onnx.external_data_helper import load_external_data_for_model, write_external_data_tensors, convert_model_to_external_data
File "/opt/conda/lib/python3.8/site-packages/onnx/external_data_helper.py", line 14, in <module>
from .onnx_pb import TensorProto, ModelProto
File "/opt/conda/lib/python3.8/site-packages/onnx/onnx_pb.py", line 8, in <module>
from .onnx_ml_pb2 import * # noqa
File "/opt/conda/lib/python3.8/site-packages/onnx/onnx_ml_pb2.py", line 32, in <module>
_descriptor.EnumValueDescriptor(
File "/opt/conda/lib/python3.8/site-packages/google/protobuf/descriptor.py", line 789, in __new__
_message.Message._CheckCalledFromGeneratedFile()
TypeError: Descriptors cannot be created directly.
If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
If you cannot immediately regenerate your protos, some other possible workarounds are:
1. Downgrade the protobuf package to 3.20.x or lower.
2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates
pip install protobuf==3.20
학습한 model weight를 darknet 형식의 weight로 변환하고, pytorch model을 onnx 형식의 model로 변환하는 부분
ONNX_EXPORT = True
로 설정python main.py --mode onnx --cfg ./cfg/yolov3.cfg --checkpoint ${saved_checkpoint_path}
https://github.com/2damin/yolov3_onnx_rt 에서 tensorrt-8.2.1 branch 사용
주행코드를 짜는 부분
trt_detection.py
이때 코드가 python2로 구현되어 있는데 실제 환경은 python3이기 때문에 에러가 발생할 수 있다
cv2.bridge를 사용할 수 없다는 에러인데 아래와 같이 변경하면 해결된다