Caffe实战之Python接口系列(七)R-CNN detection

引言

记录学习官网的例程中的一些重要语句,遇到的问题等,内容分散,建议顺序查看。
主要是调用Caffe的Python接口
源文件就在{caffe_root}/examples中(目录下面的中文标题也附有链接),安装sudo pip install jupyter打开即可运行,初学者最好是放在它指定的目录,如,否则要改很多路径。
注:eaxmples是用jupyter notebook写的,部分Cell中出现了一些特殊的用法:
1. 感叹号‘!’:用于执行系统命令,如 !pwd
2. 百分号‘%’:用法太多,如 %matplotlib inline 显示绘图窗口 详见Jupyter Notebook Viewer

目录

R-CNN检测

  • R-CNN是一种先进的检测器,通过微调Caffe模型对区域建议(proposal)进行分类。 有关R-CNN系统和模型的完整详细信息,请参阅其项目站点和论文:

    Rich feature hierarchies for accurate object detection and semantic segmentation. Ross Girshick, Jeff Donahue, Trevor Darrell, Jitendra Malik. CVPR 2014. Arxiv 2013

  • 在这个例子中,我们通过ImageNet的R-CNN模型的纯Caffe版本进行检测。 R-CNN检测器输出ILSVRC13的200个检测类别的类别分数。 请记住,这些是原始一对多的SVM分数,因此它们没有概率校准,也不能跨类别比较。 请注意,这种现成的模型仅仅是为了方便起见,而不是完整的R-CNN模型。

1. 首先RegionProposals,获取模型

  • 选择性搜索是R-CNN使用的区域提议方案。 selective_search_ijcv_with_python Python模块负责通过选择性搜索的MATLAB实现提取proposal。 要安装它,请下载模块并将其目录命名为selective_search_ijcv_with_python,在MATLAB中运行demo以编译必要的函数,然后将其添加到PYTHONPATH进行导入。 (如果您准备了自己的区域提案,或者操心此步骤,detect.py接受图像列表和边界框作为CSV文件)
  • 执行./scripts/download_model_binary.py models / bvlc_reference_rcnn_ilsvrc13获取Caffe R-CNN ImageNet模型。

2. 运行网络

  • 完成后,我们将调用捆绑的detect.py来生成区域提议并运行网络。 有关参数的解释,请执行./dectct.py –help。

    !mkdir -p _temp
    !echo `pwd`/images/fish-bike.jpg > _temp/det_input.txt
    !../python/detect.py --crop_mode=selective_search --pretrained_model=../models/bvlc_reference_rcnn_ilsvrc13/bvlc_reference_rcnn_ilsvrc13.caffemodel --model_def=../models/bvlc_reference_rcnn_ilsvrc13/deploy.prototxt --gpu --raw_scale=255 _temp/det_input.txt _temp/det_output.h5
  • 此运行处于GPU模式, 对于CPU模式检测,请在不使用–gpu参数的情况下调用detect.py。

  • 运行此操作会将带有文件名,选定窗口及其检测分数的DataFrame输出到HDF5文件。 (我们只运行一个图像,所以文件名都是一样的。)

    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    %matplotlib inline
    
    df = pd.read_hdf('_temp/det_output.h5', 'df')
    print(df.shape)
    print(df.iloc[0])
  • 选择性搜索产生1570个区域proposal,根据其内容和大小,提案的数量因图像而异 - 选择性搜索不是数量不变的。
  • 通常,在大量图像上运行时,detect.py最有效:它首先提取所有图像的窗口提议,批量处理窗口以进行高效的GPU处理,然后输出结果。 只需在images_file中列出每行的图像,它就会处理所有这些图像。
  • 尽管本指南提供了R-CNN ImageNet检测的示例,但detect.py非常巧妙,可以适应不同的Caffe模型的输入尺寸,批量大小和输出类别。 您可以根据需要切换模型定义和预训练模型。 有关描述数据集的参数,请参阅python detect.py –help。 不需要硬编码。
  • 无论如何,现在让我们加载ILSVRC13检测类名称并制作预测的DataFrame。 请注意,您需要data / ilsvrc12 / get_ilsvrc12_aux.sh提取ilsvrc2012的辅助数据。

    with open('../data/ilsvrc12/det_synset_words.txt') as f:
    labels_df = pd.DataFrame([
        {
            'synset_id': l.strip().split(' ')[0],
            'name': ' '.join(l.strip().split(' ')[1:]).split(',')[0]
        }
        for l in f.readlines()
    ])
    labels_df.sort('synset_id')
    predictions_df = pd.DataFrame(np.vstack(df.prediction.values), columns=labels_df['name'])
    print(predictions_df.iloc[0])

3. 查看激活情况

```python
plt.gray()
plt.matshow(predictions_df.values)
plt.xlabel('Classes')
plt.ylabel('Windows')
```

4. 挑出所有窗口中的最大分数并显示top类别

  • Top检测实际上是人和自行车。 下面选择好的定位并显示(这里选择得分最高的人和自行车检测)

    
    # Find, print, and display the top detections: person and bicycle.
    
    i = predictions_df['person'].argmax()
    j = predictions_df['bicycle'].argmax()
    
    
    # Show top predictions for top detection.
    
    f = pd.Series(df['prediction'].iloc[i], index=labels_df['name'])
    print('Top detection:')
    print(f.order(ascending=False)[:5])
    print('')
    
    
    # Show top predictions for second-best detection.
    
    f = pd.Series(df['prediction'].iloc[j], index=labels_df['name'])
    print('Second-best detection:')
    print(f.order(ascending=False)[:5])
    
    
    # Show top detection in red, second-best top detection in blue.
    
    im = plt.imread('images/fish-bike.jpg')
    plt.imshow(im)
    currentAxis = plt.gca()
    
    det = df.iloc[i]
    coords = (det['xmin'], det['ymin']), det['xmax'] - det['xmin'], det['ymax'] - det['ymin']
    currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor='r', linewidth=5))
    
    det = df.iloc[j]
    coords = (det['xmin'], det['ymin']), det['xmax'] - det['xmin'], det['ymax'] - det['ymin']
    currentAxis.add_patch(plt.Rectangle(*coords, fill=False, edgecolor='b', linewidth=5))

5. 对所有’bicycle’检测执行NMS,来消除重叠的窗口。

def nms_detections(dets, overlap=0.3):
    """
    Non-maximum suppression: Greedily select high-scoring detections and
    skip detections that are significantly covered by a previously
    selected detection.

    This version is translated from Matlab code by Tomasz Malisiewicz,
    who sped up Pedro Felzenszwalb's code.

    Parameters
    ----------
    dets: ndarray
        each row is ['xmin', 'ymin', 'xmax', 'ymax', 'score']
    overlap: float
        minimum overlap ratio (0.3 default)

    Output
    ------
    dets: ndarray
        remaining after suppression.
    """
    x1 = dets[:, 0]
    y1 = dets[:, 1]
    x2 = dets[:, 2]
    y2 = dets[:, 3]
    ind = np.argsort(dets[:, 4])

    w = x2 - x1
    h = y2 - y1
    area = (w * h).astype(float)

    pick = []
    while len(ind) > 0:
        i = ind[-1]
        pick.append(i)
        ind = ind[:-1]

        xx1 = np.maximum(x1[i], x1[ind])
        yy1 = np.maximum(y1[i], y1[ind])
        xx2 = np.minimum(x2[i], x2[ind])
        yy2 = np.minimum(y2[i], y2[ind])

        w = np.maximum(0., xx2 - xx1)
        h = np.maximum(0., yy2 - yy1)

        wh = w * h
        o = wh / (area[i] + area[ind] - wh)

        ind = ind[np.nonzero(o <= overlap)[0]]

    return dets[pick, :]
scores = predictions_df['bicycle']
windows = df[['xmin', 'ymin', 'xmax', 'ymax']].values
dets = np.hstack((windows, scores[:, np.newaxis]))
nms_dets = nms_detections(dets)
  • 在图像中显示前3个NMS过的“自行车”检测,并记下最高评分框(红色)与其余框之间的差距。
  • 这是一个简单的自行车实例,因为它是在该类的训练集中。 但是,人员结果是正确的检测,因为这不在该类的集合中。

6. 删除临时目录以进行清理

!rm -rf _temp

上一篇:Caffe实战之Python接口系列(六)Net Surgery(Editing model parameters)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值