淺談tensorflow中Dataset圖片的批量讀取及維度的操作詳解
三維的讀取圖片(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, h, 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, 500, 3)
image_decoded = tf.expand_dims(image_decoded, axis=0)
image_resized = tf.image.resize_image_with_crop_or_pad(image_decoded, 200, 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, 200) 這種四維 形式是可以的
with tf.Session() as sess:
print( sess.run( img ).shape ) #直接初始化就可以 ,轉(zhuǎn)換成四維報(bào)錯(cuò)誤,不知道為什么,若誰想明白,請留言 報(bào)錯(cuò)誤
#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. 映射函數(shù), 在函數(shù)中,對(duì)list中的圖片進(jìn)行讀取,和resize,細(xì)節(jié)
tf.read_file(filename) 返回的是三維的,因?yàn)檫@個(gè)每次取出一張圖片,放進(jìn)隊(duì)列中的,不需要轉(zhuǎn)化為四維
然后對(duì)圖片進(jìn)行resize, 然后每個(gè)batch進(jìn)行訪問這個(gè)函數(shù) ,所以get_next() 返回的是 [batch, w, h, c ]
3. 進(jìn)行shuffle , batch repeat的設(shè)置
4. iterator = dataset.make_one_shot_iterator() 設(shè)置迭代器
5. iterator.get_next() 獲取每個(gè)batch的圖片
'''
def _parse_function(filename):
# print(filename)
image_string = tf.read_file(filename)
image_decoded = tf.image.decode_image(image_string) #(375, 500, 3)
'''
Tensor` with type `uint8` with shape `[height, width, num_channels]` for
BMP, JPEG, and PNG images and shape `[num_frames, height, width, 3]` for
GIF images.
'''
# image_resized = tf.image.resize_images(label, [200, 200])
''' images 三維,四維的都可以
images: 4-D Tensor of shape `[batch, height, width, channels]` or
3-D Tensor of shape `[height, width, 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, 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, 200, 200, 3)
for _ in range (10):
print( sess.run(img).shape )
以上這篇淺談tensorflow中Dataset圖片的批量讀取及維度的操作詳解就是小編分享給大家的全部內(nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
flask SQLAlchemy連接數(shù)據(jù)庫及操作的實(shí)現(xiàn)
本文主要介紹了flask SQLAlchemy連接數(shù)據(jù)庫及操作的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-03-03
在Python中利用Into包整潔地進(jìn)行數(shù)據(jù)遷移的教程
這篇文章主要介紹了在Python中如何利用Into包整潔地進(jìn)行數(shù)據(jù)遷移,在數(shù)據(jù)格式的任意兩個(gè)格式之間高效地遷移數(shù)據(jù),需要的朋友可以參考下2015-03-03
Python使用Pandas對(duì)csv文件進(jìn)行數(shù)據(jù)處理的方法
這篇文章主要介紹了Python使用Pandas對(duì)csv文件進(jìn)行數(shù)據(jù)處理的方法,本文通過實(shí)例代碼相結(jié)合給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-08-08
Python中數(shù)字以及算數(shù)運(yùn)算符的相關(guān)使用
這篇文章主要介紹了Python中數(shù)字以及算數(shù)運(yùn)算符的相關(guān)使用,是Python入門學(xué)習(xí)中的基礎(chǔ)知識(shí),需要的朋友可以參考下2015-10-10
Python中標(biāo)準(zhǔn)庫array數(shù)組操作舉例詳解
這篇文章主要介紹了Python中標(biāo)準(zhǔn)庫array數(shù)組操作的相關(guān)資料,Python的array模塊提供了固定類型數(shù)組類,用于高效存儲(chǔ)同類型元素,節(jié)省內(nèi)存并支持?jǐn)?shù)值計(jì)算,需要的朋友可以參考下2025-04-04

