神剑山庄资源网 Design By www.hcban.com
在使用深度学习对图像进行训练时,对图像进行随机旋转有助于提升模型泛化能力。然而之前在做旋转等预处理工作时,都是先对图像进行旋转后保存到本地,然后再输入模型进行训练,这样的过程会增加工作量,如果图片数量较多,生成旋转的图像会占用更多的空间。直接在训练过程中便对图像进行随机旋转,可有效提升工作效率节省硬盘空间。
使用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()
结果如下:
原图:
随机旋转后的图:
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。
神剑山庄资源网 Design By www.hcban.com
神剑山庄资源网
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件!
如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
神剑山庄资源网 Design By www.hcban.com
暂无使用TensorFlow对图像进行随机旋转的实现示例的评论...
更新日志
2024年10月04日
2024年10月04日
- 孙悦.1996-伙伴【正大国际】【WAV+CUE】
- 纪钧瀚《钢琴阅读时光 雨中书店聆听轻音乐》[FLAC/分轨][399.62MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[320K/MP3][87.4MB]
- 证声音乐图书馆《走向自然 疗心爵士乐》[FLAC/分轨][184.94MB]
- 陈慧娴.2018-Priscilla-Ism演唱会3CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郑秀文.1999-我应该得到(国)【华纳】【WAV+CUE】
- 陈家慧.2011-钢琴酒吧2CD【龙吟唱片】【WAV+CUE】
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[320K/MP3][45.01MB]
- 证声音乐图书馆《雨季 蓝调吉他 Rainy Blues》[FLAC/分轨][109.13MB]
- 赞多《序章》[320K/MP3][45.54MB]
- 许巍.2004-每一刻都是崭新的【步升大风】【WAV+CUE】
- 群星.2024-四方馆影视原声带【韶愔音乐】【FLAC分轨】
- 陈雷.1997-安锁咧【金圆唱片】【WAV+CUE】
- 关淑怡.2013-MY.FAVORITE.SK.3CD【环球】【WAV+CUE】
- Sweety.2006-花言乔语【丰华】【WAV+CUE】