版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xiaoxu2050/article/details/81875971
1、Input layer
Input layer用在deploy文件测试模型效果,需要代码中手动指定网络输入数据,唯一的参数BlobShape设定输入数据的维度
caffe.proto中定义如下:
message InputParameter {
// This layer produces N >= 1 top blob(s) to be assigned manually.
// Define N shapes to set a shape for each top.
// Define 1 shape to set the same shape for every top.
// Define no shape to defer to reshaping manually.
repeated BlobShape shape = 1;
}
prototxt文件定义如下:
layer {
name:"data" #设定当前layer名称为data
type:"Input" #设置当前layer类型为Input
top:"data" #设置当前layer输出blob名称为data
#定义输入数据维度为 batchsize =1 channel=1 height=42 dim=42
input_param {shape: {dim: 1 dim: 1 dim: 42 dim:42}}
}
python读取图片输入NET代码如下:
#创建神经网络
net = caffe.Net(net_file, weight_file, caffe.TEST)
#指定caffe数据转换器data的shape等于神经网络data层shape
transformer = caffe.io.Transformer({'data':net.blobs['data'].data.shape})
#指定交换维度,caffe读取的图片是H*W*C,交换后变成C*H*W
transformer.set_transpose('data', (2,0,1))
#指定减去均值,np.load(mean_file)返回数据格式channel0数据:channel1数据:channel2数据
#mean(1).mean(1)返回每个通道的均值
transformer.set_mean('data', np.load(mean_file).mean(1).mean(1))
#caffe.io.load_image()读进来是RGB格式和(0,1)(float)
#cv2.imread()读进来直接是BGR格式和(0,255)
#这里是将RGB变换到BGR
transformer.set_chanenl_swap('data', (2,1,0))
#这里是将图像数据变成(0,255),注释掉该行代码表示使用(0,1)的数据
transformer.set_raw_scale('data',255)
#读取图片
im=caffe.io.load_image(image_path, 0)
#转换器转换数据,并赋值给net的data层
net.blobs['data'].data[...] = transformer.preprocess('data', im)
out = net.forward()
2、Data Layer层
指定Net中输入数据为LEVELDB或者LMDB
message DataParameter {
enum DB {
LEVELDB = 0;
LMDB = 1;
}
//------------这些都是data层本身的参数设定data_param
// Specify the data source.
//指定数据文件路径
optional string source = 1;
// Specify the batch size.
//指定batch size 大小
optional uint32 batch_size = 4;
// The rand_skip variable is for the data layer to skip a few data points
// to avoid all asynchronous sgd clients to start at the same point. The skip
// point would be set as rand_skip * rand(0,1). Note that rand_skip should not
// be larger than the number of keys in the database.
// DEPRECATED. Each solver accesses a different subset of the database.
optional uint32 rand_skip = 7 [default = 0];
//指定数据类型,默认是LEVELDB
optional DB backend = 8 [default = LEVELDB];
//------之后是数据增强的参数设置,transform_param
// DEPRECATED. See TransformationParameter. For data pre-processing, we can do
// simple scaling and subtracting the data mean, if provided. Note that the
// mean subtraction is always carried out before scaling.
optional float scale = 2 [default = 1];
//设置均值文件路径
optional string mean_file = 3;
// DEPRECATED. See TransformationParameter. Specify if we would like to randomly
// crop an image.
//指定裁剪后图片的大小
optional uint32 crop_size = 5 [default = 0];
// DEPRECATED. See TransformationParameter. Specify if we want to randomly mirror
// data.
//指定是否通过镜像座扩展
optional bool mirror = 6 [default = false];
// Force the encoded image to have 3 color channels
optional bool force_encoded_color = 9 [default = false];
// Prefetch queue (Increase if data feeding bandwidth varies, within the
// limit of device memory for GPU training)
optional uint32 prefetch = 10 [default = 4];
}
prototxt文件定义如下:
layer {
name:"data"
type:"Data"
top:"data"
top:"label"
include {
phase: TRAIN
}
transform_param {
mirror: true
crop_size: 42
mean_file: "/home/zxy/caffe/face_expression_recognition/lmdb/meantrain.binaryproto"
}
data_param {
source: "/home/zxy/caffe/face_expression_recognition/lmdb/fer2013_train_lmdb"
batch_size: 128
backend: LMDB
}
}