# 教你从零开始检测皮卡丘-CNN目标检测入门教程(上)

## 预设框 Default anchor boxes

import mxnet as mx from mxnet import nd from mxnet.contrib.ndarray import MultiBoxPrior n=40 # 输入形状: batch x channel x height x weight x=nd.random_uniform(shape=(1,3,n,n)) y=MultiBoxPrior(x,sizes=[.5,.25,.1],ratios=[1,2,.5]) # 取位于 (20,20) 像素点的第一个预设框 # 格式为 (x_min, y_min, x_max, y_max) boxes=y.reshape((n,n,-1,4)) print('The first anchor box at row 21, column 21:',boxes[20,20,0,:])

The first anchor box at row 21, column 21: [ 0.26249999 0.26249999 0.76249999 0.76249999] <NDArray 4 @cpu(0)>

import matplotlib.pyplot as plt def box_to_rect(box,color,linewidth=3): """convert an anchor box to a matplotlib rectangle""" box=box.asnumpy() returnplt.Rectangle( (box[0],box[1]),(box[2]-box[0]),(box[3]-box[1]), fill=False,edgecolor=color,linewidth=linewidth) colors=['blue','green','red','black','magenta'] plt.imshow(nd.ones((n,n,3)).asnumpy()) anchors=boxes[20,20,:,:] for i in range(anchors.shape[0]): plt.gca().add_patch(box_to_rect(anchors[i,:]*n,colors[i])) plt.show()

## 分类预测 Predict classes

from mxnet.gluon import nn def class_predictor(num_anchors,num_classes): """return a layer to predict classes""" returnnn.Conv2D(num_anchors*(num_classes+1),3,padding=1) cls_pred=class_predictor(5,10) cls_pred.initialize() x=nd.zeros((2,3,20,20)) print('Class prediction',cls_pred(x).shape)

Class prediction (2, 55, 20, 20)

## 预测预设框偏移 Predict anchor boxes

def box_predictor(num_anchors): """return a layer to predict delta locations""" returnnn.Conv2D(num_anchors*4,3,padding=1) box_pred=box_predictor(10) box_pred.initialize() x=nd.zeros((2,3,20,20)) print('Box prediction',box_pred(x).shape)

Box prediction (2, 40, 20, 20)

## 下采样特征层 Down-sample features

def down_sample(num_filters): """stack two Conv-BatchNorm-Relu blocks and then a pooling layer to halve the feature size""" out=nn.HybridSequential() for_inrange(2): out.add(nn.Conv2D(num_filters,3,strides=1,padding=1)) out.add(nn.BatchNorm(in_channels=num_filters)) out.add(nn.Activation('relu')) out.add(nn.MaxPool2D(2)) return out blk=down_sample(10) blk.initialize() x=nd.zeros((2,3,20,20)) print('Before',x.shape,'after',blk(x).shape)

Before (2, 3, 20, 20) after (2, 10, 10, 10)

## 整合多个特征层预测值 Manage predictions from multiple layers

# 随便创建一个大小为 20x20的预测层 feat1=nd.zeros((2,8,20,20)) print('Feature map 1',feat1.shape) cls_pred1=class_predictor(5,10) cls_pred1.initialize() y1=cls_pred1(feat1) print('Class prediction for feature map 1',y1.shape) # 下采样 ds=down_sample(16) ds.initialize() feat2=ds(feat1) print('Feature map 2',feat2.shape) cls_pred2=class_predictor(3,10) cls_pred2.initialize() y2=cls_pred2(feat2) print('Class prediction for feature map 2',y2.shape)

Feature map 1 (2, 8, 20, 20) Class prediction for feature map 1 (2, 55, 20, 20) Feature map 2 (2, 16, 10, 10) Class prediction for feature map 2 (2, 33, 10, 10)

def flatten_prediction(pred): return nd.flatten(nd.transpose(pred,axes=(0,2,3,1))) def concat_predictions(preds): return nd.concat(*preds,dim=1) flat_y1=flatten_prediction(y1) print('Flatten class prediction 1',flat_y1.shape) flat_y2=flatten_prediction(y2) print('Flatten class prediction 2',flat_y2.shape) print('Concat class predictions',concat_predictions([flat_y1,flat_y2]).shape)

Flatten class prediction 1 (2, 22000) Flatten class prediction 2 (2, 3300) Concat class predictions (2, 25300)

## 主干网络 Body network

from mxnet import gluon def body(): """return the body network""" out=nn.HybridSequential() for nfilters in [16,32,64]: out.add(down_sample(nfilters)) return out bnet=body() bnet.initialize() x=nd.zeros((2,3,256,256)) print('Body network',[y.shape for y in bnet(x)])

Body network [(64, 32, 32), (64, 32, 32)]

## 打包收工 Put all things together

from mxnet import gluon class ToySSD(gluon.Block): def __init__(self, num_classes, **kwargs): super(ToySSD, self).__init__(**kwargs) # 5个预测层，每层负责的预设框尺寸不同，由小到大，符合网络的形状 self.anchor_sizes = [[.2, .272], [.37, .447], [.54, .619], [.71, .79], [.88, .961]] # 每层的预设框都用 1，2，0.5作为长宽比候选 self.anchor_ratios = [[1, 2, .5]] * 5 self.num_classes = num_classes with self.name_scope(): self.body, self.downsamples, self.class_preds, self.box_preds = toy_ssd_model(4, num_classes) def forward(self, x): default_anchors, predicted_classes, predicted_boxes = toy_ssd_forward(x, self.body, self.downsamples, self.class_preds, self.box_preds, self.anchor_sizes, self.anchor_ratios) # 把从每个预测层输入的结果摊平并连接，以确保一一对应 anchors = concat_predictions(default_anchors) box_preds = concat_predictions(predicted_boxes) class_preds = concat_predictions(predicted_classes) # 改变下形状，为了更方便地计算softmax class_preds = nd.reshape(class_preds, shape=(0, -1, self.num_classes + 1)) return anchors, class_preds, box_preds

## 网络输出示意 Outputs of ToySSD

# 新建一个2个正类的SSD网络 net = ToySSD(2) net.initialize() x = nd.zeros((1, 3, 256, 256)) default_anchors, class_predictions, box_predictions = net(x) print('Outputs:', 'anchors', default_anchors.shape, 'class prediction', class_predictions.shape, 'box prediction', box_predictions.shape)

Outputs: anchors (1, 5444, 4) class prediction (1, 5444, 3) box prediction (1, 21776)

## 数据集 Dataset

from mxnet.test_utils import download import os.path as osp def verified(file_path, sha1hash): import hashlib sha1 = hashlib.sha1() with open(file_path, 'rb') as f: while True: data = f.read(1048576) if not data: break sha1.update(data) matched = sha1.hexdigest() == sha1hash if not matched: print('Found hash mismatch in file {}, possibly due to incomplete download.'.format(file_path)) return matched url_format = 'https://apache-mxnet.s3-accelerate.amazonaws.com/gluon/datasets/pikachu/{}' hashes = {'train.rec': 'e6bcb6ffba1ac04ff8a9b1115e650af56ee969c8', 'train.idx': 'dcf7318b2602c06428b9988470c731621716c393', 'val.rec': 'd6c33f799b4d058e82f2cb5bd9a976f69d72d520'} for k, v in hashes.items(): fname = 'pikachu_' + k target = osp.join('data', fname) url = url_format.format(k) if not osp.exists(target) or not verified(target, v): print('Downloading', target, url) download(url, fname=fname, dirname='data', overwrite=True)

DataBatch: data shapes: [(32, 3, 256, 256)] label shapes: [(32, 1, 5)]

## 示意图 Illustration

import numpy as np img = batch.data[0][0].asnumpy() # 取第一批数据中的第一张，转成numpy img = img.transpose((1, 2, 0)) # 交换下通道的顺序 img += np.array([123, 117, 104]) img = img.astype(np.uint8) # 图片应该用0-255的范围 # 在图上画出真实标签的方框 for label in batch.label[0][0].asnumpy(): if label[0] < 0: break print(label) xmin, ymin, xmax, ymax = [int(x * data_shape) for x in label[1:5]] rect = plt.Rectangle((xmin, ymin), xmax - xmin, ymax - ymin, fill=False, edgecolor=(1, 0, 0), linewidth=3) plt.gca().add_patch(rect) plt.imshow(img) plt.show()

[ 0. 0.75724518 0.34316057 0.93332517 0.70017999]

0 条评论

## 相关文章

### 深度学习效果不好？试试 Batch Normalization 吧！

Batch Normalization（简称BN）自从提出之后，因为效果特别好，很快被作为深度学习的标准工具应用在了各种场合。BN大法虽然好，但是也存在一些局...

1.4K3

3853

6355

3584

1621

7046

1514

3263

68811

### Spark MLlib知识点学习整理

MLlib的设计原理:把数据以RDD的形式表示，然后在分布式数据集上调用各种算法。MLlib就是RDD上一系列可供调用的函数的集合。

1532