在使用深度學習對圖像進行訓練時,對圖像進行隨機旋轉(zhuǎn)有助于提升模型泛化能力。然而之前在做旋轉(zhuǎn)等預處理工作時,都是先對圖像進行旋轉(zhuǎn)后保存到本地,然后再輸入模型進行訓練,這樣的過程會增加工作量,如果圖片數(shù)量較多,生成旋轉(zhuǎ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()
另外有需要云服務器可以了解下創(chuàng)新互聯(lián)scvps.cn,海內(nèi)外云服務器15元起步,三天無理由+7*72小時售后在線,公司持有idc許可證,提供“云服務器、裸金屬服務器、高防服務器、香港服務器、美國服務器、虛擬主機、免備案服務器”等云主機租用服務以及企業(yè)上云的綜合解決方案,具有“安全穩(wěn)定、簡單易用、服務可用性高、性價比高”等特點與優(yōu)勢,專為企業(yè)上云打造定制,能夠滿足用戶豐富、多元化的應用場景需求。
分享文章:使用TensorFlow對圖像進行隨機旋轉(zhuǎn)的實現(xiàn)示例-創(chuàng)新互聯(lián)
網(wǎng)站網(wǎng)址:http://www.chinadenli.net/article4/digioe.html
成都網(wǎng)站建設公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站改版、域名注冊、關鍵詞優(yōu)化、定制開發(fā)、商城網(wǎng)站、網(wǎng)站內(nèi)鏈
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)
猜你還喜歡下面的內(nèi)容