-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcub_2011.py
325 lines (246 loc) · 11.6 KB
/
cub_2011.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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# Copyright 2016 The TensorFlow Authors. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Provides data for the flowers dataset.
The dataset scripts used to create the dataset can be found at:
tensorflow/models/research/slim/datasets/download_and_convert_flowers.py
"""
from __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import os
import cv2
import random
def create_dict(img_list, label_list):
"""
args:
img_list: [img_path1, img_path2, ...]
label_list: [label1, label2, ....]
returns:
imgs_dict: {1: [...], 2: [...]
"""
assert len(img_list) == len(label_list), \
"the number of image and label does not match"
img_dict = {}
for img, label in zip(img_list, label_list):
if label not in img_dict:
img_dict[label] = [img]
else:
img_dict[label].append(img)
return img_dict, len(img_dict.keys())
def create_triplet(img_dict, batch_size, class_num):
# image dict
batch_label = np.random.permutate(class_num)
batch_label = batch_label[:batch_size]
def create_dataset(train_img_list, train_label_list, \
test_img_list, test_label_list, batch_size):
"""Gets a dataset tuple with instructions for reading flowers.
Args:
split_name: A train/validation split name.
dataset_dir: The base directory of the dataset sources.
file_pattern: The file pattern to use when matching the dataset sources.
It is assumed that the pattern contains a '%s' string so that the split
name can be inserted.
reader: The TensorFlow reader type.
Returns:
images: a batch of images
labels: a batch of labels
train_init_op: initialize iterator for training data.
test_init_op: initialize iterator for testing data.
Raises:
ValueError: if `split_name` is not a valid train/validation split.
"""
# create dataset for training data
train_dataset = tf.data.Dataset.from_tensor_slices((train_img_list, train_label_list))
# transform dataset
train_dataset = train_dataset.map(map_func=parse_function, num_parallel_calls=8)
# shuffle the dataset
train_dataset = train_dataset.shuffle(buffer_size=6000)
train_dataset = train_dataset.batch(batch_size)
iterator = tf.data.Iterator.from_structure(train_dataset.output_types, \
train_dataset.output_shapes)
images, labels = iterator.get_next()
# create dataset for test data
test_dataset = tf.data.Dataset.from_tensor_slices((test_img_list, test_label_list))
# transform dataset
test_dataset = test_dataset.map(map_func=parse_function, num_parallel_calls=8)
# shuffle the dataset
test_dataset = test_dataset.batch(batch_size)
# initialize iterator
train_init_op = iterator.make_initializer(train_dataset)
test_init_op = iterator.make_initializer(test_dataset)
return images, labels, train_init_op, test_init_op
def generate_list(root_dir, image_txt, train_test_split_txt, label_txt, offset=1):
"""
Args:
root_dir: the root directory for image data.
image_txt: a txt file listing all the image paths.
train_test_split_txt: a txt file listing the image set (training, or testing),
denoting whether the image is for training or testing.
label_txt: a txt file list all the image label.
offset: the started value of label.
Returns:
train_img_list: [train_img_path1, train_img_path2, ...]
train_label_list: [train_label1, train_label2, ....]
test_img_list: [test_img_path1, test_img_path2, ...]
test_label_list: [test_label1, test_label2, ...]
"""
# check the file existence.
# all(): logic and across all the elements in a list
if not (os.path.isdir(root_dir) and all(map(os.path.isfile, \
[image_txt, train_test_split_txt, label_txt]))):
raise IOError("Please check the files or paths existence")
train_img_list = []
train_label_list = [] # the label starts from 1
test_img_list = []
test_label_list = [] # the label starts from 1
# read all the file
# fl_txt is a list for file pointers
# with map(open, [image_txt, train_test_split_txt, label_txt]) as fl_txt:
fl_txt = map(open, [image_txt, train_test_split_txt, label_txt])
# read each file accordingly
for img_path, set_label, label in zip(*fl_txt):
# check if the image id is match, if not, continue
img_path = img_path.rstrip('\n').split(' ')
set_label = set_label.rstrip('\n').split(' ')
label = label.rstrip('\n').split(' ')
if not (img_path[0] == set_label[0] == label[0]):
print(img_path, set_label, label)
continue
# put all the data into list
if set_label[-1] == '1': # train set label is 1
train_img_list.append(os.path.join(root_dir, img_path[-1]))
train_label_list.append(int(label[-1]) - offset)
elif set_label[-1] == '0': # testing set label is 0
test_img_list.append(os.path.join(root_dir, img_path[-1]))
test_label_list.append(int(label[-1]) - offset)
else:
print("The set is unclear")
print(img_path, set_label, label)
continue
# shuffle the training list
merged_list = list(zip(train_img_list, train_label_list))
random.shuffle(merged_list)
train_img_list, train_label_list = zip(*merged_list)
# convert tuple to list
train_img_list, train_label_list = \
list(train_img_list), list(train_label_list)
return train_img_list, train_label_list, test_img_list, test_label_list
def generate_half_split_list(root_dir, image_txt, train_test_split_txt, label_txt, offset=1):
"""
Args:
root_dir: the root directory for image data.
image_txt: a txt file listing all the image paths.
train_test_split_txt: a txt file listing the image set (training, or testing),
denoting whether the image is for training or testing.
label_txt: a txt file list all the image label.
offset: the started value of label.
Returns:
train_img_list: [train_img_path1, train_img_path2, ...]
train_label_list: [train_label1, train_label2, ....]
test_img_list: [test_img_path1, test_img_path2, ...]
test_label_list: [test_label1, test_label2, ...]
"""
# check the file existence.
# all(): logic and across all the elements in a list
if not (os.path.isdir(root_dir) and all(map(os.path.isfile, \
[image_txt, train_test_split_txt, label_txt]))):
raise IOError("Please check the files or paths existence")
train_img_list = []
train_label_list = [] # the label starts from 1
test_img_list = []
test_label_list = [] # the label starts from 1
# read all the file
# fl_txt is a list for file pointers
# with map(open, [image_txt, train_test_split_txt, label_txt]) as fl_txt:
fl_txt = map(open, [image_txt, train_test_split_txt, label_txt])
# read each file accordingly
for img_path, set_label, label in zip(*fl_txt):
# check if the image id is match, if not, continue
img_path = img_path.rstrip('\n').split(' ')
set_label = set_label.rstrip('\n').split(' ')
label = label.rstrip('\n').split(' ')
if not (img_path[0] == set_label[0] == label[0]):
print(img_path, set_label, label)
continue
# put all the data into list
if int(label[-1]) <= 100: # train set with the first half classes
train_img_list.append(os.path.join(root_dir, img_path[-1]))
train_label_list.append(int(label[-1]) - offset)
elif int(label[-1]) <= 200: # testing set using the second half classes
test_img_list.append(os.path.join(root_dir, img_path[-1]))
test_label_list.append(int(label[-1]) - offset - 100)
else:
print("The set is unclear")
print(img_path, set_label, label)
continue
# shuffle the training list
merged_list = list(zip(train_img_list, train_label_list))
random.shuffle(merged_list)
train_img_list, train_label_list = zip(*merged_list)
# convert tuple to list
train_img_list, train_label_list = \
list(train_img_list), list(train_label_list)
print("training image number is {}".format(len(train_img_list)))
print("testing image number is {}".format(len(test_img_list)))
return train_img_list, train_label_list, test_img_list, test_label_list
def parse_function(file_path, label):
"""
Args:
file_path: the full path of a single image
Returns:
image: image tensor
"""
# read image into string
image_string = tf.read_file(file_path)
# decode the string into image, default channels = 3
image_resized = tf.image.resize_images(tf.image.decode_jpeg(image_string, channels=3), (299, 299))
image_resized = tf.divide(tf.cast(image_resized, tf.float32), 255.)
image_rescaled = tf.subtract(image_resized, 0.5)
image_rescaled = tf.multiply(image_rescaled, 2.0)
return image_rescaled, label
if __name__ == '__main__':
import os
os.environ['CUDA_VISIBLE_DEVICES'] = '1'
root_dir = '/raid/Guoxian_Dai/CUB_200_2011/CUB_200_2011/images'
image_txt = '/raid/Guoxian_Dai/CUB_200_2011/CUB_200_2011/images.txt'
train_test_split_txt = '/raid/Guoxian_Dai/CUB_200_2011/CUB_200_2011/train_test_split.txt'
label_txt = '/raid/Guoxian_Dai/CUB_200_2011/CUB_200_2011/image_class_labels.txt'
batch_size = 100
# generating list file
train_img_list, train_label_list, test_img_list, test_label_list \
= generate_list(root_dir, image_txt, train_test_split_txt, label_txt)
# create iterator
images, labels, train_init_op, test_init_op = \
create_dataset(train_img_list, train_label_list, \
test_img_list, test_label_list, batch_size)
config = tf.ConfigProto()
config.gpu_options.allow_growth = True
with tf.Session(config=config) as sess:
print('init train op')
sess.run(train_init_op)
print("training data load")
for i in range(20):
print("The {}-th batch".format(i))
batch_images, batch_labels = sess.run([images, labels])
print('batch_images.shape = {}'.format(batch_images.shape))
print('batch_labels.shape = {}'.format(batch_labels.shape))
print('init test op')
sess.run(test_init_op)
print('testing data load')
for i in range(20):
print('The {}-th batch'.format(i))
batch_images, batch_labels = sess.run([images, labels])
print('batch_images.shape = {}'.format(batch_images.shape))
print('batch_labels.shape = {}'.format(batch_labels.shape))