-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_CroppingAugm.py
157 lines (140 loc) · 4.29 KB
/
test_CroppingAugm.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
import tensorflow as tf
import Utils.CroppingAugm as CroppingAugm
#############
# helper functions
def dummyImage(shape=(8, 34, 33, 3), dtype=tf.float32):
return tf.random.uniform(shape, minval=0, maxval=1, dtype=dtype)
def assertSubsampled(res, crop_size, B, N):
assert 'src' in res, 'src not in res'
assert 'sampled' in res, 'sampled not in res'
assert 'positions' in res, 'positions not in res'
assert res['src'].shape == (B, crop_size, crop_size, 3)
assert res['sampled'].shape == (B, N, 3)
assert res['positions'].shape == (B, N, 2)
return
def commonSampledCropTest(F):
img = dummyImage()
B, N = img.shape[0], 10
crop_size = img.shape[1] // 2
subsample = CroppingAugm.SubsampleProcessor(crop_size, N)
res = F(img, crop_size, subsample)
assertSubsampled(res, crop_size, B, N)
return
#############
# center square crop returns the center of the image in dict
def test_centerSquareCrop_simple():
img = dummyImage()
crop_size = 16
src_size = crop_size // 2
res = CroppingAugm._centerSquareCrop(
img, crop_size,
CroppingAugm.RawProcessor(src_size)
)
assert res['src'].shape == (img.shape[0], src_size, src_size, img.shape[-1])
assert res['dest'].shape == (img.shape[0], crop_size, crop_size, img.shape[-1])
return
def test_centerSquareCrop_subsampled():
return commonSampledCropTest(CroppingAugm._centerSquareCrop)
def test_randomSharedSquareCrop_subsampled():
return commonSampledCropTest(CroppingAugm._randomSharedSquareCrop)
def test_randomSquareCrop_subsampled():
return commonSampledCropTest(
lambda img, crop_size, processor: CroppingAugm._randomSquareCrop(
img, crop_size, 0.5, 1.0, processor
)
)
def test_ultraGridCrop_subsampled():
return commonSampledCropTest(
lambda img, crop_size, processor: CroppingAugm._ultraGridCrop(
img, crop_size, 0.5, 1.0, processor
)
)
def test_ultraGridCrop():
img = dummyImage()
crop_size = img.shape[1] // 2
src_size = crop_size // 2
res = CroppingAugm._ultraGridCrop(
img, crop_size, 0.5, 1.0,
CroppingAugm.RawProcessor(src_size)
)
assert res['src'].shape == (img.shape[0], src_size, src_size, img.shape[-1])
assert res['dest'].shape == (img.shape[0], crop_size, crop_size, img.shape[-1])
return
def test_randomSquareCrop():
img = dummyImage()
crop_size = img.shape[1] // 2
src_size = crop_size // 2
res = CroppingAugm._randomSquareCrop(
img, crop_size, 0.5, 1.0,
CroppingAugm.RawProcessor(src_size)
)
assert res['src'].shape == (img.shape[0], src_size, src_size, img.shape[-1])
assert res['dest'].shape == (img.shape[0], crop_size, crop_size, img.shape[-1])
return
##########################################
def _verifyConfig(config):
img = dummyImage()
B, C = img.shape[0], img.shape[-1]
config['crop size'] = crop_size = 23
target_crop_size = 15
# no subsampling
cropper = CroppingAugm.configToCropper(
dict(**config, subsample=False),
dest_size=target_crop_size
)
res = cropper(img)
assert res['src'].shape == (B, target_crop_size, target_crop_size, C)
assert res['dest'].shape == (B, crop_size, crop_size, C)
# with subsampling
cropper = CroppingAugm.configToCropper(
dict(**config, subsample={'N': 13}),
dest_size=target_crop_size
)
res = cropper(img)
assert 'dest' not in res, 'dest in res'
assertSubsampled(res, target_crop_size, B, 13)
return
# test configToCropper
def test_configToCropper_sharedCenterCrop():
_verifyConfig({
'random crop': False,
'shared crops': True,
})
return
def test_configToCropper_sharedRandomCrop():
_verifyConfig({
'random crop': True,
'shared crops': True,
})
return
def test_configToCropper_randomCrop():
_verifyConfig({
'random crop': True,
'shared crops': False,
})
return
def test_configToCropper_ultraGridCrop():
_verifyConfig({
'random crop': True,
'shared crops': False,
'ultra grid': True,
})
return
def test_configToCropper_sobel():
img = dummyImage()
B, C = img.shape[0], img.shape[-1]
target_crop_size = 15
N = 13
cropper = CroppingAugm.configToCropper(
{
'random crop': False,
'shared crops': True,
'subsample': {'N': N},
},
dest_size=target_crop_size,
extras=['sobel']
)
res = cropper(img)
assert 'sobel' in res, 'sobel not in res'
assert res['sobel'].shape == (B, N, 6)
return