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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

tensorflow数据增强

發布時間:2024/1/23 编程问答 32 豆豆
生活随笔 收集整理的這篇文章主要介紹了 tensorflow数据增强 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
??? 相信大家都聽說過數據增強(Data Augmentation),這是在做神經網絡時非常極其重要的trick, 因為數據是寶貴的,稀有的,通過數據增強我們能讓我們的數據量迅速增大,并且能使訓練的模型具有一定抗噪能力。這篇文章主要探討一下 tensorflow 關于數據增強的API.
??? 先讀取圖片數據, 并輸出我們的圖片信息

import numpy as np from PIL import Image# #***************案例1*numpy讀取圖片數據**********************# img = Image.open("5.jpg", 'r') img.show() a = np.asarray(img, dtype=float) print(img.mode) # output RGB (1601, 1002)
沒錯,就是一個漂亮的小姐姐,hh

????????再先來個題外話,叫做 圖片數據的編碼, tensorflow能支持的格式不多, jpg,png,gif,bmp這些常用格式倒是可以的,如果你的圖片格式是很特殊的格式的話, 恐怕你就需要用其他處理方式先進行預處理了, 比如常用的醫學圖像 .nii格式 你就需要用nibable, 一些tiff,DICOM,等你可能可以用 PIL,cv2,openslide , libvips等等。

????

# ***# 讀取圖片,進行圖片解碼 # 讀取圖像的原始數據 返回值:<class 'bytes'> # 也就是讀取圖片,將其轉換成一串二進制串 image_raw_data = tf.gfile.FastGFile("5.jpg", 'rb').read()with tf.Session() as sess:# img_data--> <class 'tensorflow.python.framework.ops.Tensor'> img_data = tf.image.decode_jpeg(image_raw_data)print(img_data.eval()) # RGB模式輸出一個三維數組 # py_plot展示圖片 plt.imshow(img_data.eval())plt.show()# 將數據類型轉化為uint8 img_data = tf.image.convert_image_dtype(img_data, dtype=tf.uint8)# ***# 讀取數據,進行圖片編碼 encoded_image = tf.image.encode_png(img_data)with tf.gfile.GFile("6.png", 'wb') as f:f.write(encoded_image.eval())

??????? 接下來我們就一個個來看看,首先讀取圖片數據,并轉化成float32

image_raw_data = tf.gfile.FastGFile("5.jpg", 'rb').read() # 500x500 with tf.Session() as sess:# img_data--> <class 'tensorflow.python.framework.ops.Tensor'> img_data = tf.image.decode_jpeg(image_raw_data)img_data = tf.image.convert_image_dtype(img_data, dtype=tf.float32) print(type(img_data) # class 'tensorflow.python.framework.ops.Tensor'>

(一)調整圖片大小resize

????一般來說網絡上的圖像大小不確定,但是神經網絡的輸入節點的個數是固定的。所以與處理是需要統一圖片大小

??? 調整大小方法有以下幾個:
??? # 1、雙線性插值法
??? # 2、最近鄰居法
??? # 3、雙三次插值法
??? # 4、面積插值法

????

resize_img = tf.image.resize_images(img_data, [300, 300], method=0) # 一共提供了四種方法 # <class 'tensorflow.python.framework.ops.Tensor'> plt.imshow(resize_img.eval()) plt.show()# 0代表ResizeMethod.BILINEAR,依次類推

? 輸出結果:

???

(二)圖片剪切填充

 

# 放大圖片就自動周圍填充黑色;縮小圖片就自動從圖片中間剪切 resize_img = tf.image.resize_image_with_crop_or_pad(img_data, 600, 600) plt.imshow(resize_img.eval()) plt.show()

??? 輸出結果:

???


??? (三)圖片按比例大小縮小圖片(也是一種剪切,類似上一種方式)

????

central_cropped = tf.image.central_crop(img_data, 0.5)   plt.imshow(central_cropped.eval()) plt.show()??? 輸出結果:

????

??? (四)方框剪切

????

# # bounding_box_crop # # similar func:tf.image.pad_to_bounding_box # 圖片(0 0)位置在左上角, (50,50)指heightwidth在圖片左上角的偏移量 resize_img = tf.image.crop_to_bounding_box(img_data, 50, 50, 300, 300)

???

????(五)翻轉以及隨機翻轉

# #圖像翻轉# 數據增強 # 上下翻轉、左右翻轉、對角線翻轉 # tf.image.transpose_image(img_data) # tf.image.flip_left_right(img_data) flip_img = tf.image.flip_up_down(img_data)plt.imshow(flip_img.eval())plt.show()# 隨機翻轉 推薦應用這個方法 # 隨機上下左右、亮度、對比度、色相、飽和度 # tf.image.random_flip_up_down(img_data) # tf.image.random_brightness() # tf.image.random_contrast() # tf.image.random_hue() # tf.image.random_saturation() rand_flip_img = tf.image.random_flip_left_right(img_data, seed=1)plt.imshow(rand_flip_img.eval())plt.show()

????(六)圖像色彩調整(五 中包含了一些色彩調整函數,不過是隨機的就直接列在上面了,方便大家總結理解)

????

# 調整圖片亮度、對比度、gamma、色相、飽和度 # tf.image.adjust_contrast() # tf.image.adjust_gamma() # tf.image.adjust_hue() # tf.image.adjust_saturation() adjust_img = tf.image.adjust_brightness(img_data, -0.5) # 將圖片亮度變為均值為0,方差為1 adjust_img = tf.image.per_image_standardization(img_data) print(img_data.eval())

??? (七)圖像標注

????

# # tf.image.draw_bounding_boxes函數的輸入是一個batch的數據,也就是 # 多張圖像組成的四維矩陣 # 第一個輸入參數img_data中數據類型應該是實數,前面最初已經轉換成了tf.float32 batched = tf.expand_dims(img_data, 0) # [0.2, 0.3, 0.5, 0.8]給出的是圖像中的相對位置 [y_min, x_min, y_max, x_max] boxes = tf.constant([[[0.2, 0.3, 0.48, 0.65]]]) res = tf.image.draw_bounding_boxes(batched, boxes, name='bounding_box') plt.subplot(121), plt.imshow(img_data.eval()), plt.title('original') plt.subplot(122), plt.imshow(np.asarray(res.eval())[0]), plt.title('result') # plt.imsave(fname="save.jpg", arr=np.asarray(res.eval())[0]) # 保存圖片 plt.show()??? 上面圖片做標注不是很明顯,換張女神圖片來碼代碼~

????

??? (八)截取標記部分

????

# 隨機截取圖像上有信息含量的部分,也可以提高模型健壯性 # 此函數為圖像生成單個隨機變形的邊界框。函數輸出的是可用于裁剪原始圖像的單個邊框。 # 返回值為3個張量:beginsize bboxes。前2個張量用于 tf.slice 剪裁圖像。 # 后者可以用于 tf.image.draw_bounding_boxes 函數來畫出邊界框。 boxes = tf.constant([[[0.2, 0.3, 0.48, 0.65]]]) print(np.asarray(img_data).shape) begin, size, bbox_for_draw = tf.image.sample_distorted_bounding_box(tf.shape(img_data), bounding_boxes=boxes, min_object_covered=0.1) # batched = tf.expand_dims(tf.image.convert_image_dtype(img_data, tf.float32), 0) # image_with_box = tf.image.draw_bounding_boxes(batched, bbox_for_draw) distorted_image = tf.slice(img_data, begin, size) plt.imshow(distorted_image.eval()) plt.show()


?tensorflow 數據增強處理基本上就這些啦~用起來,訓練神經網絡才是關鍵。


創作挑戰賽新人創作獎勵來咯,堅持創作打卡瓜分現金大獎

總結

以上是生活随笔為你收集整理的tensorflow数据增强的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。