结合caffe SSD源码,解析目标检测SSD的数据增强操作

       前段时间因为项目需要,修改了SSD的损失函数和数据增强部分。现对SSD的数据增强做些归纳,都是从代码中理解的,不对之处,和大家互相讨论。写的比较简单,后面再补充。

一 、色彩,色调,亮度,饱和度distort

     prototxt中设置参数如下:

    distort_param {

     brightness_prob: 0.5

     brightness_delta: 32

     contrast_prob: 0.5

     contrast_lower: 0.5

     contrast_upper: 1.5

     hue_prob: 0.5

     hue_delta: 18

     saturation_prob: 0.5

     saturation_lower: 0.5

     saturation_upper: 1.5

     random_order_prob: 0.0

}

  对应代码在

  cv::MatApplyDistort(const cv::Mat& in_img, const DistortionParameter& param);

二  大小expand

    参数设置

    expand_param {

     prob: 0.5                         //应用expand的概率

     max_expand_ratio: 4.0      //相比原图大小最大expand的比例

    }

   对应代码在:

 template<typename Dtype> voidDataTransformer<Dtype>::ExpandImage(const   AnnotatedDatum&anno_datum,                                        AnnotatedDatum* expanded_anno_datum)中,按照“prob”设置的概率,

caffe_rng_uniform(1, 0.f, 1.f, &prob); “max_expand_ratio”设置的最大比例,

caffe_rng_uniform(1, 1.f, max_expand_ratio,&expand_ratio);计算随机expand倍数,先生成expand图像,随机计算位置,将原图copy进expand图像,其他区域值为meanvalue。

caffe_rng_uniform(1, 0.f,static_cast<float>(height - img_height), &h_off);

 caffe_rng_uniform(1, 0.f, static_cast<float>(width - img_width),&w_off);

 h_off = floor(h_off);

 w_off = floor(w_off);

cv::Rect bbox_roi(w_off, h_off, img_width,img_height);

 img.copyTo((*expand_img)(bbox_roi));

然后映射标注框坐标:

TransformAnnotation(anno_datum, do_resize,expand_bbox, do_mirror,

                     expanded_anno_datum->mutable_annotation_group());

三  长宽比变换,随机sample

batch_sampler {

     sampler {

       min_scale: 0.3                      //缩放比例

       max_scale: 1.0

       min_aspect_ratio: 0.5           //长宽比

       max_aspect_ratio: 2.0

     }

     sample_constraint {

       min_jaccard_overlap: 0.5      //box合适判断

     }

     max_sample: 1

     max_trials: 50

    }

 

void SampleBBox(const Sampler& sampler,NormalizedBBox* sampled_bbox);

按照设置的缩放比例范围、长宽比随机生成box,再按照min_jaccard_overlap判断生成的box是否合格(生成的box至少与1个标注框overlap大于设置的min_jaccard_overlap),该过程最多尝试max_tria次,找到max_sample:个满足条件的box即本次samplec成功。

void GenerateSamples(constNormalizedBBox& source_bbox,

                     constvector<NormalizedBBox>& object_bboxes,

                     const BatchSampler&batch_sampler,

                    vector<NormalizedBBox>* sampled_bboxes) {

  intfound = 0;

  for(int i = 0; i < batch_sampler.max_trials(); ++i) {

   if (batch_sampler.has_max_sample() &&

       found >= batch_sampler.max_sample()) {

     break;

}

。。。

按照sample的box 裁剪:

this->data_transformers_[thread_id]->CropImage(*expand_datum,

                                          sampled_bboxes[rand_idx],

                                          sampled_datum);

 

4、mirror

    左右镜像

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值