文章目录

  • ​​问题背景​​
  • ​​参考资料​​
  • ​​代码案例封装​​

问题背景

在面对一些小图片的分类问题的时候,比如检测出一种缺陷,要识别是哪个缺陷,往往需要大量的样本集,只要是基于统计学,不管是传统机器学习,比如支持向量机,还是各种神经网络,比如resnet,都是需要样本集支撑的,针对这种不需要标注,只需要把标的物截取出来保存的数据集,往往很难收集,这也是统计学方案的一个通病,所以,就需要想办法,做数据增强。

参考资料


代码案例封装

基于Augmentor做一个数据增强的案例,使用函数之前需要安装Augmentor

@router.post('/data_augmentation')
def data_augmentation(img_path: str = Body(...), num: int = Body(...)):
# 0.先删除之前的缓存
output_dir = os.path.join(img_path, 'output')
items = os.listdir(output_dir)
for item in items:
file_path = os.path.join(img_path, item)
os.remove(file_path)
# 1. 指定图片所在目录
p = Augmentor.Pipeline(img_path)
# 2. 增强操作
# 旋转 概率0.7,向左最大旋转角度10,向右最大旋转角度10
p.rotate(probability=0.7, max_left_rotation=10, max_right_rotation=10)
# 放大 概率0.3,最小为1.1倍,最大为1.6倍;1不做变换
p.zoom(probability=0.3, min_factor=1.1, max_factor=1.6)
# resize 同一尺寸 200 x 200
p.resize(probability=1, height=200, width=200)
# 3. 指定增强后图片数目总量
p.sample(num)

return "success"