-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
180 lines (139 loc) · 5.68 KB
/
dataset.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
import os
import glob
import numpy as np
import cv2
from sklearn.utils import shuffle
def load_train(train_path, image_size, classes):
images = []
images_path = []
labels = []
ids = []
cls = []
print('Reading training images')
for fld in classes: # assuming data directory has a separate folder for each class, and that each folder is named after the class
index = classes.index(fld)
print('Loading {} files (Index: {})'.format(fld, index))
path = os.path.join(train_path, fld, '*g')
files = glob.glob(path)
for fl in files:
images_path.append(fl)
# image = cv2.imread(fl)
# image = cv2.resize(image, (image_size, image_size), cv2.INTER_LINEAR)
# images.append(image)
label = np.zeros(len(classes))
label[index] = 1.0
labels.append(label)
flbase = os.path.basename(fl)
ids.append(flbase)
cls.append(fld)
#images = np.array(images)
labels = np.array(labels)
ids = np.array(ids)
cls = np.array(cls)
return images_path, labels, ids, cls
def load_test(test_path, image_size):
path = os.path.join(test_path, '*g')
files = sorted(glob.glob(path))
X_test = []
X_test_id = []
print("Reading test images")
for fl in files:
flbase = os.path.basename(fl)
img = cv2.imread(fl)
img = cv2.resize(img, (image_size, image_size), cv2.INTER_LINEAR)
X_test.append(img)
X_test_id.append(flbase)
### because we're not creating a DataSet object for the test images, normalization happens here
X_test = np.array(X_test, dtype=np.uint8)
X_test = X_test.astype('float32')
X_test = X_test / 255
return X_test, X_test_id
class DataSet(object):
def __init__(self, images_path, image_size, labels, ids, cls):
"""Construct a DataSet. one_hot arg is used only if fake_data is true."""
self._num_examples = len(images_path)
# Convert shape from [num examples, rows, columns, depth]
# to [num examples, rows*columns] (assuming depth == 1)
# Convert from [0, 255] -> [0.0, 1.0].
#images = images.astype(np.float32)
#images = np.multiply(images, 1.0 / 255.0)
self._image_size = image_size
self._images_path = images_path
self._labels = labels
self._ids = ids
self._cls = cls
self._epochs_completed = 0
self._index_in_epoch = 0
@property
def images_path(self):
return self._images_path
@property
def labels(self):
return self._labels
@property
def ids(self):
return self._ids
@property
def cls(self):
return self._cls
@property
def num_examples(self):
return self._num_examples
@property
def epochs_completed(self):
return self._epochs_completed
def next_batch(self, batch_size):
"""Return the next `batch_size` examples from this data set."""
start = self._index_in_epoch
self._index_in_epoch += batch_size
if self._index_in_epoch > self._num_examples:
# Finished epoch
self._epochs_completed += 1
# # Shuffle the data (maybe)
# perm = np.arange(self._num_examples)
# np.random.shuffle(perm)
# self._images = self._images[perm]
# self._labels = self._labels[perm]
# Start next epoch
start = 0
self._index_in_epoch = batch_size
assert batch_size <= self._num_examples
end = self._index_in_epoch
images = []
image_normalize_mean = [0.485, 0.456, 0.406]
image_normalize_std = [0.229, 0.224, 0.225]
for i in range(start, end):
image = cv2.imread(self._images_path[i])
image = cv2.resize(image, (self._image_size, self._image_size), cv2.INTER_LINEAR)
images.append(image)
images = np.array(images)
images = images.astype(np.float32)
# Normalization and Standardization
images = np.multiply(images, 1.0 / 255.0)
for image in images:
for channel in range(3) :
image[:, :, channel] -= image_normalize_mean[channel]
image[:, :, channel] /= image_normalize_std[channel]
return images, self._labels[start:end], self._ids[start:end], self._cls[start:end]
def read_train_sets(train_path, image_size, classes, validation_size=0):
class DataSets(object):
pass
data_sets = DataSets()
images_path, labels, ids, cls = load_train(train_path, image_size, classes)
images_path, labels, ids, cls = shuffle(images_path, labels, ids, cls) # shuffle the data
if isinstance(validation_size, float):
validation_size = int(validation_size * len(images_path))
validation_images_path = images_path[:validation_size]
validation_labels = labels[:validation_size]
validation_ids = ids[:validation_size]
validation_cls = cls[:validation_size]
train_images_path = images_path[validation_size:]
train_labels = labels[validation_size:]
train_ids = ids[validation_size:]
train_cls = cls[validation_size:]
data_sets.train = DataSet(train_images_path, image_size, train_labels, train_ids, train_cls)
data_sets.valid = DataSet(validation_images_path, image_size, validation_labels, validation_ids, validation_cls)
return data_sets
def read_test_set(test_path, image_size):
images, ids = load_test(test_path, image_size)
return images, ids