现如今越来越多的研友开始涉足深度学习领域了,深度学习的入门离不开两个关键点:模型和数据,各种的模型也层出不穷,这里介绍一下我个人常用的深度学习工具箱:OpenMMlab。
你可以在里面找到你想用到的几乎所有模型,涉及语音识别、目标检测、语义分割、实例分割、自然语言处理、视频分析、姿态检测等各个领域,并且在不断更新网络模型,你可以在里面找到你想要的各个领域模型,并且方便的使用不同模型进行实验。
说完模型介绍一下我自己使用的数据增强方法:虽然比较简单,但是实用性强、扩展性高,可以用于语义分割、实例分割的数据扩充(目标检测没有尝试,如果文件格式类似也是可以)。
放代码:
from PIL import Image
import os
import glob
import json
import base64
"""
1.这里只是通过简单的旋转翻转进行增强,可以扩充7倍,json文件名格式为****.json,中间不要带.如果命名格式为a_1,可以将73,74中 ('.','_l.') 更换为('_','_l'),需要修改7处。
2.也可以通过亮度,色度,对比度,锐度的方法进行增强,但不建议使用平移的方法,因为要考虑平移时会导致回归框超出范围的情况。
3.扩充时,需要将json文件和对应原始图片放在同一文件夹下(E:\make_dataset\beijing\\),二者名称需要相同,这里图片格式为jpg,按需修改。
4.感谢对面emo怪的鼎力协助,我真的会谢:)
"""
# 数据集路径
path = r"E:\make_dataset\beijing\\"
# 生成数据的保存路径
save_path = r"E:\make_dataset\beijing\\"
# 当前数据集图片格式/
file_format = ".jpg"
# 替换格式jpg -> json
replace_format = ".json"
# 左右翻转文件名附加字符
LR = "lr_"
# 上下翻转文件名附加字符
TB = "tb_"
# 获取数据集目录的图片数据集
img_list = glob.glob(path + "*" + file_format) #glob查找符合特定规则的文件路径名
print("左右翻转-start")
# 1.遍历图片
for i in range(len(img_list)):
# 图片路径
img_path = img_list[i]
# print(img_path)
# 对应json路径
json_path = img_list[i].replace(file_format, replace_format)
# 判断json文件是否存在
is_exists = os.path.exists(json_path)
if is_exists:
# 打开json文件
f = open(json_path, encoding='utf-8')
# 读取json
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))
# 2.遍历shapes
for i2 in range(len(setting['shapes'])):
# 3.遍历每个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
# 从json获取文件名
# file_name = setting['imagePath'].split('\\')[-1]
img_name = img_path.split("\\")[-1] #-1表示保留最后一位
# print(img_name)
# 修改json文件名
setting['imagePath'] = img_name.replace('.','_l.')
full_path = save_path + img_name.replace('.','_l.')
full_path1 = full_path.replace(file_format, replace_format)
# print(full_path1)
# 图片转换
pri_image = Image.open(img_path)
# 左右镜面翻转FLIP_LEFT_RIGHT
pri_image.transpose(Image.FLIP_LEFT_RIGHT).save(full_path)
# 将转换后的图片进行base64加密
with open(full_path, 'rb') as f:
setting['imageData'] = base64.b64encode(f.read()).decode()
string = json.dumps(setting)
# 将修改后的json写入文件
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
# file_name = setting['imagePath'].split('\\')[-1]
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('.','_u.')
# setting['imagePath'] = TB + file_name
full_path = save_path + img_name.replace('.','_u.')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
# 上下镜面翻转FLIP_TOP_BOTTOM
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
# 从json获取文件名
# file_name = setting['imagePath'].split('\\')[-1]
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('.','_90.')
# setting['imagePath'] = TB + file_name
full_path = save_path + img_name.replace('.','_90.')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
# 顺时针旋转90度ROTATE_90
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]
new_x = temp_y
new_y = temp_x
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
# 从json获取文件名
# file_name = setting['imagePath'].split('\\')[-1]
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('.','_90u.')
# setting['imagePath'] = TB + file_name
full_path = save_path + img_name.replace('.','_90u.')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
# 顺时针旋转90度ROTATE_90
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]
new_x = height - temp_y
new_y = width - temp_x
setting['shapes'][i2]['points'][i3][0] = new_x
setting['shapes'][i2]['points'][i3][1] = new_y
# 从json获取文件名
# file_name = setting['imagePath'].split('\\')[-1]
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('.','_90l.')
# setting['imagePath'] = TB + file_name
full_path = save_path + img_name.replace('.','_90l.')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
# 顺时针旋转90度ROTATE_90
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]
# 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
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
# 从json获取文件名
# file_name = setting['imagePath'].split('\\')[-1]
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('.','_180.')
# setting['imagePath'] = TB + file_name
full_path = save_path + img_name.replace('.','_180.')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
# 顺时针旋转180度ROTATE_180
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
# file_name = setting['imagePath'].split('\\')[-1]
img_name = img_path.split("\\")[-1]
setting['imagePath'] = img_name.replace('.','_270.')
# setting['imagePath'] = TB + file_name
full_path = save_path + img_name.replace('.','_270.')
full_path1 = full_path.replace(file_format, replace_format)
pri_image = Image.open(img_path)
# 顺时针旋转270度ROTATE_270
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主,那么我们下期再见*