-
Notifications
You must be signed in to change notification settings - Fork 41
/
data_generator.py
63 lines (51 loc) · 1.83 KB
/
data_generator.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
"""
Data Generator
"""
import os
import numpy as np
import cv2
from tensorflow.keras.utils import Sequence
def parse_image(img_path, image_size):
image_rgb = cv2.imread(img_path, 1)
h, w, _ = image_rgb.shape
if (h == image_size) and (w == image_size):
pass
else:
image_rgb = cv2.resize(image_rgb, (image_size, image_size))
image_rgb = image_rgb/255.0
return image_rgb
def parse_mask(mask_path, image_size):
mask = cv2.imread(mask_path, -1)
h, w = mask.shape
if (h == image_size) and (w == image_size):
pass
else:
mask = cv2.resize(mask, (image_size, image_size))
mask = np.expand_dims(mask, -1)
mask = mask/255.0
return mask
class DataGen(Sequence):
def __init__(self, image_size, images_path, masks_path, batch_size=8):
self.image_size = image_size
self.images_path = images_path
self.masks_path = masks_path
self.batch_size = batch_size
self.on_epoch_end()
def __getitem__(self, index):
if(index+1)*self.batch_size > len(self.images_path):
self.batch_size = len(self.images_path) - index*self.batch_size
images_path = self.images_path[index*self.batch_size : (index+1)*self.batch_size]
masks_path = self.masks_path[index*self.batch_size : (index+1)*self.batch_size]
images_batch = []
masks_batch = []
for i in range(len(images_path)):
## Read image and mask
image = parse_image(images_path[i], self.image_size)
mask = parse_mask(masks_path[i], self.image_size)
images_batch.append(image)
masks_batch.append(mask)
return np.array(images_batch), np.array(masks_batch)
def on_epoch_end(self):
pass
def __len__(self):
return int(np.ceil(len(self.images_path)/float(self.batch_size)))