东方耀AI技术分享

 找回密码
 立即注册

QQ登录

只需一步,快速开始

搜索
热搜: 活动 交友 discuz
查看: 4859|回复: 3
打印 上一主题 下一主题

[课堂笔记] 如何在caffe中增加实时数据增强?

[复制链接]

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14437
QQ
跳转到指定楼层
楼主
发表于 2020-7-21 11:42:22 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
如何在caffe中增加实时数据增强?



数据层type: "ImageData"

网络结构的prototxt

transform_param {
      mean_value: 127.5
      mean_value: 127.5
      mean_value: 127.5
      scale: 0.0078125
      mirror: true     # 代表随机的左右翻转
      contrast_brightness_adjustment: true    # 对比度调节
        min_contrast: 0.8    # 最小的对比度乘子(min alpha),默认0.8
        max_contrast: 1.2    # 最大对比度乘子(max alpha),默认1.2
        max_color_shift: 20
        smooth_filtering: true
        max_smooth: 6       # 最大平滑乘子,默认6   进行高斯平滑
        apply_probability: 0.5
  }
  

报错:"caffe.TransformationParameter" has no field named "contrast_brightness_adjustment".


在MY_caffe_root/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];


  // Image augmentation  dfy加的 数据增强的
  // Specify the range of scaling factor for doing resizing
  optional float min_scaling_factor = 8 [default = 0.75];
  optional float max_scaling_factor = 9 [default = 1.50];
  // Specify the angle for doing rotation
  optional uint32 max_rotation_angle = 10 [default = 0];
  // Specify the contrast, brightness, smooth and color shift for augmentation
  optional bool contrast_brightness_adjustment = 11 [default = false];
  optional bool smooth_filtering = 12 [default = false];
  optional float min_contrast = 14 [default = 0.8];
  optional float max_contrast = 15 [default = 1.2];
  optional uint32 max_brightness_shift = 16 [default = 5];
  optional float max_smooth = 17 [default = 6];
  optional uint32 max_color_shift = 20 [default = 0];

  // Min side resizing, keep aspect ratio
  optional uint32 min_side_min = 13 [default = 0];
  optional uint32 min_side_max = 21 [default = 0];
  optional uint32 min_side = 22 [default = 0];

  optional float apply_probability = 18 [default = 0.5];
  optional bool debug_params = 19 [default = false];
}


修改源码:
MY_caffe_root/src/caffe/data_transformer.cpp


阅读源码“data_transformer.cpp”可知,执行顺序是mean(file)、mean(value)、crop、mirror、scale。

如有其它特殊的预处理可在此添加代码





之后  make clean

make all -j8
make test -j8
make pycaffe -j8

第七步:拷贝编译后的libcaffe.so至caffe的虚拟环境

在caffe源码项目下/python目录下 打开终端:
cp -rf caffe/ ~/miniconda3/envs/py2_caffe_source/lib/python2.7/
cp -rf ../.build_release/lib/* ~/miniconda3/envs/py2_caffe_source/lib/














data_transformer.cpp

24.39 KB, 阅读权限: 10, 下载次数: 0

让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14437
QQ
沙发
 楼主| 发表于 2020-7-21 11:49:34 | 只看该作者
数据增广代码参考caffe-augmentation
https://github.com/twtygqyy/caffe-augmentation
Realtime data augmentation is implemented within the ImageData layer.
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14437
QQ
板凳
 楼主| 发表于 2020-7-21 12:01:42 | 只看该作者
待研究:减少显存消耗的,代码来自caffe-Mem-Op,主要代码在net.cpp里。
https://github.com/yjxiong/caffe
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

1365

主题

1856

帖子

1万

积分

管理员

Rank: 10Rank: 10Rank: 10

积分
14437
QQ
地板
 楼主| 发表于 2020-7-21 13:47:07 | 只看该作者
使用caffe-augmentation时怎么修改original caffe吗?需要修改哪些?
让天下人人学会人工智能!人工智能的前景一片大好!
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

QQ|Archiver|手机版|小黑屋|人工智能工程师的摇篮 ( 湘ICP备2020019608号-1 )

GMT+8, 2024-5-5 03:41 , Processed in 0.798685 second(s), 21 queries .

Powered by Discuz! X3.4

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表