使用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 相关文章推荐
linux 下实现python多版本安装实践
Nov 18 Python
在Python中操作字符串之rstrip()方法的使用
May 19 Python
python使用wmi模块获取windows下的系统信息 监控系统
Oct 27 Python
Python 中 list 的各项操作技巧
Apr 13 Python
Python生成密码库功能示例
May 23 Python
解决python selenium3启动不了firefox的问题
Oct 13 Python
Python使用combinations实现排列组合的方法
Nov 13 Python
Python3爬虫学习之将爬取的信息保存到本地的方法详解
Dec 12 Python
python celery分布式任务队列的使用详解
Jul 08 Python
python函数局部变量、全局变量、递归知识点总结
Nov 15 Python
python的slice notation的特殊用法详解
Dec 27 Python
Python如何读写二进制数组数据
Aug 01 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/01 无线电
关于PHP session 存储方式的详细介绍
2013/06/25 PHP
php技术实现加载字体并保存成图片
2015/07/27 PHP
Laravel中Facade的加载过程与原理详解
2017/09/22 PHP
php-7.3.6 编译安装过程
2020/02/11 PHP
使用jQuery全局事件ajaxStart为特定请求实现提示效果的代码
2010/12/30 Javascript
JS记录用户登录次数实现代码
2014/01/15 Javascript
Javascript学习笔记之相等符号与严格相等符号
2014/11/23 Javascript
jquery制作漂亮的弹出层提示消息特效
2014/12/23 Javascript
jQuery团购倒计时特效实现方法
2015/05/07 Javascript
js中字符串编码函数escape()、encodeURI()、encodeURIComponent()区别详解
2016/04/01 Javascript
基于jquery插件编写countdown计时器
2016/06/12 Javascript
jQuery ajax中使用confirm,确认是否删除的简单实例
2016/06/17 Javascript
用nodeJS搭建本地文件服务器的几种方法小结
2017/03/16 NodeJs
[03:28]2014DOTA2国际邀请赛 走近EG战队天才中单Arteezy
2014/07/12 DOTA
解析Python中的变量、引用、拷贝和作用域的问题
2015/04/07 Python
Python中最常用的操作列表的几种方法归纳
2015/04/24 Python
Python随机数random模块使用指南
2016/09/09 Python
pandas 根据列的值选取所有行的示例
2018/11/07 Python
python之mock模块基本使用方法详解
2019/06/27 Python
python 读写文件包含多种编码格式的解决方式
2019/12/20 Python
利用Python实现Excel的文件间的数据匹配功能
2020/06/16 Python
python 删除excel表格重复行,数据预处理操作
2020/07/06 Python
基于python实现可视化生成二维码工具
2020/07/08 Python
python如何写try语句
2020/07/14 Python
python自动化测试三部曲之unittest框架的实现
2020/10/07 Python
CSS3动画和HTML5新特性详解
2020/08/31 HTML / CSS
Kipling澳洲官网:购买凯浦林包包
2020/12/17 全球购物
面向游戏玩家和书呆子的极客订阅盒:Loot Crate
2020/11/25 全球购物
本科生求职简历的自我评价
2013/10/21 职场文书
外语系毕业生找工作的求职信
2013/11/28 职场文书
给护士表扬信
2014/01/19 职场文书
公司司机岗位职责
2014/02/07 职场文书
一年级语文教学反思
2014/02/13 职场文书
大一新生学期自我评价
2014/04/09 职场文书
讲文明知礼仪演讲稿
2014/09/13 职场文书