日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

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

發(fā)布時間:2023/12/10 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow 旋转图片_使用TensorFlow对图像进行随机旋转的实现示例 小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.

https://www.jb51.net/article/178934.htm

在使用深度學(xué)習對圖像進行訓(xùn)練時,對圖像進行隨機旋轉(zhuǎn)有助于提升模型泛化能力。然而之前在做旋轉(zhuǎn)等預(yù)處理工作時,都是先對圖像進行旋轉(zhuǎn)后保存到本地,然后再輸入模型進行訓(xùn)練,這樣的過程會增加工作量,如果圖片數(shù)量較多,生成旋轉(zhuǎn)的圖像會占用更多的空間。直接在訓(xùn)練過程中便對圖像進行隨機旋轉(zhuǎn),可有效提升工作效率節(jié)省硬盤空間。

使用TensorFlow對圖像進行隨機旋轉(zhuǎn)如下:

TensorFlow版本為1.13.1

#-*- coding:utf-8 -*-

'''

使用TensorFlow進行圖像的隨機旋轉(zhuǎn)示例

'''

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對圖像進行隨機旋轉(zhuǎn)

:param input_image: 圖像輸入

:param min_angle: 最小旋轉(zhuǎn)角度

:param max_angle: 最大旋轉(zhuǎn)角度

:return: 旋轉(zhuǎn)后的圖像

'''

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()

結(jié)果如下:

原圖:

隨機旋轉(zhuǎn)后的圖:

總結(jié)

以上是生活随笔為你收集整理的tensorflow 旋转图片_使用TensorFlow对图像进行随机旋转的实现示例的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯,歡迎將生活随笔推薦給好友。