資源簡介
幾個月前自己上手YOLOV3-keras,自己訓練了一個數(shù)據(jù)集。在測試的時候,發(fā)現(xiàn)源碼作者的測試不好用。自己稍稍修改了一下。
幾點改進
(1)批量測試圖片
將待測試的圖片放入 './test'路徑下。
測試的時候,第一張圖片需要的時間大約是 2s左右,因為需要加載模型,所需時間就相對較長一些。在博主的機器上,測試一一張圖片的時間大約是0.1s左右;
(2)保存測試結(jié)果
完成測試后,將測試的結(jié)果保存在result文件夾中。方便以后查看
(3)將測試結(jié)果輸出為一個result.txt文件。
result.txt內(nèi)容包含了每一個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/‘??#待檢測圖片的位置
#?創(chuàng)建創(chuàng)建一個存儲檢測結(jié)果的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)
#創(chuàng)建一個記錄檢測結(jié)果的文件
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
評論
共有 條評論