当前位置:网站首页>Implementation of object detection case based on SSD
Implementation of object detection case based on SSD
2022-04-23 17:54:00 【Stephen_ Tao】
List of articles
SSD Introduce
SSD It is characterized by :
- SSD Combined with the YOLO Return to thought and Faster-RCNN Medium Anchor Mechanism , The multi-scale region of each position in the whole map is used for regression , Both keep YOLO Fast features , It also guarantees the following of window prediction Faster-RCNN It's just as accurate .
- SSD The core of is to use convolution kernel to predict a series of Default Bounding Boxes Categories 、 Coordinate offset .
SSD structure
With VGG-16 Based on , Use VGG The first five convolutions of , Add from CONV6 At the beginning 5 A convolution structure , Enter picture requirements 300*300.
SSD Algorithm flow
Case realization
By building models , Load the trained parameters , Achieve specific types of object detection tasks
Import package
from nets.ssd_net import SSD300
from keras.preprocessing.image import load_img,img_to_array
from imageio import imread
from keras.applications.imagenet_utils import preprocess_input
from utils.ssd_utils import BBoxUtility
import matplotlib.pyplot as plt
import numpy as np
import os
Define initialization function
Function call in the form of class
class SSDTest(object):
def __init__(self):
# Define the identification category
self.classes_name = ['Aeroplane', 'Bicycle', 'Bird', 'Boat', 'Bottle',
'Bus', 'Car', 'Cat', 'Chair', 'Cow', 'Diningtable',
'Dog', 'Horse', 'Motorbike', 'Person', 'Pottedplant',
'Sheep', 'Sofa', 'Train', 'Tvmonitor']
self.classes_nums = len(self.classes_name) + 1
self.input_shape = (300,300,3)
In the initialization function , Defines the category name of object detection , Total number of categories detected ( Include background categories ), And enter the shape and size of the picture .
Model construction and operation
def model_process(self):
# establish SSD300 The basic model of
model = SSD300(self.input_shape,num_classes=self.classes_nums)
# Import trained parameters
model.load_weights('./ckpt/weights_SSD300.hdf5',by_name=True)
feature = []
images_data = []
# Traverse images All the pictures in the catalog
for pic_name in os.listdir('./images/'):
img_path = os.path.join('./images/',pic_name)
img = load_img(img_path,target_size=(self.input_shape[0],self.input_shape[1]))
img = img_to_array(img)
feature.append(img)
images_data.append(imread(img_path))
inputs = preprocess_input(np.asarray(feature))
preds = model.predict(inputs)
print(preds.shape)
# Non maximum suppression
bbox_util = BBoxUtility(self.classes_nums)
results = bbox_util.detection_out(preds)
print(results[0].shape,results[1].shape)
return results,images_data
Result processing and picture marking
def tag_picture(self,images,results):
for i,img in enumerate(images):
pre_label = results[i][:, 0]
pre_conf = results[i][:, 1]
pre_xmin = results[i][:, 2]
pre_ymin = results[i][:, 3]
pre_xmax = results[i][:, 4]
pre_ymax = results[i][:, 5]
# print("label:{}, probability:{}, xmin:{}, ymin:{}, xmax:{}, ymax:{}".
# format(pre_label, pre_conf, pre_xmin, pre_ymin, pre_xmax, pre_ymax))
# Filter out the results with low confidence
top_indices = [i for i,conf in enumerate(pre_conf) if conf>=0.6]
top_conf = pre_conf[top_indices]
top_label_indices = pre_label[top_indices].tolist()
top_xmin = pre_xmin[top_indices]
top_ymin = pre_ymin[top_indices]
top_xmax = pre_xmax[top_indices]
top_ymax = pre_ymax[top_indices]
print("label:{}, probability:{}, xmin:{}, ymin:{}, xmax:{}, ymax:{}".
format(top_label_indices, top_conf, top_xmin, top_ymin, top_xmax, top_ymax))
# print(np.array(images)/255.0)
# The plot
colors = plt.cm.hsv(np.linspace(0, 1, 21)).tolist()
plt.figure()
plt.imshow(img / 255.)
currentAxis = plt.gca()
for i in range(top_conf.shape[0]):
xmin = int(round(top_xmin[i] * img.shape[1]))
ymin = int(round(top_ymin[i] * img.shape[0]))
xmax = int(round(top_xmax[i] * img.shape[1]))
ymax = int(round(top_ymax[i] * img.shape[0]))
# Get the prediction probability of the picture , name , Define display colors
score = top_conf[i]
label = int(top_label_indices[i])
label_name = self.classes_name[label - 1]
display_txt = '{:0.2f}, {}'.format(score, label_name)
coords = (xmin, ymin), xmax - xmin + 1, ymax - ymin + 1
color = colors[label]
# Show box
currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor=color, linewidth=2))
# The upper left corner shows the probability and name
currentAxis.text(xmin, ymin, display_txt, bbox={
'facecolor': color, 'alpha': 0.5})
plt.show()
Write the main function
if __name__ == '__main__':
ssd = SSDTest()
results,images = ssd.model_process()
ssd.tag_picture(images,results)
Running results
I chose 4 A picture , The following is a SSD The running results of the target detection algorithm :
You can see , use SSD The effect of target detection is better .
版权声明
本文为[Stephen_ Tao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230548468276.html
边栏推荐
- Sword finger offer 22 The penultimate node in the linked list - speed pointer
- 2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试
- Comparison between xtask and kotlin coroutine
- 2022制冷与空调设备运行操作判断题及答案
- Nat Commun|在生物科学领域应用深度学习的当前进展和开放挑战
- C language implements memcpy, memset, strcpy, strncpy, StrCmp, strncmp and strlen
- 高德地图搜索、拖拽 查询地址
- 2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
- Construction of functions in C language programming
- 92. Reverse linked list II byte skipping high frequency question
猜你喜欢
随机推荐
C1 notes [task training chapter I]
2022年广东省安全员A证第三批(主要负责人)特种作业证考试题库及在线模拟考试
C1小笔记【任务训练篇一】
Leak detection and vacancy filling (6)
Dry goods | how to extract thumbnails quickly?
JS forms the items with the same name in the array object into the same array according to the name
440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
ES6
394. String decoding - auxiliary stack
Construction of functions in C language programming
Cross domain settings of Chrome browser -- including new and old versions
JS get link? The following parameter name or value, according to the URL? Judge the parameters after
Leak detection and vacancy filling (VII)
Halo 开源项目学习(二):实体类与数据表
958. Complete binary tree test
587. Install fence / Sword finger offer II 014 Anagrams in strings
2022制冷与空调设备运行操作判断题及答案
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
MySQL进阶学习之SQL优化【插入,主键,排序,分组,分页,计数】
Kubernetes service discovery monitoring endpoints