File size: 53,796 Bytes
14114e8 |
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 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 |
# Copyright (c) Meta Platforms, Inc. and affiliates. All Rights Reserved
"""
Transforms and data augmentation for both image + bbox.
"""
import logging
import numbers
import random
from collections.abc import Sequence
from typing import Iterable
import torch
import torchvision.transforms as T
import torchvision.transforms.functional as F
import torchvision.transforms.v2.functional as Fv2
from PIL import Image as PILImage
from sam3.model.box_ops import box_xyxy_to_cxcywh, masks_to_boxes
from sam3.train.data.sam3_image_dataset import Datapoint
from torchvision.transforms import InterpolationMode
def crop(
datapoint,
index,
region,
v2=False,
check_validity=True,
check_input_validity=True,
recompute_box_from_mask=False,
):
if v2:
rtop, rleft, rheight, rwidth = (int(round(r)) for r in region)
datapoint.images[index].data = Fv2.crop(
datapoint.images[index].data,
top=rtop,
left=rleft,
height=rheight,
width=rwidth,
)
else:
datapoint.images[index].data = F.crop(datapoint.images[index].data, *region)
i, j, h, w = region
# should we do something wrt the original size?
datapoint.images[index].size = (h, w)
for obj in datapoint.images[index].objects:
# crop the mask
if obj.segment is not None:
obj.segment = F.crop(obj.segment, int(i), int(j), int(h), int(w))
# crop the bounding box
if recompute_box_from_mask and obj.segment is not None:
# here the boxes are still in XYXY format with absolute coordinates (they are
# converted to CxCyWH with relative coordinates in basic_for_api.NormalizeAPI)
obj.bbox, obj.area = get_bbox_xyxy_abs_coords_from_mask(obj.segment)
else:
if recompute_box_from_mask and obj.segment is None and obj.area > 0:
logging.warning(
"Cannot recompute bounding box from mask since `obj.segment` is None. "
"Falling back to directly cropping from the input bounding box."
)
boxes = obj.bbox.view(1, 4)
max_size = torch.as_tensor([w, h], dtype=torch.float32)
cropped_boxes = boxes - torch.as_tensor([j, i, j, i], dtype=torch.float32)
cropped_boxes = torch.min(cropped_boxes.reshape(-1, 2, 2), max_size)
cropped_boxes = cropped_boxes.clamp(min=0)
obj.area = (cropped_boxes[:, 1, :] - cropped_boxes[:, 0, :]).prod(dim=1)
obj.bbox = cropped_boxes.reshape(-1, 4)
for query in datapoint.find_queries:
if query.semantic_target is not None:
query.semantic_target = F.crop(
query.semantic_target, int(i), int(j), int(h), int(w)
)
if query.image_id == index and query.input_bbox is not None:
boxes = query.input_bbox
max_size = torch.as_tensor([w, h], dtype=torch.float32)
cropped_boxes = boxes - torch.as_tensor([j, i, j, i], dtype=torch.float32)
cropped_boxes = torch.min(cropped_boxes.reshape(-1, 2, 2), max_size)
cropped_boxes = cropped_boxes.clamp(min=0)
# cur_area = (cropped_boxes[:, 1, :] - cropped_boxes[:, 0, :]).prod(dim=1)
# if check_input_validity:
# assert (
# (cur_area > 0).all().item()
# ), "Some input box got cropped out by the crop transform"
query.input_bbox = cropped_boxes.reshape(-1, 4)
if query.image_id == index and query.input_points is not None:
print(
"Warning! Point cropping with this function may lead to unexpected results"
)
points = query.input_points
# Unlike right-lower box edges, which are exclusive, the
# point must be in [0, length-1], hence the -1
max_size = torch.as_tensor([w, h], dtype=torch.float32) - 1
cropped_points = points - torch.as_tensor([j, i, 0], dtype=torch.float32)
cropped_points[:, :, :2] = torch.min(cropped_points[:, :, :2], max_size)
cropped_points[:, :, :2] = cropped_points[:, :, :2].clamp(min=0)
query.input_points = cropped_points
if check_validity:
# Check that all boxes are still valid
for obj in datapoint.images[index].objects:
assert obj.area > 0, "Box {} has no area".format(obj.bbox)
return datapoint
def hflip(datapoint, index):
datapoint.images[index].data = F.hflip(datapoint.images[index].data)
w, h = datapoint.images[index].data.size
for obj in datapoint.images[index].objects:
boxes = obj.bbox.view(1, 4)
boxes = boxes[:, [2, 1, 0, 3]] * torch.as_tensor(
[-1, 1, -1, 1]
) + torch.as_tensor([w, 0, w, 0])
obj.bbox = boxes
if obj.segment is not None:
obj.segment = F.hflip(obj.segment)
for query in datapoint.find_queries:
if query.semantic_target is not None:
query.semantic_target = F.hflip(query.semantic_target)
if query.image_id == index and query.input_bbox is not None:
boxes = query.input_bbox
boxes = boxes[:, [2, 1, 0, 3]] * torch.as_tensor(
[-1, 1, -1, 1]
) + torch.as_tensor([w, 0, w, 0])
query.input_bbox = boxes
if query.image_id == index and query.input_points is not None:
points = query.input_points
points = points * torch.as_tensor([-1, 1, 1]) + torch.as_tensor([w, 0, 0])
query.input_points = points
return datapoint
def get_size_with_aspect_ratio(image_size, size, max_size=None):
w, h = image_size
if max_size is not None:
min_original_size = float(min((w, h)))
max_original_size = float(max((w, h)))
if max_original_size / min_original_size * size > max_size:
size = max_size * min_original_size / max_original_size
if (w <= h and w == size) or (h <= w and h == size):
return (h, w)
if w < h:
ow = int(round(size))
oh = int(round(size * h / w))
else:
oh = int(round(size))
ow = int(round(size * w / h))
return (oh, ow)
def resize(datapoint, index, size, max_size=None, square=False, v2=False):
# size can be min_size (scalar) or (w, h) tuple
def get_size(image_size, size, max_size=None):
if isinstance(size, (list, tuple)):
return size[::-1]
else:
return get_size_with_aspect_ratio(image_size, size, max_size)
if square:
size = size, size
else:
cur_size = (
datapoint.images[index].data.size()[-2:][::-1]
if v2
else datapoint.images[index].data.size
)
size = get_size(cur_size, size, max_size)
old_size = (
datapoint.images[index].data.size()[-2:][::-1]
if v2
else datapoint.images[index].data.size
)
if v2:
datapoint.images[index].data = Fv2.resize(
datapoint.images[index].data, size, antialias=True
)
else:
datapoint.images[index].data = F.resize(datapoint.images[index].data, size)
new_size = (
datapoint.images[index].data.size()[-2:][::-1]
if v2
else datapoint.images[index].data.size
)
ratios = tuple(float(s) / float(s_orig) for s, s_orig in zip(new_size, old_size))
ratio_width, ratio_height = ratios
for obj in datapoint.images[index].objects:
boxes = obj.bbox.view(1, 4)
scaled_boxes = boxes * torch.as_tensor(
[ratio_width, ratio_height, ratio_width, ratio_height], dtype=torch.float32
)
obj.bbox = scaled_boxes
obj.area *= ratio_width * ratio_height
if obj.segment is not None:
obj.segment = F.resize(obj.segment[None, None], size).squeeze()
for query in datapoint.find_queries:
if query.semantic_target is not None:
query.semantic_target = F.resize(
query.semantic_target[None, None], size
).squeeze()
if query.image_id == index and query.input_bbox is not None:
boxes = query.input_bbox
scaled_boxes = boxes * torch.as_tensor(
[ratio_width, ratio_height, ratio_width, ratio_height],
dtype=torch.float32,
)
query.input_bbox = scaled_boxes
if query.image_id == index and query.input_points is not None:
points = query.input_points
scaled_points = points * torch.as_tensor(
[ratio_width, ratio_height, 1],
dtype=torch.float32,
)
query.input_points = scaled_points
h, w = size
datapoint.images[index].size = (h, w)
return datapoint
def pad(datapoint, index, padding, v2=False):
old_h, old_w = datapoint.images[index].size
h, w = old_h, old_w
if len(padding) == 2:
# assumes that we only pad on the bottom right corners
if v2:
datapoint.images[index].data = Fv2.pad(
datapoint.images[index].data, (0, 0, padding[0], padding[1])
)
else:
datapoint.images[index].data = F.pad(
datapoint.images[index].data, (0, 0, padding[0], padding[1])
)
h += padding[1]
w += padding[0]
else:
if v2:
# left, top, right, bottom
datapoint.images[index].data = Fv2.pad(
datapoint.images[index].data,
(padding[0], padding[1], padding[2], padding[3]),
)
else:
# left, top, right, bottom
datapoint.images[index].data = F.pad(
datapoint.images[index].data,
(padding[0], padding[1], padding[2], padding[3]),
)
h += padding[1] + padding[3]
w += padding[0] + padding[2]
datapoint.images[index].size = (h, w)
for obj in datapoint.images[index].objects:
if len(padding) != 2:
obj.bbox += torch.as_tensor(
[padding[0], padding[1], padding[0], padding[1]], dtype=torch.float32
)
if obj.segment is not None:
if v2:
if len(padding) == 2:
obj.segment = Fv2.pad(
obj.segment[None], (0, 0, padding[0], padding[1])
).squeeze(0)
else:
obj.segment = Fv2.pad(obj.segment[None], tuple(padding)).squeeze(0)
else:
if len(padding) == 2:
obj.segment = F.pad(obj.segment, (0, 0, padding[0], padding[1]))
else:
obj.segment = F.pad(obj.segment, tuple(padding))
for query in datapoint.find_queries:
if query.semantic_target is not None:
if v2:
if len(padding) == 2:
query.semantic_target = Fv2.pad(
query.semantic_target[None, None],
(0, 0, padding[0], padding[1]),
).squeeze()
else:
query.semantic_target = Fv2.pad(
query.semantic_target[None, None], tuple(padding)
).squeeze()
else:
if len(padding) == 2:
query.semantic_target = F.pad(
query.semantic_target[None, None],
(0, 0, padding[0], padding[1]),
).squeeze()
else:
query.semantic_target = F.pad(
query.semantic_target[None, None], tuple(padding)
).squeeze()
if query.image_id == index and query.input_bbox is not None:
if len(padding) != 2:
query.input_bbox += torch.as_tensor(
[padding[0], padding[1], padding[0], padding[1]],
dtype=torch.float32,
)
if query.image_id == index and query.input_points is not None:
if len(padding) != 2:
query.input_points += torch.as_tensor(
[padding[0], padding[1], 0], dtype=torch.float32
)
return datapoint
class RandomSizeCropAPI:
def __init__(
self,
min_size: int,
max_size: int,
respect_boxes: bool,
consistent_transform: bool,
respect_input_boxes: bool = True,
v2: bool = False,
recompute_box_from_mask: bool = False,
):
self.min_size = min_size
self.max_size = max_size
self.respect_boxes = respect_boxes # if True we can't crop a box out
self.respect_input_boxes = respect_input_boxes
self.consistent_transform = consistent_transform
self.v2 = v2
self.recompute_box_from_mask = recompute_box_from_mask
def _sample_no_respect_boxes(self, img):
w = random.randint(self.min_size, min(img.width, self.max_size))
h = random.randint(self.min_size, min(img.height, self.max_size))
return T.RandomCrop.get_params(img, (h, w))
def _sample_respect_boxes(self, img, boxes, points, min_box_size=10.0):
"""
Assure that no box or point is dropped via cropping, though portions
of boxes may be removed.
"""
if len(boxes) == 0 and len(points) == 0:
return self._sample_no_respect_boxes(img)
if self.v2:
img_height, img_width = img.size()[-2:]
else:
img_width, img_height = img.size
minW, minH, maxW, maxH = (
min(img_width, self.min_size),
min(img_height, self.min_size),
min(img_width, self.max_size),
min(img_height, self.max_size),
)
# The crop box must extend one pixel beyond points to the bottom/right
# to assure the exclusive box contains the points.
minX = (
torch.cat([boxes[:, 0] + min_box_size, points[:, 0] + 1], dim=0)
.max()
.item()
)
minY = (
torch.cat([boxes[:, 1] + min_box_size, points[:, 1] + 1], dim=0)
.max()
.item()
)
minX = min(img_width, minX)
minY = min(img_height, minY)
maxX = torch.cat([boxes[:, 2] - min_box_size, points[:, 0]], dim=0).min().item()
maxY = torch.cat([boxes[:, 3] - min_box_size, points[:, 1]], dim=0).min().item()
maxX = max(0.0, maxX)
maxY = max(0.0, maxY)
minW = max(minW, minX - maxX)
minH = max(minH, minY - maxY)
w = random.uniform(minW, max(minW, maxW))
h = random.uniform(minH, max(minH, maxH))
if minX > maxX:
# i = random.uniform(max(0, minX - w + 1), max(maxX, max(0, minX - w + 1)))
i = random.uniform(max(0, minX - w), max(maxX, max(0, minX - w)))
else:
i = random.uniform(
max(0, minX - w + 1), max(maxX - 1, max(0, minX - w + 1))
)
if minY > maxY:
# j = random.uniform(max(0, minY - h + 1), max(maxY, max(0, minY - h + 1)))
j = random.uniform(max(0, minY - h), max(maxY, max(0, minY - h)))
else:
j = random.uniform(
max(0, minY - h + 1), max(maxY - 1, max(0, minY - h + 1))
)
return [j, i, h, w]
def __call__(self, datapoint, **kwargs):
if self.respect_boxes or self.respect_input_boxes:
if self.consistent_transform:
# Check that all the images are the same size
w, h = datapoint.images[0].data.size
for img in datapoint.images:
assert img.data.size == (w, h)
all_boxes = []
# Getting all boxes in all the images
if self.respect_boxes:
all_boxes += [
obj.bbox.view(-1, 4)
for img in datapoint.images
for obj in img.objects
]
# Get all the boxes in the find queries
if self.respect_input_boxes:
all_boxes += [
q.input_bbox.view(-1, 4)
for q in datapoint.find_queries
if q.input_bbox is not None
]
if all_boxes:
all_boxes = torch.cat(all_boxes, 0)
else:
all_boxes = torch.empty(0, 4)
all_points = [
q.input_points.view(-1, 3)[:, :2]
for q in datapoint.find_queries
if q.input_points is not None
]
if all_points:
all_points = torch.cat(all_points, 0)
else:
all_points = torch.empty(0, 2)
crop_param = self._sample_respect_boxes(
datapoint.images[0].data, all_boxes, all_points
)
for i in range(len(datapoint.images)):
datapoint = crop(
datapoint,
i,
crop_param,
v2=self.v2,
check_validity=self.respect_boxes,
check_input_validity=self.respect_input_boxes,
recompute_box_from_mask=self.recompute_box_from_mask,
)
return datapoint
else:
for i in range(len(datapoint.images)):
all_boxes = []
# Get all boxes in the current image
if self.respect_boxes:
all_boxes += [
obj.bbox.view(-1, 4) for obj in datapoint.images[i].objects
]
# Get all the boxes in the find queries that correspond to this image
if self.respect_input_boxes:
all_boxes += [
q.input_bbox.view(-1, 4)
for q in datapoint.find_queries
if q.image_id == i and q.input_bbox is not None
]
if all_boxes:
all_boxes = torch.cat(all_boxes, 0)
else:
all_boxes = torch.empty(0, 4)
all_points = [
q.input_points.view(-1, 3)[:, :2]
for q in datapoint.find_queries
if q.input_points is not None
]
if all_points:
all_points = torch.cat(all_points, 0)
else:
all_points = torch.empty(0, 2)
crop_param = self._sample_respect_boxes(
datapoint.images[i].data, all_boxes, all_points
)
datapoint = crop(
datapoint,
i,
crop_param,
v2=self.v2,
check_validity=self.respect_boxes,
check_input_validity=self.respect_input_boxes,
recompute_box_from_mask=self.recompute_box_from_mask,
)
return datapoint
else:
if self.consistent_transform:
# Check that all the images are the same size
w, h = datapoint.images[0].data.size
for img in datapoint.images:
assert img.data.size == (w, h)
crop_param = self._sample_no_respect_boxes(datapoint.images[0].data)
for i in range(len(datapoint.images)):
datapoint = crop(
datapoint,
i,
crop_param,
v2=self.v2,
check_validity=self.respect_boxes,
check_input_validity=self.respect_input_boxes,
recompute_box_from_mask=self.recompute_box_from_mask,
)
return datapoint
else:
for i in range(len(datapoint.images)):
crop_param = self._sample_no_respect_boxes(datapoint.images[i].data)
datapoint = crop(
datapoint,
i,
crop_param,
v2=self.v2,
check_validity=self.respect_boxes,
check_input_validity=self.respect_input_boxes,
recompute_box_from_mask=self.recompute_box_from_mask,
)
return datapoint
class CenterCropAPI:
def __init__(self, size, consistent_transform, recompute_box_from_mask=False):
self.size = size
self.consistent_transform = consistent_transform
self.recompute_box_from_mask = recompute_box_from_mask
def _sample_crop(self, image_width, image_height):
crop_height, crop_width = self.size
crop_top = int(round((image_height - crop_height) / 2.0))
crop_left = int(round((image_width - crop_width) / 2.0))
return crop_top, crop_left, crop_height, crop_width
def __call__(self, datapoint, **kwargs):
if self.consistent_transform:
# Check that all the images are the same size
w, h = datapoint.images[0].data.size
for img in datapoint.images:
assert img.size == (w, h)
crop_top, crop_left, crop_height, crop_width = self._sample_crop(w, h)
for i in range(len(datapoint.images)):
datapoint = crop(
datapoint,
i,
(crop_top, crop_left, crop_height, crop_width),
recompute_box_from_mask=self.recompute_box_from_mask,
)
return datapoint
for i in range(len(datapoint.images)):
w, h = datapoint.images[i].data.size
crop_top, crop_left, crop_height, crop_width = self._sample_crop(w, h)
datapoint = crop(
datapoint,
i,
(crop_top, crop_left, crop_height, crop_width),
recompute_box_from_mask=self.recompute_box_from_mask,
)
return datapoint
class RandomHorizontalFlip:
def __init__(self, consistent_transform, p=0.5):
self.p = p
self.consistent_transform = consistent_transform
def __call__(self, datapoint, **kwargs):
if self.consistent_transform:
if random.random() < self.p:
for i in range(len(datapoint.images)):
datapoint = hflip(datapoint, i)
return datapoint
for i in range(len(datapoint.images)):
if random.random() < self.p:
datapoint = hflip(datapoint, i)
return datapoint
class RandomResizeAPI:
def __init__(
self, sizes, consistent_transform, max_size=None, square=False, v2=False
):
if isinstance(sizes, int):
sizes = (sizes,)
assert isinstance(sizes, Iterable)
self.sizes = list(sizes)
self.max_size = max_size
self.square = square
self.consistent_transform = consistent_transform
self.v2 = v2
def __call__(self, datapoint, **kwargs):
if self.consistent_transform:
size = random.choice(self.sizes)
for i in range(len(datapoint.images)):
datapoint = resize(
datapoint, i, size, self.max_size, square=self.square, v2=self.v2
)
return datapoint
for i in range(len(datapoint.images)):
size = random.choice(self.sizes)
datapoint = resize(
datapoint, i, size, self.max_size, square=self.square, v2=self.v2
)
return datapoint
class ScheduledRandomResizeAPI(RandomResizeAPI):
def __init__(self, size_scheduler, consistent_transform, square=False):
self.size_scheduler = size_scheduler
# Just a meaningful init value for super
params = self.size_scheduler(epoch_num=0)
sizes, max_size = params["sizes"], params["max_size"]
super().__init__(sizes, consistent_transform, max_size=max_size, square=square)
def __call__(self, datapoint, **kwargs):
assert "epoch" in kwargs, "Param scheduler needs to know the current epoch"
params = self.size_scheduler(kwargs["epoch"])
sizes, max_size = params["sizes"], params["max_size"]
self.sizes = sizes
self.max_size = max_size
datapoint = super(ScheduledRandomResizeAPI, self).__call__(datapoint, **kwargs)
return datapoint
class RandomPadAPI:
def __init__(self, max_pad, consistent_transform):
self.max_pad = max_pad
self.consistent_transform = consistent_transform
def _sample_pad(self):
pad_x = random.randint(0, self.max_pad)
pad_y = random.randint(0, self.max_pad)
return pad_x, pad_y
def __call__(self, datapoint, **kwargs):
if self.consistent_transform:
pad_x, pad_y = self._sample_pad()
for i in range(len(datapoint.images)):
datapoint = pad(datapoint, i, (pad_x, pad_y))
return datapoint
for i in range(len(datapoint.images)):
pad_x, pad_y = self._sample_pad()
datapoint = pad(datapoint, i, (pad_x, pad_y))
return datapoint
class PadToSizeAPI:
def __init__(self, size, consistent_transform, bottom_right=False, v2=False):
self.size = size
self.consistent_transform = consistent_transform
self.v2 = v2
self.bottom_right = bottom_right
def _sample_pad(self, w, h):
pad_x = self.size - w
pad_y = self.size - h
assert pad_x >= 0 and pad_y >= 0
pad_left = random.randint(0, pad_x)
pad_right = pad_x - pad_left
pad_top = random.randint(0, pad_y)
pad_bottom = pad_y - pad_top
return pad_left, pad_top, pad_right, pad_bottom
def __call__(self, datapoint, **kwargs):
if self.consistent_transform:
# Check that all the images are the same size
w, h = datapoint.images[0].data.size
for img in datapoint.images:
assert img.size == (w, h)
if self.bottom_right:
pad_right = self.size - w
pad_bottom = self.size - h
padding = (pad_right, pad_bottom)
else:
padding = self._sample_pad(w, h)
for i in range(len(datapoint.images)):
datapoint = pad(datapoint, i, padding, v2=self.v2)
return datapoint
for i, img in enumerate(datapoint.images):
w, h = img.data.size
if self.bottom_right:
pad_right = self.size - w
pad_bottom = self.size - h
padding = (pad_right, pad_bottom)
else:
padding = self._sample_pad(w, h)
datapoint = pad(datapoint, i, padding, v2=self.v2)
return datapoint
class RandomMosaicVideoAPI:
def __init__(self, prob=0.15, grid_h=2, grid_w=2, use_random_hflip=False):
self.prob = prob
self.grid_h = grid_h
self.grid_w = grid_w
self.use_random_hflip = use_random_hflip
def __call__(self, datapoint, **kwargs):
if random.random() > self.prob:
return datapoint
# select a random location to place the target mask in the mosaic
target_grid_y = random.randint(0, self.grid_h - 1)
target_grid_x = random.randint(0, self.grid_w - 1)
# whether to flip each grid in the mosaic horizontally
if self.use_random_hflip:
should_hflip = torch.rand(self.grid_h, self.grid_w) < 0.5
else:
should_hflip = torch.zeros(self.grid_h, self.grid_w, dtype=torch.bool)
for i in range(len(datapoint.images)):
datapoint = random_mosaic_frame(
datapoint,
i,
grid_h=self.grid_h,
grid_w=self.grid_w,
target_grid_y=target_grid_y,
target_grid_x=target_grid_x,
should_hflip=should_hflip,
)
return datapoint
def random_mosaic_frame(
datapoint,
index,
grid_h,
grid_w,
target_grid_y,
target_grid_x,
should_hflip,
):
# Step 1: downsize the images and paste them into a mosaic
image_data = datapoint.images[index].data
is_pil = isinstance(image_data, PILImage.Image)
if is_pil:
H_im = image_data.height
W_im = image_data.width
image_data_output = PILImage.new("RGB", (W_im, H_im))
else:
H_im = image_data.size(-2)
W_im = image_data.size(-1)
image_data_output = torch.zeros_like(image_data)
downsize_cache = {}
for grid_y in range(grid_h):
for grid_x in range(grid_w):
y_offset_b = grid_y * H_im // grid_h
x_offset_b = grid_x * W_im // grid_w
y_offset_e = (grid_y + 1) * H_im // grid_h
x_offset_e = (grid_x + 1) * W_im // grid_w
H_im_downsize = y_offset_e - y_offset_b
W_im_downsize = x_offset_e - x_offset_b
if (H_im_downsize, W_im_downsize) in downsize_cache:
image_data_downsize = downsize_cache[(H_im_downsize, W_im_downsize)]
else:
image_data_downsize = F.resize(
image_data,
size=(H_im_downsize, W_im_downsize),
interpolation=InterpolationMode.BILINEAR,
antialias=True, # antialiasing for downsizing
)
downsize_cache[(H_im_downsize, W_im_downsize)] = image_data_downsize
if should_hflip[grid_y, grid_x].item():
image_data_downsize = F.hflip(image_data_downsize)
if is_pil:
image_data_output.paste(image_data_downsize, (x_offset_b, y_offset_b))
else:
image_data_output[:, y_offset_b:y_offset_e, x_offset_b:x_offset_e] = (
image_data_downsize
)
datapoint.images[index].data = image_data_output
# Step 2: downsize the masks and paste them into the target grid of the mosaic
# (note that we don't scale input/target boxes since they are not used in TA)
for obj in datapoint.images[index].objects:
if obj.segment is None:
continue
assert obj.segment.shape == (H_im, W_im) and obj.segment.dtype == torch.uint8
segment_output = torch.zeros_like(obj.segment)
target_y_offset_b = target_grid_y * H_im // grid_h
target_x_offset_b = target_grid_x * W_im // grid_w
target_y_offset_e = (target_grid_y + 1) * H_im // grid_h
target_x_offset_e = (target_grid_x + 1) * W_im // grid_w
target_H_im_downsize = target_y_offset_e - target_y_offset_b
target_W_im_downsize = target_x_offset_e - target_x_offset_b
segment_downsize = F.resize(
obj.segment[None, None],
size=(target_H_im_downsize, target_W_im_downsize),
interpolation=InterpolationMode.BILINEAR,
antialias=True, # antialiasing for downsizing
)[0, 0]
if should_hflip[target_grid_y, target_grid_x].item():
segment_downsize = F.hflip(segment_downsize[None, None])[0, 0]
segment_output[
target_y_offset_b:target_y_offset_e, target_x_offset_b:target_x_offset_e
] = segment_downsize
obj.segment = segment_output
return datapoint
class ScheduledPadToSizeAPI(PadToSizeAPI):
def __init__(self, size_scheduler, consistent_transform):
self.size_scheduler = size_scheduler
size = self.size_scheduler(epoch_num=0)["sizes"]
super().__init__(size, consistent_transform)
def __call__(self, datapoint, **kwargs):
assert "epoch" in kwargs, "Param scheduler needs to know the current epoch"
params = self.size_scheduler(kwargs["epoch"])
self.size = params["resolution"]
return super(ScheduledPadToSizeAPI, self).__call__(datapoint, **kwargs)
class IdentityAPI:
def __call__(self, datapoint, **kwargs):
return datapoint
class RandomSelectAPI:
"""
Randomly selects between transforms1 and transforms2,
with probability p for transforms1 and (1 - p) for transforms2
"""
def __init__(self, transforms1=None, transforms2=None, p=0.5):
self.transforms1 = transforms1 or IdentityAPI()
self.transforms2 = transforms2 or IdentityAPI()
self.p = p
def __call__(self, datapoint, **kwargs):
if random.random() < self.p:
return self.transforms1(datapoint, **kwargs)
return self.transforms2(datapoint, **kwargs)
class ToTensorAPI:
def __init__(self, v2=False):
self.v2 = v2
def __call__(self, datapoint: Datapoint, **kwargs):
for img in datapoint.images:
if self.v2:
img.data = Fv2.to_image_tensor(img.data)
# img.data = Fv2.to_dtype(img.data, torch.uint8, scale=True)
# img.data = Fv2.convert_image_dtype(img.data, torch.uint8)
else:
img.data = F.to_tensor(img.data)
return datapoint
class NormalizeAPI:
def __init__(self, mean, std, v2=False):
self.mean = mean
self.std = std
self.v2 = v2
def __call__(self, datapoint: Datapoint, **kwargs):
for img in datapoint.images:
if self.v2:
img.data = Fv2.convert_image_dtype(img.data, torch.float32)
img.data = Fv2.normalize(img.data, mean=self.mean, std=self.std)
else:
img.data = F.normalize(img.data, mean=self.mean, std=self.std)
for obj in img.objects:
boxes = obj.bbox
cur_h, cur_w = img.data.shape[-2:]
boxes = box_xyxy_to_cxcywh(boxes)
boxes = boxes / torch.tensor(
[cur_w, cur_h, cur_w, cur_h], dtype=torch.float32
)
obj.bbox = boxes
for query in datapoint.find_queries:
if query.input_bbox is not None:
boxes = query.input_bbox
cur_h, cur_w = datapoint.images[query.image_id].data.shape[-2:]
boxes = box_xyxy_to_cxcywh(boxes)
boxes = boxes / torch.tensor(
[cur_w, cur_h, cur_w, cur_h], dtype=torch.float32
)
query.input_bbox = boxes
if query.input_points is not None:
points = query.input_points
cur_h, cur_w = datapoint.images[query.image_id].data.shape[-2:]
points = points / torch.tensor([cur_w, cur_h, 1.0], dtype=torch.float32)
query.input_points = points
return datapoint
class ComposeAPI:
def __init__(self, transforms):
self.transforms = transforms
def __call__(self, datapoint, **kwargs):
for t in self.transforms:
datapoint = t(datapoint, **kwargs)
return datapoint
def __repr__(self):
format_string = self.__class__.__name__ + "("
for t in self.transforms:
format_string += "\n"
format_string += " {0}".format(t)
format_string += "\n)"
return format_string
class RandomGrayscale:
def __init__(self, consistent_transform, p=0.5):
self.p = p
self.consistent_transform = consistent_transform
self.Grayscale = T.Grayscale(num_output_channels=3)
def __call__(self, datapoint: Datapoint, **kwargs):
if self.consistent_transform:
if random.random() < self.p:
for img in datapoint.images:
img.data = self.Grayscale(img.data)
return datapoint
for img in datapoint.images:
if random.random() < self.p:
img.data = self.Grayscale(img.data)
return datapoint
class ColorJitter:
def __init__(self, consistent_transform, brightness, contrast, saturation, hue):
self.consistent_transform = consistent_transform
self.brightness = (
brightness
if isinstance(brightness, list)
else [max(0, 1 - brightness), 1 + brightness]
)
self.contrast = (
contrast
if isinstance(contrast, list)
else [max(0, 1 - contrast), 1 + contrast]
)
self.saturation = (
saturation
if isinstance(saturation, list)
else [max(0, 1 - saturation), 1 + saturation]
)
self.hue = hue if isinstance(hue, list) or hue is None else ([-hue, hue])
def __call__(self, datapoint: Datapoint, **kwargs):
if self.consistent_transform:
# Create a color jitter transformation params
(
fn_idx,
brightness_factor,
contrast_factor,
saturation_factor,
hue_factor,
) = T.ColorJitter.get_params(
self.brightness, self.contrast, self.saturation, self.hue
)
for img in datapoint.images:
if not self.consistent_transform:
(
fn_idx,
brightness_factor,
contrast_factor,
saturation_factor,
hue_factor,
) = T.ColorJitter.get_params(
self.brightness, self.contrast, self.saturation, self.hue
)
for fn_id in fn_idx:
if fn_id == 0 and brightness_factor is not None:
img.data = F.adjust_brightness(img.data, brightness_factor)
elif fn_id == 1 and contrast_factor is not None:
img.data = F.adjust_contrast(img.data, contrast_factor)
elif fn_id == 2 and saturation_factor is not None:
img.data = F.adjust_saturation(img.data, saturation_factor)
elif fn_id == 3 and hue_factor is not None:
img.data = F.adjust_hue(img.data, hue_factor)
return datapoint
class RandomAffine:
def __init__(
self,
degrees,
consistent_transform,
scale=None,
translate=None,
shear=None,
image_mean=(123, 116, 103),
log_warning=True,
num_tentatives=1,
image_interpolation="bicubic",
):
"""
The mask is required for this transform.
if consistent_transform if True, then the same random affine is applied to all frames and masks.
"""
self.degrees = degrees if isinstance(degrees, list) else ([-degrees, degrees])
self.scale = scale
self.shear = (
shear if isinstance(shear, list) else ([-shear, shear] if shear else None)
)
self.translate = translate
self.fill_img = image_mean
self.consistent_transform = consistent_transform
self.log_warning = log_warning
self.num_tentatives = num_tentatives
if image_interpolation == "bicubic":
self.image_interpolation = InterpolationMode.BICUBIC
elif image_interpolation == "bilinear":
self.image_interpolation = InterpolationMode.BILINEAR
else:
raise NotImplementedError
def __call__(self, datapoint: Datapoint, **kwargs):
for _tentative in range(self.num_tentatives):
res = self.transform_datapoint(datapoint)
if res is not None:
return res
if self.log_warning:
logging.warning(
f"Skip RandomAffine for zero-area mask in first frame after {self.num_tentatives} tentatives"
)
return datapoint
def transform_datapoint(self, datapoint: Datapoint):
_, height, width = F.get_dimensions(datapoint.images[0].data)
img_size = [width, height]
if self.consistent_transform:
# Create a random affine transformation
affine_params = T.RandomAffine.get_params(
degrees=self.degrees,
translate=self.translate,
scale_ranges=self.scale,
shears=self.shear,
img_size=img_size,
)
for img_idx, img in enumerate(datapoint.images):
this_masks = [
obj.segment.unsqueeze(0) if obj.segment is not None else None
for obj in img.objects
]
if not self.consistent_transform:
# if not consistent we create a new affine params for every frame&mask pair Create a random affine transformation
affine_params = T.RandomAffine.get_params(
degrees=self.degrees,
translate=self.translate,
scale_ranges=self.scale,
shears=self.shear,
img_size=img_size,
)
transformed_bboxes, transformed_masks = [], []
for i in range(len(img.objects)):
if this_masks[i] is None:
transformed_masks.append(None)
# Dummy bbox for a dummy target
transformed_bboxes.append(torch.tensor([[0, 0, 0, 0]]))
else:
transformed_mask = F.affine(
this_masks[i],
*affine_params,
interpolation=InterpolationMode.NEAREST,
fill=0.0,
)
if img_idx == 0 and transformed_mask.max() == 0:
# We are dealing with a video and the object is not visible in the first frame
# Return the datapoint without transformation
return None
transformed_bbox = masks_to_boxes(transformed_mask)
transformed_bboxes.append(transformed_bbox)
transformed_masks.append(transformed_mask.squeeze())
for i in range(len(img.objects)):
img.objects[i].bbox = transformed_bboxes[i]
img.objects[i].segment = transformed_masks[i]
img.data = F.affine(
img.data,
*affine_params,
interpolation=self.image_interpolation,
fill=self.fill_img,
)
return datapoint
class RandomResizedCrop:
def __init__(
self,
consistent_transform,
size,
scale=None,
ratio=None,
log_warning=True,
num_tentatives=4,
keep_aspect_ratio=False,
):
"""
The mask is required for this transform.
if consistent_transform if True, then the same random resized crop is applied to all frames and masks.
"""
if isinstance(size, numbers.Number):
self.size = (int(size), int(size))
elif isinstance(size, Sequence) and len(size) == 1:
self.size = (size[0], size[0])
elif len(size) != 2:
raise ValueError("Please provide only two dimensions (h, w) for size.")
else:
self.size = size
self.scale = scale if scale is not None else (0.08, 1.0)
self.ratio = ratio if ratio is not None else (3.0 / 4.0, 4.0 / 3.0)
self.consistent_transform = consistent_transform
self.log_warning = log_warning
self.num_tentatives = num_tentatives
self.keep_aspect_ratio = keep_aspect_ratio
def __call__(self, datapoint: Datapoint, **kwargs):
for _tentative in range(self.num_tentatives):
res = self.transform_datapoint(datapoint)
if res is not None:
return res
if self.log_warning:
logging.warning(
f"Skip RandomResizeCrop for zero-area mask in first frame after {self.num_tentatives} tentatives"
)
return datapoint
def transform_datapoint(self, datapoint: Datapoint):
if self.keep_aspect_ratio:
original_size = datapoint.images[0].size
original_ratio = original_size[1] / original_size[0]
ratio = [r * original_ratio for r in self.ratio]
else:
ratio = self.ratio
if self.consistent_transform:
# Create a random crop transformation
crop_params = T.RandomResizedCrop.get_params(
img=datapoint.images[0].data,
scale=self.scale,
ratio=ratio,
)
for img_idx, img in enumerate(datapoint.images):
if not self.consistent_transform:
# Create a random crop transformation
crop_params = T.RandomResizedCrop.get_params(
img=img.data,
scale=self.scale,
ratio=ratio,
)
this_masks = [
obj.segment.unsqueeze(0) if obj.segment is not None else None
for obj in img.objects
]
transformed_bboxes, transformed_masks = [], []
for i in range(len(img.objects)):
if this_masks[i] is None:
transformed_masks.append(None)
# Dummy bbox for a dummy target
transformed_bboxes.append(torch.tensor([[0, 0, 0, 0]]))
else:
transformed_mask = F.resized_crop(
this_masks[i],
*crop_params,
size=self.size,
interpolation=InterpolationMode.NEAREST,
)
if img_idx == 0 and transformed_mask.max() == 0:
# We are dealing with a video and the object is not visible in the first frame
# Return the datapoint without transformation
return None
transformed_masks.append(transformed_mask.squeeze())
transformed_bbox = masks_to_boxes(transformed_mask)
transformed_bboxes.append(transformed_bbox)
# Set the new boxes and masks if all transformed masks and boxes are good.
for i in range(len(img.objects)):
img.objects[i].bbox = transformed_bboxes[i]
img.objects[i].segment = transformed_masks[i]
img.data = F.resized_crop(
img.data,
*crop_params,
size=self.size,
interpolation=InterpolationMode.BILINEAR,
)
return datapoint
class ResizeToMaxIfAbove:
# Resize datapoint image if one of its sides is larger that max_size
def __init__(
self,
max_size=None,
):
self.max_size = max_size
def __call__(self, datapoint: Datapoint, **kwargs):
_, height, width = F.get_dimensions(datapoint.images[0].data)
if height <= self.max_size and width <= self.max_size:
# The original frames are small enough
return datapoint
elif height >= width:
new_height = self.max_size
new_width = int(round(self.max_size * width / height))
else:
new_height = int(round(self.max_size * height / width))
new_width = self.max_size
size = new_height, new_width
for index in range(len(datapoint.images)):
datapoint.images[index].data = F.resize(datapoint.images[index].data, size)
for obj in datapoint.images[index].objects:
obj.segment = F.resize(
obj.segment[None, None],
size,
interpolation=InterpolationMode.NEAREST,
).squeeze()
h, w = size
datapoint.images[index].size = (h, w)
return datapoint
def get_bbox_xyxy_abs_coords_from_mask(mask):
"""Get the bounding box (XYXY format w/ absolute coordinates) of a binary mask."""
assert mask.dim() == 2
rows = torch.any(mask, dim=1)
cols = torch.any(mask, dim=0)
row_inds = rows.nonzero().view(-1)
col_inds = cols.nonzero().view(-1)
if row_inds.numel() == 0:
# mask is empty
bbox = torch.zeros(1, 4, dtype=torch.float32)
bbox_area = 0.0
else:
ymin, ymax = row_inds.min(), row_inds.max()
xmin, xmax = col_inds.min(), col_inds.max()
bbox = torch.tensor([xmin, ymin, xmax, ymax], dtype=torch.float32).view(1, 4)
bbox_area = float((ymax - ymin) * (xmax - xmin))
return bbox, bbox_area
class MotionBlur:
def __init__(self, kernel_size=5, consistent_transform=True, p=0.5):
assert kernel_size % 2 == 1, "Kernel size must be odd."
self.kernel_size = kernel_size
self.consistent_transform = consistent_transform
self.p = p
def __call__(self, datapoint: Datapoint, **kwargs):
if random.random() >= self.p:
return datapoint
if self.consistent_transform:
# Generate a single motion blur kernel for all images
kernel = self._generate_motion_blur_kernel()
for img in datapoint.images:
if not self.consistent_transform:
# Generate a new motion blur kernel for each image
kernel = self._generate_motion_blur_kernel()
img.data = self._apply_motion_blur(img.data, kernel)
return datapoint
def _generate_motion_blur_kernel(self):
kernel = torch.zeros((self.kernel_size, self.kernel_size))
direction = random.choice(["horizontal", "vertical", "diagonal"])
if direction == "horizontal":
kernel[self.kernel_size // 2, :] = 1.0
elif direction == "vertical":
kernel[:, self.kernel_size // 2] = 1.0
elif direction == "diagonal":
for i in range(self.kernel_size):
kernel[i, i] = 1.0
kernel /= kernel.sum()
return kernel
def _apply_motion_blur(self, image, kernel):
if isinstance(image, PILImage.Image):
image = F.to_tensor(image)
channels = image.shape[0]
kernel = kernel.to(image.device).unsqueeze(0).unsqueeze(0)
blurred_image = torch.nn.functional.conv2d(
image.unsqueeze(0),
kernel.repeat(channels, 1, 1, 1),
padding=self.kernel_size // 2,
groups=channels,
)
return F.to_pil_image(blurred_image.squeeze(0))
class LargeScaleJitter:
def __init__(
self,
scale_range=(0.1, 2.0),
aspect_ratio_range=(0.75, 1.33),
crop_size=(640, 640),
consistent_transform=True,
p=0.5,
):
"""
Args:rack
scale_range (tuple): Range of scaling factors (min_scale, max_scale).
aspect_ratio_range (tuple): Range of aspect ratios (min_aspect_ratio, max_aspect_ratio).
crop_size (tuple): Target size of the cropped region (width, height).
consistent_transform (bool): Whether to apply the same transformation across all frames.
p (float): Probability of applying the transformation.
"""
self.scale_range = scale_range
self.aspect_ratio_range = aspect_ratio_range
self.crop_size = crop_size
self.consistent_transform = consistent_transform
self.p = p
def __call__(self, datapoint: Datapoint, **kwargs):
if random.random() >= self.p:
return datapoint
# Sample a single scale factor and aspect ratio for all frames
log_ratio = torch.log(torch.tensor(self.aspect_ratio_range))
scale_factor = torch.empty(1).uniform_(*self.scale_range).item()
aspect_ratio = torch.exp(
torch.empty(1).uniform_(log_ratio[0], log_ratio[1])
).item()
for idx, img in enumerate(datapoint.images):
if not self.consistent_transform:
# Sample a new scale factor and aspect ratio for each frame
log_ratio = torch.log(torch.tensor(self.aspect_ratio_range))
scale_factor = torch.empty(1).uniform_(*self.scale_range).item()
aspect_ratio = torch.exp(
torch.empty(1).uniform_(log_ratio[0], log_ratio[1])
).item()
# Compute the dimensions of the jittered crop
original_width, original_height = img.data.size
target_area = original_width * original_height * scale_factor
crop_width = int(round((target_area * aspect_ratio) ** 0.5))
crop_height = int(round((target_area / aspect_ratio) ** 0.5))
# Randomly select the top-left corner of the crop
crop_x = random.randint(0, max(0, original_width - crop_width))
crop_y = random.randint(0, max(0, original_height - crop_height))
# Extract the cropped region
datapoint = crop(datapoint, idx, (crop_x, crop_y, crop_width, crop_height))
# Resize the cropped region to the target crop size
datapoint = resize(datapoint, idx, self.crop_size)
return datapoint
|