Convert Darknet model to Keras model
Nov 1, 2020
- keras-yolo3 클론
https://github.com/qqwweee/keras-yolo3
2. yolo3-tiny.weights 다운로드
https://pjreddie.com/media/files/yolov3-tiny.weights
위 링크를 클릭하면 다운이 된다. 다운받은 파일을 위에 클론한 레포지토리에 넣는다.
3. weights를 .h 로 변환
잘 돌아가는지 테스트해보자.
python convert.py yolov3-tiny.cfg yolov3-tiny.weights model_data/yolo_tiny.h5
위의 코드를 실행시키자 아래와 같은 오류가 발생했다.
File "/Users/kimnan-young/opt/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py", line 703, in is_tensorreturn isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)AttributeError: module 'tensorflow.python.framework.ops' has no attribute '_TensorLike'
4. Tensorlike module 오류 해결
/Users/kimnan-young/opt/anaconda3/lib/python3.7/site-packages/tensorflow/python/framework/ops.py
파일의 212line 을 보자.
def is_dense_tensor_like(t):return isinstance(t, core_tf_types.Tensor)
리턴 값의 두번째 파라미터 값(core_tf_types.Tensor)을 복사한다.
/Users/kimnan-young/opt/anaconda3/lib/python3.7/site-packages/keras/backend/tensorflow_backend.py
파일에서 문제가 발생한 703line을 아래와 같이 수정한다.
def is_tensor(x):# return isinstance(x, tf_ops._TensorLike) or tf_ops.is_dense_tensor_like(x)return isinstance(x, tf_ops.core_tf_types.Tensor) or tf_ops.is_dense_tensor_like(x)
주석처리 한 부분이 고치기 전이고 두번째 리턴이 고친 후이다.
5. .h 파일 생성
yolo_tiny.h5 파일이 생성되었다.
에서 dodg.jpg 파일을 다운 받고 아래 코드를 실행시켰다.
from IPython.display import display
from PIL import Image
from yolo import YOLO
def objectDetection(file, model_path, class_path):
yolo = YOLO(model_path=model_path, classes_path=class_path, anchors_path="model_data/tiny_yolo_anchors.txt")
image = Image.open(file)
result_image = yolo.detect_image(image)
display(result_image)
objectDetection('dog.jpg', 'model_data/yolo_tiny.h5', 'model_data/coco_classes.txt')
잘 돌아간다.