NotImplementedError error using custom generator

235 Views Asked by At

I use custom generator for training my data. It should inherit keras.utils.Sequence and should have defined such methods: init,len,on_epoch_end,getitem. when I fit my model "NotImplemented Error" occurred. I know its about one of these overrided function but I dont know how can I handle it

class DataGenerator(tf.keras.utils.Sequence):
  def __init__(self, root_dir=r'../data/val_test', image_folder='img/', mask_folder='masks/', 
               batch_size=4, image_size=288, nb_y_features=1, 
               augmentation=None,
               suffle=True):
      # self.image_filenames = listdir_fullpath(os.path.join(root_dir, image_folder))
      self.image_filenames = np.sort([os.path.join(os.path.join(root_dir, image_folder), f)
       for f in os.listdir(os.path.join(root_dir, image_folder))])

      # self.mask_names = listdir_fullpath(os.path.join(root_dir, mask_folder))
      self.mask_names = np.sort([os.path.join(os.path.join(root_dir, mask_folder), f)
       for f in os.listdir(os.path.join(root_dir, mask_folder))])

      self.batch_size = batch_size
      self.augmentation = augmentation
      self.image_size = image_size
      self.nb_y_features = nb_y_features
      self.suffle = suffle
        
  # def listdir_fullpath(d):
  #   return np.sort([os.path.join(d, f) for f in os.listdir(d)])

  def __getitem__(self, index):
    data_index_min = int(index*self.batch_size)
    data_index_max = int(min((index+1)*self.batch_size, len(self.image_filenames)))

    indexes = self.image_filenames[data_index_min:data_index_max]
    this_batch_size = len(indexes) # The last batch can be smaller than the others

    X = np.empty((this_batch_size, self.image_size, self.image_size, 3), dtype=np.float32)
    y = np.empty((this_batch_size, self.image_size, self.image_size, self.nb_y_features), dtype=np.uint8)

    for i, sample_index in enumerate(indexes):
      X_sample, y_sample = self.read_image_mask(self.image_filenames[index * self.batch_size + i], 
                                                self.mask_names[index * self.batch_size + i])

      #if augmentation is defined, we assume its a train set
      if self.augmentation is not None:

      # Augmentation code
        augmented = self.augmentation(self.image_size)(image=X_sample, mask=y_sample)
        image_augm = augmented['image']
        mask_augm = augmented['mask'].reshape(self.image_size, self.image_size, self.nb_y_features)

        # divide by 255 to normalize images from 0 to 1
        X[i, ...] = image_augm/255
        y[i, ...] = mask_augm/255
      else:
          ...
    
    return X,y

history = model.fit(train_generator,
                    epochs=EPOCHS,
                    steps_per_epoch = spe_train,
                    callbacks=callbacks,
                    validation_data = validation_generator,
                    validation_steps=spe_val)

this is error:

NotImplementedError                       Traceback (most recent call last)
<ipython-input-36-fa9c887c02c7> in <module>
     17                     callbacks=callbacks,
     18                     validation_data = validation_generator,
---> 19                     validation_steps=spe_val)

1 frames
/usr/local/lib/python3.7/dist-packages/keras/utils/data_utils.py in __len__(self)
    489         The number of batches in the Sequence.
    490     """
--> 491     raise NotImplementedError
    492 
    493   def on_epoch_end(self):

NotImplementedError: 
0

There are 0 best solutions below