• 大小: 3KB
    文件类型: .zip
    金币: 1
    下载: 0 次
    发布日期: 2021-05-27
  • 语言: 其他
  • 标签: 深度学习  

资源简介

几个月前自己上手YOLOV3-keras,自己训练了一个数据集。在测试的时候,发现源码作者的测试不好用。自己稍稍修改了一下。 几点改进 (1)批量测试图片 将待测试的图片放入 './test'路径下。 测试的时候,第一张图片需要的时间大约是 2s左右,因为需要加载模型,所需时间就相对较长一些。在博主的机器上,测试一一张图片的时间大约是0.1s左右; (2)保存测试结果 完成测试后,将测试的结果保存在result文件夹中。方便以后查看 (3)将测试结果输出为一个result.txt文件。 result.txt内容包含了每一个bbox的信息。

资源截图

代码片段和文件信息

# -*- coding: utf-8 -*-
“““
Class definition of YOLO_v3 style detection model on image and video
“““

import colorsys
import os
from timeit import default_timer as timer
import time

import numpy as np
from keras import backend as K
from keras.models import load_model
from keras.layers import Input
from PIL import Image ImageFont ImageDraw

from yolo3.model import yolo_eval yolo_body tiny_yolo_body
from yolo3.utils import letterbox_image
from keras.utils import multi_gpu_model

path = ‘./test/‘  #待检测图片的位置

# 创建创建一个存储检测结果的dir
result_path = ‘./result‘
if not os.path.exists(result_path):
    os.makedirs(result_path)

# result如果之前存放的有文件,全部清除
for i in os.listdir(result_path):
    path_file = os.path.join(result_pathi)  
    if os.path.isfile(path_file):
        os.remove(path_file)

#创建一个记录检测结果的文件
txt_path =result_path + ‘/result.txt‘
file = open(txt_path‘w‘)  

class YOLO(object):
    _defaults = {
        “model_path“: ‘model_data/yolo.h5‘
        “anchors_path“: ‘model_data/yolo_anchors.txt‘
        “classes_path“: ‘model_data/coco_classes.txt‘
        “score“ : 0.3
        “iou“ : 0.45
        “model_image_size“ : (416 416)
        “gpu_num“ : 1
    }

    @classmethod
    def get_defaults(cls n):
        if n in cls._defaults:
            return cls._defaults[n]
        else:
            return “Unrecognized attribute name ‘“ + n + “‘“

    def __init__(self **kwargs):
        self.__dict__.update(self._defaults) # set up default values
        self.__dict__.update(kwargs) # and update with user overrides
        self.class_names = self._get_class()
        self.anchors = self._get_anchors()
        self.sess = K.get_session()
        self.boxes self.scores self.classes = self.generate()

    def _get_class(self):
        classes_path = os.path.expanduser(self.classes_path)
        with open(classes_path) as f:
            class_names = f.readlines()
        class_names = [c.strip() for c in class_names]
        return class_names

    def _get_anchors(self):
        anchors_path = os.path.expanduser(self.anchors_path)
        with open(anchors_path) as f:
            anchors = f.readline()
        anchors = [float(x) for x in anchors.split(‘‘)]
        return np.array(anchors).reshape(-1 2)

    def generate(self):
        model_path = os.path.expanduser(self.model_path)
        assert model_path.endswith(‘.h5‘) ‘Keras model or weights must be a .h5 file.‘

        # Load model or construct model and load weights.
        num_anchors = len(self.anchors)
        num_classes = len(self.class_names)
        is_tiny_version = num_anchors==6 # default setting
        try:
            self.yolo_model = load_model(model_path compile=False)
        except:
            self.yolo_model = tiny_yolo_body(Input(shape=(NoneNone3)) num_anchors//2 num_classes) \
                if is_tiny_version else yolo_body(Input(shape=(NoneNone3)) num_anchors//3 num_classes)
            self.yolo_model.load_weights(self.model_path) # make sur

 属性            大小     日期    时间   名称
----------- ---------  ---------- -----  ----
     文件        8628  2019-04-15 18:12  yolo_test.py
     文件         164  2019-04-15 19:35  readme.txt

评论

共有 条评论