淺談tensorflow中Dataset圖片的批量讀取及維度的操作詳解
阿新 • • 發佈:2020-01-21
三維的讀取圖片(w,h,c):
import tensorflow as tf import glob import os def _parse_function(filename): # print(filename) image_string = tf.read_file(filename) image_decoded = tf.image.decode_image(image_string) # (375,500,3) image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded,200,200) return image_resized with tf.Session() as sess: print( sess.run( img ).shape )
讀取批量圖片的讀取圖片(b,w,c):
import tensorflow as tf import glob import os ''' Dataset 批量讀取圖片 ''' def _parse_function(filename): # print(filename) image_string = tf.read_file(filename) image_decoded = tf.image.decode_image(image_string) # (375,3) image_decoded = tf.expand_dims(image_decoded,axis=0) image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded,200) return image_resized img = _parse_function('../pascal/VOCdevkit/VOC2012/JPEGImages/2007_000068.jpg') # image_resized = tf.image.resize_image_with_crop_or_pad( tf.truncated_normal((1,220,300,3))*10,200) 這種四維 形式是可以的 with tf.Session() as sess: print( sess.run( img ).shape ) #直接初始化就可以 ,轉換成四維報錯誤,不知道為什麼,若誰想明白,請留言 報錯誤 #InvalidArgumentError (see above for traceback): Input shape axis 0 must equal 4,got shape [5]
Databae的操作:
import tensorflow as tf import glob import os ''' Dataset 批量讀取圖片: 原因: 1. 先定義圖片名的list,存放在Dataset中 from_tensor_slices() 2. 對映函式, 在函式中,對list中的圖片進行讀取,和resize,細節 tf.read_file(filename) 返回的是三維的,因為這個每次取出一張圖片,放進佇列中的,不需要轉化為四維 然後對圖片進行resize,然後每個batch進行訪問這個函式 ,所以get_next() 返回的是 [batch,c ] 3. 進行shuffle,batch repeat的設定 4. iterator = dataset.make_one_shot_iterator() 設定迭代器 5. iterator.get_next() 獲取每個batch的圖片 ''' def _parse_function(filename): # print(filename) image_string = tf.read_file(filename) image_decoded = tf.image.decode_image(image_string) #(375,3) ''' Tensor` with type `uint8` with shape `[height,width,num_channels]` for BMP,JPEG,and PNG images and shape `[num_frames,height,3]` for GIF images. ''' # image_resized = tf.image.resize_images(label,[200,200]) ''' images 三維,四維的都可以 images: 4-D Tensor of shape `[batch,channels]` or 3-D Tensor of shape `[height,channels]`. size: A 1-D int32 Tensor of 2 elements: `new_height,new_width`. The new size for the images. ''' image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded,200) # return tf.squeeze(mage_resized,axis=0) return image_resized filenames = glob.glob( os.path.join('../pascal/VOCdevkit/VOC2012/JPEGImages',"*." + 'jpg') ) dataset = tf.data.Dataset.from_tensor_slices((filenames)) dataset = dataset.map(_parse_function) dataset = dataset.shuffle(10).batch(2).repeat(10) iterator = dataset.make_one_shot_iterator() img = iterator.get_next() with tf.Session() as sess: # print( sess.run(img).shape ) #(4,3) for _ in range (10): print( sess.run(img).shape )
以上這篇淺談tensorflow中Dataset圖片的批量讀取及維度的操作詳解就是小編分享給大家的全部內容了,希望能給大家一個參考,也希望大家多多支援我們。