-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy pathinput_data.py
162 lines (138 loc) · 5.95 KB
/
input_data.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
import os
import numpy as np
from PIL import Image as im
import csv
def dense_to_one_hot(labels_dense):
"""Convert class labels from scalars to one-hot vectors."""
num_classes = int(np.amax(labels_dense) + 1)
num_labels = labels_dense.shape[0]
index_offset = np.arange(num_labels) * num_classes
labels_one_hot = np.zeros((num_labels, num_classes))
labels_one_hot.flat[index_offset + labels_dense.ravel()] = 1
return labels_one_hot
class DataSet(object):
def __init__(self, images, labels, one_hot=False):
#images_2 is series
assert images.shape[0] == labels.shape[0], ('images_1.shape: %s labels_1.shape: %s' % (images.shape, labels.shape))
self._num_examples = images.shape[0]
# Convert shape from [num examples, rows, columns, depth]
# to [num examples, rows*columns] (assuming depth == 1)
# assert images.shape[3] == 1
#images = images.reshape(images.shape[0], images.shape[1] * images.shape[2] * images.shape[3])
# Convert from [0, 255] -> [-1.0, 1.0].
images = images.astype(np.float32)
images = np.multiply(images, 1.0 / 127.5) - 1.
self._images = images
self._labels = labels
self._epochs_completed = 0
self._index_in_epoch = 0
@property
def images(self):
return self._images
@property
def labels(self):
return self._labels
@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
print("epoch " + str(self._epochs_completed))
# Shuffle the data
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
return self._images[start:end], self._labels[start:end]
def load_data_from_pickle(data_file, label_file, image_shape):
import pickle
print(data_file)
output = open(data_file, 'rb')
labels = pickle.load(output)
images = pickle.load(output)
output.close()
images = np.reshape(images, (np.shape(labels)[0], image_shape[0], image_shape[1], image_shape[2]))
return images, labels
def load_data(data_file, label_file, image_shape, onehot):
print(data_file)
images = np.genfromtxt(data_file, delimiter=' ')
labels = np.genfromtxt(label_file, usecols=(1), delimiter=' ')
if onehot:
labels = dense_to_one_hot(labels.astype(int))
return images, labels
def load_data_from_file(data_file, label_file, image_shape, onehot):
print(data_file)
labelsall = np.genfromtxt(label_file, delimiter=' ', dtype=None)
labelsshape = np.shape(labelsall)
images = np.zeros((labelsshape[0], image_shape[0], image_shape[1], image_shape[2]))
labels = np.zeros((labelsshape[0]))
count = 0
for line in labelsall:
labels[count] = line[1]
imagefile = im.open(data_file + line[0].decode("utf-8"))
imagefile = imagefile.convert('RGB')
images[count] = np.array(imagefile)
imagefile.close()
if count % 1000 == 0:
print(count)
count += 1
if onehot:
labels = dense_to_one_hot(labels.astype(int))
return images, labels
def read_data_sets(train_file, train_label, shape, test_file="", test_label="", test_ratio=0.1, validation_ratio=0.0, pickle=True, boring=False, onehot=False):
class DataSets(object):
pass
data_sets = DataSets()
if (pickle):
train_images, train_labels = load_data_from_pickle(train_file, train_label, shape)
if test_file:
test_images, test_labels = load_data_from_pickle(test_file, test_label, shape)
else:
test_size = int(test_ratio * float(train_labels.shape[0]))
test_images = train_images[:test_size]
test_labels = train_labels[:test_size]
train_images = train_images[test_size:]
train_labels = train_labels[test_size:]
elif(boring):
train_images, train_labels = load_data_from_file(train_file, train_label, shape, onehot)
if test_file:
test_images, test_labels = load_data_from_file(test_file, test_label, shape, onehot)
else:
test_size = int(test_ratio * float(train_labels.shape[0]))
test_images = train_images[:test_size]
test_labels = train_labels[:test_size]
train_images = train_images[test_size:]
train_labels = train_labels[test_size:]
else:
train_images, train_labels = load_data(train_file, train_label, shape, onehot)
if test_file:
test_images, test_labels = load_data(test_file, test_label, shape, onehot)
else:
test_size = int(test_ratio * float(train_labels.shape[0]))
test_images = train_images[:test_size]
test_labels = train_labels[:test_size]
train_images = train_images[test_size:]
train_labels = train_labels[test_size:]
validation_size = int(validation_ratio * float(train_labels.shape[0]))
validation_images = train_images[:validation_size]
validation_labels = train_labels[:validation_size]
train_images = train_images[validation_size:]
train_labels = train_labels[validation_size:]
data_sets.train = DataSet(train_images, train_labels)
data_sets.validation = DataSet(validation_images, validation_labels)
data_sets.test = DataSet(test_images, test_labels)
print("data loaded")
return data_sets