[Object Detection] Convert Darknet yolov3 model to keras model
Darknet Yolo v3 의 .weights 파일을 Keras의 .h5 로 변환하는 방법인데,
클래스는 제대로 찾을 수 있지만 (사람, 바이크 등)
아무래도 프레임워크간의 변환이니 정확도(box score)가 손실된다.
또한, Keras가 Tensorflow에 내장되면서
Yolo v3 모델을 Keras 모델로 바꾸는 코드들이 Cuda 9.0 을 지원하거나, 잘못된 코드들을 가지고 있는 경우가 있다.
(Yolo v2 모델 변환하는 방법은 잘 나와있는 듯)
그래서 변환하지 않는 방법을 추천한다.
변환하지않고, 그냥 darknet 프레임워크를 이용하여 실행하는 방법을 추천한다.
1. python 에서 darknet 프레임워크를 사용하여 weights, cfg , ... 파일들을 불러와 실행하는 방법
https://github.com/pjreddie/darknet/tree/master/python
pjreddie/darknet
Convolutional Neural Networks. Contribute to pjreddie/darknet development by creating an account on GitHub.
github.com
그래도 정확도 손실을 감내하고, 클래스만 찾거나, BBOX가 대충 나와도 모델 변환을 하고싶다면 아래를 참고.
2. Convert Yolo v3 model to Keras model
2-1. 방법 1
* 잘못된 코드 수정해야함
darknet 학습을 통해 만들어진 .weights 파일을
텐서플로우에서 실행하기 위하여 keras 파일인 .h5 파일로 변환할 수 있는 깃헙이다.
https://github.com/xiaochus/YOLOv3
xiaochus/YOLOv3
Keras implementation of yolo v3 object detection. Contribute to xiaochus/YOLOv3 development by creating an account on GitHub.
github.com
* process_image 함수 시작 전 코드 삽입
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
* 잘못된 코드 수정
col = np.tile(np.arange(0, grid_w), grid_h).reshape(-1, grid_w)
row = np.tile(np.arange(0, grid_h).reshape(-1, 1), grid_w)
col = col.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
row = row.reshape(grid_h, grid_w, 1, 1).repeat(3, axis=-2)
사용 방법
$ python yad2k.py cfg/yolo.cfg yolov3.weights data/yolo.h5
만들어진 .h5 파일을 이용하여 demo 를 실행해볼 수도 있다.
$ python demo.py
2-2. 방법 2 : Cuda 9.0을 지원하는 모델 변환 방법
https://github.com/qqwweee/keras-yolo3
qqwweee/keras-yolo3
A Keras implementation of YOLOv3 (Tensorflow backend) - qqwweee/keras-yolo3
github.com
필자는 cuda 10.0 을 쓰고있어서 시도하지 못했다.
cuda 10.0 환경에서 시도한다면 다음과 같은 에러가 뜬다.
tensorflow.python.framework.errors_impl.InternalError: Blas SGEMM launch failed : m=43264, n=32, k=64
https://github.com/qqwweee/keras-yolo3/issues/362
Blas SGEMM launch failed · Issue #362 · qqwweee/keras-yolo3
when I run "python yolo_video.py --image", it loads model successfully. But then I input the name of test picture, error occurs as follows: Input image filename:1.jpg (416, 416, 3) 2019-0...
github.com
2-3. 방법 3
https://github.com/thtrieu/darkflow
thtrieu/darkflow
Translate darknet to tensorflow. Load trained weights, retrain/fine-tune using tensorflow, export constant graph def to mobile devices - thtrieu/darkflow
github.com
Yolo v3 를 지원하지 않아서 다음과 같은 에러가 뜸
Parsing ./cfg/yolov3.cfg Layer [shortcut] not implemented
https://github.com/thtrieu/darkflow/issues/665
YOLOv3 support · Issue #665 · thtrieu/darkflow
https://pjreddie.com/darknet/yolo/ Can I use YOLOv3 cfg/weights with darkflow correctly?
github.com
잘못 변환이 이루어졌을 때 다음과 같은 메세지가 뜬다.
ValueError: operands could not be broadcast together with shapes (13,13,2) (1,1,3,2)
Darknet 가중치 파일을 텐서플로우에서 돌리기 위하여 pb 파일로 바꾸는 깃허브도 있다.
https://github.com/jinyu121/DW2TF
jinyu121/DW2TF
Darknet Weights to TensorFlow. Contribute to jinyu121/DW2TF development by creating an account on GitHub.
github.com
'AI Research Topic > Object Detection' 카테고리의 다른 글
[Object Detection] Darknet python (2) | 2019.09.04 |
---|---|
[Object Detection] Darknet 학습 시 적절한 Weight 고르기 (0) | 2019.09.04 |
[Object Detection] 객체 탐지를 위한 데이터 주석 Yolo 형식으로 변환하기 (9) | 2019.08.19 |
[Object Detection] Image Labeling Tool (5) | 2019.07.08 |
[Object Detection] Feature Pyramid Network (FPN) (6) | 2019.04.06 |