1 Star 0 Fork 1

钟龙申/Keras-YOLOv4

forked from wolver/Keras-YOLOv4 
加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
该仓库未声明开源许可证文件(LICENSE),使用请关注具体项目描述及其代码上游依赖。
克隆/下载
demo.py 3.05 KB
一键复制 编辑 原始数据 按行查看 历史
miemie2013 提交于 2020-05-29 23:02 . readme
#! /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/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)
time_stat = deque(maxlen=20)
start_time = time.time()
end_time = time.time()
path_dir = os.listdir('images/test')
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: {0:.6f}s per image'.format(cost / num_imgs))
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/zhonglongshen/Keras-YOLOv4.git
[email protected]:zhonglongshen/Keras-YOLOv4.git
zhonglongshen
Keras-YOLOv4
Keras-YOLOv4
master

搜索帮助