Caffe-数据层
@[DeepLearning]
本节介绍如何设置数据层(Data Layer)及其参数。
数据层是一个模型的最底层,它是模型的入口,它不仅提供数据的输入,也提供数据从Blobs转换成别的格式进行保存和输出,通常数据预处理操作也会在这一层实现。
数据可接受以下几个来源:
1. 数据库(LevelDB和LMDB)
2. 内存
3. hdf5
4. 图片格式文件
layer {
name: "cifar"
type: "Data"
top: "data"
top: "label"
include {
phase: TRAIN
}
trasform_param {
scale: 0.00390625
mean_file = "examples/cifar10/mean.binaryproto"
}
data_param {
source: "examples/cifar10/cifar10_train_lmdb"
batch_size: 128
backend: LMDB
}
}
name: 层的名称,随意命名
type: 层的类型,如果是Data表示使用LevelDB或者LMDB,
top或bottom: bottom用来输入数据,top用来输出数据,若只有top没有bottom,则此层只有输出没有输入。如果有多个top或者bottom,即表示有多个输出或输入。
data和label: 数据层中至少有一个命名为data的top,如有第二个,通常命名为label,这种(data,label)配对是分类模型所必需的。
include: 指定该层是用于训练阶段的层还是测试阶段的层,如果没有include参数,表示该层既在训练模型中,又在测试模型中。
transform_param: 数据的预处理,它在 caffe/src/caffe/proto/caffe.proto中的定义如下
// Message that stores parameters used to apply transformation
// to the data layer's data
message 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 = 1 [default = 1];
// Specify if we want to randomly mirror data.
optional bool mirror = 2 [default = false];
// Specify if we would like to randomly crop an image.
optional uint32 crop_size = 3 [default = 0];
// mean_file and mean_value cannot be specified at the same time
optional string mean_file = 4;
// if specified can be repeated once (would subtract it from all the channels)
// or can be repeated the same number of times as channels
// (would subtract them from the corresponding channel)
repeated float mean_value = 5;
// Force the decoded image to have 3 color channels.
optional bool force_color = 6 [default = false];
// Force the decoded image to have 1 color channels.
optional bool force_gray = 7 [default = false];
}
data_param: 根据数据来源不同进行不同的设置
1. LEVELDB和LMDB
层类型: Data
必填参数:
1.source: 数据库文件的路径
2.batch_size: 网络单次输入数据的数量
可选参数:
1.rand_skip: 跳过开头的 rand_skip * rand(0,1)个数据,通常在异步随机梯度下降法里使用;
2.backend[default LEVELDB ]: 选择使用LEVELDB还是LMDB
2.内存数据
层类型: MemoryData
必填参数:
batch_size, channels, height, width: 指定从内存中读取的数据块的尺寸memory data 层 直 接 从 内 存 中 读 取 数 据 而 不 用 拷 贝 。 使 用 这 个 层 时 需 要 调 用MemoryDataLayer::Reset (C++) 或者 Net.set_input_arrays(Python) 来指定数据来源(四维按行存储的数组), 每次读取一个大小为 batch-sized 的数据块。
示例:
layer {
top: "data"
top: "label"
name: "memory_data"
type: "MemoryData"
memory_data_param{
batch_size: 64
height: 32
width: 32
channels: 3
}
transform_param {
scale: 0.0078125
mean_file: "mean.proto"
mirror: false
}
}
3.HDF5 Input
层类型:HDF5Data
必填参数:
1.source: 文件路径
2.batch_size: 网络单次输入数据的数量
可选参数:
1.shuffle [default false]: 是否打乱数据
这里的打乱是指多个hdf5文件的打乱、每个文件内部数据的打乱,不包括数据与数据之间的打乱。
layer {
name: "data"
type: "HDF5Data"
top: "data"
top: "label"
hdf5_data_param {
source: "examples/hdf5_classification/data/train.txt"
batch_size: 32 }
}
4.HDF5 Output
层类型:HDF5Output
必填参数:
1.file_name: 写入文件的路径
HDF5 output 层执行了一个与数据读取相反的操作, 它将数据写进硬盘。
5.图像数据
层类型: ImageData
必填参数:
1.source : text 文件的路径名,该 text 文件的每一行存储一张图片的路径名和对应的标签;
2.batch_size: 打包成 batch 的图片数量。
可选参数:
1.rand_skip: 跳过开头的 rand_skip * rand(0,1)个数据,通常在异步随机梯度下降法里使用;
2.shuffle [default false]: 是否打乱数据
3.new_height, new_width: 根据设置的值,输入的图片将会被调整成给定的尺寸。
4.is_color [default true]: 是否是彩色图像
6.来源于图像数据的Windows数据
层类型WidowData
必填参数:
必须设置的参数:
1.source: 一个文本文件的名字
2.batch_size: 每一次处理的数据个数,即图片数
可选参数比较多,这里不一一列举,想详细了解的请查看caffe.proto文件