-
Notifications
You must be signed in to change notification settings - Fork 21
Expand file tree
/
Copy pathdata_generator.py
More file actions
115 lines (85 loc) · 3.49 KB
/
data_generator.py
File metadata and controls
115 lines (85 loc) · 3.49 KB
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import os
import random
import cv2 as cv
import numpy as np
from keras.utils import Sequence
from keras.utils import to_categorical
from config import batch_size, img_rows, img_cols, num_classes, color_map
train_images_folder = 'data/instance-level_human_parsing/Training/Images'
train_categories_folder = 'data/instance-level_human_parsing/Training/Category_ids'
valid_images_folder = 'data/instance-level_human_parsing/Validation/Images'
valid_categories_folder = 'data/instance-level_human_parsing/Validation/Category_ids'
def get_category(categories_folder, name):
filename = os.path.join(categories_folder, name + '.png')
semantic = cv.imread(filename, 0)
return semantic
def to_bgr(y_pred):
ret = np.zeros((img_rows, img_cols, 3), np.float32)
for r in range(320):
for c in range(320):
color_id = y_pred[r, c]
# print("color_id: " + str(color_id))
ret[r, c, :] = color_map[color_id]
ret = ret.astype(np.uint8)
return ret
def random_choice(image_size):
height, width = image_size
crop_height, crop_width = 320, 320
x = random.randint(0, max(0, width - crop_width))
y = random.randint(0, max(0, height - crop_height))
return x, y
def safe_crop(mat, x, y):
crop_height, crop_width = 320, 320
if len(mat.shape) == 2:
ret = np.zeros((crop_height, crop_width), np.float32)
else:
ret = np.zeros((crop_height, crop_width, 3), np.float32)
crop = mat[y:y + crop_height, x:x + crop_width]
h, w = crop.shape[:2]
ret[0:h, 0:w] = crop
return ret
class DataGenSequence(Sequence):
def __init__(self, usage):
self.usage = usage
if usage == 'train':
id_file = 'data/instance-level_human_parsing/Training/train_id.txt'
self.images_folder = train_images_folder
self.categories_folder = train_categories_folder
else:
id_file = 'data/instance-level_human_parsing/Validation/val_id.txt'
self.images_folder = valid_images_folder
self.categories_folder = valid_categories_folder
with open(id_file, 'r') as f:
self.names = f.read().splitlines()
np.random.shuffle(self.names)
def __len__(self):
return int(np.ceil(len(self.names) / float(batch_size)))
def __getitem__(self, idx):
i = idx * batch_size
length = min(batch_size, (len(self.names) - i))
batch_x = np.empty((length, img_rows, img_cols, 3), dtype=np.float32)
batch_y = np.empty((length, img_rows, img_cols, num_classes), dtype=np.float32)
for i_batch in range(length):
name = self.names[i]
filename = os.path.join(self.images_folder, name + '.jpg')
image = cv.imread(filename)
image_size = image.shape[:2]
category = get_category(self.categories_folder, name)
x, y = random_choice(image_size)
image = safe_crop(image, x, y)
category = safe_crop(category, x, y)
if np.random.random_sample() > 0.5:
image = np.fliplr(image)
category = np.fliplr(category)
x = image / 255.
y = category
batch_x[i_batch, :, :, 0:3] = x
batch_y[i_batch, :, :] = to_categorical(y, num_classes)
i += 1
return batch_x, batch_y
def on_epoch_end(self):
np.random.shuffle(self.names)
def train_gen():
return DataGenSequence('train')
def valid_gen():
return DataGenSequence('valid')