Caffe源码解读(六): Caffe的I/O模块

I/O模块介绍

在caffe中,I/O模块就是数据层,数据层能够读取磁盘数据,如DataLayer层。数据能过数据层进入 caffe 网络:数据层处于网络的最底层, 数据可以从高效率的数据库中读取(如 LevelDB 或 LMDB), 可以直接从内存中读取, 若对读写效率要求不高也可以从硬盘上的 HDFT 文件或者普通的图片文件读取。

DataLayer层的参数配置

如下是DataLayer层的参数配置,DataLayer类定义在Data_layer.h中。

layer {
  name: "cifar"         //该层的名称
  type: "Data"          //层类型指定为Data,即采用DataLayer,表示数据来源于LevelDB或LMDB。
  top: "data"//每一层用bottom来输入数据,用top来输出数据。有多个top或bottom,表示有多个blobs数据的输入和输出。
  top: "label"
  include {
    phase: TRAIN        //表示该层只在训练时使用,不在测试时使用。
  }
  transform_param {     //TransformationParameter类型的transform_param,用于数据预处理。
    scale: 0.00390625   //此处预处理表示1/255,即像素归一化到0-1
  }
  data_param {          //这里的data_param是Data中的定义的参数。
    source: "cifar10_train_lmdb"   //指定数据来源文件。
    batch_size: 100
    backend: LMDB                  //文件类型为lmdb
  }
}

上述代码从name到transform_param都在LayerParameter中定义,data_param在DataParameter中定义。

所有数据预处理都在这里设置:

transform_param {
    scale: 0.00390625
    mean_file_size: “examples/cifar10/mean.binaryproto"  # 用一个配置文件来进行均值操作
    mirror: 1  # 1表示开启镜像,0表示关闭,也可用ture和false来表示
    crop_size: 227  # 剪裁一个 227*227的图块,在训练阶段随机剪裁,在测试阶段从中间裁剪
  }

通常数据的预处理(如减去均值, 放大缩小, 裁剪和镜像等),Caffe使用OpenCV做处理。

DataLayer的参数

DataLayer的参数如下:

message DataParameter {
  enum DB {
    LEVELDB = 0;
    LMDB = 1;
  }
  // Specify the data source.
  optional string source = 1;
  // Specify the 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];
  optional DB backend = 8 [default = LEVELDB];
  // 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];
}

1、数据来自于数据库(如LevelDB和LMDB)
层类型(layer type):Data
必须设置的参数:
source: 包含数据库的目录名称,如examples/mnist/mnist_train_lmdb
batch_size: 每次处理的数据个数,如64
可选的参数:
rand_skip: 在开始的时候,路过某个数据的输入。通常对异步的SGD很有用。
backend: 选择是采用LevelDB还是LMDB, 默认是LevelDB.

其他类型的I/O模块

MemoryData——数据来自于内存时

层类型:MemoryData
必须设置的参数:
batch_size:每一次处理的数据个数,比如2
channels:通道数
height:高度
width: 宽度
示例:

layer {
  top: "data"
  top: "label"
  name: "memory_data"
  type: "MemoryData"
  memory_data_param{
    batch_size: 2
    height: 100
    width: 100
    channels: 1
  }
  transform_param {
    scale: 0.0078125
    mean_file: "mean.proto"
    mirror: false
  }
}

ImageData——数据来自于图片

层类型:ImageData
必须设置的参数:
source: 一个文本文件的名字,每一行给定一个图片文件的名称和标签(label)
batch_size: 每一次处理的数据个数,即图片数
可选参数:
rand_skip: 在开始的时候,路过某个数据的输入。通常对异步的SGD很有用。
shuffle: 随机打乱顺序,默认值为false
new_height,new_width: 如果设置,则将图片进行resize
示例:

layer {
  name: "data"
  type: "ImageData"
  top: "data"
  top: "label"
  transform_param {
    mirror: false
    crop_size: 227
    mean_file: "data/ilsvrc12/imagenet_mean.binaryproto"
  }
  image_data_param {
    source: "examples/_temp/file_list.txt"
    batch_size: 50
    new_height: 256
    new_width: 256
  }
}

数据来自于HDF5

层类型:HDF5Data
必须设置的参数:
source: 读取的文件名称
batch_size: 每一次处理的数据个数
示例:

layer {
  name: "data"
  type: "HDF5Data"
  top: "data"
  top: "label"
  hdf5_data_param {
    source: "examples/hdf5_classification/data/train.txt"
    batch_size: 10
  }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值