-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmnist_integrated.py
More file actions
240 lines (191 loc) · 7.82 KB
/
mnist_integrated.py
File metadata and controls
240 lines (191 loc) · 7.82 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
"""Identify mnist digits."""
import argparse
import struct
from pathlib import Path
from typing import Optional, Tuple
import matplotlib.pyplot as plt
import numpy as np
import torch
import torch.nn as nn
import torch.optim as optim
from torch.func import grad
from tqdm import tqdm
from input_opt import CNN
def get_mnist_test_data() -> Tuple[np.ndarray, np.ndarray]:
"""Return the mnist test data set in numpy arrays.
Returns:
(array, array): A touple containing the test
images and labels.
"""
with open("./data/MNIST/t10k-images-idx3-ubyte", "rb") as f:
_, size = struct.unpack(">II", f.read(8))
nrows, ncols = struct.unpack(">II", f.read(8))
data = np.array(np.fromfile(f, dtype=np.dtype(np.uint8).newbyteorder(">")))
img_data_test = data.reshape((size, nrows, ncols))
with open("./data/MNIST/t10k-labels-idx1-ubyte", "rb") as f:
_, size = struct.unpack(">II", f.read(8))
lbl_data_test = np.array(np.fromfile(f, dtype=np.dtype(np.uint8)))
# if gpu:
# return cp.array(img_data_test), cp.array(lbl_data_test)
return img_data_test, lbl_data_test
def get_mnist_train_data() -> Tuple[np.ndarray, np.ndarray]:
"""Load the mnist training data set.
Returns:
(array, array): A touple containing the training
images and labels.
"""
with open("./data/MNIST/train-images-idx3-ubyte", "rb") as f:
_, size = struct.unpack(">II", f.read(8))
nrows, ncols = struct.unpack(">II", f.read(8))
data = np.array(np.fromfile(f, dtype=np.dtype(np.uint8).newbyteorder(">")))
img_data_train = data.reshape((size, nrows, ncols))
with open("./data/MNIST/train-labels-idx1-ubyte", "rb") as f:
_, size = struct.unpack(">II", f.read(8))
lbl_data_train = np.array(np.fromfile(f, dtype=np.dtype(np.uint8)))
# if gpu:
# return cp.array(img_data_train), cp.array(lbl_data_train)
return img_data_train, lbl_data_train
def normalize(
data: np.ndarray, mean: Optional[float] = None, std: Optional[float] = None
) -> Tuple[np.ndarray, float, float]:
"""Normalize the input array.
After normalization the input
distribution should be approximately standard normal.
Args:
data (np.array): The input array.
mean (float): Data mean, re-computed if None.
Defaults to None.
std (float): Data standard deviation,
re-computed if None. Defaults to None.
Returns:
np.array, float, float: Normalized data, mean and std.
"""
if mean is None:
mean = np.mean(data)
if std is None:
std = np.std(data)
return (data - mean) / std, mean, std
def cross_entropy(label: torch.Tensor, out: torch.Tensor) -> torch.Tensor:
"""Compute the cross entropy of one-hot encoded labels and the network output.
Args:
label (torch.Tensor): The image labels of shape [batch_size, 10].
out (torch.Tensor): The network output of shape [batch_size, 10].
Returns:
torch.Tensor, The loss scalar.
"""
left = -label * torch.log(out + 1e-8)
right = -(1 - label) * torch.log(1 - out + 1e-8)
return torch.mean(left + right)
def get_acc(cnn, img_data, label_data):
"""Compute the network accuracy."""
img_data = torch.tensor(img_data).type(torch.float32).cuda()
label_data = torch.tensor(label_data).cuda()
out = cnn(torch.unsqueeze(img_data, 1))
rec = torch.argmax(out, axis=1)
acc = torch.sum((rec == label_data).type(torch.float32)) / len(label_data)
return acc
@torch.no_grad()
def integrate_gradients(net, test_images, output_digit, steps_m=300):
"""Calculate integrated gradients."""
g_list = []
for test_image_x in tqdm(test_images, desc="Integrating Gradients"):
# list for the gradients
step_g_list = []
# TODO: create a black reference image via `zeros_like`` .
# TODO: Loop over the integration steps.
for current_step_k in range(steps_m):
pass
# TODO: compute the input to F from equation 5 in the slides.
# TODO: define a forward pass for torch.func.grad
# TODO: use torch.grad to find the gradient with repsect to the input image.
# TODO: append the gradient to your list
# TODO: Return the sum of the of the list elements.
net.zero_grad()
# TODO: Return the correct output.
return torch.zeros_like(test_images)
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Train Networks on MNIST.")
parser.add_argument("--lr", type=float, default=0.001, help="Learning Rate")
args = parser.parse_args()
print(args)
rng = np.random.default_rng(seed=42)
batch_size = 50
val_size = 500
epochs = 20
img_data_train, lbl_data_train = get_mnist_train_data()
img_data_val, lbl_data_val = img_data_train[:val_size], lbl_data_train[:val_size]
img_data_train, lbl_data_train = (
img_data_train[val_size:],
lbl_data_train[val_size:],
)
img_data_train, mean, std = normalize(img_data_train)
img_data_val, _, _ = normalize(img_data_val, mean, std)
my_file = Path("weights.pth")
if not my_file.is_file():
cnn = CNN().cuda()
opt = optim.Adam(lr=args.lr, params=cnn.parameters())
cost_fun = nn.CrossEntropyLoss()
for e in range(epochs):
shuffled = rng.permutation(len(img_data_train))
img_data_train = img_data_train[shuffled]
lbl_data_train = lbl_data_train[shuffled]
img_batches = np.split(
img_data_train, img_data_train.shape[0] // batch_size, axis=0
)
label_batches = np.split(
lbl_data_train, lbl_data_train.shape[0] // batch_size, axis=0
)
bar = tqdm(
zip(img_batches, label_batches),
total=len(img_batches),
desc="Training CNN",
)
for img_batch, label_batch in bar:
img_batch, label_batch = (
torch.tensor(np_array).cuda()
for np_array in (img_batch, label_batch)
)
img_batch = img_batch.type(torch.float32)
# cel = cross_entropy(nn.one_hot(label_batches[no], num_classes=10),
# out)
y_hat = cnn(torch.unsqueeze(img_batch, 1))
ce_val = cost_fun(y_hat, label_batch)
ce_val.backward()
opt.step()
opt.zero_grad()
bar.set_description("Loss: {:2.3f}".format(ce_val.item()))
print("Epoch: {}, loss: {}".format(e, ce_val.item()))
# train_acc = get_acc(img_data_train, lbl_data_train)
val_acc = get_acc(cnn, img_data_val, lbl_data_val)
print("Validation accuracy: {:3.3f}".format(val_acc))
torch.save(cnn.state_dict(), "weights.pth")
else:
cnn_weights = torch.load(open("weights.pth", "rb"))
cnn = CNN()
cnn.load_state_dict(cnn_weights)
cnn.cuda()
print("Training done. Testing...")
img_data_test, lbl_data_test = get_mnist_test_data()
img_data_test, mean, std = normalize(img_data_test, mean, std)
test_acc = get_acc(cnn, img_data_test, lbl_data_test)
print("Done. Test acc: {}".format(test_acc))
print("IG for digit 0")
cnn = cnn.cpu()
ig_0 = integrate_gradients(
cnn,
torch.tensor(img_data_test[:400]).type(torch.float32),
output_digit=0,
steps_m=300,
)
plt.imshow(ig_0)
plt.savefig("integrated_gradients_0.jpg")
print("IG for digit 6")
ig_6 = integrate_gradients(
cnn,
torch.tensor(img_data_test[:400]).type(torch.float32),
output_digit=1,
steps_m=300,
)
plt.imshow(ig_6)
plt.savefig("integrated_gradients_6.jpg")
print("stop")