-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest.py
More file actions
145 lines (118 loc) · 3.56 KB
/
test.py
File metadata and controls
145 lines (118 loc) · 3.56 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
import neural_model as network
import torch
import torch.nn as nn
from torch.autograd import Variable
import torch.optim as optim
import numpy as np
from copy import deepcopy
import torch
import visdom
import dataset
from scipy.sparse.linalg import eigs
import options_parser as op
SIZE = 64
COLORS = 3
vis = visdom.Visdom('http://127.0.0.1', use_incoming_socket=False)
vis.close(env='main')
vis.close(env='test')
def vis_image(image):
out = torch.nn.Upsample(scale_factor=2)(deepcopy(image).unsqueeze(0))
out = (out - out.min()) / (out.max() - out.min())
vis.image(out.squeeze(0), env='test')
return out
def iterate(net, image, num_steps=None):
net.eval()
o = deepcopy(image)
x = image
# Uncomment to add noise to inputs
"""
l1 = SIZE - SIZE//2
l2 = SIZE + SIZE//2
c1 = SIZE - SIZE//2
c2 = SIZE + SIZE//2
x[:, l1:l2, c1:c2] = np.random.rand(COLORS, l2 - l1, c2 - c1)
#"""
x = torch.from_numpy(x).view(1, COLORS, SIZE, SIZE).double().cuda()
x.requires_grad = False
num_iterations = num_steps
frames = []
threshold = 1e-17
diff = np.float("inf")
count = 0
vis_image(x[0])
if num_steps is None:
num_steps = np.float('inf')
with torch.no_grad():
while diff > threshold and count < num_steps:
n_x = net(x)
count += 1
diff = torch.mean(torch.pow(n_x - x, 2)).cpu().data.numpy().item()
if count < 2:
vis_image(n_x[0])
del(x)
x = n_x
frames.append(deepcopy(x.cpu().data))
original = torch.from_numpy(image).double().unsqueeze(0)
o = torch.from_numpy(o).double().unsqueeze(0)
last = frames[-1]
error = torch.mean(torch.pow(last - o, 2))
original = nn.Upsample(scale_factor=2)(original)
original = (original - original.min()) / (original.max() - original.min())
last = nn.Upsample(scale_factor=2)(last)
last = (last - last.min()) / (last.max() - last.min())
pair = torch.cat([original, last], 0)
title = 'MSE: ' + str(error)
vis.images(pair, nrow=2, opts=dict(title=title),
env='test')
return error
def fast_jacobian(net, x, noutputs):
x = torch.from_numpy(x).double().view(-1, 3, SIZE, SIZE).cuda()
x = x.squeeze()
n = x.size()[0]
x = x.repeat(noutputs, 1, 1, 1)
x.requires_grad_(True)
y = net(x)
y = y.view(-1, 3 * SIZE * SIZE)
y.backward(torch.eye(noutputs).double().cuda())
return x.grad.data
def main(options):
seed = options.seed
torch.manual_seed(seed)
np.random.seed(seed)
torch.cuda.manual_seed(seed)
net = network.Net()
d = torch.load('trained_cnn_model.pth')
net.load_state_dict(d['state_dict'])
net.double()
net.cuda()
train_frames, _ = dataset.make_dataset()
frames, _ = dataset.make_test_dataset()
count = 0
#"""
for f in train_frames:
f = deepcopy(f.numpy())
J = fast_jacobian(net, f, 3 * SIZE * SIZE)
J = J.view(-1, 3 * SIZE * SIZE)
J = J.cpu().data.numpy()
s, _ = eigs(J, k=1, tol=1e-3)
top = np.abs(s)
print(top)
if top < 1:
count += 1
del J
print("Attractors: ", count)
#"""
#"""
avg_error = 0
for f in frames:
f = deepcopy(f.numpy())
count += 1
error = iterate(net, f)
#print(error)
avg_error += error
print(count)
print("AVERAGE ERROR: ", avg_error/count)
#"""
if __name__ == "__main__":
options = op.setup_options()
main(options)