代码先锋网 代码片段及技术文章聚合

使用Caffe进行图片分类

技术标签: 深度学习  图片分类  caffe

首先,贴参考:caffe_root\caffe-master\examples\imagenet, (这是windows下的脚本运行,用于训练建模)
caffe_root\caffe-master\examples\cpp_classification\classification.cpp(用于分类)
所使用的顺序是1.create_imagenet.sh(将图片转成lmdb格式,并统一大小) 。2.make_imagenet_mean.sh(生成均值文件)。3.train_caffenet.sh(训练网络)。4.classification.cpp(对图片进行分类)

1.图片格式转换

Caffe是用训练集和交叉验证集进行建模,最后使用测试集测试。
1.TrainSet 2.CrossValidate Set(val) 3.TestSet (最好将不同的种类的这三个文件分开),网上百度点数据即可。这步是将图片转换成LMDB格式,首先看create_imagenet.sh,它其实就是一个命令:

$TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $TRAIN_DATA_ROOT \
    $DATA/Train.txt \
    $EXAMPLE/ilsvrc12_train_lmdb

convert_imageset在 caffeBuilder/Build/x64/Release/ (当然这需要你编译过Caffe,并且选择x64和Release),所以修改上面的环境变量TOOLS为caffeBuilder/Build/x64/Release/(路径没写全,因为每个人的不一样,我写的是绝对路径),resize_height将图片处理后的高度像素,同理resize_widthshuffle为打乱图片顺序,TRAIN_DATA_ROOT为存放的训练集的路径,Train.txt是图片+标签,$EXAMPLE/ilsvrc12_train_lmdb为输出路径,随意填写路径。
发现没有Train.txt。使$DATA加上Train.txt里面的每一行都可以指定一张图片,后面再加标签即可,为了方便,之前将每类都分开存放,这是就可以用cmd打开Train,输入:

dir /s /on /b > $DATA/***Train.txt

$DATA,***Train.txt按自己情况进行替换,之后使用notepad等打开,加入标签,然后将每一类的TrainSet,以及Train.txt放在一起,就是我们要输入的数据和不同种类(标签)。之后,将CrossValidate Set也进行数据和文本的准备,在create_imagenet.sh中的

GLOG_logtostderr=1 $TOOLS/convert_imageset \
    --resize_height=$RESIZE_HEIGHT \
    --resize_width=$RESIZE_WIDTH \
    --shuffle \
    $VAL_DATA_ROOT \
    $DATA/Val.txt \
    $EXAMPLE/ilsvrc12_val_lmdb

就是对交叉验证集的转换。
Tips:如果使用脚本运行,那么路径上不能有中文。

2.图片均值化

打开make_imagenet_mean.sh,只对TrainSet进行均值化

$TOOLS/compute_image_mean $EXAMPLE/ilsvrc12_train_lmdb \
  $DATA/imagenet_mean.binaryproto

$TOOLS 路径相同,ilsvrc12_train_lmdb 就是生成的lmdb文件,写好路径。imagenet_mean.binaryproto为输出的文件,随意写,后缀不变就行。

3.建模训练

打开train_caffenet.sh

./build/tools/caffe train \
    --solver=models/bvlc_reference_caffenet/solver.prototxt

caffe就是caffe.exe,编译过caffe的都有,solver.prototxt就参考Caffe\caffe-master\examples\mnist\lenet_solver.prototxt吧,上面都有很详细的英文解释,可以根据自己的需要改,第二行

net: "examples/mnist/lenet_train_test.prototxt"

改成自己写的prototxt,我们也直接使用它的lenet_train_test.prototxt,这个文件存放的是网络的结构,我对其进行大概的解释

name: "LeNet"
layer {
  name: "mnist"
  type: "Data"   //这层是数据层
  top: "data"    //两种输入 1,data blob(像素)
  top: "label"   //2,label blob(标签)
  include {
    phase: TRAIN  //训练时
  }
  transform_param {
    scale: 0.00390625   // 1/256=0.00390625 当初生成lmdb的时候改成了多少?
  }
  data_param {
    source: "examples/mnist/mnist_train_lmdb"  //lmdb source
    batch_size: 64   //test_iter *batch_size = 样本总数
    backend: LMDB
  }
}
layer {
  name: "mnist"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TEST  //val的test阶段
  }
  transform_param {
    scale: 0.00390625
  }
  data_param {
    source: "examples/mnist/mnist_test_lmdb"
    batch_size: 100
    backend: LMDB
  }
}
layer {
  name: "conv1"
  type: "Convolution"  //卷积
  bottom: "data"     //输入数据
  top: "conv1"       //输出卷积后数据  
  param {
    lr_mult: 1       //same as the learning rate given by the solver
  }
  param {
    lr_mult: 2       //bias,两倍的学习率会更好的拟合
  }
  convolution_param {
    num_output: 20   //输出神经元个数
    kernel_size: 5   //核大小
    stride: 1
    weight_filler {
      type: "xavier"   //初始化权重,满足xavier
    }
    bias_filler {
      type: "constant" //bias 全为0
    }
  }
}
layer {
  name: "pool1" 
  type: "Pooling"  //Pooling层
  bottom: "conv1"
  top: "pool1"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "conv2"
  type: "Convolution"
  bottom: "pool1"
  top: "conv2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  convolution_param {
    num_output: 50
    kernel_size: 5
    stride: 1
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "pool2"
  type: "Pooling"
  bottom: "conv2"
  top: "pool2"
  pooling_param {
    pool: MAX
    kernel_size: 2
    stride: 2
  }
}
layer {
  name: "ip1"
  type: "InnerProduct"   //FC层,全连接
  bottom: "pool2"
  top: "ip1"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 500
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "relu1"     //类似于sigmod
  type: "ReLU"
  bottom: "ip1"
  top: "ip1"
}
layer {
  name: "ip2"
  type: "InnerProduct"
  bottom: "ip1"
  top: "ip2"
  param {
    lr_mult: 1
  }
  param {
    lr_mult: 2
  }
  inner_product_param {
    num_output: 10
    weight_filler {
      type: "xavier"
    }
    bias_filler {
      type: "constant"
    }
  }
}
layer {
  name: "accuracy"
  type: "Accuracy"     //精度层,用于TEST阶段
  bottom: "ip2"
  bottom: "label"
  top: "accuracy"
  include {
    phase: TEST
  }
}
layer {
  name: "loss"   //就是loss```
  type: "SoftmaxWithLoss"
  bottom: "ip2"
  bottom: "label"
  top: "loss"
}

运行。
这里写图片描述
建模成功。

4.分类

编译classification.cpp,找到classification.exe,在cmd中输入classification model_file trained_file mean_file label_file picture.jpg
model_file: deploy.prototxt(改写自训练的模型,即lenet_train_test.prototxt)
trained_file :caffenet_train_iter_900.caffemodel ,训练出来的caffemodel
mean_file : imagenet_mean.binaryproto,均值文件
label_file: 自己写的标签文件,例如:0 车 1 人 2 其他,分成三行写哦。
picture.jpg需要分类的picture
关于deploy. prototxt的写法:

  1. 删除两个Data层(用于Train和Test的)。
  2. 在最前面添加一层
layer {
  name: "data"
  type: "Input"
  top: "data"
  input_param { shape: { dim: 5 dim: 3 dim: 128 dim: 128} }

第一个dim代表batch_size,第二个dim代表channel个数,第三第四个分别是width和height图片的。
3.删除卷积层中的参数:

weight_filler {
type: "gaussian"
std: 0.01
}
bias_filler {
type: "constant"
value: 1
}

因为不需要学习了。
4.删除TEST

layer {
name: "accuracy"
type: "Accuracy"
bottom: "fc8"
bottom: "label"
top: "accuracy"
include {
phase: TEST
}

原来的最后一层loss层改为pro层,将SoftmaxWithLoss替换为Softmax ,删除其中的bottom:”label”行,然后将最后一层loss层改为pro层。
Ok!全部搞定,运行下。
这里写图片描述
成功了!结果似乎不错。

版权声明:本文为Xyzx043874原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/Xyzx043874/article/details/72802353

智能推荐

Caffe分类模型测试图片

1 说明 对于分类模型,每个输入的图像都对应一个标签,Caffe 有自带的模块来测试单个图像,这里只讲怎么使用,测试源码另作分析。 2 测试程序 该测试程序的源码位于 caffe/examples/cpp_classification/classification.cpp,编译后的可执行程序位于 caffe/build/examples/cpp_classification/classificat...

caffe (10) 使用python测试多张图片统计分类结果

直接上代码啦, test.py cifar10_quick.prototxt is as follows: cifar10_quick_train_test.prototxt is as follows:...

使用卷积神经网络进行图片分类 4

利用训练好的模型开发图片分类程序 一、实验介绍 1.1 实验内容 在snapshot目录下已经有我们训练好的模型的参数,为了利用我们的卷积神经网络模型和这些参数去对图像进行分类,我们这次实验就来编写代码实现一个图片分类程序。 1.2 实验知识点 caffe python api 1.3 实验环境 python 2.7 opencv 2.4.11 caffe 1.0.0 二、实验步骤 2.1 准备d...

使用卷积神经网络进行图片分类 3

控制caffe模型的训练过程 一、实验介绍 1.1 实验内容 上次实验,我们已经构建好了卷积神经网络,我们的模型已经蓄势待发,准备接受训练了。为了控制训练进程,记录训练过程中的各种数据,caffe还需要定义另一个solver.prototxt文件,这次实验我们就来完成它,并开始激动人心的训练过程。 1.2 实验知识点 可变的学习速率 正则化 1.3 实验环境 caffe 1.0.0 二、实验步骤 ...

猜你喜欢

使用卷积神经网络进行图片分类 2

使用caffe构建卷积神经网络 一、实验介绍 1.1 实验内容 上一次实验我们介绍了卷积神经网络的基本原理,本次实验我们将学习如何使用深度学习框架caffe构建卷积神经网络,你将看到在深度学习框架上搭建和训练模型是一件非常简单快捷的事情(当然,是在你已经理解了基本原理的前提下)。如果上一次实验中的一些知识点你还理解的不够透彻,这次以及之后的实验正是通过实际操作加深对它们理解的好机会。 1.2 实验...

Caffe:使用python脚本实现Mnist分类

用过pytorch之后,现在使用prototxt构建网络,用shell脚本对网络训练总是感觉别扭。 幸好caffe提供了python接口,这篇博客就是使用python脚本实现Mnist分类,可以和上一篇文章对比学习Caffe:实现LeNet网络对Mnist做分类...

使用caffe版本ssd模型进行测试视频流或者图片

代码链接在** 使用cd $HOME mkdir build cd build cmake … make -j7 进行编译, 可以看到main.cpp中的main中三个值,设置好三个值并且选择model下的文件,将训练好的文件更改名就能开始训练了 `...

caffe使用训练好的模型对自己的一张图片进行测试

  前面学习了如何训练模型,也学了如何用测试集测试模型。但是好像还不会拿一张自己的图片去模型中进行测试。这篇文章就主要聊聊如何使用训练好的模型对自己的一张图片进行测试!(前面提到了做的项目是藏文识别,本篇文章就以藏文MNIST数据集模型作为举例,请各位重点关注不同的地方)   基本步骤如下:    1.准备测试数据  ...

Caffe:利用训练好的模型进行分类

    以大神训练好的模型为基础,利用自己的数据进行了finetune之后,下一步就可以真正使用模型来进行分类操作了。具体步骤如下:     1. 编辑分类网络的配置文件deploy.prototxt     deploy文件是真正使用模型时候用的,其结构与train_val.prototxt基本上是一样的,只是输入和输出模块有区别。因此...