我是靠谱客的博主 天真篮球,最近开发中收集的这篇文章主要介绍用tensorflow进行数据增强用tensorflow进行数据增强,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

深度学习数据增强@TOC

用tensorflow进行数据增强

这几天跑代码,数据量不多,为了防止过拟合和加强训练准确度,就想着做一些图片的数据增强,在网上找了一些代码,能实现功能但是巨慢,甚至比我深度学习训练都慢,昨天花了一点时间写了一个基于tensorflow的,就很快,记录分享一下。

我的这个是用于深度学习的,所以里面包含了json文件的修改,实现的功能很简单,就是同一张图片调对比度,调亮度,加噪声之类的多生成几张图片,修改原始图片json文件中的文件名得到衍生图片的对应标签,这样数据量就能快速增加。

import json
import os, sys
import cv2
import tensorflow as tf
from PIL import Image
import PIL
import matplotlib.pyplot as plt
def get_json_data(json_path,img_path):
with open(json_path, 'rb')as f:
params = json.load(f)
# 加载json文件中的内容给params
params['imagePath'] = img_path
# 这两行控制修改的内容 时间有限就写的很草率
dict = params
# 将修改后的内容保存在dict中
f.close()
# 关闭json读模式
return dict
# 返回dict字典内容
def write_json_data(dict):
# 写入json文件
with open(json_path1, 'w')as r:
# 定义为写模式,名称定义为r
json.dump(dict, r, indent=2)
# indent控制间隔
# 将dict写入名称为r的文件中
r.close()
# 关闭json写模式
# 图片文件夹路径
file_dir = r'D:/Desktop/sudo/images/'
#保存图片的文件夹
json_dir = r'D:/Desktop/sudo/jsons/'
#保存json文件的文件夹
for img_name in os.listdir(file_dir):
img_path = file_dir + img_name
json_path = json_dir + img_name.strip('jpg') + 'json'
print(img_path)
print(json_path)
raw_img = tf.io.read_file(img_path)
img = tf.image.decode_jpeg(raw_img)
dict = {}
# 调整对比度
img_morecon = tf.image.adjust_contrast(img,0.5)
img_morecon = tf.image.encode_jpeg(img_morecon,quality= 100)
new_path = file_dir + img_name.strip('.jpg') + '_morecon.jpg'
with tf.io.gfile.GFile(new_path, 'wb') as file:
file.write(img_salt.numpy())
img_path = file_dir + img_name.strip('.jpg') + '_morecon.jpg'
the_revised_dict = get_json_data(json_path, img_path)
json_path1 = json_dir + img_name.strip('.jpg') + '_morecon.json'
# 修改json文件后保存的路径
write_json_data(the_revised_dict)
img_lessat = tf.image.adjust_saturation(img, -2)
img_lessat = tf.image.encode_jpeg(img_lessat, quality=100)
new_path = file_dir + img_name.strip('.jpg') + '_lesssat.jpg'
with tf.io.gfile.GFile(new_path, 'wb') as file:
file.write(img_gauss.numpy())
img_path = file_dir + img_name.strip('.jpg') + '_lesssat.jpg'
the_revised_dict = get_json_data(json_path,img_path)
json_path1 = json_dir + img_name.strip('.jpg') + '_lesssat.json'
# 修改json文件后保存的路径
write_json_data(the_revised_dict)
# 变亮、变暗
img_darker = tf.image.adjust_brightness(img, -0.1)
img_darker = tf.image.encode_jpeg(img_darker, quality=100)
new_path = file_dir + img_name.strip('.jpg') + '_darker.jpg'
with tf.io.gfile.GFile(new_path, 'wb') as file:
file.write(img_darker.numpy())
img_path = file_dir + img_name.strip('.jpg') + '_darker.jpg'
the_revised_dict = get_json_data(json_path, img_path)
json_path1 = json_dir + img_name.strip('.jpg') + '_darker.json'
# 修改json文件后保存的路径
write_json_data(the_revised_dict)
img_brighter = tf.image.adjust_brightness(img, 0.1)
img_brighter = tf.image.encode_jpeg(img_brighter, quality=100)
new_path = file_dir + img_name.strip('.jpg') + '_brighter.jpg'
with tf.io.gfile.GFile(new_path, 'wb') as file:
file.write(img_brighter.numpy())
img_path = file_dir + img_name.strip('.jpg') + '_brighter.jpg'
the_revised_dict = get_json_data(json_path, img_path)
json_path1 = json_dir + img_name.strip('.jpg') + '_brighter.json'
# 修改json文件后保存的路径
write_json_data(the_revised_dict)
#
#变成灰阶g
gray = tf.image.rgb_to_grayscale(img)
#
cv2.GaussianBlur(图像,卷积核,标准差)
gray = tf.image.encode_jpeg(gray, quality=100)
new_path = file_dir + img_name.strip('.jpg') + '_gray.jpg'
with tf.io.gfile.GFile(new_path, 'wb') as file:
file.write(blur.numpy())
img_path = file_dir + img_name.strip('.jpg') + '_gray.jpg'
the_revised_dict = get_json_data(json_path, img_path)
json_path1 = json_dir + img_name.strip('.jpg') + '_gray.json'
# 修改json文件后保g存的路径
write_json_data(the_revised_dict)
noise = tf.random.normal(shape=img.shape, mean=0, stddev=50)
drop_noise = tf.nn.dropout(noise, 0.01)
noise = tf.add(img, tf.cast(drop_noise, dtype=tf.uint8))
noise = tf.image.encode_jpeg(noise, quality=100)
new_path = file_dir + img_name.strip('.jpg') + '_noise.jpg'
with tf.io.gfile.GFile(new_path, 'wb') as file:
file.write(noise.numpy())
img_path = file_dir + img_name.strip('.jpg') + '_noise.jpg'
the_revised_dict = get_json_data(json_path, img_path)
json_path1 = json_dir + img_name.strip('.jpg') + '_noise.json'
# 修改json文件后保g存的路径
write_json_data(the_revised_dict)

最后

以上就是天真篮球为你收集整理的用tensorflow进行数据增强用tensorflow进行数据增强的全部内容,希望文章能够帮你解决用tensorflow进行数据增强用tensorflow进行数据增强所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(55)

评论列表共有 0 条评论

立即
投稿
返回
顶部