This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
image_path = 'path to images' | |
mask_path = 'path to masks' | |
training_generator = DataGenerator(train_idx, labels, image_path, mask_path) | |
validation_generator = DataGenerator(val_idx, labels, image_path, mask_path) | |
# Design model | |
model = Sequential() | |
[...] # Architecture | |
model.compile() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __getitem__(self, index): | |
'Generate one batch of data' | |
# Generate indexes of the batch | |
indexes = self.indexes[index*self.batch_size: (index+1)*self.batch_size] | |
# Find list of IDs | |
list_IDs_temp = [self.list_IDs[k] for k in indexes] | |
# Generate data | |
X = self._generate_X(list_IDs_temp) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def on_epoch_end(self): | |
'Updates indexes after each epoch' | |
self.indexes = np.arange(len(self.list_IDs)) | |
if self.shuffle == True: | |
np.random.shuffle(self.indexes) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def __len__(self): | |
'Denotes the number of batches per epoch' | |
return int(np.floor(len(self.list_IDs) / self.batch_size)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DataGenerator(Sequence): | |
'Generates data for Keras' | |
def __init__(self, list_IDs, labels, image_path, mask_path, | |
to_fit=True, batch_size=32, dim=(256,256), | |
n_channels=1, n_classes=10, shuffle=True): | |
'Initialization' | |
self.list_IDs = list_IDs | |
self.labels = labels | |
self.image_path = image_path | |
self.mask_path = mask_path |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
model.fit_generator( | |
train_generator, | |
steps_per_epoch=2000, | |
epochs=50, | |
validation_data=validation_generator, | |
validation_steps=800) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from tensorflow.keras.preprocessing.image import ImageDataGenerator | |
train_datagen = ImageDataGenerator( | |
rescale=1./255, | |
shear_range=0.2, | |
zoom_range=0.2, | |
horizontal_flip=True) | |
test_datagen = ImageDataGenerator(rescale=1./255) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import numpy as np | |
import cv2 | |
from tensorflow.keras.utils import Sequence | |
class DataGenerator(Sequence): | |
"""Generates data for Keras | |
Sequence based data generator. Suitable for building data generator for training and prediction. | |
""" | |
def __init__(self, list_IDs, labels, image_path, mask_path, |