簡単にtenssorflowの中で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  ) #         ,        ,      ,     ,       
  #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 )
以上のような浅談tenssorflowの中でDatasetの画像の大量読み取りと次元操作の詳細は小編集が皆さんに提供した内容の全部です。