代码:https://github.com/bubbliiiing/yolo3-pytorch

对数据集转换成VOC格式,代码与上面可得。
yolo3整体的文件夹构架如下:

本文使用VOC格式进行训练。
训练前将标签文件放在VOCdevkit文件夹下的VOC2007文件夹下的Annotation中。

训练前将图片文件放在VOCdevkit文件夹下的VOC2007文件夹下的JPEGImages中。

在训练前利用voc2yolo3.py文件生成对应的txt。

再运行根目录下的voc_annotation.py,运行前需要将classes改成你自己的classes。
就会生成对应的2007_train.txt,每一行对应其图片位置及其真实框的位置。

在训练前需要修改model_data里面的voc_classes.txt文件,需要将classes改成你自己的classes。同时还需要修改utils/config.py文件,修改内部的Num_Classes变成所分的种类的数量。

增强代码如下:
普通增强:

from PIL import Image, ImageDraw
import numpy as np
from matplotlib.colors import rgb_to_hsv, hsv_to_rgbdef rand(a=0, b=1):return np.random.rand()*(b-a) + adef get_random_data(annotation_line, input_shape, random=True, max_boxes=20, jitter=.5, hue=.1, sat=1.5, val=1.5, proc_img=True):'''random preprocessing for real-time data augmentation'''line = annotation_line.split()image = Image.open(line[0])iw, ih = image.sizeh, w = input_shapebox = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])# 对图像进行缩放并且进行长和宽的扭曲new_ar = w/h * rand(1-jitter,1+jitter)/rand(1-jitter,1+jitter)scale = rand(.25,2)if new_ar < 1:nh = int(scale*h)nw = int(nh*new_ar)else:nw = int(scale*w)nh = int(nw/new_ar)image = image.resize((nw,nh), Image.BICUBIC)# 将图像多余的部分加上灰条dx = int(rand(0, w-nw))dy = int(rand(0, h-nh))new_image = Image.new('RGB', (w,h), (128,128,128))new_image.paste(image, (dx, dy))image = new_image# 翻转图像flip = rand()<.5if flip: image = image.transpose(Image.FLIP_LEFT_RIGHT)# 色域扭曲hue = rand(-hue, hue)sat = rand(1, sat) if rand()<.5 else 1/rand(1, sat)val = rand(1, val) if rand()<.5 else 1/rand(1, val)x = rgb_to_hsv(np.array(image)/255.)x[..., 0] += huex[..., 0][x[..., 0]>1] -= 1x[..., 0][x[..., 0]<0] += 1x[..., 1] *= satx[..., 2] *= valx[x>1] = 1x[x<0] = 0image_data = hsv_to_rgb(x) # numpy array, 0 to 1# 将box进行调整box_data = np.zeros((max_boxes,5))if len(box)>0:np.random.shuffle(box)box[:, [0,2]] = box[:, [0,2]]*nw/iw + dxbox[:, [1,3]] = box[:, [1,3]]*nh/ih + dyif flip: box[:, [0,2]] = w - box[:, [2,0]]box[:, 0:2][box[:, 0:2]<0] = 0box[:, 2][box[:, 2]>w] = wbox[:, 3][box[:, 3]>h] = hbox_w = box[:, 2] - box[:, 0]box_h = box[:, 3] - box[:, 1]box = box[np.logical_and(box_w>1, box_h>1)] # discard invalid boxif len(box)>max_boxes: box = box[:max_boxes]box_data[:len(box)] = boxreturn image_data, box_data
def normal_(annotation_line, input_shape):'''random preprocessing for real-time data augmentation'''line = annotation_line.split()image = Image.open(line[0])box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])return image, boxif __name__ == "__main__":with open("2007_train.txt") as f:lines = f.readlines()a = np.random.randint(0,len(lines))line = lines[a]image_data, box_data = normal_(line,[416,416])img = image_datafor j in range(len(box_data)):thickness = 3left, top, right, bottom  = box_data[j][0:4]draw = ImageDraw.Draw(img)for i in range(thickness):draw.rectangle([left + i, top + i, right - i, bottom - i],outline=(255,255,255))img.show()image_data, box_data = get_random_data(line,[416,416])print(box_data)img = Image.fromarray((image_data*255).astype(np.uint8))for j in range(len(box_data)):thickness = 3left, top, right, bottom  = box_data[j][0:4]draw = ImageDraw.Draw(img)for i in range(thickness):draw.rectangle([left + i, top + i, right - i, bottom - i],outline=(255,255,255))img.show()# img = Image.open(r"F:\Collection\yolo_Collection\keras-yolo3-master\Mobile-yolo3-master/VOCdevkit/VOC2007/JPEGImages/00000.jpg")# left, top, right, bottom = 527,377,555,404# draw = ImageDraw.Draw(img)# draw.rectangle([left, top, right, bottom])# img.show()

YOLO v4 Mosaic数据增强方法:

from PIL import Image, ImageDraw
import numpy as np
from matplotlib.colors import rgb_to_hsv, hsv_to_rgb
import math
def rand(a=0, b=1):return np.random.rand()*(b-a) + adef merge_bboxes(bboxes, cutx, cuty):merge_bbox = []for i in range(len(bboxes)):for box in bboxes[i]:tmp_box = []x1,y1,x2,y2 = box[0], box[1], box[2], box[3]if i == 0:if y1 > cuty or x1 > cutx:continueif y2 >= cuty and y1 <= cuty:y2 = cutyif y2-y1 < 5:continueif x2 >= cutx and x1 <= cutx:x2 = cutxif x2-x1 < 5:continueif i == 1:if y2 < cuty or x1 > cutx:continueif y2 >= cuty and y1 <= cuty:y1 = cutyif y2-y1 < 5:continueif x2 >= cutx and x1 <= cutx:x2 = cutxif x2-x1 < 5:continueif i == 2:if y2 < cuty or x2 < cutx:continueif y2 >= cuty and y1 <= cuty:y1 = cutyif y2-y1 < 5:continueif x2 >= cutx and x1 <= cutx:x1 = cutxif x2-x1 < 5:continueif i == 3:if y1 > cuty or x2 < cutx:continueif y2 >= cuty and y1 <= cuty:y2 = cutyif y2-y1 < 5:continueif x2 >= cutx and x1 <= cutx:x1 = cutxif x2-x1 < 5:continuetmp_box.append(x1)tmp_box.append(y1)tmp_box.append(x2)tmp_box.append(y2)tmp_box.append(box[-1])merge_bbox.append(tmp_box)return merge_bboxdef get_random_data(annotation_line, input_shape, random=True, hue=.1, sat=1.5, val=1.5, proc_img=True):'''random preprocessing for real-time data augmentation'''h, w = input_shapemin_offset_x = 0.4min_offset_y = 0.4scale_low = 1-min(min_offset_x,min_offset_y)scale_high = scale_low+0.2image_datas = [] box_datas = []index = 0place_x = [0,0,int(w*min_offset_x),int(w*min_offset_x)]place_y = [0,int(h*min_offset_y),int(w*min_offset_y),0]for line in annotation_line:# 每一行进行分割line_content = line.split()# 打开图片image = Image.open(line_content[0])image = image.convert("RGB") # 图片的大小iw, ih = image.size# 保存框的位置box = np.array([np.array(list(map(int,box.split(',')))) for box in line_content[1:]])# image.save(str(index)+".jpg")# 是否翻转图片flip = rand()<.5if flip and len(box)>0:image = image.transpose(Image.FLIP_LEFT_RIGHT)box[:, [0,2]] = iw - box[:, [2,0]]# 对输入进来的图片进行缩放new_ar = w/hscale = rand(scale_low, scale_high)if new_ar < 1:nh = int(scale*h)nw = int(nh*new_ar)else:nw = int(scale*w)nh = int(nw/new_ar)image = image.resize((nw,nh), Image.BICUBIC)# 进行色域变换hue = rand(-hue, hue)sat = rand(1, sat) if rand()<.5 else 1/rand(1, sat)val = rand(1, val) if rand()<.5 else 1/rand(1, val)x = rgb_to_hsv(np.array(image)/255.)x[..., 0] += huex[..., 0][x[..., 0]>1] -= 1x[..., 0][x[..., 0]<0] += 1x[..., 1] *= satx[..., 2] *= valx[x>1] = 1x[x<0] = 0image = hsv_to_rgb(x)image = Image.fromarray((image*255).astype(np.uint8))# 将图片进行放置,分别对应四张分割图片的位置dx = place_x[index]dy = place_y[index]new_image = Image.new('RGB', (w,h), (128,128,128))new_image.paste(image, (dx, dy))image_data = np.array(new_image)/255# Image.fromarray((image_data*255).astype(np.uint8)).save(str(index)+"distort.jpg")index = index + 1box_data = []# 对box进行重新处理if len(box)>0:np.random.shuffle(box)box[:, [0,2]] = box[:, [0,2]]*nw/iw + dxbox[:, [1,3]] = box[:, [1,3]]*nh/ih + dybox[:, 0:2][box[:, 0:2]<0] = 0box[:, 2][box[:, 2]>w] = wbox[:, 3][box[:, 3]>h] = hbox_w = box[:, 2] - box[:, 0]box_h = box[:, 3] - box[:, 1]box = box[np.logical_and(box_w>1, box_h>1)]box_data = np.zeros((len(box),5))box_data[:len(box)] = boximage_datas.append(image_data)box_datas.append(box_data)img = Image.fromarray((image_data*255).astype(np.uint8))for j in range(len(box_data)):thickness = 3left, top, right, bottom  = box_data[j][0:4]draw = ImageDraw.Draw(img)for i in range(thickness):draw.rectangle([left + i, top + i, right - i, bottom - i],outline=(255,255,255))img.show()# 将图片分割,放在一起cutx = np.random.randint(int(w*min_offset_x), int(w*(1 - min_offset_x)))cuty = np.random.randint(int(h*min_offset_y), int(h*(1 - min_offset_y)))new_image = np.zeros([h,w,3])new_image[:cuty, :cutx, :] = image_datas[0][:cuty, :cutx, :]new_image[cuty:, :cutx, :] = image_datas[1][cuty:, :cutx, :]new_image[cuty:, cutx:, :] = image_datas[2][cuty:, cutx:, :]new_image[:cuty, cutx:, :] = image_datas[3][:cuty, cutx:, :]# 对框进行进一步的处理new_boxes = merge_bboxes(box_datas, cutx, cuty)return new_image, new_boxesdef normal_(annotation_line, input_shape):'''random preprocessing for real-time data augmentation'''line = annotation_line.split()image = Image.open(line[0])box = np.array([np.array(list(map(int,box.split(',')))) for box in line[1:]])iw, ih = image.sizeimage = image.transpose(Image.FLIP_LEFT_RIGHT)box[:, [0,2]] = iw - box[:, [2,0]]return image, boxif __name__ == "__main__":with open("2007_train.txt") as f:lines = f.readlines()a = np.random.randint(0,len(lines))# index = 0# line_all = lines[a:a+4]# for line in line_all:#     image_data, box_data = normal_(line,[416,416])#     img = image_data#     for j in range(len(box_data)):#         thickness = 3#         left, top, right, bottom  = box_data[j][0:4]#         draw = ImageDraw.Draw(img)#         for i in range(thickness):#             draw.rectangle([left + i, top + i, right - i, bottom - i],outline=(255,255,255))#     img.show()#     # img.save(str(index)+"box.jpg")#     index = index+1line = lines[a:a+4]image_data, box_data = get_random_data(line,[416,416])img = Image.fromarray((image_data*255).astype(np.uint8))for j in range(len(box_data)):thickness = 3left, top, right, bottom  = box_data[j][0:4]draw = ImageDraw.Draw(img)for i in range(thickness):draw.rectangle([left + i, top + i, right - i, bottom - i],outline=(255,255,255))img.show()# img.save("box_all.jpg")

数据增强:YoloV4当中的Mosaic数据增强方法相关推荐

  1. 睿智的目标检测28——YoloV4当中的Mosaic数据增强方法

    睿智的目标检测28--YoloV4当中的Mosaic数据增强方法 学习前言 代码下载 什么是Mosaic数据增强方法 实现思路 全部代码 1.数据增强 2.调用代码 学习前言 哈哈哈!我又来数据增强了 ...

  2. YoloV4当中的Mosaic数据增强方法(附代码讲解)

    上一期中讲解了图像分类和目标检测中的数据增强的区别和联系,这期讲解数据增强的进阶版- yolov4中的Mosaic数据增强方法以及CutMix. 前言 Yolov4的mosaic数据增强参考了CutM ...

  3. mosaic数据增强_YoloV4当中的Mosaic数据增强方法(附代码详细讲解)

    上一期中讲解了图像分类和目标检测中的数据增强的区别和联系,这期讲解数据增强的进阶版- yolov4中的Mosaic数据增强方法以及CutMix. 前言 Yolov4的mosaic数据增强参考了CutM ...

  4. yolov4中的mosaic数据增强

    文章详细讲解yolov4中的mosaic数据增强方法以及代码细节,如有错误,希望指正. 参考代码链接:https://github.com/bubbliiiing/yolov4-keras 1.下述代 ...

  5. 几种数据增强:Mixup,Cutout,CutMix 和yolov4中的 Mosaic

    作者:RayChiu_Labloy 版权声明:著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处 目录 1.几种数据增强的比较 2.What does model learn with ...

  6. mosaic数据增强

    mosaic数据增强则利用了四张图片,对四张图片进行拼接,每一张图片都有其对应的框框,将四张图片拼接之后就获得一张新的图片,同时也获得这张图片对应的框框,然后我们将这样一张新的图片传入到神经网络当中去 ...

  7. 浅谈yolov4中的一部分数据增强

    浅谈yolov4中的数据增强 前言 数据增强 数据增强步骤 1.对图片进行水平翻转 2.对图片进行缩放 3.对图片HSV色域变换 4. Mosaic数据增强 5. 总代码 前言 在接下来的几天,我将解 ...

  8. 数据增强之Mosaic数据增强的优点、Mixup,Cutout,CutMix的区别

    一.Mosaic data augmentation Mosaic数据增强方法是YOLOV4论文中提出来的,主要思想是将四张图片进行随机裁剪,再拼接到一张图上作为训练数据. 这样做有以下几个优点: 1 ...

  9. 【目标检测】(10) Mosaic 数据增强方法,附Python完整代码

    各位同学好,今天和大家分享一下目标检测算法中常用的图像数据增强方法 Mosaic.先放张图看效果.将四张图片缩放后裁剪拼接在一起,并调整检测框的坐标位置,处理位于图像边缘的检测框.文末有完整代码 1. ...

最新文章

  1. ActiveMQ快速入门
  2. excel随机抽取_Excel条件格式,扮靓你的报表
  3. dev c++运行没有结果_「C/C++」一行注释也能影响运行结果?
  4. android 每分钟广播,每1分钟Android发布一次警报管理器?
  5. 淘淘相关工具类【json,httpClient,id,FTP,exception,cookie(包括共享cookie的设置等)】
  6. 【数据分析就业实战】——缺失值的常见处理方法
  7. python更改整列小时分钟_利用python对excel中一列的时间数据更改格式操作
  8. 高阶函数 map,reduce, filter的用法
  9. 聚类 | Map-Equation多级网络聚类模型——InfoMap
  10. 蓝桥杯2015年第六届C/C++省赛A组第八题-饮料换购
  11. Extjs study
  12. 51Nod 1637 幸运数字转换(思维)
  13. GoPose人工智能运动分析软件
  14. 一、Spark大数据技术基础
  15. 查看电脑系统基本信息
  16. 数字图像处理入门-邻域、连通性、通路和距离
  17. 《雷军:我向阿里学到了三点,这是创业成功的核心》有感
  18. 编程之余对人品的感悟
  19. ae2020不支持的视频驱动程序_Premiere 2020安装后,不支持视频驱动程序,怎么解决?...
  20. Lazy Prices公司年报内容变动碰上股价偷懒

热门文章

  1. [GYM101173K] CERC 16 K.Key Knocking 构造
  2. [小o地图-数据] - 获取全国行政区划轮廓数据(上)
  3. android app文件夹,android app文件目录结构
  4. sql sever 存储过程总结及实验
  5. Codeforces Round #599 (Div. 2) B2. Character Swap (Hard Version)
  6. 程序员找工作遇到的“了解、熟悉、精通”的三种技能要求实际上是这样的标准!
  7. linux由浅入深学习一
  8. php mailer altbody,PHP_phpmailer 中文使用说明(简易版),phpmailer v5.1下载 A开头: $AltBody - phpStudy...
  9. 眼底图像血管增强与分割--(5)基于Hessian矩阵的Frangi滤波算法
  10. 如何使用Transformers和Tokenizers从头开始训练新的语言模型