Caffe 中数据及部署层的使用

Caffe 中数据及部署层的使用

文章目录

  • Caffe 中数据及部署层的使用
    • 一、Caffe 中的数据层
      • 1、数据预处理层参数
      • 2、数据层参数
      • 3、数据层示例
    • 二、Caffe 中的`部署网络`与`训练网络`的相互转换
    • 三、网络结构可视化工具
    • 四、参考资料


一、Caffe 中的数据层

Caffe 中数据及部署层的使用_第1张图片

1、数据预处理层参数

可参考 caffe ssd train.prototxt 中的数据预处理 !

/* transform_param */
message TransformationParameter {
  optional bool mirror = 2 [default = false];  // 以 0.5 的概率随机做水平镜像,训练阶段做,测试阶段不用
  optional uint32 crop_size = 3 [default = 0];  // 训练时在图像上随机 crop,测试时取中点进行 crop
  optional uint32 crop_h = 11 [default = 0];  // 长宽比不同时可以定义不同的 crop 宽和高
  optional uint32 crop_w = 12 [default = 0];

  // mean_file and mean_value cannot be specified at the same time
  // mean subtraction is always carried out before scaling
  optional string mean_file = 4; 
  repeated float mean_value = 5;  // 减均值, 127.5 或着 0
  optional float scale = 1 [default = 1];  // 1/128.0 或者 1/256.0
  
  optional ResizeParameter resize_param = 8;
  optional DistortionParameter distort_param = 13;
  optional ExpansionParameter expand_param = 14;
  optional NoiseParameter noise_param = 9;
  optional EmitConstraint emit_constraint = 10;
  
  // 不常用
  optional bool force_color = 6 [default = false];
  optional bool force_gray = 7 [default = false];
}


/* resize_param */
message ResizeParameter {
  optional float prob = 1 [default = 1];  // Probability of using this resize policy
  optional Resize_mode resize_mode = 2 [default = WARP];   // 还有 FIT_SMALL_SIZE 和 FIT_LARGE_SIZE_AND_PAD 这两种缩放模式
  optional uint32 height = 3 [default = 0];
  optional uint32 width = 4 [default = 0];
  repeated Interp_mode interp_mode = 7;  // interpolation for for resizing,当指定多种时,网络会随机选取其中一种
  
   // A parameter used to update bbox in FIT_SMALL_SIZE mode.
  optional uint32 height_scale = 8 [default = 0];  
  optional uint32 width_scale = 9 [default = 0];

  // Padding mode for BE_SMALL_SIZE_AND_PAD mode and object centering
  optional Pad_mode pad_mode = 5 [default = CONSTANT];   
  // if specified can be repeated once (would fill all the channels)
  // or can be repeated the same number of times as channels
  // (would use it them to the corresponding channel)
  repeated float pad_value = 6;
  

  enum Resize_mode {
    WARP = 1;
    FIT_SMALL_SIZE = 2;
    FIT_LARGE_SIZE_AND_PAD = 3;
  }

  enum Pad_mode {
    CONSTANT = 1;
    MIRRORED = 2;
    REPEAT_NEAREST = 3;
  }
    
  enum Interp_mode { //Same as in OpenCV
    LINEAR = 1;
    AREA = 2;
    NEAREST = 3;
    CUBIC = 4;
    LANCZOS4 = 5;
  }
}


/* distort_param */
message DistortionParameter {
  optional float brightness_prob = 1 [default = 0.0];    // The probability of adjusting brightness.
  optional float brightness_delta = 2 [default = 0.0];
  // Amount to add to the pixel values within [-delta, delta].
  // The possible value is within [0, 255]. Recommend 32.

  optional float contrast_prob = 3 [default = 0.0];   // The probability of adjusting contrast.
  optional float contrast_lower = 4 [default = 0.0];  
  optional float contrast_upper = 5 [default = 0.0];   
  // Lower and  Upper bound for random contrast factor. Recommend 0.5 and 1.5.

  optional float hue_prob = 6 [default = 0.0];    // The probability of adjusting hue.
  optional float hue_delta = 7 [default = 0.0];
  // Amount to add to the hue channel within [-delta, delta].
  // The possible value is within [0, 180]. Recommend delta=18.

  optional float saturation_prob = 8 [default = 0.0];    // The probability of adjusting saturation.
  optional float saturation_lower = 9 [default = 0.0];
  optional float saturation_upper = 10 [default = 0.0];
  // Lower and Upper bound for the random saturation factor. Recommend 0.5 and 1.5.

  optional float random_order_prob = 11 [default = 0.0];  // The probability of randomly order the image channels.
}


/* expand_param */
message ExpansionParameter {
  optional float prob = 1 [default = 1];    // Probability of using this expansion policy
  optional float max_expand_ratio = 2 [default = 1.];    // The ratio to expand the image. Recommend 4.0
}


/* noise_param */
message NoiseParameter {
  optional float prob = 1 [default = 0];  // Probability of using this policy
  optional bool gauss_blur = 5 [default = false];  // Gaussian blur
  optional bool saltpepper = 9 [default = false];    // Salt-and-pepper noise
  optional bool convert_to_hsv = 12 [default = false];  // Color space conversion
  optional bool decolorize = 4 [default = false];  // Grayscale
  optional bool inverse = 3 [default = false];     // Color inversion
  
  optional bool hist_eq = 2 [default = false];     // Histogram equalized
  optional float jpeg = 6 [default = -1];   // JPEG compression quality (-1 = no compression)
  optional bool posterize = 7 [default = false];    // Posterization
  optional bool erode = 8 [default = false];    // Erosion
  optional SaltPepperParameter saltpepper_param = 10;
  optional bool clahe = 11 [default = false];    // Local histogram equalization
  optional bool convert_to_lab = 13 [default = false];    // Color space conversion
}

/* emit_constraint */
message EmitConstraint {
  // ssd 中 batch_sampler 的额外限制条件(当 random crop box 与 gt boxes 的 IoU 大于设定值,且 gt_box 的中点落在 crop box 中,将 crop box 的标签标记为 gt_box 的标签) 
  optional EmitType emit_type = 1 [default = CENTER];
  optional float emit_overlap = 2;  // If emit_type is MIN_OVERLAP, provide the emit_overlap.

  enum EmitType {
    CENTER = 0;
    MIN_OVERLAP = 1;
  }
}

2、数据层参数

// 1、ImageData Layer 参数
message ImageDataParameter {
  optional string source = 1;
  optional uint32 batch_size = 4 [default = 1];
  optional bool shuffle = 8 [default = false];
  optional uint32 new_height = 9 [default = 0];
  optional uint32 new_width = 10 [default = 0];
  optional uint32 rand_skip = 7 [default = 0];
  
  # 不常用
  optional bool is_color = 11 [default = true];
  optional string root_folder = 12 [default = ""];
}


// 2、Data Layer 参数
message DataParameter {
  enum DB {
    LEVELDB = 0;
    LMDB = 1;
  }
  optional string source = 1;  // LMDB 数据目录
  optional uint32 batch_size = 4;
  optional DB backend = 8 [default = LEVELDB];
  optional uint32 rand_skip = 7 [default = 0];  // rand_skip * rand(0,1)

  // 不常用
  optional bool force_encoded_color = 9 [default = false];
  optional uint32 prefetch = 10 [default = 4];  // Number of batches to prefetch to host memory
}


// 3、HDF5Data Layer 参数
message HDF5DataParameter {
  optional string source = 1;
  optional uint32 batch_size = 2;
  optional bool shuffle = 3 [default = false];
}

3、数据层示例

// 1、ImageData Layer 示例
layer {
  name: "data"
  type: "ImageData"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
    mirror: true
    crop_size: 224
    mean_value: 127.5  # or mean_value: [127.5, 127.5, 127.5]
    scale: 0.0078125   # 1 / 128.0
  }
  image_data_param {
    source: "examples/hdf5_classification/data/train.txt"
    batch_size: 64
    shuffle: true
    new_height: 224
    new_width: 224 # 对输入图像强行 resize
  }
}


// 2、Data Layer 示例
layer {
  name: "data"
  type: "Data"
  top: "data"
  top: "label"
  include {
    phase: TRAIN
  }
  transform_param {
	mirror: true
	crop_size: 227
    mean_file: "examples/cifar10/mean.binaryproto"  # mean_value: 127.5 or [127.5, 127.5, 127.5]
    scale: 0.0078125  # 1 / 128.0  
  }
  data_param {
    source: "examples/cifar10/cifar10_train_lmdb"
    batch_size: 64
    backend: LMDB  # 需要显示指定一下,默认是 LEVELDB
  }
}


// 3、HDF5Data Layer  示例
layer {
  name: "data"
  type: "HDF5Data"
  top: "img_data"
  top: "img_label"
  include {
    phase: TRAIN
  }
  hdf5_data_param {
    source: "train_h5_list.txt"   // 所有需要读取的 hdf5 文件的路径
    batch_size: 128
    shuffle: true 
  }
}

二、Caffe 中的部署网络训练网络的相互转换

  • train.prototxt --> deploy.prototxt
    • Data Layer --> Input Layer
    • SoftmaxWithLoss and Accuracy Layer --> Softmax Layer
  • deploy.prototxt --> train.prototxt
    • Input Layer --> Data Layer

    • Softmax Layer --> SoftmaxWithLoss and Accuracy Layer
      Caffe 中数据及部署层的使用_第2张图片

      layer {
        name: "top1/acc"
        type: "Accuracy"
        bottom: "fc8"
        bottom: "label"
        top: "top1/acc"
        include {
          phase: TEST
        }
      }
      layer {
        name: "top5/acc"
        type: "Accuracy"
        bottom: "fc8"
        bottom: "label"
        top: "top5/acc"
        include {
          phase: TEST
        }
        accuracy_param {
          top_k: 5
        }
      }
      

三、网络结构可视化工具

  • NetScope:shift+enter 查看
  • Netron:几乎支持所有平台网络模型的查看,包括权重的查看

四、参考资料

1、https://github.com/weiliu89/caffe/blob/ssd/src/caffe/proto/caffe.proto
2、使用python进行Caffe数据源lmdb的制作
3、使用Python制作Caffe的数据源hdf5
4、生成hdf5文件用于多标签训练
5、Caffe中HDF5Data例子
6、Caffe中使用HDF5制作多标签数据

你可能感兴趣的:(Caffe)