Spaces:
Sleeping
Sleeping
File size: 11,548 Bytes
b20c769 |
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 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 |
import random
import unittest
import torch
from einops import repeat
from src.masking import (
MASKING_MODES,
MAX_MASKING_STRATEGIES,
SPACE_BAND_GROUPS_IDX,
SPACE_TIME_BANDS_GROUPS_IDX,
STATIC_BAND_GROUPS_IDX,
TIME_BAND_GROUPS_IDX,
batch_mask_random,
batch_mask_space,
batch_mask_time,
check_modes_for_conflicts,
filter_unmasking_mode_candidates,
weighted_sample_without_replacement,
)
class TestMasking(unittest.TestCase):
def check_all_values_in_masks(
self, space_time_mask, space_mask, time_mask, static_mask, masking_modes, unmasking_modes
):
self.assertTrue(
(space_time_mask == 2).any()
| (space_mask == 2).any()
| (time_mask == 2).any()
| (static_mask == 2).any(),
f"2 check failed for {masking_modes}, {unmasking_modes}",
)
self.assertTrue(
(space_time_mask == 0).any()
| (space_mask == 0).any()
| (time_mask == 0).any()
| (static_mask == 0).any(),
f"0 check failed for {masking_modes}, {unmasking_modes}",
)
self.assertTrue(
(space_time_mask == 1).any()
| (space_mask == 1).any()
| (time_mask == 1).any()
| (static_mask == 1).any(),
f"1 check failed for {masking_modes}, {unmasking_modes}",
)
def test_mask_by_time(self):
# testing specific failure modes
self._test_mask_by_for_f(
batch_mask_time,
[
("static", "LS"),
("static", "location"),
("space", "WC"),
("space_time", "S2_SWIR"),
("space", "DW"),
("space_time", "S2_NIR_20m"),
],
[("time", "TC"), ("time", "VIIRS")],
)
for _ in range(100):
num_masking_modes = random.choice(list(range(2, MAX_MASKING_STRATEGIES + 1)))
num_unmasking_modes = 1
masking_modes = weighted_sample_without_replacement(
MASKING_MODES, weights=[1] * len(MASKING_MODES), k=num_masking_modes
)
unmasking_modes = weighted_sample_without_replacement(
MASKING_MODES, weights=[1] * len(MASKING_MODES), k=num_unmasking_modes
)
self.assertTrue(
len(unmasking_modes) == num_unmasking_modes, f"Got {len(unmasking_modes)}"
)
masking_modes, unmasking_modes = check_modes_for_conflicts(
masking_modes, unmasking_modes
)
self.assertTrue(
len(unmasking_modes) == num_unmasking_modes, f"Got {len(unmasking_modes)}"
)
self.assertTrue(len(masking_modes) >= 1, f"Got {len(masking_modes)}")
for m_m in masking_modes:
self.assertTrue(m_m not in unmasking_modes, f"{m_m} in {unmasking_modes}")
for u_m in unmasking_modes:
self.assertTrue(u_m not in masking_modes, f"{u_m} in {masking_modes}")
self.assertTrue(len(masking_modes) >= 1)
self.assertTrue(len(unmasking_modes) >= 1)
self._test_mask_by_for_f(batch_mask_space, masking_modes, unmasking_modes)
def test_mask_by_space(self):
for _ in range(100):
num_masking_modes = random.choice(list(range(2, MAX_MASKING_STRATEGIES + 1)))
num_unmasking_modes = 1
masking_modes = weighted_sample_without_replacement(
MASKING_MODES, weights=[1] * len(MASKING_MODES), k=num_masking_modes
)
unmasking_modes = weighted_sample_without_replacement(
MASKING_MODES, weights=[1] * len(MASKING_MODES), k=num_unmasking_modes
)
self.assertTrue(
len(unmasking_modes) == num_unmasking_modes, f"Got {len(unmasking_modes)}"
)
masking_modes, unmasking_modes = check_modes_for_conflicts(
masking_modes, unmasking_modes
)
self.assertTrue(
len(unmasking_modes) == num_unmasking_modes, f"Got {len(unmasking_modes)}"
)
self.assertTrue(len(masking_modes) >= 1, f"Got {len(masking_modes)}")
for m_m in masking_modes:
self.assertTrue(m_m not in unmasking_modes, f"{m_m} in {unmasking_modes}")
for u_m in unmasking_modes:
self.assertTrue(u_m not in masking_modes, f"{u_m} in {masking_modes}")
self.assertTrue(len(masking_modes) >= 1)
self.assertTrue(len(unmasking_modes) >= 1)
self._test_mask_by_for_f(batch_mask_space, masking_modes, unmasking_modes)
def _test_mask_by_for_f(self, f, masking_modes, unmasking_modes):
for t in range(4, 8):
b, h, w = 2, 16, 16
space_time_input = torch.ones((b, h, w, t, 8))
space_input = torch.ones((b, h, w, 8))
time_input = torch.ones((b, t, 8))
static_input = torch.ones((b, 8))
months = repeat(torch.arange(0, t), "t -> b t", b=b)
ratio = 0.25
output = f(
space_time_input,
space_input,
time_input,
static_input,
months,
encode_ratio=ratio,
decode_ratio=ratio,
mode=masking_modes,
decoder_mode=unmasking_modes,
patch_size=4,
)
self.check_all_values_in_masks(
output.space_time_mask,
output.space_mask,
output.time_mask,
output.static_mask,
masking_modes,
unmasking_modes,
)
self.assertEqual(
(b, h, w, t, len(SPACE_TIME_BANDS_GROUPS_IDX)),
output.space_time_mask.shape,
)
self.assertEqual((b, h, w, len(SPACE_BAND_GROUPS_IDX)), output.space_mask.shape)
self.assertEqual((b, t, len(TIME_BAND_GROUPS_IDX)), output.time_mask.shape)
self.assertEqual((b, len(STATIC_BAND_GROUPS_IDX)), output.static_mask.shape)
def test_mask_by_random(self):
b, t, h, w, p = 2, 8, 16, 16, 4
h_tokens, w_tokens = h / p, w / p
space_time_input = torch.ones((b, h, w, t, 8))
space_input = torch.ones((b, h, w, 8))
time_input = torch.ones((b, t, 8))
static_input = torch.ones((b, 8))
months = repeat(torch.arange(0, t), "t -> b t", b=b)
ratio = 0.25
output = batch_mask_random(
space_time_input,
space_input,
time_input,
static_input,
months,
encode_ratio=ratio,
decode_ratio=ratio,
patch_size=p,
ignore_band_groups=["DW", "DW_static"],
)
self.check_all_values_in_masks(
output.space_time_mask,
output.space_mask,
output.time_mask,
output.static_mask,
"random",
"random",
)
self.assertEqual(
(b, h, w, t, len(SPACE_TIME_BANDS_GROUPS_IDX)), output.space_time_mask.shape
)
self.assertEqual((b, h, w, len(SPACE_BAND_GROUPS_IDX)), output.space_mask.shape)
self.assertEqual((b, t, len(TIME_BAND_GROUPS_IDX)), output.time_mask.shape)
self.assertEqual((b, len(STATIC_BAND_GROUPS_IDX)), output.static_mask.shape)
for i in range(1, p):
self.assertTrue(
torch.equal(
output.space_time_mask[:, i::p, i::p],
output.space_time_mask[:, i - 1 :: p, i - 1 :: p],
)
)
self.assertTrue(
torch.equal(
output.space_mask[:, i::p, i::p],
output.space_mask[:, i - 1 :: p, i - 1 :: p],
)
)
space_time_per_token = output.space_time_mask[:, i::p, i::p]
space_time_encode_per_instance = (
space_time_per_token[space_time_per_token == 0] + 1
).sum()
space_time_decode_per_instance = space_time_per_token[space_time_per_token == 2].sum()
space_per_token = output.space_mask[:, i::p, i::p]
space_encode_per_instance = (space_per_token[space_per_token == 0] + 1).sum()
space_decode_per_instance = space_per_token[space_per_token == 2].sum()
time_per_token = output.time_mask
time_encode_per_instance = (time_per_token[time_per_token == 0] + 1).sum()
time_decode_per_instance = time_per_token[time_per_token == 2].sum()
static_per_token = output.static_mask
static_encode_per_instance = (static_per_token[static_per_token == 0] + 1).sum()
static_decode_per_instance = static_per_token[static_per_token == 2].sum()
total_tokens = (
(h_tokens * w_tokens * t * len(SPACE_TIME_BANDS_GROUPS_IDX))
# -1 because we have now masked out dynamic world
+ (h_tokens * w_tokens * (len(SPACE_BAND_GROUPS_IDX) - 1))
+ (t * len(TIME_BAND_GROUPS_IDX))
# -1 because we have now masked out dynamic world static
+ (len(STATIC_BAND_GROUPS_IDX) - 1)
) * b
# handles off by one errors
self.assertTrue(
(
space_time_encode_per_instance
+ space_encode_per_instance
+ time_encode_per_instance
+ static_encode_per_instance
<= int(total_tokens * ratio) + 1
).all()
and (
space_time_encode_per_instance
+ space_encode_per_instance
+ time_encode_per_instance
+ static_encode_per_instance
>= int(total_tokens * ratio) - 1
).all()
)
self.assertTrue(
(
# hacky but the / 2 lets us easily handle the fact
# we are summing over values == 2, not 1
(
space_time_decode_per_instance
+ space_decode_per_instance
+ time_decode_per_instance
+ static_decode_per_instance
)
/ 2
<= (int(total_tokens * ratio) + 1)
).all()
and (
(
space_time_decode_per_instance
+ space_decode_per_instance
+ time_decode_per_instance
+ static_decode_per_instance
)
/ 2
>= int(total_tokens * ratio) - 1
).all()
)
# check DW was masked out
expected_mask_index = list(SPACE_BAND_GROUPS_IDX.keys()).index("DW")
self.assertTrue((output.space_mask[:, :, :, expected_mask_index] == 1).all())
expected_mask_index = list(STATIC_BAND_GROUPS_IDX.keys()).index("DW_static")
self.assertTrue((output.static_mask[:, expected_mask_index] == 1).all())
def test_filter_candidates(self):
candidates = [
[("space", "SRTM"), ("space_time", "S2_RGB")],
[("space", "SRTM"), ("space", "DW")],
[("space", "DW")],
]
ignore_bands = ["DW"]
outputs = filter_unmasking_mode_candidates(candidates, ignore_bands)
print(outputs)
self.assertEqual(outputs, [[("space", "SRTM"), ("space_time", "S2_RGB")]])
|