tensorflow 에서 Dataset 그림 의 대량 읽 기 및 차원 에 대한 작업

3419 단어 tensorflow
3 차원 읽 기 그림 (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   )  #         ,        ,      ,     ,        
    #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, w, h, 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, 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 )


좋은 웹페이지 즐겨찾기