如何使用“MapDataset”作为 ImageDataGenerator 的输入?

问题描述

我编写了一个代码,将图像作为输入,并将 29 个类的估计值作为输出。 该代码在没有数据增强的情况下运行良好。但是,我无法让它为 TensorFlow 模型训练增加图像。 这是错误

TypeError: float() argument must be a string or a number,not 'MapDataset'

下面是获取我的训练图像及其相应标签函数一个 29 列的数组)。

如果您有任何想法/建议,我将不胜感激。

def get_training_dataset(image_paths,label_map_paths):
'''
  Prepares shuffled batches of the training set.
  
  Args:
    image_paths (list of strings) -- paths to each image file in the train set
    label_map_paths (list of strings) -- paths to each label map in the train set

  Returns:
    tf Dataset containing the preprocessed train set
'''
  training_dataset = tf.data.Dataset.from_tensor_slices((image_paths,label_map_paths))
  training_dataset = training_dataset.map(map_filename_to_image_and_mask)

  datagen = ImageDataGenerator(
    featurewise_center=True,featurewise_std_normalization=True,rotation_range=10,width_shift_range=0.1,height_shift_range=0.1,shear_range = 0.1,horizontal_flip=True,vertical_flip=True)
    
  training_dataset = datagen.flow(training_dataset)  
  return training_dataset

这是我在上面的函数中使用的另外两个函数

def get_dataset_slice_paths(image_dir,image_list):
  '''
  generates the lists of image 
  
  Args:
    image_dir (string) -- path to the input images directory
    image_file_list -- list of the input images (to be used for filtering the data from csv file)

  Returns:
    image_paths (list of strings) -- paths to each image file

  '''
  image_paths = [os.path.join(image_dir,fname) for fname in image_list]
  label_map = np.empty([0,29])
  for fname in image_list:
      label_map = np.append(label_map,ESI_data[ESI_data['FileName']== fname].iloc[:,6:35],axis=0)
  label_map = np.asarray(label_map).astype('float32')
  return image_paths,label_map

def map_filename_to_image_and_mask(t_filename,label_map):
    '''  
    Preprocesses the dataset by:
    * resizing the input image
    * normalizing the input image pixels

     
    Args:
    t_filename (string) -- path to the raw input image
    label_map (array) -- a 29-column array with values for each class

    Returns:
    image (tensor) -- preprocessed image
    annotation -- fraction cover of each species as tensor 

    '''
    #convert images and mask files to tensor
    img_raw = tf.io.read_file(t_filename)
    image = tf.image.decode_jpeg(img_raw)
    annotation = tf.convert_to_tensor(label_map,dtype= tf.float32)
    
    #resize image and segmentation mask
    image = tf.image.resize(image,(height,width,))

    image = tf.reshape(image,3,))
    
    #normalize pixels in the input image
    image = image/127.5
    image -=1
    
    return image,annotation

解决方法

截至今天,我的问题的解决方案是完全避免使用 MapDataset。 这是修改后的 get_training_dataset 函数:

def get_training_dataset(image_paths,label_map_paths):
'''
  Prepares shuffled batches of the training set.
  
  Args:
    image_paths (list of strings) -- paths to each image file in the train set
    label_map_paths (list of strings) -- paths to each label map in the train set

  Returns:
    tf Dataset containing the preprocessed train set
  '''
  training_dataset_img,training_dataset_lab = map_filename_to_training_dataset(image_paths,label_map_paths)
  
  datagen = ImageDataGenerator(
    rotation_range=10,width_shift_range=0.2,height_shift_range=0.2,zoom_range = [0.95,1.05],shear_range = 0.5,fill_mode = "reflect",horizontal_flip=True,vertical_flip =True)
  training_dataset = datagen.flow(training_dataset_img,training_dataset_lab,batch_size=BATCH_SIZE,shuffle=True)
  
  return training_dataset
def map_filename_to_training_dataset(t_filename,label_map):
    
    '''  
    Preprocesses the dataset by:
    * resizing the input image
    * normalizing the input image pixels

     
    Args:
    t_filename (string) -- path to the raw input image
    label_map (array) -- a 29-column array with values for each class

      Returns:
    image (tensor) -- preprocessed image
    annotation -- fraction cover of each species as tensor 

    '''
    i=0
    image_set = np.empty((len(label_map),224,3))
    annotation_set = list()
    for fname in t_filename:
    #convert images and mask files to tensor
        annotation = tf.convert_to_tensor(label_map[i],dtype= tf.float32)
        img_raw = tf.io.read_file(fname)
        image = tf.image.decode_jpeg(img_raw)
           
        #resize image and segmentation mask
        image = tf.image.resize(image,(height,width,))

        image = tf.reshape(image,3,))
    
        #normalize pixels in the input image
        image = image/127.5
        image -=1
        image_set[i,:,:] =  image
        annotation_set.append(annotation)
        i+=1
    
    return image_set,annotation_set

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...