-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinput_data.py
More file actions
377 lines (290 loc) · 12.2 KB
/
Copy pathinput_data.py
File metadata and controls
377 lines (290 loc) · 12.2 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
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
import os.path
import io
import urllib2
from httplib import HTTPException
from ssl import CertificateError
from PIL import Image
from resizeimage import resizeimage
import numpy as np
# Minimum size will eliminate single pixel and flickr missing photo images
MINIMUM_FILE_SIZE = 5000
IMAGENET_LINKS_URL = "http://www.image-net.org/api/text/imagenet.synset.geturls?wnid="
IMAGE_DIRECTORY = "ImageNet_data/"
URL_DIRECTORY = os.path.join(IMAGE_DIRECTORY, "bad_urls")
RAW_IMAGE_HEIGHT = 256
RAW_IMAGE_WIDTH = 256
IMAGE_HEIGHT = 224
IMAGE_WIDTH = 224
def is_good_url(url, class_id):
"""
Checks if the given url is not on the class's bad url list
:param url: url to be checked
:param class_id: ImageNet class is, used to find appropriate list of bad urls
:return: True if not on bad url list, False otherwise
"""
if not os.path.exists(URL_DIRECTORY):
return True
file_name = class_id + ".txt"
file_path = os.path.join(URL_DIRECTORY, file_name)
if not os.path.exists(file_path):
return True
if url in open(file_path).read():
return False
return True
def store_bad_url(url, class_id):
"""
Stores the given url in the class's bad url file
:param url: url to be blacklisted
:param class_id: class that url corresponds to
:return:
"""
if not os.path.exists(URL_DIRECTORY):
os.mkdir(URL_DIRECTORY)
file_name = class_id + ".txt"
file_path = os.path.join(URL_DIRECTORY, file_name)
with open(file_path, "a") as urls_file:
urls_file.write(url)
def download_image(url, download_path):
"""
Downloads a single image from a url to a specific path
:param url: url of image
:param download_path: full path of saved image file
:return: true if successfully downloaded, false otherwise
"""
print "Downloading from " + url
try:
fd = urllib2.urlopen(url, timeout=3)
image_file = io.BytesIO(fd.read())
image = Image.open(image_file)
size = image.size
if size[0] < IMAGE_WIDTH or size[1] < IMAGE_HEIGHT: # Image too small
return False
resized = resizeimage.resize_cover(image, (RAW_IMAGE_WIDTH, RAW_IMAGE_HEIGHT))
resized.save(download_path, 'jpeg', icc_profile=resized.info.get('icc_profile'))
except (IOError, HTTPException, CertificateError, resizeimage.ImageSizeError) as e:
print e
return False
# Check if photo meets minimum size requirement
size = os.path.getsize(download_path)
if size < MINIMUM_FILE_SIZE:
os.remove(download_path)
print "Invalid Image: " + url
return False
# Try opening as array to see if there are any errors
try:
load_image_as_array(download_path)
except ValueError as e:
os.remove(download_path)
return False
return True
def download_class_images(class_id, num_images, work_directory):
"""
Downloads images of the corresponding class and puts them in a folder
:param class_id: ImageNet id of the class, name of folder
:param num_images: Maximum number of images to download
:param work_directory: Directory where all image class folders are kept
"""
if not os.path.exists(work_directory):
os.mkdir(work_directory)
class_folder_path = os.path.join(work_directory, class_id)
if not os.path.exists(class_folder_path):
os.mkdir(class_folder_path)
links_url = IMAGENET_LINKS_URL + class_id
previous_images = os.listdir(class_folder_path)
images = len(previous_images)
print "{0} images found for class {1}".format(images, class_id)
if images >= num_images:
return
for url in urllib2.urlopen(links_url):
if images >= num_images:
break
url = url[:url.find('?')] # remove all query strings
if not is_good_url(url, class_id):
continue
image_name = url.rsplit('/')[-1]
image_name = image_name.strip('\n\r')
download_path = os.path.join(class_folder_path, image_name)
if ".gif" in image_name:
continue
if os.path.isfile(download_path):
continue
if download_image(url, download_path):
images += 1
print images
else:
store_bad_url(url, class_id)
print "{0} total images for {1}".format(images, class_id)
def download_dataset(class_ids, num_images):
"""
Downloads and resizes images from the specified class ids and stores them in the work directory
:param class_ids: list of ImageNet ids
:param num_images: maximum number of images to download in each set
"""
for class_id in class_ids:
print "Starting download for " + class_id
download_class_images(class_id, num_images, IMAGE_DIRECTORY)
def load_image_as_array(filepath):
"""
Loads a single image and returns it as an array
:param filepath: path to image file
:return: array of image with size IMAGE_WIDTH*IMAGE_HEIGHT*3
"""
im = Image.open(filepath)
if len(np.shape(im)) is 2:
array = np.empty((RAW_IMAGE_HEIGHT, RAW_IMAGE_WIDTH, 3), dtype=np.uint8)
array[:, :, :] = np.array(im)[:, :, np.newaxis]
else:
array = np.array(im)
return array.astype(np.float32)
def create_one_hot_vector(index, length):
"""
Creates a one-hot vector with that specified length and a 1 at the specified index
:param index: index of 1 in vector
:param length: length of vector
:return: one-hot vector
"""
assert length > 0, "One-hot vector length must be a positive number"
assert 0 <= index < length, "Index (%s) must be between 0 and length(%s)" % (index, length)
vector = np.zeros(length)
vector[index] = 1
return vector
def load_all_images(class_ids, num_images):
"""
Loads images from the given classes and returns them in an array, along with a list of one-hot vector labels
:param class_ids: ImageNet ids of classes to be retrieved
:param num_images: maximum number of images to return per class, actual number may be smaller
:return: list of images for each class, list of labels
"""
download_dataset(class_ids, num_images)
num_classes = len(class_ids)
all_images = []
all_labels = []
for index, class_id in enumerate(class_ids):
class_path = os.path.join(IMAGE_DIRECTORY, class_id)
files = [f for f in os.listdir(class_path) if os.path.isfile(os.path.join(class_path, f))]
num_class_files = min(len(files), num_images)
for n in range(0, num_class_files):
image = load_image_as_array(os.path.join(class_path, files[n]))
all_images.append(image)
all_labels.append(create_one_hot_vector(index, num_classes))
return np.array(all_images), np.array(all_labels)
def transform_images(images, randomize = False):
"""
Takes a list of images and gives each random augmentations. Images may be flipped horizontally and randomly cropped
to final size
:param images: list of images
:return: list of augmented images
"""
assert IMAGE_WIDTH <= RAW_IMAGE_WIDTH
assert IMAGE_HEIGHT <= RAW_IMAGE_HEIGHT
transformed = []
images = images.reshape(images.shape[0], RAW_IMAGE_HEIGHT, RAW_IMAGE_WIDTH, 3)
for i in range(0, len(images)):
image = images[i]
if randomize:
left_padding = np.random.randint(0, RAW_IMAGE_WIDTH - IMAGE_WIDTH)
top_padding = np.random.randint(0, RAW_IMAGE_HEIGHT - IMAGE_HEIGHT)
cropped_image = image[top_padding:top_padding + IMAGE_HEIGHT, left_padding:left_padding + IMAGE_WIDTH]
if np.random.ranf() <= 0.5:
cropped_image = cropped_image[:, ::-1, :]
else:
left_padding = (RAW_IMAGE_WIDTH - IMAGE_WIDTH)/2
top_padding = (RAW_IMAGE_HEIGHT - IMAGE_HEIGHT)/2
cropped_image = image[top_padding:top_padding + IMAGE_HEIGHT, left_padding:left_padding + IMAGE_WIDTH]
transformed.append(cropped_image)
transformed = np.asarray(transformed)
return transformed.reshape(transformed.shape[0], IMAGE_HEIGHT*IMAGE_WIDTH, 3)
class DataSet(object):
def __init__(self, images, labels):
"""Construct a DataSet using the given images and labels
"""
assert images.shape[0] == labels.shape[0], (
'images.shape: %s labels.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, 3] (assuming depth == 3)
assert images.shape[3] == 3
images = images.reshape(images.shape[0],
images.shape[1] * images.shape[2], 3)
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, random_crop=False):
"""Return the next `batch_size` examples from this data set.
Images are cropped to final image size by selecting a random sample"""
assert batch_size <= self._num_examples
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
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
end = self._index_in_epoch
raw_images = self._images[start:end]
return transform_images(raw_images, randomize=random_crop), self._labels[start:end]
def create_datasets(class_ids, num_samples=1000, val_fraction=0.1, test_fraction=0.1):
"""
Creates training, validation, and test datasets from the given class ids using the desired proportions
:param class_ids: ImageNet class ids of all classes to include
:param num_samples: maximum sample images for each class
:param val_fraction: fraction of images to put into validation set
:param test_fraction: fraction of images to put into test set
:return: training_set, validation_set, test_dataset
"""
assert 0 <= val_fraction <= 0.25, "Validation fraction %s must be between 0 and 0.25" % val_fraction
assert 0 <= test_fraction <= 0.25, "Test fraction %s must be between 0 and 0.25" % test_fraction
all_images, all_labels = load_all_images(class_ids, num_samples)
total_num_images = len(all_images)
# Shuffle all images before splitting
perm = np.arange(total_num_images)
np.random.shuffle(perm)
all_images = all_images[perm]
all_labels = all_labels[perm]
validation_size = int(total_num_images * val_fraction)
test_size = int(total_num_images * test_fraction)
validation_images = all_images[:validation_size]
validation_labels = all_labels[:validation_size]
test_images = all_images[validation_size:validation_size + test_size]
test_labels = all_labels[validation_size:validation_size + test_size]
train_images = all_images[validation_size + test_size:]
train_labels = all_labels[validation_size + test_size:]
# Mean normalization
training_mean = np.mean(train_images)
train_images -= training_mean
validation_images -= training_mean
test_images -= training_mean
# Std dev normalization
training_std_dev = np.std(train_images)
train_images /= training_std_dev
validation_images /= training_std_dev
test_images /= training_std_dev
train_dataset = DataSet(train_images, train_labels)
validation_dataset = DataSet(validation_images, validation_labels)
test_dataset = DataSet(test_images, test_labels)
return train_dataset, validation_dataset, test_dataset
def main():
images, labels = load_all_images(["n02084071"], 10)
transformed = transform_images(images)
if __name__ == "__main__":
main()