I have a dataset that consists of images saved as npy files and masks also saved as npy files.
I would like to train a UNet model on this dataset and to do so I created the following data generator using keras.utils.Sequence.
class CloudDataGen(tf.keras.utils.Sequence):
def __init__(
self,
list_ids,
batch_size,
input_size=(512, 512),
num_channels=3,
shuffle=True,
mode=None):
self.list_ids = list_ids
self.batch_size = batch_size
self.input_size = input_size
self.num_channels = num_channels
self.shuffle = shuffle
self.mode = mode
self.on_epoch_end()
def on_epoch_end(self):
self.indexes = np.arange(len(self.list_ids))
if self.shuffle:
np.random.shuffle(self.indexes)
def __len__(self):
return int(np.floor(len(self.list_ids) / self.batch_size))
def __data_generation(self, ids):
x = np.empty((self.batch_size, *self.input_size, self.num_channels), dtype=int)
y = np.empty((self.batch_size, *self.input_size, 1), dtype=int)
for i, ID in enumerate(ids):
if self.mode == 'train':
image_dir = TRAIN_SOURCES + TRAIN_SOURCE_NAME + ID + '/image.npy'
else:
image_dir = TEST_SOURCES + TEST_SOURCE_NAME + ID + '/image.npy'
curr = np.load(image_dir)
x[i,] = curr.astype(int)
if self.mode == 'train':
img = TRAIN_LABELS + TRAIN_LABEL_NAME + ID + '/labels.npy'
else:
img = TEST_LABELS + TEST_LABEL_NAME + ID + '/labels.npy'
y[i] = np.load(img)
return x, y
def __getitem__(self, index):
indexes = self.indexes[index * self.batch_size:(index + 1) * self.batch_size]
list_ids_current_batch = [self.list_ids[k] for k in indexes]
x, y = self.__data_generation(list_ids_current_batch)
return x, y
I then instantiate two generators, one for training and one for validation and invoke the fit method like this
unet_model.fit(train_generator,
validation_data=validation_generator,
epochs=NUM_EPOCHS,
steps_per_epoch=STEPS
)
Unfortunately on my MacBook Pro it seems that the program is stuck at the first step of the fit phase and nothing else happens. Does anyone know why it is not working correctly? How can I fix this problem? Thanks!