-
Notifications
You must be signed in to change notification settings - Fork 0
/
imagenet.py
101 lines (82 loc) · 3.66 KB
/
imagenet.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
import time
import imageio
import numpy as np
from tensorflow.keras.preprocessing.image import ImageDataGenerator
path = './tiny-imagenet-200/'
def get_id_dictionary():
id_dict = {}
for i, line in enumerate(open( path + 'wnids.txt', 'r')):
id_dict[line.replace('\n', '')] = i
return id_dict
def get_class_to_id_dict():
id_dict = get_id_dictionary()
all_classes = {}
result = {}
for i, line in enumerate(open( path + 'words.txt', 'r')):
n_id, word = line.split('\t')[:2]
all_classes[n_id] = word
for key, value in id_dict.items():
result[value] = (key, all_classes[key])
return result
def get_data(id_dict):
print('starting loading data')
train_data, test_data = [], []
train_labels, test_labels = [], []
t = time.time()
for key, value in id_dict.items():
train_data += [imageio.imread( path + 'train/{}/images/{}_{}.JPEG'.format(key, key, str(i)), pilmode='RGB') for i in range(500)]
train_labels_ = np.array([[0]*200]*500)
train_labels_[:, value] = 1
#print(train_labels_.shape)
train_labels += train_labels_.tolist()
#print(np.array(train_labels)[-1])
for line in open( path + 'val/val_annotations.txt'):
img_name, class_id = line.split('\t')[:2]
test_data.append(imageio.imread( path + 'val/images/{}'.format(img_name) ,pilmode='RGB'))
test_labels_ = np.array([[0]*200])
test_labels_[0, id_dict[class_id]] = 1
test_labels += test_labels_.tolist()
print('finished loading data, in {} seconds'.format(time.time() - t))
return np.array(train_data), np.array(train_labels), np.array(test_data), np.array(test_labels)
train_data, train_labels, test_data, test_labels = get_data(get_id_dictionary())
print( "train data shape: ", train_data.shape )
print( "train label shape: ", train_labels.shape )
print( "test data shape: ", test_data.shape )
print( "test_labels.shape: ", test_labels.shape )
def shuffle_data(train_data, train_labels ):
size = len(train_data)
train_idx = np.arange(size)
np.random.shuffle(train_idx)
return train_data[train_idx], train_labels[train_idx]
train_data, train_labels = shuffle_data(train_data, train_labels)
# input image dimensions
img_rows, img_cols = 64, 64
# The images are RGB
img_channels = 3
# The data, shuffled and split between train and test sets:
X_train = train_data
Y_train = train_labels
X_test = test_data
Y_test = test_labels
X_train = X_train.astype('float32')
X_test = X_test.astype('float32')
# subtract mean and normalize
mean_image = np.mean(X_train, axis=0)
X_train -= mean_image
X_test -= mean_image
X_train /= 128.
X_test /= 128.
datagen = ImageDataGenerator(
featurewise_center=False, # set input mean to 0 over the dataset
samplewise_center=False, # set each sample mean to 0
featurewise_std_normalization=False,# divide inputs by std of the dataset
samplewise_std_normalization=False, # divide each input by its std
zca_whitening=False, # apply ZCA whitening
rotation_range=0, # randomly rotate images in the range (degrees, 0 to 180)
width_shift_range=0.1, # randomly shift images horizontally (fraction of total width)
height_shift_range=0.1, # randomly shift images vertically (fraction of total height)
horizontal_flip=True, # randomly flip images
vertical_flip=False ) # randomly flip images
# Compute quantities required for featurewise normalization
# (std, mean, and principal components if ZCA whitening is applied).
datagen.fit(X_train)