fab-body-estimation / fab /data /utils /prepare_batch.py
multimodalart's picture
multimodalart HF Staff
Upload folder using huggingface_hub
83c6c2d verified
Raw History Blame Contribute Delete
2.63 kB
# 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