当前位置:网站首页>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
边栏推荐
- Kubernetes 服务发现 监控Endpoints
- Cloud native Virtualization: building edge computing instances based on kubevirt
- 41. 缺失的第一个正数
- 2022江西储能技术展会,中国电池展,动力电池展,燃料电池展
- Write a regular
- 440. 字典序的第K小数字(困难)-字典树-数节点-字节跳动高频题
- Vite configure proxy proxy to solve cross domain
- Flask项目的部署详解
- cv_ Solution of mismatch between bridge and opencv
- 圆环回原点问题-字节跳动高频题
猜你喜欢
Welcome to the markdown editor
JVM class loading mechanism
编译原理 求first集 follow集 select集预测分析表 判断符号串是否符合文法定义(有源码!!!)
Tell the truth of TS
Halo open source project learning (II): entity classes and data tables
Flask项目的部署详解
Halo open source project learning (II): entity classes and data tables
Logic regression principle and code implementation
EasymodbusTCP之clientexample解析
Client example analysis of easymodbustcp
随机推荐
Error in created hook: "referenceerror:" promise "undefined“
470. Rand10() is implemented with rand7()
2022江西光伏展,中國分布式光伏展會,南昌太陽能利用展
SystemVerilog (VI) - variable
Timestamp to formatted date
Encapsulate a timestamp to date method on string prototype
云原生虚拟化:基于 Kubevirt 构建边缘计算实例
.104History
Eigen learning summary
Sword finger offer 03 Duplicate number in array
102. Sequence traversal of binary tree
Dry goods | how to extract thumbnails quickly?
SQL optimization for advanced learning of MySQL [insert, primary key, sort, group, page, count]
386. 字典序排数(中等)-迭代-全排列
122. 买卖股票的最佳时机 II-一次遍历
Element calculation distance and event object
2022 judgment questions and answers for operation of refrigeration and air conditioning equipment
Gaode map search, drag and drop query address
Add animation to the picture under V-for timing
列錶的使用-增删改查