Home > OS >  Why do my train_generator and val_generator produce the same picture?
Why do my train_generator and val_generator produce the same picture?

Time:03-06

I set aside a validation split like below:

val_samples = 60
train_imgs = coco_imgs[:-val_samples]
train_masks = coco_masks[:-val_samples]
val_imgs = coco_imgs[-val_samples:]
val_masks = coco_masks[-val_samples:]

My train_imgs and val_imgs show different pictures:

fig, ax = plt.subplots(ncols =2, figsize = (10,3), sharex = True, sharey = True)
ax[0].imshow(train_imgs[14])
ax[1].imshow(val_imgs[14])

Then I write data generator function:

class DataGenerator(keras.utils.Sequence):
  def __init__(self, input_img, input_mask, image_size, 
               augmentation, batch_size):
    self.image_size = img_size
    self.augmentation = augmentation
    self.batch_size = batch_size
    self.input_img = train_imgs
    self.input_mask = train_masks

  def __len__(self):
        return len(self.input_img) // self.batch_size

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

     indexes = self.input_img[data_index_min:data_index_max]
     this_batch_size = len(indexes)
     
     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 = self.input_img[index * self.batch_size   i]
          y_sample = self.input_mask[index * self.batch_size   i]
          if self.augmentation is True:
              aug = transform(image = X_sample, mask = y_sample)
              img_aug = aug['image']
              mask_aug = aug['mask']
              X[i, ...] = img_aug/255
              y[i, ...] = mask_aug.reshape(self.image_size , self.image_size , self.nb_y_features).astype(np.uint8)
          else:
              pass
     return X, y

Here is my train_generator and val_generator.

train_generator = DataGenerator(input_img = train_imgs, input_mask = train_masks, image_size = img_size,
                                augmentation=True, batch_size = 5)
val_generator = DataGenerator(input_img = val_imgs, input_mask = val_masks, image_size = img_size,
                                augmentation=True, batch_size = 5)

They show the same picture with train_imgs.

for i in range(3):
  X_sample_temp, y_sample_temp = train_generator[2]
  fig, ax = plt.subplots(ncols=2)
  ax[0].imshow(X_sample_temp[4])
  ax[1].imshow(y_sample_temp[4,:,:,0])
  plt.show()

and

for i in range(3):
  X_sample_temp, y_sample_temp = val_generator[2]
  fig, ax = plt.subplots(ncols=2)
  ax[0].imshow(X_sample_temp[4])
  ax[1].imshow(y_sample_temp[4,:,:,0])
  plt.show()

I want val_generator produces same pictures with val_imgs, but I don't know how to fix it. I appreciate any inputs.

CodePudding user response:

In __init__ you have hardcoded

    self.input_img  = train_imgs
    self.input_mask = train_masks

so all generators use the same train_imgs train_masks
but you should use parameters input_img and input_mask

    self.input_img  = input_img
    self.input_mask = input_mask

I'm not sure if it is not typo in self.image_size = img_size
because it should be self.image_size = image_size

  • Related