tensorflow图片预处理,随机亮度,旋转,剪切,翻转。
来源:互联网 发布:网络应急预案演练过程 编辑:程序博客网 时间:2024/05/18 03:59
图像预处理是一个非常简单,通过提高训练数据的多样性,进而对训练模型的召回率,适应性有着非常大的提升作用。
运行效果:
另外在训练时,需要更多的训练次数,比如说我对每张图片进行了一次旋转,那么训练次数就要提高一倍。也就是说训练集多样性增加,同时训练次数也要增加。
代码:
import tensorflow as tffrom scipy import miscimport numpy as np#随机旋转图片def random_rotate_image(image_file, num): with tf.Graph().as_default(): tf.set_random_seed(666) file_contents = tf.read_file(image_file) image = tf.image.decode_image(file_contents, channels=3) image_rotate_en_list = [] def random_rotate_image_func(image): #旋转角度范围 angle = np.random.uniform(low=-30.0, high=30.0) return misc.imrotate(image, angle, 'bicubic') for i in range(num): image_rotate = tf.py_func(random_rotate_image_func, [image], tf.uint8) image_rotate_en_list.append(tf.image.encode_png(image_rotate)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) results = sess.run(image_rotate_en_list) for idx,re in enumerate(results): with open('data/'+str(idx)+'.png','wb') as f: f.write(re)#随机左右翻转图片def random_flip_image(image_file, num): with tf.Graph().as_default(): tf.set_random_seed(666) file_contents = tf.read_file(image_file) image = tf.image.decode_image(file_contents, channels=3) image_flip_en_list = [] for i in range(num): image_flip = tf.image.random_flip_left_right(image) image_flip_en_list.append(tf.image.encode_png(image_flip)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) results = sess.run(image_flip_en_list) for idx,re in enumerate(results): with open('data/'+str(idx)+'.png','wb') as f: f.write(re)#随机变化图片亮度def random_brightness_image(image_file, num): with tf.Graph().as_default(): tf.set_random_seed(666) file_contents = tf.read_file(image_file) image = tf.image.decode_image(file_contents, channels=3) image_bright_en_list = [] for i in range(num): image_bright = tf.image.random_brightness(image, max_delta=0.3) image_bright_en_list.append(tf.image.encode_png(image_bright)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) results = sess.run(image_bright_en_list) for idx,re in enumerate(results): with open('data/'+str(idx)+'.png','wb') as f: f.write(re)#随机裁剪图片def random_crop_image(image_file, num): with tf.Graph().as_default(): tf.set_random_seed(666) file_contents = tf.read_file(image_file) image = tf.image.decode_image(file_contents, channels=3) image_crop_en_list = [] for i in range(num): #裁剪后图片分辨率保持160x160,3通道 image_crop = tf.random_crop(image, [160, 160, 3]) image_crop_en_list.append(tf.image.encode_png(image_crop)) with tf.Session() as sess: sess.run(tf.global_variables_initializer()) sess.run(tf.local_variables_initializer()) results = sess.run(image_crop_en_list) for idx,re in enumerate(results): with open('data/'+str(idx)+'.png','wb') as f: f.write(re)if __name__ == '__main__': #处理图片,进行20次随机处理,并将处理后的图片保存到输入图片相同的路径下 random_brightness_image('data/test.png', 20)
运行效果:
随机裁剪
随机亮度
随机旋转
随机翻转
更多图像处理操作,请查看tensorflow官方文档http://www.tensorfly.cn/tfdoc/api_docs/python/image.html
阅读全文
0 0
- tensorflow图片预处理,随机亮度,旋转,剪切,翻转。
- tensorflow下有关图片的随机翻转、随机调整亮度以及对比度函数
- tensorflow下有关图片的随机翻转、随机调整亮度以及对比度函数
- 上传图片,剪切,旋转
- 强大的PHP 图片处理类(水印、透明度、缩放、锐化、旋转、翻转、剪切、反色)
- 图片翻转 和旋转
- [TensorFlow实战] 图片预处理
- 旋转图片,及带剪切的旋转
- PHP图片处理类实现缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色等功能
- PHP图片处理类实现缩放、剪切、相框、水印、锐化、旋转、翻转、透明度、反色等功能
- java图片旋转,缩放,翻转
- 图片旋转缩放翻转效果
- matlab 图片的剪切和旋转
- TensorFlow 图像预处理(二) 图像翻转,图像色彩调整
- 图片工具类(图片上传、压缩、旋转、剪切)
- Image类--2--旋转和翻转图片
- android中图片的翻转和旋转
- android中图片的翻转和旋转
- 欢迎使用CSDN-markdown编辑器
- log4j配置及日志按级别分别生成日志文件
- SpringMVC配置文件
- 读书笔记:《明朝那些事第三部:妖孽宫廷》
- 说说IO(二)- IO模型
- tensorflow图片预处理,随机亮度,旋转,剪切,翻转。
- LeetCode 20. Valid Parentheses--验证括号是否有效
- 拥塞控制算法分类
- 【转载】关于View的ScrollTo, getScrollX 和 getScrollY
- javascript设计模式--设计原则
- [推荐]docker中文学习文档
- 解决python入门object() takes no parameters
- JavaScript设计模式--高阶函数
- 校招笔试题