目标检测-mAP测试

红太狼 2022-10-24 01:42 359阅读 0赞

个人微信公众号

在这里插入图片描述

keras-yolov3源码

  1. git clone https://github.com/qqwweee/keras-yolo3.git

目录

  1. 测试图片的准备
  2. 数据标签文件的格式转换(XML转换为txt格式)
  3. 模型对测试图片的检测以及检测结果坐标文件的生成
  4. MAP测试
  5. 结果

说明:

  1. 本次试验中,map的测试采用的是人脸图片数据;
  2. 本试验中模型的训练/MAP测试只使用了部分试验数据,因此,最后的MAP值并不高,仅供参考;

一、测试图片准备

1:首先,从GitHub上下载相关的代码,并保存到keras_yolo项目文件夹内;

  1. git clone https://github.com/Cartucho/mAP

2:进入mAP子目录,将需要测试的图片以及图片对应的XML文件放置对应的文件夹内;
测试图片放入的目录为: mAP/input/images-optional
图片对应的XML放入的目录为:mAP/input/ground-truth

二、 测试图片的标签格式转换(.txt)

本次测试中,数据格式是的VOC格式,查询文件中Readme发现,有现成的代码可用,无需做修改;
打开终端,进入子目录、运行代码:

  1. cd mAP/scripts/extra
  2. python convert_gt_xml.py

代码运行完后,测试数据的GT坐标会保存在对应的txt文件内,XML文件会另存在目录中的backup文件夹内;

三、 模型对测试图片进行测试并生成结果坐标信息保存至txt文件

1.测试代码如下,需放置在keras_yolo/ 项目文件夹下,与yolo_video.py在同级目录;

2.运行代码,将会生成对应的测试结果并保存在指定的目录中;
python yolo_detect.py

  1. # -*- coding: utf-8 -*-
  2. """
  3. Class definition of YOLO_v3 style detection model on image and video
  4. """
  5. import colorsys
  6. import os
  7. import sys
  8. from timeit import default_timer as timer
  9. import numpy as np
  10. from keras import backend as K
  11. from keras.models import load_model
  12. from keras.layers import Input
  13. from PIL import Image, ImageFont, ImageDraw
  14. from yolo3.model import yolo_eval, yolo_body, tiny_yolo_body
  15. from yolo3.utils import letterbox_image
  16. import os
  17. from keras.utils import multi_gpu_model
  18. class YOLO(object):
  19. _defaults = {
  20. "model_path": '/your/path/to/keras-yolo3/model_data/face_h5/face.h5', ##训练好的模型的路径
  21. "anchors_path": 'model_data/tiny_yolo_anchors.txt',
  22. "classes_path": 'model_data/face.txt',
  23. "score" : 0.3,
  24. "iou" : 0.45,
  25. "model_image_size" : (416, 416),
  26. "gpu_num" : 0
  27. }
  28. @classmethod
  29. def get_defaults(cls, n):
  30. if n in cls._defaults:
  31. return cls._defaults[n]
  32. else:
  33. return "Unrecognized attribute name '" + n + "'"
  34. def __init__(self, **kwargs):
  35. self.__dict__.update(self._defaults) # set up default values
  36. self.__dict__.update(kwargs) # and update with user overrides
  37. self.class_names = self._get_class()
  38. self.anchors = self._get_anchors()
  39. self.sess = K.get_session()
  40. self.boxes, self.scores, self.classes = self.generate()
  41. def _get_class(self):
  42. classes_path = os.path.expanduser(self.classes_path)
  43. with open(classes_path) as f:
  44. class_names = f.readlines()
  45. class_names = [c.strip() for c in class_names]
  46. return class_names
  47. def _get_anchors(self):
  48. anchors_path = os.path.expanduser(self.anchors_path)
  49. with open(anchors_path) as f:
  50. anchors = f.readline()
  51. anchors = [float(x) for x in anchors.split(',')]
  52. return np.array(anchors).reshape(-1, 2)
  53. def generate(self):
  54. model_path = os.path.expanduser(self.model_path)
  55. assert model_path.endswith('.h5'), 'Keras model or weights must be a .h5 file.'
  56. # Load model, or construct model and load weights.
  57. num_anchors = len(self.anchors)
  58. num_classes = len(self.class_names)
  59. is_tiny_version = num_anchors==6 # default setting
  60. try:
  61. self.yolo_model = load_model(model_path, compile=False)
  62. except:
  63. self.yolo_model = tiny_yolo_body(Input(shape=(None,None,3)), num_anchors//2, num_classes) \
  64. if is_tiny_version else yolo_body(Input(shape=(None,None,3)), num_anchors//3, num_classes)
  65. self.yolo_model.load_weights(self.model_path) # make sure model, anchors and classes match
  66. else:
  67. assert self.yolo_model.layers[-1].output_shape[-1] == \
  68. num_anchors/len(self.yolo_model.output) * (num_classes + 5), \
  69. 'Mismatch between model and given anchor and class sizes'
  70. print('{} model, anchors, and classes loaded.'.format(model_path))
  71. # Generate colors for drawing bounding boxes.
  72. hsv_tuples = [(x / len(self.class_names), 1., 1.)
  73. for x in range(len(self.class_names))]
  74. self.colors = list(map(lambda x: colorsys.hsv_to_rgb(*x), hsv_tuples))
  75. self.colors = list(
  76. map(lambda x: (int(x[0] * 255), int(x[1] * 255), int(x[2] * 255)),
  77. self.colors))
  78. np.random.seed(10101) # Fixed seed for consistent colors across runs.
  79. np.random.shuffle(self.colors) # Shuffle colors to decorrelate adjacent classes.
  80. np.random.seed(None) # Reset seed to default.
  81. # Generate output tensor targets for filtered bounding boxes.
  82. self.input_image_shape = K.placeholder(shape=(2, ))
  83. if self.gpu_num>=2:
  84. self.yolo_model = multi_gpu_model(self.yolo_model, gpus=self.gpu_num)
  85. boxes, scores, classes = yolo_eval(self.yolo_model.output, self.anchors,
  86. len(self.class_names), self.input_image_shape,
  87. score_threshold=self.score, iou_threshold=self.iou)
  88. return boxes, scores, classes
  89. def detect_image(self, image):
  90. start = timer()
  91. if self.model_image_size != (None, None):
  92. assert self.model_image_size[0]%32 == 0, 'Multiples of 32 required'
  93. assert self.model_image_size[1]%32 == 0, 'Multiples of 32 required'
  94. boxed_image = letterbox_image(image, tuple(reversed(self.model_image_size)))
  95. else:
  96. new_image_size = (image.width - (image.width % 32),
  97. image.height - (image.height % 32))
  98. boxed_image = letterbox_image(image, new_image_size)
  99. image_data = np.array(boxed_image, dtype='float32')
  100. print(image_data.shape)
  101. image_data /= 255.
  102. image_data = np.expand_dims(image_data, 0) # Add batch dimension.
  103. out_boxes, out_scores, out_classes = self.sess.run(
  104. [self.boxes, self.scores, self.classes],
  105. feed_dict={
  106. self.yolo_model.input: image_data,
  107. self.input_image_shape: [image.size[1], image.size[0]],
  108. K.learning_phase(): 0
  109. })
  110. print('Found {} boxes for {}'.format(len(out_boxes), 'img'))
  111. font = ImageFont.truetype(font='font/FiraMono-Medium.otf',
  112. size=np.floor(3e-2 * image.size[1] + 0.5).astype('int32'))
  113. thickness = (image.size[0] + image.size[1]) // 300
  114. for i, c in reversed(list(enumerate(out_classes))):
  115. predicted_class = self.class_names[c]
  116. box = out_boxes[i]
  117. score = out_scores[i]
  118. label = '{} {:.2f}'.format(predicted_class, score)
  119. draw = ImageDraw.Draw(image)
  120. label_size = draw.textsize(label, font)
  121. top, left, bottom, right = box
  122. top = max(0, np.floor(top + 0.5).astype('int32'))
  123. left = max(0, np.floor(left + 0.5).astype('int32'))
  124. bottom = min(image.size[1], np.floor(bottom + 0.5).astype('int32'))
  125. right = min(image.size[0], np.floor(right + 0.5).astype('int32'))
  126. print(label, (left, top), (right, bottom))
  127. #new_f=open("/home/shan/xws/pro/keras-yolo3/detection-results/"+tmp_file.replace(".jpg", ".txt"), "a")
  128. new_f.write("%s %s %s %s %s\n" % (label, left, top, right, bottom))
  129. if top - label_size[1] >= 0:
  130. text_origin = np.array([left, top - label_size[1]])
  131. else:
  132. text_origin = np.array([left, top + 1])
  133. # My kingdom for a good redistributable image drawing library.
  134. for i in range(thickness):
  135. draw.rectangle(
  136. [left + i, top + i, right - i, bottom - i],
  137. outline=self.colors[c])
  138. draw.rectangle(
  139. [tuple(text_origin), tuple(text_origin + label_size)],
  140. fill=self.colors[c])
  141. draw.text(text_origin, label, fill=(0, 0, 0), font=font)
  142. del draw
  143. end = timer()
  144. print(end - start)
  145. return image
  146. def close_session(self):
  147. self.sess.close()
  148. if __name__ == '__main__':
  149. # yolo=YOLO()
  150. # path = '1.jpg'
  151. # try:
  152. # image = Image.open(path)
  153. # except:
  154. # print('Open Error! Try again!')
  155. # else:
  156. # r_image = yolo.detect_image(image)
  157. # r_image.show()
  158. # yolo.close_session()
  159. #strat1=timer()
  160. dirname="/your/path/to/map/input/image-optional/" ##该目录为测试照片的存储路径,每次测试照片的数量可以自己设定
  161. path=os.path.join(dirname)
  162. pic_list=os.listdir(path)
  163. count=0
  164. yolo=YOLO()
  165. for filename in pic_list:
  166. tmp_file=pic_list[count]
  167. new_f=open("/your/path/to/map/input/detection-results/"+tmp_file.replace(".jpg", ".txt"), "a") #预测坐标生成txt文件保存的路径
  168. abs_path=path+pic_list[count]
  169. image = Image.open(abs_path)
  170. r_image = yolo.detect_image(image)
  171. count=count+1
  172. #end1=timer()
  173. print(count)
  174. yolo.close_session()

四、 MAP结果测试

  1. python main.py

五、 MAP结果(一类/部分少量数据测试结果)

在这里插入图片描述

参考链接

  • YoloV3_Keras模型MAP测试
  • Keras_Yolov3 实现人脸检测

发表评论

表情:
评论列表 (有 0 条评论,359人围观)

还没有评论,来说两句吧...

相关阅读

    相关 理解目标检测当中的mAP

    我们在评价一个目标检测算法的“好坏”程度的时候,往往采用的是pascal voc 2012的评价标准mAP。 网上一些资料博客参差不齐,缺乏直观易懂的正确说明。希望这篇博文能

    相关 目标检测评价指标—mAP

    对于了解目标检测的评价指标是很重要的,否则自己不懂这个代表什么意思,如何调参。网上有太多资料,可是杂乱,甚至有一乱说,我在学习mAP的时候,记录下自己所学,总结下,也分享出来,