-
Notifications
You must be signed in to change notification settings - Fork 0
/
Data_loader.py
70 lines (55 loc) · 2.6 KB
/
Data_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
from tensorflow.keras.preprocessing.image import ImageDataGenerator
from tensorflow.keras.utils import to_categorical
class DataLoader:
def train_image_gen(image_tile_dir, mask_tile_dir, batch_size=16, seed=42):
datagen = ImageDataGenerator(
horizontal_flip=True,
vertical_flip=True,
fill_mode='reflect',
validation_split=0.2)
train_image_generator = datagen.flow_from_directory(
directory=image_tile_dir,
color_mode='rgb',
target_size = (128, 128),
class_mode=None,
shuffle=True,
seed=42,
subset='training')
train_mask_generator = datagen.flow_from_directory(
directory=mask_tile_dir,
color_mode='grayscale',
target_size = (128, 128),
class_mode=None,
shuffle=True,
seed=42,
subset='training')
train_generator = zip(train_image_generator, train_mask_generator)
for (img, mask) in train_generator:
mask = to_categorical(mask, num_classes=2)
yield (img, mask)
def val_image_gen(image_tile_dir, mask_tile_dir, batch_size=16, seed=42):
datagen = ImageDataGenerator(
horizontal_flip=True,
vertical_flip=True,
fill_mode='reflect',
validation_split=0.2)
val_image_generator = datagen.flow_from_directory(
directory=image_tile_dir,
color_mode='rgb',
target_size = (128, 128),
class_mode=None,
shuffle=True,
seed=42,
subset='validation')
val_mask_generator = datagen.flow_from_directory(
directory=mask_tile_dir,
color_mode='grayscale',
target_size = (128, 128),
class_mode=None,
shuffle=True,
seed=42,
subset='validation')
val_generator = zip(val_image_generator, val_mask_generator)
for (img, mask) in val_generator:
mask = to_categorical(mask, num_classes=2)
yield (img, mask)