前段时间因为项目需要,修改了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
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
caffe_rng_uniform(1, 0.f, static_cast
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
const BatchSampler&batch_sampler,
vector
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
左右镜像