使用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魔法方法-属性转换和类的表示详解
Jul 22 Python
分析python切片原理和方法
Dec 19 Python
python中numpy的矩阵、多维数组的用法
Feb 05 Python
TensorFlow实现Softmax回归模型
Mar 09 Python
python通过微信发送邮件实现电脑关机
Jun 20 Python
78行Python代码实现现微信撤回消息功能
Jul 26 Python
对python的bytes类型数据split分割切片方法
Dec 04 Python
selenium跳过webdriver检测并模拟登录淘宝
Jun 12 Python
Selenium向iframe富文本框输入内容过程图解
Apr 10 Python
Python unittest框架操作实例解析
Apr 13 Python
django跳转页面传参的实现
Sep 17 Python
Python使用plt.boxplot()函数绘制箱图、常用方法以及含义详解
Aug 14 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
无线电广播与收音机发展的历史回眸
2021/03/02 无线电
PHP文件锁函数flock()详细介绍
2014/11/18 PHP
PHP中SimpleXML函数用法分析
2014/11/26 PHP
PHP中使用curl伪造IP的简单方法
2015/08/07 PHP
ThinkPHP3.1.x修改成功与失败跳转页面的方法
2017/09/29 PHP
JS阻止用户多次提交示例代码
2014/03/26 Javascript
基于jQuery实现的菜单切换效果
2015/10/16 Javascript
jQuery实现动态文字搜索功能
2017/01/05 Javascript
Vue press 支持图片放大功能的实例代码
2018/11/09 Javascript
vue的for循环使用方法
2019/02/12 Javascript
深入理解react 组件类型及使用场景
2019/03/07 Javascript
让mocha支持ES6模块的方法实现
2020/01/14 Javascript
在vue项目中引用Antv G2,以饼图为例讲解
2020/10/28 Javascript
Python中List.index()方法的使用教程
2015/05/20 Python
python中json格式数据输出的简单实现方法
2016/10/31 Python
python pandas 对时间序列文件处理的实例
2018/06/22 Python
python 将print输出的内容保存到txt文件中
2018/07/17 Python
Python实现处理逆波兰表达式示例
2018/07/30 Python
在Python中获取两数相除的商和余数方法
2018/11/10 Python
在Python中字典根据多项规则排序的方法
2019/01/21 Python
python3 下载网络图片代码实例
2019/08/27 Python
python中导入 train_test_split提示错误的解决
2020/06/19 Python
关于python3.7安装matplotlib始终无法成功的问题的解决
2020/07/28 Python
通过Python pyecharts输出保存图片代码实例
2020/11/25 Python
VSCode中autopep8无法运行问题解决方案(提示Error: Command failed,usage)
2021/03/02 Python
adidas美国官网:adidas US
2016/09/21 全球购物
FOREO官方网站:LUNA露娜洁面仪
2016/11/28 全球购物
一套带答案的C++笔试题
2014/01/10 面试题
工作失职检讨书范文
2014/01/16 职场文书
中文教师求职信
2014/02/22 职场文书
无毒社区工作方案
2014/05/23 职场文书
电力工程合作意向书
2015/05/11 职场文书
人与自然观后感
2015/06/16 职场文书
张丽莉事迹观后感
2015/06/16 职场文书
HTML5页面音频自动播放的实现方式
2021/06/21 HTML / CSS
基于Apache Hudi在Google云构建数据湖平台的思路详解
2022/04/07 Servers