-
Notifications
You must be signed in to change notification settings - Fork 1
/
test.py
81 lines (66 loc) · 2.86 KB
/
test.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
from pathlib import Path
import numpy as np
import util
import cppn_init
import warnings
import unittest
cwd = Path('.')
test_img_dir = cwd / 'test_images'
class TestUtilFunctions(unittest.TestCase):
def check_test_img_exists(self):
self.assertTrue((test_img_dir / '0.png').exists())
self.assertTrue((test_img_dir / '0.png').is_file())
def test_img_save_and_load(self):
test_img = util.load_image(test_img_dir / '2.png', size=64)
self.assertEqual(type(test_img), np.ndarray)
self.assertEqual(test_img.shape, (64, 64, 3))
self.assertEqual(test_img.dtype, np.float64)
util.save_image(test_img_dir / 'test.png', test_img)
file = (test_img_dir / 'test.png')
self.assertTrue(file.exists())
self.assertTrue(file.is_file())
self.assertTrue(np.allclose(test_img, util.load_image(file)))
file.unlink()
# TODO test gif saving
def test_api_querying(self):
self.assertEqual(util._URL, 'https://phinau.de/trasi')
test_img = util.load_image(test_img_dir / '2.png', size=64)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
confidence = util.get_confidence(test_img)
self.assertEqual(type(confidence), np.float64)
self.assertEqual(confidence, 0.99997401)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
confidence = util.get_confidence(test_img, target_class='Zulässige Höchstgeschwindigkeit (30)')
self.assertEqual(confidence, 0.00002541)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
confidence = util.get_confidence(test_img,
'Schnee- oder Eisglätte')
self.assertEqual(confidence, 1e-10)
class TestCPPN(unittest.TestCase):
def test_similarity(self):
test_img = util.load_image(test_img_dir / '2.png')
test_img2 = np.clip(test_img + 0.1, 0.0, 1.0)
with warnings.catch_warnings():
warnings.simplefilter('ignore')
sim1 = cppn_init.similarity(test_img, test_img, True)
sim2 = cppn_init.similarity(test_img, test_img2, True)
self.assertEqual(sim1, 1.0)
self.assertLess(sim2, 1.0)
def test_mutation(self):
weights = np.random.rand(100)
self.assertEqual(weights.shape, cppn_init.mutate(weights, 0.5, 0.05).shape)
def test_cppn(self):
cppn = cppn_init.init_cppn_from_img(util.load_image(test_img_dir / '2.png'))
with warnings.catch_warnings():
warnings.simplefilter('ignore')
self.assertTrue(cppn)
self.assertEqual(cppn.render_image().shape, (64, 64, 3))
weights = cppn.get_weights()
cppn.set_weights(weights)
cppn.reset()
cppn.render_image()
if __name__ == '__main__':
unittest.main()