IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 人工智能 -> 深度学习数据增强 -> 正文阅读

[人工智能]深度学习数据增强

数据增强

现如今越来越多的研友开始涉足深度学习领域了,深度学习的入门离不开两个关键点:模型和数据,各种的模型也层出不穷,这里介绍一下我个人常用的深度学习工具箱: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"
# 替换格式jpg -> json
replace_format = ".json"
# 左右翻转文件名附加字符
LR = "lr_"
# 上下翻转文件名附加字符
TB = "tb_"
# 获取数据集目录的图片数据集
img_list = glob.glob(path + "*" + file_format) #glob查找符合特定规则的文件路径名

print(img_list) #!!!!
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(, 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(file_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)
        # 图片转换
        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]
                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

        # 从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]
                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

        # 从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主,那么我们下期再见*

  人工智能 最新文章
2022吴恩达机器学习课程——第二课(神经网
第十五章 规则学习
FixMatch: Simplifying Semi-Supervised Le
数据挖掘Java——Kmeans算法的实现
大脑皮层的分割方法
【翻译】GPT-3是如何工作的
论文笔记:TEACHTEXT: CrossModal Generaliz
python从零学(六)
详解Python 3.x 导入(import)
【答读者问27】backtrader不支持最新版本的
上一篇文章      下一篇文章      查看所有文章
加:2022-03-10 22:31:02  更:2022-03-10 22:32:23 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年11日历 -2024/11/26 17:34:48-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码