美文网首页
caffe(三) 针对MNIST数据集训练遮挡识别模型

caffe(三) 针对MNIST数据集训练遮挡识别模型

作者: NoneLand | 来源:发表于2017-03-04 12:27 被阅读299次

    参考

    识别效果

    遮挡识别效果

    左侧为输入图像,其中随机1/4被遮挡,右侧图形为对应的原图像。

    训练过程

    参考官方关于PythonLayer的使用示例,首先创建PythonLayer脚本,代码为 :

    # -*- coding:utf-8 -*-
    # Modified: NoneLand
    
    import caffe
    import random
    
    
    class BlankSquareLayer(caffe.Layer):
    
        def setup(self, bottom, top):
            assert len(bottom) == 1, "Requires a single layer.bottom"
            assert bottom[0].data.ndim >= 3, "Requires image data"
            assert len(top) == 1, "Requires a single layer.top"
    
        def reshape(self, bottom, top):
            top[0].reshape(*bottom[0].data.shape)
    
        def forward(self, bottom, top):
            top[0].data[...] = bottom[0].data[...]
            height, width = top[0].data.shape[-2], top[0].data.shape[-1]
            h_offset, w_offset = random.randrange(height/2), random.randrange(width/2)
            top[0].data[..., h_offset:h_offset+height, w_offset:width+w_offset] = 0
    
        def backward(self, top, propagate_down, bottom):
            pass
    

    理解以上代码除了需要对caffe的结构有一定了解,关于numpy的用法如下:

    numpy dots usage np.zeros_like() func

    然后修改CAFFEROOT/examples/mnist目录下的lenet_train_test.prototxt文件,在mnistconv1两层之间加入以下

    layer {
      name: "blank_square"
      type: "Python"
      bottom: "data"
      top: "data"
      python_param{
        module: "pythonLayerTest"  # 上述PythonLayer的文件名,其所在路径需添加到PYTHONPATH中
        layer: "BlankSquareLayer" # 定义的PythonLayer类
      }
      # 以下三行用于控制该层的使用情况
      #include {
      # phase: TRAIN
      #}
    }
    

    其中,添加到PYTHONPATH参考参考部分链接3。修改之后的网络结构如下图所示

    lenetOclude网络结构

    然后使用./examples/mnist/train_lenet.sh就可以进行训练了。

    注意:以上直接修改文件的做法会影响原来lenet的网络结构,建议复制文档,然后再修改。修改完之后,再复制一份lenet_solver.prototxt并重命名,并将net:参数指向修改的网络模型文件。在CAFFEROOT目录下使用./build/tools//caffe train --solver=examples/mnist/lenetOclude_solver.prototxt $@进行训练。

    训练结果显示,不使用遮挡的测试准确率在95%左右,而使用遮挡的测试准确率为60%左右,这与随机有关。这从之前的识别效果图中也可以看出。

    相关文章

      网友评论

          本文标题:caffe(三) 针对MNIST数据集训练遮挡识别模型

          本文链接:https://www.haomeiwen.com/subject/xtodgttx.html