-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathunit_tests.py
More file actions
149 lines (109 loc) · 7.24 KB
/
unit_tests.py
File metadata and controls
149 lines (109 loc) · 7.24 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
from detectron2.structures.instances import *
from detectron2.structures.boxes import *
import unittest
import data_utils
class TestGetRandomWord(unittest.TestCase):
def testEmptyVocab(self):
with self.assertRaises(ValueError):
data_utils.get_random_word([])
def testOneVocab(self):
vocab = ["One"]
self.assertEqual("One", data_utils.get_random_word(vocab))
def testTwoVocab(self):
vocab = ["One", "Two"]
self.assertTrue(data_utils.get_random_word(vocab) in vocab)
def testSmallVocab(self):
vocab = ["This", "is", "my", "vocab", "list"]
self.assertTrue(data_utils.get_random_word(vocab) in vocab)
class TestGetImgTensors(unittest.TestCase):
def ParameterizeTest(self, test_detections, test_max_detections, test_height, test_width, test_classes,
test_fclayers, test_fcdim, test_used_fclayer):
x1 = torch.rand((test_detections, 1)) * test_width
y1 = torch.rand((test_detections, 1)) * test_height
x2 = torch.rand((test_detections, 1)) * test_width
y2 = torch.rand((test_detections, 1)) * test_height
test_boxes = Boxes(torch.cat([x1, y1, x2, y2], dim=1))
# Nx4 (x1, y1, x2, y2) range = image height/width
test_pred_classes = torch.randint(low=0, high=test_classes, size=[test_detections])
# shape = [100], in range [0, 1600] number of object classes
test_box_features = torch.rand((test_detections, test_fclayers * test_fcdim)) * 10
# shape = [100, 4096] (2048 per layer)
test_probs = torch.rand((test_detections, test_classes + 1))
test_probs = test_probs / torch.sum(test_probs, dim=1, keepdim=True)
test_scores = torch.max(test_probs[:, :-1], dim=1)[0] # shape = [100], in range [0,1]
test_pred = {}
test_instances = Instances((test_height, test_width))
test_instances.set("pred_boxes", test_boxes)
test_instances.set("scores", test_scores)
test_instances.set("pred_classes", test_pred_classes)
test_instances.set("fc_box_features", test_box_features)
test_instances.set("probs", test_probs)
test_pred['instances'] = test_instances
box_features, vis_pe = data_utils.get_img_tensors(test_pred, fc_layer=test_used_fclayer, fc_dim=test_fcdim,
num_classes=test_classes, max_detections=test_max_detections)
self.assertEqual((test_max_detections, test_fcdim), box_features.shape)
self.assertEqual((test_max_detections, 7 + test_classes), vis_pe.shape)
def testNormalCase(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=480, test_width=640,
test_classes=1600, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testOtherLayer(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=480, test_width=640,
test_classes=1600, test_fclayers=2, test_fcdim=2048, test_used_fclayer=1)
def testLessDetections(self):
self.ParameterizeTest(test_detections=73, test_max_detections=100, test_height=480, test_width=640,
test_classes=1600, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testLessMaxDetections(self):
self.ParameterizeTest(test_detections=73, test_max_detections=85, test_height=480, test_width=640,
test_classes=1600, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testMoreDetectionsThanMax(self):
self.ParameterizeTest(test_detections=100, test_max_detections=85, test_height=480, test_width=640,
test_classes=1600, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testOtherSizes(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=640, test_width=480,
test_classes=1600, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testOtherSizes2(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=580, test_width=817,
test_classes=1600, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testOtherSizes3(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=57, test_width=712,
test_classes=1600, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testLessClasses(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=480, test_width=640,
test_classes=800, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testMoreClasses(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=480, test_width=640,
test_classes=3200, test_fclayers=2, test_fcdim=2048, test_used_fclayer=0)
def testLessLayers(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=480, test_width=640,
test_classes=3200, test_fclayers=1, test_fcdim=2048, test_used_fclayer=0)
def testMoreLayers(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=480, test_width=640,
test_classes=3200, test_fclayers=4, test_fcdim=2048, test_used_fclayer=3)
def testLessFCDims(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=480, test_width=640,
test_classes=3200, test_fclayers=2, test_fcdim=1024, test_used_fclayer=0)
def testMoreFCDims(self):
self.ParameterizeTest(test_detections=100, test_max_detections=100, test_height=480, test_width=640,
test_classes=3200, test_fclayers=2, test_fcdim=4096, test_used_fclayer=0)
class TestPrepVisPE(unittest.TestCase):
def ParameterizeTest(self, test_batch_size, test_detections, test_classes):
bbox_preds=torch.rand((test_batch_size, test_detections, 6))
cls_probs=torch.rand((test_batch_size, test_detections, test_classes+1))
vis_pe = data_utils.prep_vis_pe(bbox_preds=bbox_preds, cls_probs=cls_probs)
self.assertEqual((test_batch_size, test_detections, test_classes+7), vis_pe.shape)
def testNormalCase(self):
self.ParameterizeTest(test_batch_size=32, test_detections=100, test_classes=1600)
def testLessBatch(self):
self.ParameterizeTest(test_batch_size=16, test_detections=100, test_classes=1600)
def testMoreBatch(self):
self.ParameterizeTest(test_batch_size=64, test_detections=100, test_classes=1600)
def testLessDetections(self):
self.ParameterizeTest(test_batch_size=32, test_detections=50, test_classes=1600)
def testMoreDetections(self):
self.ParameterizeTest(test_batch_size=32, test_detections=150, test_classes=1600)
def testLessClasses(self):
self.ParameterizeTest(test_batch_size=32, test_detections=100, test_classes=800)
def testMoreClasses(self):
self.ParameterizeTest(test_batch_size=32, test_detections=100, test_classes=3200)
if __name__ == '__main__':
unittest.main()