-
Notifications
You must be signed in to change notification settings - Fork 3
/
test_student.py
111 lines (77 loc) · 3.28 KB
/
test_student.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
import load_alexnet
import student
import numpy as np
import pytest
# relative error threshold
THRESH = 1e-2
#### LOAD ALEXNET ####
# Fix the PYTHONPATH before importing caffe
import os, sys
caffe_root = os.path.expanduser('~/caffe/')
sys.path.insert(0, caffe_root + 'python')
import caffe
net, transformer = load_alexnet.load_alexnet(caffe_root, gpu=False)
def _load(fname):
""" Helper to load compressed .npz arrays """
return np.load(fname)['arr_0']
def relative_error(expected, actual, epsilon=1e-5):
""" Mean relative error between two arrays """
return np.mean(
np.abs(expected - actual) /
np.maximum(np.abs(actual), epsilon)
)
def test_compute_dscore_dimage():
data = _load('expected/data.npz')
expected = _load('expected/compute_dscore_dimage-grad.npz')
net.blobs['data'].data[0, ...] = data
net.forward()
actual = student.compute_dscore_dimage(net, data, 254)
# relative error must be small
assert relative_error(expected, actual) < THRESH
def test_normalized_sgd_with_momentum_update():
data = _load('expected/data.npz')
grad = _load('expected/normalized_sgd_with_momentum_update-grad.npz')
velocity = _load('expected/normalized_sgd_with_momentum_update-velocity.npz')
expected_data = _load('expected/normalized_sgd_with_momentum_update-new_data.npz')
expected_velocity = _load('expected/normalized_sgd_with_momentum_update-new_velocity.npz')
momentum = 0.99
learning_rate = 100
new_data, new_velocity = student.normalized_sgd_with_momentum_update(
data, grad, velocity, momentum, learning_rate)
assert relative_error(expected_data, new_data) < THRESH
assert relative_error(expected_velocity, new_velocity) < THRESH
def test_fooling_image_gradient():
data = _load('expected/data.npz')
expected = _load('expected/fooling_image_gradient-grad.npz')
orig_data = _load('expected/fooling_image_gradient-orig_data.npz')
net.blobs['data'].data[0, ...] = data
net.forward()
target_class = 113
regularization = 1e-3
actual = student.fooling_image_gradient(
net, orig_data, data, target_class, regularization)
# relative error must be small
assert relative_error(expected, actual) < THRESH
def test_class_visualization_gradient():
cur_data = _load('expected/class_visualization_gradient-cur_data.npz')
expected = _load('expected/class_visualization_gradient-grad.npz')
net.blobs['data'].data[0, ...] = cur_data
net.forward()
target_class = 234
regularization = 1e-3
actual = student.class_visualization_gradient(
net, cur_data, target_class, regularization)
# relative error must be small
assert relative_error(expected, actual) < THRESH
def test_feature_inversion_gradient():
blob_name = 'conv3'
regularization = 2e-3
target_feat = _load('expected/feature_inversion_gradient-target_feat.npz')
cur_data = _load('expected/feature_inversion_gradient-cur_data.npz')
expected = _load('expected/feature_inversion_gradient-grad.npz')
net.blobs['data'].data[0, ...] = cur_data
net.forward(end=blob_name)
actual = student.feature_inversion_gradient(
net, cur_data, blob_name, target_feat, regularization)
# relative error must be small
assert relative_error(expected, actual) < THRESH