forked from miemie2013/Keras-YOLOv4
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdemo.py
112 lines (90 loc) · 3.45 KB
/
demo.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
#! /usr/bin/env python
# coding=utf-8
# ================================================================
#
# Author : miemie2013
# Created date: 2020-05-20 15:35:27
# Description : keras_yolov4
#
# ================================================================
from collections import deque
import datetime
import cv2
import os
import time
import numpy as np
import tensorflow as tf
import keras.layers as layers
from tools.cocotools import get_classes
from model.yolov4 import YOLOv4
from model.decode_np import Decode
import logging
FORMAT = '%(asctime)s-%(levelname)s: %(message)s'
logging.basicConfig(level=logging.INFO, format=FORMAT)
logger = logging.getLogger(__name__)
# 6G的卡,训练时如果要预测,则设置use_gpu = False,否则显存不足。
use_gpu = False
use_gpu = True
# 显存分配。
if use_gpu:
os.environ["CUDA_VISIBLE_DEVICES"] = "0"
else:
os.environ["CUDA_VISIBLE_DEVICES"] = "-1"
from keras.backend.tensorflow_backend import set_session
config = tf.ConfigProto()
config.gpu_options.per_process_gpu_memory_fraction = 1.0
set_session(tf.Session(config=config))
if __name__ == '__main__':
# classes_path = 'data/voc_classes.txt'
classes_path = 'data/coco_classes.txt'
# model_path可以是'yolov4.h5'、'./weights/step00001000.h5'这些。
model_path = 'yolov4.h5'
# model_path = './weights/step00070000.h5'
# input_shape越大,精度会上升,但速度会下降。
# input_shape = (320, 320)
input_shape = (416, 416)
# input_shape = (608, 608)
# 验证时的分数阈值和nms_iou阈值
conf_thresh = 0.05
nms_thresh = 0.45
# 是否给图片画框。不画可以提速。读图片、后处理还可以继续优化。
draw_image = True
# draw_image = False
num_anchors = 3
all_classes = get_classes(classes_path)
num_classes = len(all_classes)
inputs = layers.Input(shape=(None, None, 3))
yolo = YOLOv4(inputs, num_classes, num_anchors)
yolo.load_weights(model_path, by_name=True)
_decode = Decode(conf_thresh, nms_thresh, input_shape, yolo, all_classes)
if not os.path.exists('images/res/'): os.mkdir('images/res/')
path_dir = os.listdir('images/test')
# warm up
if use_gpu:
for k, filename in enumerate(path_dir):
image = cv2.imread('images/test/' + filename)
image, boxes, scores, classes = _decode.detect_image(image, draw_image=False)
if k == 10:
break
time_stat = deque(maxlen=20)
start_time = time.time()
end_time = time.time()
num_imgs = len(path_dir)
start = time.time()
for k, filename in enumerate(path_dir):
image = cv2.imread('images/test/' + filename)
image, boxes, scores, classes = _decode.detect_image(image, draw_image)
# 估计剩余时间
start_time = end_time
end_time = time.time()
time_stat.append(end_time - start_time)
time_cost = np.mean(time_stat)
eta_sec = (num_imgs - k) * time_cost
eta = str(datetime.timedelta(seconds=int(eta_sec)))
logger.info('Infer iter {}, num_imgs={}, eta={}.'.format(k, num_imgs, eta))
if draw_image:
cv2.imwrite('images/res/' + filename, image)
logger.info("Detection bbox results save in images/res/{}".format(filename))
cost = time.time() - start
logger.info('total time: {0:.6f}s'.format(cost))
logger.info('Speed: %.6fs per image, %.1f FPS.'%((cost / num_imgs), (num_imgs / cost)))