-
Notifications
You must be signed in to change notification settings - Fork 0
/
data_serialize.py
186 lines (134 loc) · 5.49 KB
/
data_serialize.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
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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
# encoding='utf-8'
#import sys
#sys.path.remove('/opt/ros/kinetic/lib/python2.7/dist-packages')
import numpy as np
import os
from imutils import paths
import imageio
import cv2
import random
import progressbar
import json
# classes
from data_to_hdf5 import HDF5writer
# neccessary parameters
from parameters import palette
from parameters import img_path, label_path, hdf5_path, dataset_mean
from parameters import image_shape, width, height, batchSize, bufferSize
from parameters import dataset_length, hdf5_val_path
from parameters import val_size, img_val_path, label_val_path
# get paths to images and labels
imagePaths = list(paths.list_images(img_path))
labelPaths = list(paths.list_images(label_path))
# shuffle
c = list(zip(imagePaths, labelPaths))
random.shuffle(c)
(imagePaths, labelPaths) = zip(*c)
# create or delete validation directory depending on needs
val_exist = os.path.isdir('./dataset/validation')
if val_exist:
training_split_img = imagePaths
training_split_label = labelPaths
val_imagePaths = list(paths.list_images(img_val_path))
val_labelPaths = list(paths.list_images(label_val_path))
# shuffle
c = list(zip(val_imagePaths, val_labelPaths))
random.shuffle(c)
(val_split_img, val_split_label) = zip(*c)
else:
training_split_img = imagePaths[val_size:]
training_split_label = labelPaths[val_size:]
val_split_img = imagePaths[:val_size]
val_split_label = labelPaths[:val_size]
# calculate input dimensions
img_dims = (len(training_split_img), height*width*3)
label_dims = (len(training_split_label), height*width*1)
v_img_dims = (len(val_split_img), height*width*3)
v_label_dims = (len(val_split_label), height*width*1)
# initialize lists to store the MEAN VALUES of the dataset
(R, G, B) = ([], [], [])
# Create an instance of the HDF5 serializer class
writer = HDF5writer(img_dims, label_dims, hdf5_path)
# TRAINING DATA
# initialize ProgressBar
widgets = ["Converting training data into HDF5:", progressbar.Percentage(), " ",
progressbar.Bar(), " ", progressbar.ETA()]
pbar = progressbar.ProgressBar(maxval=len(training_split_img), widgets=widgets).start()
for i in np.arange(0, len(training_split_img), batchSize):
batchImages = training_split_img[i:i + batchSize]
batchLabels = training_split_label[i:i + batchSize]
# lists to store the batches during loop
batch_img = []
batch_label = []
for (j, imagePath) in enumerate(batchImages):
img = imageio.imread(imagePath, as_gray=False, pilmode="RGB")
img_array = cv2.resize(img, (512, 288))
# mean claculation for actual image
(b, g, r) = cv2.mean(img_array)[:3]
R.append(r)
G.append(g)
B.append(b)
img_array.reshape(-1, height, width, 3)
batch_img.append(img_array)
for (k, labelPath) in enumerate(batchLabels):
label = imageio.imread(labelPath, as_gray=False, pilmode="RGB")
label = cv2.resize(label, (width, height))
int_array = np.ndarray(shape=(height, width), dtype=int)
int_array[:,:] = 0
# rgb to integer
for rgb, idx in palette.items():
int_array[(label==rgb).all(2)] = idx
int_array.reshape(-1, height, width, 1)
batch_label.append(int_array)
batch_img = np.array(batch_img).reshape(-1, height*width*3)
batch_label = np.array(batch_label).reshape(-1, height*width*1)
# using the add() function from HDF5 writer class to serialize
writer.add(batch_img, batch_label)
# updating the ProgressBar
pbar.update(i)
pbar.finish()
writer.close()
print("[INFO] TRAINING Dataset serialized successfully!")
print("[INFO] serializing means...")
# saving mean values
D = {"R": np.mean(R), "G": np.mean(G), "B": np.mean(B)}
f = open(dataset_mean, "w")
f.write(json.dumps(D))
f.close()
print("[INFO] RGB mean values are calculated and saved across the dataset.")
val_writer = HDF5writer(v_img_dims, v_label_dims, hdf5_val_path)
# VALIDATION DATA
# initialize ProgressBar
widgets = ["Converting validation data into HDF5:", progressbar.Percentage(), " ",
progressbar.Bar(), " ", progressbar.ETA()]
pbar = progressbar.ProgressBar(maxval=len(val_split_img), widgets=widgets).start()
for i in np.arange(0, len(val_split_img), batchSize):
v_batchImages = val_split_img[i:i + batchSize]
v_batchLabels = val_split_label[i:i + batchSize]
# lists to store the batches during loop
v_batch_img = []
v_batch_label = []
for (j, imagePath) in enumerate(v_batchImages):
img = imageio.imread(imagePath, as_gray=False, pilmode="RGB")
img_array = cv2.resize(img, (512, 288))
img_array.reshape(-1, height, width, 3)
v_batch_img.append(img_array)
for (k, labelPath) in enumerate(v_batchLabels):
label = imageio.imread(labelPath, as_gray=False, pilmode="RGB")
label = cv2.resize(label, (width, height))
int_array = np.ndarray(shape=(height, width), dtype=int)
int_array[:,:] = 0
# rgb to integer
for rgb, idx in palette.items():
int_array[(label==rgb).all(2)] = idx
int_array.reshape(-1, height, width, 1)
v_batch_label.append(int_array)
v_batch_img = np.array(v_batch_img).reshape(-1, height*width*3)
v_batch_label = np.array(v_batch_label).reshape(-1, height*width*1)
# using the add() function from HDF5 writer class to serialize
val_writer.add(v_batch_img, v_batch_label)
# updating the ProgressBar
pbar.update(i)
pbar.finish()
val_writer.close()
print("[INFO] VALIDATION Dataset serialized successfully!")