-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathload_data.py
More file actions
197 lines (137 loc) · 6.1 KB
/
load_data.py
File metadata and controls
197 lines (137 loc) · 6.1 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
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
187
188
189
190
191
192
193
194
195
196
import numpy as np
import json
import matplotlib
matplotlib.use("TkAgg")
from PIL import Image
import random
import itertools
def load_data_feautre_train(feautre="" ,root_path = "/Users/HongDam/PycharmProjects/theanoTest/image/",
image_index_filename = "new_dic_for_search.json",
image_size=(28, 28),
dtype='float32'):
with open(root_path + image_index_filename, 'r') as json_file:
feautre_dic = json.load(json_file)
if feautre_dic.get(feautre) == None:
print("err : No match feautre")
return
feautre_list = feautre_dic[unicode(feautre)]
#feauter_list : list of filename
#unnecessary
del feautre_dic[unicode(feautre)]
another_list = list(itertools.chain.from_iterable(feautre_dic.values()))
another_list = list(set(another_list) - set(feautre_list))
random.shuffle(another_list)
random.shuffle(feautre_list)
# add symbol
feautre_list = ["1" + x for x in feautre_list]
# anoter list : feauter list = 8 : 2
another_list = another_list[:len(feautre_list)]
train_index_list = feautre_list[:len(feautre_list)//2] + another_list[:len(another_list)//2]
valid_index_list = feautre_list[len(feautre_list)//2:-len(feautre_list)//4] + another_list[len(another_list)//2:-len(another_list)//4]
test_index_list = feautre_list[-len(feautre_list)//4:] + another_list[-len(another_list)//4:]
random.shuffle(train_index_list)
random.shuffle(valid_index_list)
random.shuffle(test_index_list)
X_train = []
y_train = []
X_valid = []
y_valid = []
X_test = []
y_test = []
def load_image_and_label(filename, dtype='float32'):
if filename[0] == "1":
label = 1
filename = filename[1:]
else:
label = 0
image = Image.open(open(root_path + filename))
width, height = image.size
if width != image_size[0] or height != image_size[1]:
image = image.resize((image_size[0], image_size[1]), Image.ANTIALIAS)
image = np.asarray(image, dtype=dtype) / 256
image = image.reshape(1, 3, image_size[0], image_size[1])
return image.astype(dtype), label
for index in train_index_list:
image, label = load_image_and_label(index, dtype)
X_train.append(image)
y_train.append(label)
X_train = np.concatenate(X_train, axis=0)
y_train = np.asarray(y_train)
for index in valid_index_list:
image, label = load_image_and_label(index, dtype)
X_valid.append(image)
y_valid.append(label)
X_valid = np.concatenate(X_valid, axis=0)
y_valid = np.asarray(y_valid)
for index in test_index_list:
image, label = load_image_and_label(index, dtype)
X_test.append(image)
y_test.append(label)
X_test = np.concatenate(X_test, axis=0)
y_test = np.asarray(y_test)
# print X_train.shape, y_train.shape, X_test.shape, y_test.shape, X_valid.shape, y_valid.shape
return X_train, y_train.astype('int32'), X_valid, y_valid.astype('int32'), X_test, y_test.astype('int32')
def load_dataset(root_path = "/Users/HongDam/PycharmProjects/theanoTest/image/",
image_index_filename = "new_dic_for_training_list.json",
categories_range = [(0, 6997), (6998,12510), (12511,19317), (19318,25029), (25030,28576),(28577,41483)],
image_size = (28,28),
dtype = 'float32'):
category_list = ['knit', 'outer', 'pants', 'shirts', 'suit', 'tee']
with open(root_path + image_index_filename, 'r') as json_file:
image_filename = json.load(json_file)
def load_image_and_label(filename, dtype='float32'):
image = Image.open(open(root_path + filename))
#resize image
width, height = image.size
if width != image_size[0] or height != image_size[1]:
image = image.resize((image_size[0],image_size[1]), Image.ANTIALIAS)
image = np.asarray(image, dtype=dtype)/256
image = image.reshape(1,3,image_size[0],image_size[1])
#create label
label = category_list.index(filename.split('/')[0])
#labal = np.asarray([labal])
return image.astype(dtype), label
## make random index list
## train, test, valid
train_index_list = []
valid_index_list = []
test_index_list = []
for i in range(len(category_list)):
shuffle_list = range(categories_range[i][0], categories_range[i][1] + 1)
random.shuffle(shuffle_list)
valid_index_list.append(shuffle_list[:len(shuffle_list)/12])
train_index_list.append(shuffle_list[len(valid_index_list[i]):])
# merge
valid_index_list = list(itertools.chain.from_iterable(valid_index_list))
train_index_list = list(itertools.chain.from_iterable(train_index_list))
# shuffle
random.shuffle(valid_index_list)
random.shuffle(train_index_list)
valid_index_list, test_index_list = valid_index_list[:-1000], valid_index_list[-1000:]
#train_index_list, valid_index_list, test_index_list = train_index_list[:50], train_index_list[50:60],train_index_list[60:70]
X_train = []
y_train = []
X_valid = []
y_valid = []
X_test = []
y_test = []
for index in train_index_list:
image, label = load_image_and_label(image_filename[index], dtype)
X_train.append(image)
y_train.append(label)
X_train = np.concatenate(X_train, axis=0)
y_train = np.asarray(y_train)
for index in valid_index_list:
image, label = load_image_and_label(image_filename[index], dtype)
X_valid.append(image)
y_valid.append(label)
X_valid = np.concatenate(X_valid, axis=0)
y_valid = np.asarray(y_valid)
for index in test_index_list:
image, label = load_image_and_label(image_filename[index], dtype)
X_test.append(image)
y_test.append(label)
X_test = np.concatenate(X_test, axis=0)
y_test = np.asarray(y_test)
#print X_train.shape, y_train.shape, X_test.shape, y_test.shape, X_valid.shape, y_valid.shape
return X_train, y_train.astype('int32'), X_valid, y_valid.astype('int32'), X_test, y_test.astype('int32')