Caffe实战之Python接口系列(五)Multilabel classification on PASCAL using python data-layers

引言

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

目录

用Python数据层在PASCAL上进行多标签分类

  1. 多标签分类是多类别分类的泛化,其中每个实例(图像)属于多个类别。例如一幅图像既属于‘沙滩’类别,又属于‘度假图片类别’。在多类别分类中,一张图像仅属于单个类别。
  2. Caffe通过SigmoidCrossEntropyLoss层支持多标签分类,下面使用Python数据层加载数据。 数据也可以通过HDF5或LMDB数据层提供,但python数据层提供了无限的灵活性。

1. 前期准备

  1. 确保用了WITH_PYTHON_LAYER := 1编译Caffe
  2. 下载PASCAL VOC 2012
  3. 导入需要的模块

    import sys 
    import os
    
    import numpy as np
    import os.path as osp
    import matplotlib.pyplot as plt
    
    from copy import copy
    
    % matplotlib inline
    plt.rcParams['figure.figsize'] = (6, 6)
    
    caffe_root = '../'  # this file is expected to be in {caffe_root}/examples
    sys.path.append(caffe_root + 'python')
    import caffe # If you get "No module named _caffe", either you have not built pycaffe or you have the wrong path.
    
    from caffe import layers as L, params as P # Shortcuts to define the net prototxt.
    
    sys.path.append("pycaffe/layers") # the datalayers we will use are in this directory.
    sys.path.append("pycaffe") # the tools file is in this folder
    
    import tools #this contains some tools that we need
  4. 设置数据集的路径并初始化Caffe

    
    # set data root directory, e.g:
    
    pascal_root = osp.join(caffe_root, 'data/pascal/VOC2012')
    
    
    # these are the PASCAL classes, we'll need them later.
    
    classes = np.asarray(['aeroplane', 'bicycle', 'bird', 'boat', 'bottle', 'bus', 'car', 'cat', 'chair', 'cow', 'diningtable', 'dog', 'horse', 'motorbike', 'person', 'pottedplant', 'sheep', 'sofa', 'train', 'tvmonitor'])
    
    
    # make sure we have the caffenet weight downloaded.
    
    if not os.path.isfile(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel'):
        print("Downloading pre-trained CaffeNet model...")
        !../scripts/download_model_binary.py ../models/bvlc_reference_caffenet
    
    
    # initialize caffe for gpu mode
    
    caffe.set_mode_gpu()
    caffe.set_device(0)

2. 定义网络prototxt

  1. caffe.NetSpec 来定义网络,注意使用SigmoidCrossEntropyLoss层,用于多标签分类损失,还有Python数据层的定义!

    
    # helper function for common structures
    
    def conv_relu(bottom, ks, nout, stride=1, pad=0, group=1):
        conv = L.Convolution(bottom, kernel_size=ks, stride=stride,
                                    num_output=nout, pad=pad, group=group)
        return conv, L.ReLU(conv, in_place=True)
    
    
    # another helper function
    
    def fc_relu(bottom, nout):
        fc = L.InnerProduct(bottom, num_output=nout)
        return fc, L.ReLU(fc, in_place=True)
    
    
    # yet another helper function
    
    def max_pool(bottom, ks, stride=1):
        return L.Pooling(bottom, pool=P.Pooling.MAX, kernel_size=ks, stride=stride)
    
    
    # main netspec wrapper
    
    def caffenet_multilabel(data_layer_params, datalayer):
        # setup the python data layer 
        n = caffe.NetSpec()
        n.data, n.label = L.Python(module = 'pascal_multilabel_datalayers', layer = datalayer, 
                                   ntop = 2, param_str=str(data_layer_params))
    
        # the net itself
        n.conv1, n.relu1 = conv_relu(n.data, 11, 96, stride=4)
        n.pool1 = max_pool(n.relu1, 3, stride=2)
        n.norm1 = L.LRN(n.pool1, local_size=5, alpha=1e-4, beta=0.75)
        n.conv2, n.relu2 = conv_relu(n.norm1, 5, 256, pad=2, group=2)
        n.pool2 = max_pool(n.relu2, 3, stride=2)
        n.norm2 = L.LRN(n.pool2, local_size=5, alpha=1e-4, beta=0.75)
        n.conv3, n.relu3 = conv_relu(n.norm2, 3, 384, pad=1)
        n.conv4, n.relu4 = conv_relu(n.relu3, 3, 384, pad=1, group=2)
        n.conv5, n.relu5 = conv_relu(n.relu4, 3, 256, pad=1, group=2)
        n.pool5 = max_pool(n.relu5, 3, stride=2)
        n.fc6, n.relu6 = fc_relu(n.pool5, 4096)
        n.drop6 = L.Dropout(n.relu6, in_place=True)
        n.fc7, n.relu7 = fc_relu(n.drop6, 4096)
        n.drop7 = L.Dropout(n.relu7, in_place=True)
        n.score = L.InnerProduct(n.drop7, num_output=20)
        n.loss = L.SigmoidCrossEntropyLoss(n.score, n.label)
    
        return str(n.to_proto())

3. 创建网络和求解器文件,对于求解器,使用“tools”模块中的CaffeSolver类

  • 该网络使用一个python数据层PascalMultilabelDataLayerSync,位于{caffe_root}/examples/pycaffe/layers/pascal_multilabel_datalayers.py,看看代码,非常简单,可以完全控制数据和标签。

  • 下面和之前一样加载Caffe求解器

    solver = caffe.SGDSolver(osp.join(workdir, 'solver.prototxt'))
    solver.net.copy_from(caffe_root + 'models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel')
    solver.test_nets[0].share_with(solver.net)
    solver.step(1)
  • 检查加载的数据

    transformer = tools.SimpleTransformer() # This is simply to add back the bias, re-shuffle the color channels to RGB, and so on...
    image_index = 0 # First image in the batch.
    plt.figure()
    plt.imshow(transformer.deprocess(copy(solver.net.blobs['data'].data[image_index, ...])))
    gtlist = solver.net.blobs['label'].data[image_index, ...].astype(np.int)
    plt.title('GT: {}'.format(classes[np.where(gtlist)]))
    plt.axis('off');
  • 补充:简单介绍一下{caffe_root}/examples/pycaffe/tools.py,主要是定义了两个类SimpleTransformerCaffeSolver
    1. SimpleTransformer是为Caffe预处理和逆向处理图像,总共有四种方法分别为:set_meanset_scalepreprocessdeprocess,其中预处理包括:转数据类型、改通道(RGB->BGR)、减均值、调比例、改维度(HWC->CHW),逆向处理则是相反操作!
    2. CaffeSolver是一个用于创建solver.prototxt文件的类,它设置了默认值并可以导出求解器参数文件,主要有两个方法:add_from_file用于读取prototxt文件并更新实例参数;write用于导出求解器参数(按字母排序)到指定路径。

4. 训练网络

  • 首先需要一些方法来测试精度,汉明距离(Hamming distance)是多标签问题中广泛使用的,写一个简单的测试循环如下:

    def hamming_distance(gt, est):
        return sum([1 for (g, e) in zip(gt, est) if g == e]) / float(len(gt))
    
    def check_accuracy(net, num_batches, batch_size = 128):
        acc = 0.0
        for t in range(num_batches):
            net.forward()
            gts = net.blobs['label'].data
            ests = net.blobs['score'].data > 0
            for gt, est in zip(gts, ests): #for each ground truth and estimated label vector
                acc += hamming_distance(gt, est)
        return acc / (num_batches * batch_size)
  • 都准备好了,开始训练一段时间

    for itt in range(6):
        solver.step(100)
        print 'itt:{:3d}'.format((itt + 1) * 100), 'accuracy:{0:.4f}'.format(check_accuracy(solver.test_nets[0], 50))
  • 检查输出结果,看起来精度在增加,似乎收敛很快,但是一开始精度就这么高有些不正常,原因可能是 PASCAL数据集只有20类,而每张图片通常只有1-2类,GT很稀疏,因此如果预测全为零会产生很高的准确度,下面来检查一下是不是这样:

    def check_baseline_accuracy(net, num_batches, batch_size = 128):
        acc = 0.0
        for t in range(num_batches):
            net.forward()
            gts = net.blobs['label'].data
            ests = np.zeros((batch_size, len(gts)))
            for gt, est in zip(gts, ests): #for each ground truth and estimated label vector
                acc += hamming_distance(gt, est)
        return acc / (num_batches * batch_size)
    
    print 'Baseline accuracy:{0:.4f}'.format(check_baseline_accuracy(solver.test_nets[0], 5823/128))

6. 查看一些预测结果

test_net = solver.test_nets[0]
for image_index in range(5):
    plt.figure()
    plt.imshow(transformer.deprocess(copy(test_net.blobs['data'].data[image_index, ...])))
    gtlist = test_net.blobs['label'].data[image_index, ...].astype(np.int)
    estlist = test_net.blobs['score'].data[image_index, ...] > 0
    plt.title('GT: {} \n EST: {}'.format(classes[np.where(gtlist)], classes[np.where(estlist)]))
    plt.axis('off')

上一篇:Caffe实战之Python接口系列(四)Brewing Logistic Regression then Going Deeper

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

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值