Spaces:
Running on Zero
Running on Zero
File size: 2,626 Bytes
83c6c2d | 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 | # Copyright (c) Meta Platforms, Inc. and affiliates.
# SPDX-License-Identifier: LicenseRef-SAM-License
# From SAM 3D Body (https://github.com/facebookresearch/sam-3d-body), modified for FAB; licence: third_party_licenses/SAM-License.txt
import numpy as np
import torch
from torch.utils.data import default_collate
class NoCollate:
def __init__(self, data):
self.data = data
def prepare_batch(
img,
transform,
boxes,
masks=None,
masks_score=None,
cam_int=None,
include_masks=True,
):
"""Prepare an inference batch; mask-free callers may omit generated empty masks.
Supplied masks cannot be omitted. The default preserves the original mask-aware path.
"""
if not include_masks and (masks is not None or masks_score is not None):
raise ValueError("Cannot omit supplied masks or mask scores")
height, width = img.shape[:2]
# construct batch data samples
data_list = []
for idx in range(boxes.shape[0]):
data_info = dict(img=img)
data_info["bbox"] = boxes[idx] # shape (4,)
data_info["bbox_format"] = "xyxy"
if masks is not None:
data_info["mask"] = masks[idx].copy()
if masks_score is not None:
data_info["mask_score"] = masks_score[idx]
else:
data_info["mask_score"] = np.array(1.0, dtype=np.float32)
elif include_masks:
data_info["mask"] = np.zeros((height, width, 1), dtype=np.uint8)
data_info["mask_score"] = np.array(0.0, dtype=np.float32)
data_list.append(transform(data_info))
batch = default_collate(data_list)
max_num_person = batch["img"].shape[0]
for key in [
"img",
"img_size",
"ori_img_size",
"bbox_center",
"bbox_scale",
"bbox",
"affine_trans",
"mask",
"mask_score",
]:
if key in batch:
batch[key] = batch[key].unsqueeze(0).float()
if "mask" in batch:
batch["mask"] = batch["mask"].unsqueeze(2)
batch["person_valid"] = torch.ones((1, max_num_person))
if cam_int is not None:
batch["cam_int"] = cam_int.to(batch["img"])
else:
# Default camera intrinsics according image size
batch["cam_int"] = torch.tensor(
[
[
[(height**2 + width**2) ** 0.5, 0, width / 2.0],
[0, (height**2 + width**2) ** 0.5, height / 2.0],
[0, 0, 1],
]
],
).to(batch["img"])
batch["img_ori"] = [NoCollate(img)]
return batch
|