Single Shot Multibox Detection (SSD)实战(下)
Single Shot Multibox Detection (SSD)实战(下)
2. Training
将逐步解释如何训练SSD模型进行目标检测。
2.1. Data Reading and Initialization
创建的Pikachu数据集。
batch_size = 32
train_iter, _ = d2l.load_data_pikachu(batch_size)
Pikachu数据集中有1个类别。在定义模块之后,我们需要初始化模型参数并定义优化算法。
ctx, net = d2l.try_gpu(), TinySSD(num_classes=1)
net.initialize(init=init.Xavier(), ctx=ctx)
trainer = gluon.Trainer(net.collect_params(), 'sgd',
{'learning_rate': 0.2, 'wd': 5e-4})
2.2. Defining Loss and Evaluation Functions
目标检测有两种损失。一是锚箱类损失。为此,我们可以简单地重用我们在图像分类中使用的交叉熵损失函数。第二个损失是正锚箱偏移损失。偏移量预测是一个规范化问题。但是,在这里,我们没有使用前面介绍的平方损失。相反,我们使用L1范数损失,即预测值与地面真实值之差的绝对值。mask变量bbox_masks从损失计算中删除负锚定框和填充锚定框。最后,我们加入锚箱类别和补偿损失,以找到模型的最终损失函数。
cls_loss = gluon.loss.SoftmaxCrossEntropyLoss()
bbox_loss = gluon.loss.L1Loss()
def calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels, bbox_masks):
cls = cls_loss(cls_preds, cls_labels)
bbox = bbox_loss(bbox_preds * bbox_masks, bbox_labels * bbox_masks)
return cls + bbox
我们可以用准确率来评价分类结果。当我们使用L1范数损失,我们将使用平均绝对误差来评估包围盒预测结果。
def cls_eval(cls_preds, cls_labels):
# Because the category prediction results are placed in the final
# dimension, argmax must specify this dimension
return float((cls_preds.argmax(axis=-1) == cls_labels).sum())
def bbox_eval(bbox_preds, bbox_labels, bbox_masks):
return float((np.abs((bbox_labels - bbox_preds) * bbox_masks)).sum())

2.3. Training the Model
在模型训练过程中,我们必须在模型的正向计算过程中生成多尺度锚盒(anchors),并预测每个锚盒的类别(cls_preds)和偏移量(bbox_preds)。然后,我们根据标签信息Y标记每个锚定框的类别(cls_labels)和偏移量(bbox_labels)。最后,我们使用预测和标记的类别和偏移量值计算损失函数。为了简化代码,这里不计算训练数据集。
num_epochs, timer = 20, d2l.Timer()
animator = d2l.Animator(xlabel='epoch', xlim=[1, num_epochs],
legend=['class error', 'bbox mae'])
for epoch in range(num_epochs):
# accuracy_sum, mae_sum, num_examples, num_labels
metric = d2l.Accumulator(4)
train_iter.reset() # Read data from the start.
for batch in train_iter:
timer.start()
X = batch.data[0].as_in_ctx(ctx)
Y = batch.label[0].as_in_ctx(ctx)
with autograd.record():
# Generate multiscale anchor boxes and predict the category and
# offset of each
anchors, cls_preds, bbox_preds = net(X)
# Label the category and offset of each anchor box
bbox_labels, bbox_masks, cls_labels = npx.multibox_target(
anchors, Y, cls_preds.transpose(0, 2, 1))
# Calculate the loss function using the predicted and labeled
# category and offset values
l = calc_loss(cls_preds, cls_labels, bbox_preds, bbox_labels,
bbox_masks)
l.backward()
trainer.step(batch_size)
metric.add(cls_eval(cls_preds, cls_labels), cls_labels.size,
bbox_eval(bbox_preds, bbox_labels, bbox_masks),
bbox_labels.size)
cls_err, bbox_mae = 1-metric[0]/metric[1], metric[2]/metric[3]
animator.add(epoch+1, (cls_err, bbox_mae))
print('class err %.2e, bbox mae %.2e' % (cls_err, bbox_mae))
print('%.1f examples/sec on %s' % (train_iter.num_image/timer.stop(), ctx))
class err 2.35e-03, bbox mae 2.68e-03
4315.5 examples/sec on gpu(0)

3. Prediction
在预测阶段,我们要检测图像中所有感兴趣的对象。下面,我们读取测试图像并转换其大小。然后,我们将其转换为卷积层所需的四维格式。
img = image.imread('../img/pikachu.jpg')
feature = image.imresize(img, 256, 256).astype('float32')
X = np.expand_dims(feature.transpose(2,
0, 1), axis=0)
利用MultiBoxDetection函数,我们根据锚定框及其预测的偏移量来预测边界框。然后,我们使用非最大值抑制来移除类似的边界框。
def predict(X):
anchors, cls_preds, bbox_preds =
net(X.as_in_ctx(ctx))
cls_probs = npx.softmax(cls_preds).transpose(0,
2, 1)
output = npx.multibox_detection(cls_probs,
bbox_preds, anchors)
idx = [i for i, row in
enumerate(output[0]) if row[0] != -1]
return output[0, idx]
output = predict(X)
最后,我们取置信度至少为0.3的所有边界框,并将它们显示为最终输出。
def display(img, output, threshold):
d2l.set_figsize((5, 5))
fig = d2l.plt.imshow(img.asnumpy())
for row in output:
score = float(row[1])
if score < threshold:
continue
h, w = img.shape[0:2]
bbox = [row[2:6] * np.array((w,
h, w, h), ctx=row.ctx)]
d2l.show_bboxes(fig.axes, bbox,
'%.2f' % score, 'w')
display(img, output, threshold=0.3)

4. Loss
Function
由于空间的限制,我们在本实验中忽略了SSD模型的一些实现细节。您能否在以下方面进一步改进该模型?
For the predicted offsets, replace L1L1 norm loss with L1L1 regularization loss. This
loss function uses a square function around zero for greater smoothness. This
is the regularized area controlled by the hyperparameter σσ:
When σσ is large, this loss is similar
to the L1L1 norm loss. When the value is
small, the loss function is smoother.
sigmas = [10, 1, 0.5]
lines = ['-', '--', '-.']
x = np.arange(-2, 2, 0.1)
d2l.set_figsize()
for l, s in zip(lines, sigmas):
y = npx.smooth_l1(x, scalar=s)
d2l.plt.plot(x.asnumpy(), y.asnumpy(), l, label='sigma=%.1f' % s)
d2l.plt.legend


def focal_loss(gamma, x):
return -(1 - x) ** gamma * np.log(x)
x = np.arange(0.01, 1, 0.01)
for l, gamma in zip(lines, [0, 1, 5]):
y = d2l.plt.plot(x.asnumpy(), focal_loss(gamma, x).asnumpy(), l,
label='gamma=%.1f' % gamma)
d2l.plt.legend();

Training
and Prediction
When
an object is relatively large compared to the image, the model normally adopts
a larger input image size.
This generally produces a large
number of negative anchor boxes when labeling anchor box categories. We can
sample the negative anchor boxes to better balance the data categories. To do
this, we can set the MultiBoxTarget function’s negative_mining_ratio parameter.
Assign hyper-parameters with different weights to the
anchor box category loss and positive anchor box offset loss in the loss
function.
Refer to the SSD paper. What methods
can be used to evaluate the precision of object detection models?
5. Summary
- SSD is a multiscale object detection model. This model generates different
numbers of anchor boxes of different sizes based on the base network block
and each multiscale feature block and predicts the categories and offsets
of the anchor boxes to detect objects of different sizes. - During SSD model training, the loss function is calculated using the
predicted and labeled category and offset values.
Single Shot Multibox Detection (SSD)实战(下)的更多相关文章
- Single Shot Multibox Detection (SSD)实战(上)
Single Shot Multibox Detection (SSD)实战(上) 介绍了边界框.锚框.多尺度对象检测和数据集.现在,我们将利用这些背景知识构建一个目标检测模型:单次多盒检测(SSD) ...
- 论文笔记 SSD: Single Shot MultiBox Detector
转载自:https://zhuanlan.zhihu.com/p/33544892 前言 目标检测近年来已经取得了很重要的进展,主流的算法主要分为两个类型(参考RefineDet):(1)two-st ...
- SSD: Single Shot MultiBox Detector
By Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, Scott Reed, Cheng-Yang Fu, Alexande ...
- 目标检测--SSD: Single Shot MultiBox Detector(2015)
SSD: Single Shot MultiBox Detector 作者: Wei Liu, Dragomir Anguelov, Dumitru Erhan, Christian Szegedy, ...
- SSD(single shot multibox detector)算法及Caffe代码详解[转]
转自:AI之路 这篇博客主要介绍SSD算法,该算法是最近一年比较优秀的object detection算法,主要特点在于采用了特征融合. 论文:SSD single shot multibox det ...
- SSD(Single Shot MultiBox Detector)的安装配置和运行
下文图文介绍转自watersink的博文SSD(Single Shot MultiBox Detector)不得不说的那些事. 该方法出自2016年的一篇ECCV的oral paper,SSD: Si ...
- SSD(single shot multibox detector)
SSD,全称Single Shot MultiBox Detector,是Wei Liu在ECCV 2016上提出的一种目标检测算法,截至目前是主要的检测框架之一,相比Faster RCNN有明显的速 ...
- SSD: Single Shot MultiBox Detector 编译方法总结
SSD是一个基于单网络的目标检测框架,它是基于caffe实现的,所以下面的教程是基于已经编译好的caffe进行编译的. caffe的编译可以参考官网 caffe Installation Instal ...
- [论文理解]SSD:Single Shot MultiBox Detector
SSD:Single Shot MultiBox Detector Intro SSD是一套one-stage算法实现目标检测的框架,速度很快,在当时速度超过了yolo,精度也可以达到two-stag ...
随机推荐
- 深入浅出带你玩转sqlilabs(五)-布尔/延时盲注与二次注入
SQL测试-基于布尔,延时盲注 布尔,延时注入常用到的判断语句 regexp regexp '^xiaodi[a-z]' 匹配xiaodi及xiaodi...等 if if(条件,5,0) 条件成立 ...
- angr脚本——以angrctf解题记录为参考
angr脚本--以angrctf解题记录为参考 angr是用于逆向工程中进行二进制分析的一个python框架 符号执行 (Symbolic Execution)是一种程序分析技术.其可以通过分 ...
- 前端小白的学习之路html与css的较量【一】
html和css的较量 web结构的组成 html标签规则 快速生成一个html html的基本结构 标签的关系 标签 标题标签 段落 图片 超链接 a 属性 a标签里面的值 字符实体 新增的标签 1 ...
- [CSP-J2019 江西] 道路拆除 题解
发现大家都是将路径拆成三条链来做,这里提供一种暴力的乱搞方法. 思路 看到这一道题的第一想法就是跑最短路.可是仔细想想就发现,由于重合的路径只算一遍,所以导致两条最短路不一定是最优解. 接着,看到数据 ...
- 【js】Leetcode每日一题-叶子相似的树
[js]Leetcode每日一题-叶子相似的树 [题目描述] 请考虑一棵二叉树上所有的叶子,这些叶子的值按从左到右的顺序排列形成一个 叶值序列 . 举个例子,如上图所示,给定一棵叶值序列为 (6, 7 ...
- SpringBoot 项目 部署 jar方式
SpringBoot部署-jar方式 步骤1部署方式 Springboot 和我们之前学习的web 应用程序不一样,其本质上是一个 Java 应用程序,那么又如何部署呢? 通常来说,Springbo ...
- Java 给Word添加数字签名
本文以Java程序代码为例,介绍如何给Word文档添加数字签名. 程序运行环境 IntedliJ IDEA JDK 1.8.0 Jar包:spire.doc.jar 4.5.1 Word文档:.doc ...
- 手写Spring MVC框架(一) 实现简易版mvc框架
前言 前面几篇文章中,我们讲解了Spring MVC执⾏的⼤致原理及关键组件的源码解析,今天,我们来模仿它⼿写⾃⼰的mvc框架. 先梳理一下需要实现的功能点: tomcat加载配置文件web.xml: ...
- 推荐一个不得不知道的 Visual Studio 快捷键
不得不说,Visual Studio 内置了很多非常棒的快捷键,借助于这些快捷键我们甚至不需要再使用鼠标,就可以快速高效的编写代码,因此学习和熟悉这些快捷键是值得的. 其中有一个快捷键是我非常喜欢,也 ...
- Spring Cloud Gateway之全局异常拦截器
/** * @version 2019/8/14 * @description: 异常拦截器 * @modified: */ @Slf4j public class JsonExceptionHand ...