使用TensorFlow对图像进行随机旋转的实现示例


Posted in Python onJanuary 20, 2020

在使用深度学习对图像进行训练时,对图像进行随机旋转有助于提升模型泛化能力。然而之前在做旋转等预处理工作时,都是先对图像进行旋转后保存到本地,然后再输入模型进行训练,这样的过程会增加工作量,如果图片数量较多,生成旋转的图像会占用更多的空间。直接在训练过程中便对图像进行随机旋转,可有效提升工作效率节省硬盘空间。

使用TensorFlow对图像进行随机旋转如下:

TensorFlow版本为1.13.1

#-*- coding:utf-8 -*-
'''
  使用TensorFlow进行图像的随机旋转示例
'''
 
import tensorflow as tf
import numpy as np
import cv2
import matplotlib.pyplot as plt
 
 
img = cv2.imread('tf.jpg')
img = cv2.resize(img,(220,220))
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
 
def tf_rotate(input_image, min_angle = -np.pi/2, max_angle = np.pi/2):
  '''
  TensorFlow对图像进行随机旋转
  :param input_image: 图像输入
  :param min_angle: 最小旋转角度
  :param max_angle: 最大旋转角度
  :return: 旋转后的图像
  '''
  distorted_image = tf.expand_dims(input_image, 0)
  random_angles = tf.random.uniform(shape=(tf.shape(distorted_image)[0],), minval = min_angle , maxval = max_angle)
  distorted_image = tf.contrib.image.transform(
    distorted_image,
    tf.contrib.image.angles_to_projective_transforms(
      random_angles, tf.cast(tf.shape(distorted_image)[1], tf.float32), tf.cast(tf.shape(distorted_image)[2], tf.float32)
    ))
  rotate_image = tf.squeeze(distorted_image, [0])
  return rotate_image
 
global_init = tf.global_variables_initializer()
with tf.Session() as sess:
  init = tf.initialize_local_variables()
  sess.run([init, global_init])
  coord = tf.train.Coordinator()
  threads = tf.train.start_queue_runners(coord=coord)
  image = tf.placeholder(shape=(220, 220, 3), dtype=tf.float32)
 
  rotate_image = tf_rotate(image, -np.pi/2, np.pi/2)
  output = sess.run(rotate_image, feed_dict={image:img})
  # print('output:',output)
  plt.imshow(output.astype('uint8'))
  plt.title('rotate image')
  plt.show()

结果如下:

原图:

使用TensorFlow对图像进行随机旋转的实现示例

随机旋转后的图:

使用TensorFlow对图像进行随机旋转的实现示例

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持三水点靠木。

Python 相关文章推荐
python实现linux下使用xcopy的方法
Jun 28 Python
Python检测网站链接是否已存在
Apr 07 Python
浅谈Python的垃圾回收机制
Dec 17 Python
用Python实现KNN分类算法
Dec 22 Python
Python 函数基础知识汇总
Mar 09 Python
python 定时修改数据库的示例代码
Apr 08 Python
python实现播放音频和录音功能示例代码
Dec 30 Python
python 实现图片旋转 上下左右 180度旋转的示例
Jan 24 Python
Python Numpy 控制台完全输出ndarray的实现
Feb 19 Python
15行Python代码实现免费发送手机短信推送消息功能
Feb 27 Python
pyqt5实现井字棋的示例代码
Dec 07 Python
分享Python获取本机IP地址的几种方法
Mar 17 Python
TensorFLow 不同大小图片的TFrecords存取实例
Jan 20 #Python
python各层级目录下import方法代码实例
Jan 20 #Python
Python 识别12306图片验证码物品的实现示例
Jan 20 #Python
如何基于python实现归一化处理
Jan 20 #Python
tensorflow入门:tfrecord 和tf.data.TFRecordDataset的使用
Jan 20 #Python
tensorflow入门:TFRecordDataset变长数据的batch读取详解
Jan 20 #Python
python如何通过pyqt5实现进度条
Jan 20 #Python
You might like
PHP中模拟链表和链表的基本操作示例
2016/02/27 PHP
PHP实现二维数组中的查找算法小结
2018/06/09 PHP
Laravel使用RabbitMQ的方法示例
2019/06/18 PHP
PHP的HTTP客户端Guzzle简单使用方法分析
2019/10/30 PHP
使用js获取QueryString的方法小结
2010/02/28 Javascript
Jquery CheckBox全选方法代码附js checkbox全选反选代码
2010/06/09 Javascript
javascript下高性能字符串连接StringBuffer类
2010/08/16 Javascript
jquery实现不同大小浏览器使用不同的css样式表的方法
2014/04/02 Javascript
jQuery实现的多屏图像图层切换效果实例
2015/05/07 Javascript
jquery+CSS实现的多级竖向展开树形TRee菜单效果
2015/08/24 Javascript
JS实现3D图片旋转展示效果代码
2015/09/22 Javascript
原生js开发的日历插件
2017/02/04 Javascript
Web技术实现移动监测的介绍
2017/09/18 Javascript
jQuery实现根据身份证号获取生日、年龄、性别等信息的方法
2019/01/09 jQuery
微信小程序图片自适应实现解析
2020/01/21 Javascript
Python深入学习之上下文管理器
2014/08/31 Python
Python访问MySQL封装的常用类实例
2014/11/11 Python
使用Python中的tkinter模块作图的方法
2017/02/07 Python
django+js+ajax实现刷新页面的方法
2017/05/22 Python
pandas系列之DataFrame 行列数据筛选实例
2018/04/12 Python
python+splinter实现12306网站刷票并自动购票流程
2018/09/25 Python
Python3实现二叉树的最大深度
2019/09/30 Python
Django框架之中间件MiddleWare的实现
2019/12/30 Python
Pandas对DataFrame单列/多列进行运算(map, apply, transform, agg)
2020/06/14 Python
利用PyTorch实现VGG16教程
2020/06/24 Python
django中cookiecutter的使用教程
2020/12/03 Python
微软香港官网及网上商店:Microsoft HK
2016/09/01 全球购物
应用电子专业学生的自我评价
2013/10/16 职场文书
24岁生日感言
2014/01/13 职场文书
会计顶岗实习心得
2014/01/25 职场文书
2015年元旦文艺汇演主持词
2014/03/26 职场文书
培训班开班仪式主持词
2014/03/28 职场文书
房屋授权无偿使用证明
2014/11/29 职场文书
标准离婚协议书范文下载
2014/11/30 职场文书
未中标通知书
2015/04/17 职场文书
SpringBoot 集成短信和邮件 以阿里云短信服务为例
2022/04/22 Java/Android