数据增强
现如今越来越多的研友开始涉足深度学习领域了,深度学习的入门离不开两个关键点:模型和数据,各种的模型也层出不穷,这里介绍一下我个人常用的深度学习工具箱:OpenMMlab。 你可以在里面找到你想用到的几乎所有模型,涉及语音识别、目标检测、语义分割、实例分割、自然语言处理、视频分析、姿态检测等各个领域,并且在不断更新网络模型,你可以在里面找到你想要的各个领域模型,并且方便的使用不同模型进行实验。 说完模型介绍一下我自己使用的数据增强方法:虽然比较简单,但是实用性强、扩展性高,可以用于语义分割、实例分割的数据扩充(目标检测没有尝试,如果文件格式类似也是可以)。
放代码:
from PIL import Image
import os
import glob
import json
import base64
"""
1.这里只是通过简单的旋转翻转进行增强,可以扩充7倍,也可以通过亮度,色度,对比度,锐度的方法进行增强,但不建议使用平移的方法,因为要考虑平移时会导致回归框超出范围的情况。
2.扩充时,需要将json文件和对应原始图片放在同一文件夹下(E:\make_dataset\beijing\\),二者名称需要相同,这里图片格式为jpg,按需修改。
3.感谢对面emo怪的鼎力协助,我真的会谢:)
"""
path = r"E:\make_dataset\beijing\\"
save_path = r"E:\make_dataset\beijing\\"
file_format = ".jpg"
replace_format = ".json"
LR = "lr_"
TB = "tb_"
img_list = glob.glob(path + "*" + file_format)
print(img_list)
print("左右翻转-start")
for i in range(len(img_list)):
img_path = img_list[i]
print(img_path)
json_path = img_list[i].replace(, replace_format)
is_exists = os.path.exists(json_path)
if is_exists:
f = open(json_path, encoding='utf-8')
setting = json.load(f)
width = setting['imageWidth']
height = setting['imageHeight']
mid_width = width / 2
mid_height = height / 2
print("中轴:x-" + str(mid_width) + ",y-" + str(mid_height))
for i2 in range(len(setting['shapes'])):
for i3 in range(len(setting['shapes'][i2]['points'])):
temp_x = setting['shapes'][i2]['points'][i3][0]
temp_y = setting['shapes'][i2]['points'][i3][1]
if temp_x > mid_width:
dis = temp_x - mid_width
new_x = mid_width - dis
elif temp_x < mid_width:
dis = mid_width - temp_x
new_x = mid_width + dis
else:
new_x = temp_x
new_y = temp_y
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('_','_l')
full_path = save_path + img_name.replace('_','_l')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
pri_image.transpose(Image.FLIP_LEFT_RIGHT).save(full_path)
with open(full_path, 'rb') as f:
setting['imageData'] = base64.b64encode(f.read()).decode()
string = json.dumps(setting)
with open(full_path1, 'w', encoding='utf-8') as f:
f.write(string)
f.close()
print(img_path + "-------转换完成")
setting = None
else:
print(json_path + "-------文件不存在")
print("左右翻转-end")
print("上下翻转-start")
for i in range(len(img_list)):
img_path = img_list[i]
json_path = img_list[i].replace(file_format, replace_format)
is_exists = os.path.exists(json_path)
if is_exists:
f = open(json_path, encoding='utf-8')
setting = json.load(f)
width = setting['imageWidth']
height = setting['imageHeight']
mid_width = width / 2
mid_height = height / 2
for i2 in range(len(setting['shapes'])):
for i3 in range(len(setting['shapes'][i2]['points'])):
temp_x = setting['shapes'][i2]['points'][i3][0]
temp_y = setting['shapes'][i2]['points'][i3][1]
if temp_y > mid_height:
dis = temp_y - mid_height
new_y = mid_height - dis
elif temp_y < mid_height:
dis = mid_height - temp_y
new_y = mid_height + dis
else:
new_y = temp_y
new_x = temp_x
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('_','_u')
full_path = save_path + img_name.replace('_','_u')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
pri_image.transpose(Image.FLIP_TOP_BOTTOM).save(full_path)
with open(full_path, 'rb') as f:
setting['imageData'] = base64.b64encode(f.read()).decode()
string = json.dumps(setting)
with open(full_path1, 'w', encoding='utf-8') as f:
f.write(string)
f.close()
print(img_path + "-------转换完成")
setting = None
else:
print(json_path + "-------文件不存在")
print("上下翻转-end")
print("90度-start")
for i in range(len(img_list)):
img_path = img_list[i]
json_path = img_list[i].replace(file_format, replace_format)
is_exists = os.path.exists(json_path)
if is_exists:
f = open(json_path, encoding='utf-8')
setting = json.load(f)
width = setting['imageWidth']
height = setting['imageHeight']
mid_width = width / 2
mid_height = height / 2
for i2 in range(len(setting['shapes'])):
for i3 in range(len(setting['shapes'][i2]['points'])):
temp_x = setting['shapes'][i2]['points'][i3][0]
temp_y = setting['shapes'][i2]['points'][i3][1]
if temp_x > mid_width:
new_x = temp_y
dis = temp_x - mid_width
new_y = mid_width - dis
elif temp_x < mid_width:
new_x = temp_y
dis = mid_width - temp_x
new_y = mid_width + dis
else:
new_x = temp_y
new_y = temp_x
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('_','_90_')
full_path = save_path + img_name.replace('_','_90_')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
pri_image.transpose(Image.ROTATE_90).save(full_path)
with open(full_path, 'rb') as f:
setting['imageData'] = base64.b64encode(f.read()).decode()
string = json.dumps(setting)
with open(full_path1, 'w', encoding='utf-8') as f:
f.write(string)
f.close()
print(img_path + "-------转换完成")
setting = None
else:
print(json_path + "-------文件不存在")
print("90度-end")
print("90度+上下翻转-start")
for i in range(len(img_list)):
img_path = img_list[i]
json_path = img_list[i].replace(file_format, replace_format)
is_exists = os.path.exists(json_path)
if is_exists:
f = open(json_path, encoding='utf-8')
setting = json.load(f)
width = setting['imageWidth']
height = setting['imageHeight']
mid_width = width / 2
mid_height = height / 2
for i2 in range(len(setting['shapes'])):
for i3 in range(len(setting['shapes'][i2]['points'])):
temp_x = setting['shapes'][i2]['points'][i3][0]
temp_y = setting['shapes'][i2]['points'][i3][1]
if temp_x > mid_width:
new_x1 = temp_y
dis = temp_x - mid_width
new_y1 = mid_width - dis
elif temp_x < mid_width:
new_x1 = temp_y
dis = mid_width - temp_x
new_y1 = mid_width + dis
else:
new_x1 = temp_y
new_y1 = temp_x
if new_y1 > mid_height:
dis = new_y1 - mid_height
new_y = mid_height - dis
elif new_y1 < mid_height:
dis = mid_height - new_y1
new_y = mid_height + dis
else:
new_y = new_y1
new_x = new_x1
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('_','_90u_')
full_path = save_path + img_name.replace('_','_90u_')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
rotate_90 = pri_image.transpose(Image.ROTATE_90)
rotate_90.transpose(Image.FLIP_TOP_BOTTOM).save(full_path)
with open(full_path, 'rb') as f:
setting['imageData'] = base64.b64encode(f.read()).decode()
string = json.dumps(setting)
with open(full_path1, 'w', encoding='utf-8') as f:
f.write(string)
f.close()
print(img_path + "-------转换完成")
setting = None
else:
print(json_path + "-------文件不存在")
print("90度+上下翻转-end")
print("90度+左右翻转-start")
for i in range(len(img_list)):
img_path = img_list[i]
json_path = img_list[i].replace(file_format, replace_format)
is_exists = os.path.exists(json_path)
if is_exists:
f = open(json_path, encoding='utf-8')
setting = json.load(f)
width = setting['imageWidth']
height = setting['imageHeight']
mid_width = width / 2
mid_height = height / 2
for i2 in range(len(setting['shapes'])):
for i3 in range(len(setting['shapes'][i2]['points'])):
temp_x = setting['shapes'][i2]['points'][i3][0]
temp_y = setting['shapes'][i2]['points'][i3][1]
if temp_x > mid_width:
new_x1 = temp_y
dis = temp_x - mid_width
new_y1 = mid_width - dis
elif temp_x < mid_width:
new_x1 = temp_y
dis = mid_width - temp_x
new_y1 = mid_width + dis
else:
new_x1 = temp_y
new_y1 = temp_x
if new_x1 > mid_width:
dis = new_x1 - mid_width
new_x = mid_width - dis
elif new_x1 < mid_width:
dis = mid_width - new_x1
new_x = mid_width + dis
else:
new_x = new_x1
new_y = new_y1
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('_','_90l_')
full_path = save_path + img_name.replace('_','_90l_')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
rotate_90 = pri_image.transpose(Image.ROTATE_90)
rotate_90.transpose(Image.FLIP_LEFT_RIGHT).save(full_path)
with open(full_path, 'rb') as f:
setting['imageData'] = base64.b64encode(f.read()).decode()
string = json.dumps(setting)
with open(full_path1, 'w', encoding='utf-8') as f:
f.write(string)
f.close()
print(img_path + "-------转换完成")
setting = None
else:
print(json_path + "-------文件不存在")
print("90度+左右翻转-end")
print("180度-start")
for i in range(len(img_list)):
img_path = img_list[i]
json_path = img_list[i].replace(file_format, replace_format)
is_exists = os.path.exists(json_path)
if is_exists:
f = open(json_path, encoding='utf-8')
setting = json.load(f)
width = setting['imageWidth']
height = setting['imageHeight']
mid_width = width / 2
mid_height = height / 2
for i2 in range(len(setting['shapes'])):
for i3 in range(len(setting['shapes'][i2]['points'])):
temp_x = setting['shapes'][i2]['points'][i3][0]
temp_y = setting['shapes'][i2]['points'][i3][1]
new_x = width - temp_x
new_y = height - temp_y
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('_','_180_')
full_path = save_path + img_name.replace('_','_180_')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
pri_image.transpose(Image.ROTATE_180).save(full_path)
with open(full_path, 'rb') as f:
setting['imageData'] = base64.b64encode(f.read()).decode()
string = json.dumps(setting)
with open(full_path1, 'w', encoding='utf-8') as f:
f.write(string)
f.close()
print(img_path + "-------转换完成")
setting = None
else:
print(json_path + "-------文件不存在")
print("180度-end")
print("270度-start")
for i in range(len(img_list)):
img_path = img_list[i]
json_path = img_list[i].replace(file_format, replace_format)
is_exists = os.path.exists(json_path)
if is_exists:
f = open(json_path, encoding='utf-8')
setting = json.load(f)
width = setting['imageWidth']
height = setting['imageHeight']
mid_width = width / 2
mid_height = height / 2
for i2 in range(len(setting['shapes'])):
for i3 in range(len(setting['shapes'][i2]['points'])):
temp_x = setting['shapes'][i2]['points'][i3][0]
temp_y = setting['shapes'][i2]['points'][i3][1]
if temp_y > mid_height:
new_y = temp_x
dis = temp_y - mid_height
new_x = mid_height - dis
elif temp_y < mid_height:
new_y = temp_x
dis = mid_height - temp_y
new_x = mid_height + dis
else:
new_y = temp_x
new_x = temp_y
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('_','_270_')
full_path = save_path + img_name.replace('_','_270_')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
pri_image.transpose(Image.ROTATE_270).save(full_path)
with open(full_path, 'rb') as f:
setting['imageData'] = base64.b64encode(f.read()).decode()
string = json.dumps(setting)
with open(full_path1, 'w', encoding='utf-8') as f:
f.write(string)
f.close()
print(img_path + "-------转换完成")
setting = None
else:
print(json_path + "-------文件不存在")
print("270度-end")
如果你觉得本文对你有帮助,记得点赞、评论、收藏,支持一下up主,那么我们下期再见*
|