【问题标题】:How do I use 'MapDataset' as input for ImageDataGenerator?如何使用“MapDataset”作为 ImageDataGenerator 的输入?
【发布时间】:2021-07-26 00:45:12
【问题描述】:

我编写了一个代码,它将图像作为输入并估计 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, (height, width,3,))
    
    #normalize pixels in the input image
    image = image/127.5
    image -=1
    
    return image, annotation

【问题讨论】:

  • 能否请你把整个错误,看看错误是从哪里来的?
  • 回溯(最近一次调用最后):文件“...my_code.py”,第 247 行,在 training_dataset = get_training_dataset(training_image_paths, training_label_map_paths) 文件“...my_code.py” ,第 174 行,get_training_dataset training_dataset = datagen.flow(training_dataset) 文件“C:\Users\pub\conda\envs\tensorflow\lib\site-packages\keras_preprocessing\image\image_data_generator.py”,第 434 行,流 dtype =self.dtype
  • 文件“C:\Users\pub\.conda\envs\tensorflow\lib\site-packages\keras_preprocessing\image\numpy_array_iterator.py”,第 121 行,在 init self.x = np.asarray(x, dtype=self.dtype) 文件“C:\Users\pub\.conda\envs\tensorflow\lib\site-packages\numpy\core_asarray.py”,第 83 行,在asarray return array(a, dtype, copy=False, order=order) TypeError: float() argument must be a string or a number, not 'MapDataset'
  • 你正在投射的 label_map 里面有什么? label_map = np.asarray(label_map).astype('float32')
  • 有 29 列,数字介于 (0,1) 之间,定义了训练照片中每个类别的百分比覆盖率。所有列的总和为 1。

标签: python tensorflow keras tensor image-augmentation


【解决方案1】:

截至今天,我的问题的解决方案是完全避免使用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,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, (height, width,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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-26
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多