-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathaccuracy_w2v.py
163 lines (139 loc) · 5.39 KB
/
accuracy_w2v.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
# Get the accuracy of a trained w2v net
import numpy as np
import getpass
import sys
import h5py
#import w2v_classifier
# Check the username, so the same code can work on all of our computers
user = getpass.getuser()
if user == 'ctnuser':
caffe_root = '/home/ctnuser/bjkomer/caffe/'
root = '/home/ctnuser/bjkomer/semantic-network/'
elif user == 'bjkomer':
caffe_root = '/home/bjkomer/caffe/'
root = '/home/bjkomer/semantic-network/'
sys.path.insert(0, caffe_root + 'python')
import caffe
"""
if user == 'ctnuser':
caffe.set_mode_gpu()
else:
caffe.set_mode_cpu()
"""
caffe.set_mode_cpu()
# load images and output labels
DIM = 200
NUM_TRAIN = 50000
NUM_TEST = 10000
data_prefix = 'data/cifar_100_caffe_hdf5/'
fnametrain = data_prefix + 'train.h5'
fnametest = data_prefix + 'test.h5'
fnametrain_label = data_prefix + 'train_w2v_label.h5'
fnametest_label = data_prefix + 'test_w2v_label.h5'
# Training
ftrain = h5py.File(fnametrain, 'r')
ftrain_w2v = h5py.File(fnametrain_label, 'r')
train_label = np.zeros((NUM_TRAIN)) # integer labels
train_image = np.zeros((NUM_TRAIN, 3, 32, 32)) # actual images
train_w2v_label = np.zeros((NUM_TRAIN, DIM)) # word2vec labels
print("Loading Training labels and images")
"""
for i, label in enumerate(ftrain['label_fine']):
train_label[i] = label
for i, image in enumerate(ftrain['data']):
train_image[i,:] = image
for i, w2v_label in enumerate(ftrain_w2v['label_w2v']):
train_w2v_label[i,:] = w2v_label
"""
print("Loading Fine Labels")
train_label = ftrain['label_fine'][()]
print("Loading Images")
train_image = ftrain['data'][()]
print("Loading Word2Vec Labels")
train_w2v_label = ftrain_w2v['label_w2v'][()]
# Testing
ftest = h5py.File(fnametest, 'r')
ftest_w2v = h5py.File(fnametest_label, 'r')
test_label = np.zeros((NUM_TEST))
test_image = np.zeros((NUM_TEST, 3, 32, 32))
test_w2v_label = np.zeros((NUM_TEST, DIM))
print("Loading Testing labels and images")
"""
for i, label in enumerate(ftest['label_fine']):
test_label[i] = label
for i, image in enumerate(ftest['data']):
test_image[i,:] = image
for i, w2v_label in enumerate(ftest_w2v['label_w2v']):
test_w2v_label[i,:] = w2v_label
"""
print("Loading Fine Labels")
test_label = ftest['label_fine'][()]
print("Loading Images")
test_image = ftest['data'][()]
print("Loading Word2Vec Labels")
test_w2v_label = ftest_w2v['label_w2v'][()]
# Get all of the unique label vectors
print("Finding unique label vector set")
b = np.ascontiguousarray(test_w2v_label).view(np.dtype((np.void, test_w2v_label.dtype.itemsize * test_w2v_label.shape[1])))
_, idx = np.unique(b, return_index=True)
all_vectors = test_w2v_label[idx]
TOL = 0.0001
# returns 1 if output is closer to vector than to all other vectors
def check_match(output, vector):
best_diff = None
best_vector = None
for v in all_vectors:
diff = np.linalg.norm(output - v)
if best_diff is None or diff < best_diff:
best_diff = diff
best_vector = v
# check that the vector is the same within some numerical tolerance
if np.linalg.norm(vector - best_vector) < TOL:
return 1
else:
return 0
print("Setting up caffe network")
########################
# Set up caffe network #
########################
net = caffe.Net(root + 'net/cifar100_w2v_deploy.prototxt',
root + 'net_output/cifar100_w2v_snapshot_iter_150000.caffemodel',
caffe.TEST)
"""
# input preprocessing: 'data' is the name of the input blob == net.inputs[0]
transformer = caffe.io.Transformer({'data': net.blobs['data'].data.shape})
transformer.set_transpose('data', (2,0,1))
transformer.set_mean('data', np.load(caffe_root + 'python/caffe/imagenet/ilsvrc_2012_mean.npy').mean(1).mean(1)) # mean pixel
transformer.set_raw_scale('data', 255) # the reference model operates on images in [0,255] range instead of [0,1]
transformer.set_channel_swap('data', (2,1,0)) # the reference model has channels in BGR order instead of RGB
"""
batch_size = 100
output_layer = 'ip_c'
net.blobs['data'].reshape(batch_size,3,32,32) # Cifar100 uses 32x32
# run caffe network on test images
test_result = np.zeros(NUM_TEST)
for batch in range(int(NUM_TEST / batch_size)):
net.blobs['data'].data[...] = test_image[batch*batch_size:(batch+1)*batch_size,:,:,:]
print("Running Testing Batch %i of %i" % (batch, int(NUM_TEST / batch_size)))
out = net.forward()
for bi in range(batch_size):
output = net.blobs[output_layer].data[bi]
test_result[batch*batch_size+bi] = check_match(output, test_w2v_label[batch*batch_size+bi,:])
# get closest label vector to the output vector
#label = w2v_classifier.classify(output)
test_accuracy = np.mean(test_result)
print("Test Accuracy: %f" % test_accuracy)
# run caffe network on training images
train_result = np.zeros(NUM_TRAIN)
for batch in range(int(NUM_TRAIN / batch_size)):
net.blobs['data'].data[...] = train_image[batch*batch_size:(batch+1)*batch_size,:,:,:]
print("Running Testing Batch %i of %i" % (batch, int(NUM_TRAIN / batch_size)))
out = net.forward()
for bi in range(batch_size):
output = net.blobs[output_layer].data[bi]
train_result[batch*batch_size+bi] = check_match(output, train_w2v_label[batch*batch_size+bi,:])
# get closest label vector to the output vector
#label = w2v_classifier.classify(output)
train_accuracy = np.mean(train_result)
print("Train Accuracy: %f" % train_accuracy)
print("Test Accuracy: %f" % test_accuracy)