-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathunit_tests.py
201 lines (183 loc) · 7.82 KB
/
unit_tests.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
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
import unittest
import patch_trainer
import constants.constants as c
import time
import torch
import matplotlib.pyplot as plt
import utils.utils as u
import numpy as np
def tensor_to_array(tensor):
max = torch.max(tensor)
if max > 1 :
print("Tensor is out of image domain with max value %f" % max)
min = torch.min(tensor)
if min < 0 :
print("Tensor is out of image domain with min value %f" % min)
return u.tensor_to_array(tensor)
class Trainer(unittest.TestCase):
def setUp(self):
self.trainer = patch_trainer.PatchTrainer(path_image_init=None,
patch_relative_size=0.08)
def test_normalization(self):
_, (ax1, ax2) = plt.subplots(1, 2)
plt.suptitle("TEST NORMALIZATION")
ax1.imshow(tensor_to_array(self.trainer.patch), interpolation='nearest')
ax1.set_title('original image')
t0 = time.time()
normalized = self.trainer.normalize(self.trainer.patch)
t1 = time.time()
ax2.imshow(tensor_to_array(normalized), interpolation='nearest')
ax2.set_title('normalized\ndeltat=%.2fms' % ((t1 - t0)*1e3))
plt.show()
def test_brightness_contrast(self):
_, (ax1, ax2) = plt.subplots(1, 2)
plt.suptitle("TEST BRIGHTNESS")
for _ in range(100):
ax1.imshow(tensor_to_array(self.trainer.patch), interpolation='nearest')
ax1.set_title('original image')
t0 = time.time()
modified = self.trainer.patch_processing_module.brightness_contrast(self.trainer.patch)
t1 = time.time()
ax2.imshow(tensor_to_array(modified), interpolation='nearest')
ax2.set_title('brightness and contrast changed\ndeltat=%.2fms' % ((t1 - t0)*1e3))
self.trainer.patch_processing_module.jitter()
plt.pause(1)
plt.show()
def test_blurring(self):
_, (ax1, ax2) = plt.subplots(1, 2)
plt.suptitle("TEST BLURRING")
for _ in range(100):
ax1.imshow(tensor_to_array(self.trainer.patch), interpolation='nearest')
ax1.set_title('original patch')
t0 = time.time()
blurred = self.trainer.patch_processing_module.blurring(self.trainer.patch)
t1 = time.time()
ax2.imshow(tensor_to_array(blurred), interpolation='nearest')
ax2.set_title('blurred patch\ndeltat=%.2fms' % ((t1 - t0)*1e3))
self.trainer.patch_processing_module.jitter()
plt.pause(1)
plt.show()
def test_noise(self):
_, (ax1, ax2) = plt.subplots(1, 2)
plt.suptitle("TEST NOISE")
for _ in range(100):
ax1.imshow(tensor_to_array(self.trainer.patch), interpolation='nearest')
ax1.set_title('original patch')
t0 = time.time()
noisy = self.trainer.patch_processing_module.noising(self.trainer.patch)
t1 = time.time()
ax2.imshow(tensor_to_array(noisy), interpolation='nearest')
ax2.set_title('noisy patch\ndeltat=%.2fms' % ((t1 - t0)*1e3))
self.trainer.patch_processing_module.jitter()
plt.pause(1)
plt.show()
def test_patch_processing_module(self):
_, (ax1, ax2) = plt.subplots(1, 2)
plt.suptitle("TEST patch_processing_module")
for _ in range(100):
ax1.imshow(tensor_to_array(self.trainer.patch), interpolation='nearest')
ax1.set_title('original patch')
t0 = time.time()
res = self.trainer.patch_processing_module(self.trainer.patch)
t1 = time.time()
ax2.imshow(tensor_to_array(res),
interpolation='nearest')
ax2.set_title('patch processing module modif\ndeltat=%.2fms' % ((t1 - t0)*1e3))
self.trainer.patch_processing_module.jitter()
plt.pause(1)
plt.show()
def test_zone(self):
_, (ax1) = plt.subplots(1, 1)
plt.suptitle("TEST RANDOM ATTACK ZONE")
x0, x1 = c.consts["X_TOP_LEFT"], c.consts["X_BOTTOM_RIGHT"]
y0, y1 = c.consts["Y_TOP_LEFT"], c.consts["Y_BOTTOM_RIGHT"]
zeros = torch.zeros(1, 3, 224, 224)
zeros[0, :, y0:y1, x0:x1] = torch.ones(3, y1 - y0, x1 - x0)
ax1.imshow(tensor_to_array(zeros))
plt.show()
def test_transformation(self):
_, (ax1, ax2, ax3) = plt.subplots(1, 3)
plt.suptitle("TEST TRANSFORMATION")
for _ in range(100):
ax1.imshow(tensor_to_array(self.trainer.patch))
t0 = time.time()
transformed, map_ = self.trainer.transfo_tool.random_transform(self.trainer.patch)
t1 = time.time()
ax2.imshow(tensor_to_array(transformed))
ax2.set_title('transformation\ndeltat=%.2fms' % ((t1 - t0)*1e3))
new_img = self.trainer.transfo_tool.undo_transform(self.trainer.patch, transformed, map_)
ax3.imshow(tensor_to_array(new_img))
dist = (self.trainer.patch - new_img).pow(2).sum().sqrt()
ax3.set_title("euclidian dist : %f" % dist)
plt.pause(1)
plt.show()
def test_total_variation(self):
_, (ax1, ax2) = plt.subplots(1, 2)
plt.suptitle("TEST TV LAMBDA_TV=%f" % c.consts["LAMBDA_TV"])
original = self.trainer._get_patch()
x = original.clone()
x.requires_grad = True
for _ in range(100):
tv_loss = self.trainer.tv_module(x)
tv_loss.backward()
with torch.no_grad():
x -= c.consts["LAMBDA_TV"] * x.grad
x.grad.zero_()
ax1.imshow(tensor_to_array(original))
ax2.imshow(tensor_to_array(x))
ax2.set_title("tv_loss=%f" % tv_loss)
plt.pause(1)
plt.show()
def test_total_variation2(self):
original = self.trainer._get_patch()
learning_rates = np.linspace(0.0001, 0.02, 100)
losses = []
for e in learning_rates :
x = original.clone()
x.requires_grad = True
for _ in range(10):
tv_loss = self.trainer.tv_module(x)
tv_loss.backward()
with torch.no_grad():
x -= e * x.grad
losses.append(float(tv_loss))
plt.xlabel("learning rate")
plt.ylabel("tv_loss")
plt.plot(learning_rates, losses)
plt.show()
def test_printability(self):
_, (ax1, ax2) = plt.subplots(1, 2)
plt.suptitle("TEST PRINT LAMBDA_PRINT=%f" % c.consts["LAMBDA_PRINT"])
original = self.trainer._get_patch()
x = original.clone()
x.requires_grad = True
for _ in range(100):
print_loss = self.trainer.print_module(x)
print_loss.backward()
with torch.no_grad():
x -= c.consts["LAMBDA_PRINT"] * x.grad
x.grad.zero_()
ax1.imshow(tensor_to_array(original))
ax2.imshow(tensor_to_array(x))
ax2.set_title("print_loss=%f" % print_loss)
plt.pause(1)
plt.show()
def test_printability2(self):
original = self.trainer._get_patch()
learning_rates = np.linspace(0.0001, 0.02, 100)
losses = []
for e in learning_rates :
x = original.clone()
x.requires_grad = True
for _ in range(10):
print_loss = self.trainer.print_module(x)
print_loss.backward()
with torch.no_grad():
x -= e * x.grad
losses.append(float(print_loss))
plt.xlabel("learning rate")
plt.ylabel("print_loss")
plt.plot(learning_rates, losses)
plt.show()
if __name__ == '__main__':
unittest.main()