File size: 26,869 Bytes
1e503d5 3fcde73 1e503d5 3fcde73 24ff807 1e503d5 3fcde73 1e503d5 3fcde73 | 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 | import os
import sys
import time
import math
import importlib
import random
import logging
import warnings
from functools import lru_cache, partial
from collections import Counter
from typing import Optional, Callable, Union, Tuple, List
from packaging import version
import json
import cv2
import numpy as np
from PIL import Image
import torch
import torchvision
from torchvision.transforms import v2
########
logger = logging.getLogger(__name__)
def is_torchcodec_available() -> bool:
import importlib.util
return importlib.util.find_spec("torchcodec") is not None
def is_decord_available() -> bool:
import importlib.util
return importlib.util.find_spec("decord") is not None
def default_device() -> str:
# Priority: cuda > mps > cpu
if torch.cuda.is_available():
return "cuda"
mps_backend = getattr(torch.backends, "mps", None)
if mps_backend is not None and torch.backends.mps.is_available():
return "mps"
return "cpu"
def batched_resize(
imgs: torch.Tensor, width: int, height: int,
method=1, interp='bicubic', channel_first=False
) -> torch.Tensor:
cv_interp = dict(
bicubic=cv2.INTER_CUBIC,
bilinear=cv2.INTER_LINEAR,
nearest=cv2.INTER_NEAREST,
area=cv2.INTER_AREA,
lanczos=cv2.INTER_LANCZOS4,
)[interp]
v2_interp = dict(
bicubic=v2.InterpolationMode.BICUBIC,
bilinear=v2.InterpolationMode.BILINEAR,
nearest=v2.InterpolationMode.NEAREST,
area=v2.InterpolationMode.BOX,
lanczos=v2.InterpolationMode.LANCZOS,
)[interp]
if method == 1: # OpenCV sequential resize
if channel_first:
imgs = imgs.permute(0, 2, 3, 1) # NCHW to NHWC
N = imgs.shape[0]
C = imgs.shape[-1]
imgs_np = imgs.cpu().numpy()
imgs_np_resized = np.empty_like(imgs_np, shape=(N, height, width, C))
for i in range(N):
cv2.resize(
imgs_np[i], (width, height), imgs_np_resized[i],
interpolation=cv_interp
)
imgs = torch.from_numpy(imgs_np_resized)
if channel_first:
imgs = imgs.permute(0, 3, 1, 2) # NHWC to NCHW
else: # torchvision resize
if not channel_first:
imgs = imgs.permute(0, 3, 1, 2) # NHWC to NCHW
imgs = v2.functional.resize(imgs, [height, width], interpolation=v2_interp, antialias=True)
if not channel_first:
imgs = imgs.permute(0, 2, 3, 1) # NCHW to NHWC
return imgs
########
# Read video backends
def _read_video_torchcodec(
video_path: str, sampler: Optional[Callable] = None,
) -> Tuple[torch.Tensor, float]:
tcd = importlib.import_module("torchcodec.decoders")
use_cuda = hasattr(tcd, 'set_cuda_backend') and torch.cuda.is_available()
VideoDecoder = tcd.set_cuda_backend("beta")(tcd.VideoDecoder) if use_cuda else tcd.VideoDecoder
decoder = VideoDecoder(
source=video_path,
dimension_order="NHWC",
num_ffmpeg_threads=0,
seek_mode="exact",
device="cuda" if use_cuda else "cpu",
)
src_frames = decoder.metadata.num_frames
src_fps = src_frames / decoder.metadata.duration_seconds
if src_fps < 1.0 or src_fps > 240.0:
print(f"warning: abnormal {src_fps=}")
src_fps = min(max(src_fps, 1.0), 240.0)
if sampler is None:
smp_frames = src_frames
frame_indices = list(range(smp_frames))
else:
smp_frames, frame_indices = sampler(src_fps, src_frames)
smp_fps = smp_frames / max(src_frames, 1e-6) * src_fps
video = decoder.get_frames_at(indices=frame_indices)
video = video.data.cpu()
return video, smp_fps
def _read_video_torchvision(
video_path: str, sampler: Optional[Callable] = None,
pts_unit: str = "sec", start_pts: float = 0.0, end_pts: float = None,
) -> Tuple[torch.Tensor, float]:
if version.parse(torchvision.__version__) < version.parse("0.19.0"):
if "http://" in video_path or "https://" in video_path:
warnings.warn("torchvision < 0.19.0 does not support http/https video path, please upgrade to 0.19.0.")
video, audio, info = torchvision.io.read_video(
video_path, output_format="THWC",
start_pts=start_pts, end_pts=end_pts, pts_unit=pts_unit,
)
src_frames, src_fps = video.size(0), info["video_fps"]
if sampler is None:
smp_frames = src_frames
else:
smp_frames, frame_indices = sampler(src_fps, src_frames)
video = video[frame_indices]
smp_fps = smp_frames / max(src_frames, 1e-6) * src_fps
return video, smp_fps
def _read_video_decord(
video_path: str, sampler: Optional[Callable] = None,
) -> Tuple[torch.Tensor, float]:
import decord
vr = decord.VideoReader(video_path)
src_frames, src_fps = len(vr), vr.get_avg_fps()
if sampler is None:
smp_frames = src_frames
frame_indices = list(range(smp_frames))
else:
smp_frames, frame_indices = sampler(src_fps, src_frames)
smp_fps = smp_frames / max(src_frames, 1e-6) * src_fps
video = vr.get_batch(frame_indices)
video = torch.from_numpy(video.asnumpy()) # THWC
return video, smp_fps
VIDEO_READER_BACKENDS = {
"torchcodec": _read_video_torchcodec,
"decord": _read_video_decord,
"torchvision": _read_video_torchvision,
}
FORCE_BAILINGNATIVE_VIDEO_READER = os.getenv("FORCE_BAILINGNATIVE_VIDEO_READER", None)
@lru_cache(maxsize=1)
def get_video_reader_backend() -> str:
if FORCE_BAILINGNATIVE_VIDEO_READER is not None:
video_reader_backend = FORCE_BAILINGNATIVE_VIDEO_READER
elif is_torchcodec_available():
video_reader_backend = "torchcodec"
elif is_decord_available():
video_reader_backend = "decord"
else:
video_reader_backend = "torchvision"
print(f"bailing-native-utils using {video_reader_backend} to read video.", file=sys.stderr)
return video_reader_backend
def load_video(video_path: str, sampler: Optional[Callable] = None) -> Tuple[torch.Tensor, float]:
print(video_path)
if isinstance(video_path, str):
video_reader_backend = get_video_reader_backend()
video_path = video_path.removeprefix("file://")
st = time.time()
try:
video, smp_fps = VIDEO_READER_BACKENDS[video_reader_backend](video_path, sampler=sampler)
except Exception as e:
logger.warning(f"[{video_reader_backend}] error, fall back to torchvision: {e}")
video, smp_fps = VIDEO_READER_BACKENDS["torchvision"](
video_path, sampler=sampler
)
logger.info(f"[{video_reader_backend}] {video_path=}, {smp_fps=}, duration={time.time() - st:.3f}s")
else:
raise NotImplementedError("only support video path str input for now.")
# output format: THWC
return video, smp_fps
########
# V1 parameters
IMAGE_FACTOR = 32
MIN_PIXELS = 4 * 32 * 32
MAX_PIXELS = 16384 * 32 * 32
MAX_RATIO = 200
VIDEO_MIN_PIXELS = 43008
VIDEO_MAX_PIXELS = 174080
VIDEO_TOTAL_PIXELS = 4718592
FRAME_FACTOR = 2
FPS = 2.0
FPS_MIN_FRAMES = 4
FPS_MAX_FRAMES = 32
VideoInput = Union[
List["Image.Image"],
"np.ndarray",
"torch.Tensor",
List["np.ndarray"],
List["torch.Tensor"],
List[List["Image.Image"]],
List[List["np.ndarrray"]],
List[List["torch.Tensor"]],
]
########
# V1 sample
def v1_sample_frames(num_frames, total_frames, sample="sequence"):
if sample == "sequence":
frame_indices = np.linspace(0, total_frames - 1, num_frames, dtype=int)
else:
intervals = np.linspace(start=0, stop=total_frames, num=num_frames + 1, dtype=int)
ranges = []
for idx, interv in enumerate(intervals[:-1]):
ranges.append((interv, intervals[idx + 1] - 1))
if sample == "random":
try:
frame_indices = [random.choice(range(x[0], x[1])) for x in ranges]
except:
frame_indices = np.random.permutation(total_frames)[:num_frames]
frame_indices.sort()
frame_indices = list(frame_indices)
if len(frame_indices) < num_frames:
padded_frame_indices = [frame_indices[-1]] * num_frames
padded_frame_indices[:len(frame_indices)] = frame_indices
frame_indices = padded_frame_indices
elif sample == "uniform":
frame_indices = [(x[0] + x[1]) // 2 for x in ranges]
if len(frame_indices) < num_frames:
frame_indices = [
frame_indices[int((num_frames - 1) * i / (num_frames - 1) + 0.5)] for i in range(num_frames)
]
else:
raise NotImplementedError
return frame_indices
def v1_smart_nframes(
ele: dict,
total_frames: int,
video_fps: Union[int, float],
) -> int:
"""calculate the number of frames for video used for model inputs.
Args:
ele (dict): a dict contains the configuration of video.
support either `fps` or `nframes`:
- nframes: the number of frames to extract for model inputs.
- fps: the fps to extract frames for model inputs.
- min_frames: the minimum number of frames of the video, only used when fps is provided.
- max_frames: the maximum number of frames of the video, only used when fps is provided.
total_frames (int): the original total number of frames of the video.
video_fps (int | float): the original fps of the video.
Raises:
ValueError: nframes should in interval [FRAME_FACTOR, total_frames].
Returns:
int: the number of frames for video used for model inputs.
"""
assert not ("max_video_fps" in ele and "nframes" in ele), "Only accept either `max_video_fps` or `nframes`"
max_frames = max(
1,
floor_by_factor(
ele.get("max_frames", min(FPS_MAX_FRAMES, total_frames)), FRAME_FACTOR
),
)
if "nframes" in ele:
nframes = min(total_frames, round_by_factor(ele["nframes"], FRAME_FACTOR), max_frames)
else:
fps = ele.get("max_video_fps", FPS)
nframes = max(1, total_frames / video_fps * fps)
if nframes > total_frames:
logger.warning(f"smart_nframes: nframes[{nframes}] > total_frames[{total_frames}]")
nframes = min(min(nframes, max_frames), total_frames)
return int(nframes)
def v1_sample_video(video_fps, total_frames, ele: dict) -> List[int]:
sample_method = ele.get("sample", "sequence")
num_frames = v1_smart_nframes(ele, total_frames, video_fps)
frame_indices = v1_sample_frames(
num_frames=num_frames, total_frames=total_frames, sample=sample_method
)
return num_frames, frame_indices
########
# V1 pre-process
def round_by_factor(number: int, factor: int) -> int:
"""Returns the closest integer to 'number' that is divisible by 'factor'."""
return round(number / factor) * factor
def ceil_by_factor(number: int, factor: int) -> int:
"""Returns the smallest integer greater than or equal to 'number' that is divisible by 'factor'."""
return math.ceil(number / factor) * factor
def floor_by_factor(number: int, factor: int) -> int:
"""Returns the largest integer less than or equal to 'number' that is divisible by 'factor'."""
return math.floor(number / factor) * factor
def smart_resize(
height: int, width: int, factor: int = IMAGE_FACTOR, min_pixels: int = MIN_PIXELS, max_pixels: int = MAX_PIXELS
) -> tuple[int, int]:
"""
Rescales the image so that the following conditions are met:
1. Both dimensions (height and width) are divisible by 'factor'.
2. The total number of pixels is within the range ['min_pixels', 'max_pixels'].
3. The aspect ratio of the image is maintained as closely as possible.
"""
if max(height, width) / min(height, width) > MAX_RATIO:
raise ValueError(
f"absolute aspect ratio must be smaller than {MAX_RATIO}, got {max(height, width) / min(height, width)}"
)
h_bar = max(factor, round_by_factor(height, factor))
w_bar = max(factor, round_by_factor(width, factor))
if h_bar * w_bar > max_pixels:
beta = math.sqrt((height * width) / max_pixels)
h_bar = floor_by_factor(height / beta, factor)
w_bar = floor_by_factor(width / beta, factor)
elif h_bar * w_bar < min_pixels:
beta = math.sqrt(min_pixels / (height * width))
h_bar = ceil_by_factor(height * beta, factor)
w_bar = ceil_by_factor(width * beta, factor)
return h_bar, w_bar
########
# V1 fetch video
def v1_fetch_video(
ele: dict,
image_factor: int = IMAGE_FACTOR,
return_video_sample_fps: bool = False,
return_video_timestamp: bool = False,
return_metadata: bool = False,
) -> torch.Tensor | list[Image.Image]:
if isinstance(ele["video"], str) and 'frames_list://' not in ele["video"]:
video, smp_fps = load_video(
ele["video"], sampler=partial(v1_sample_video, ele=ele)
)
if "resized_height" in ele and "resized_width" in ele:
resized_height, resized_width = smart_resize(
ele["resized_height"],
ele["resized_width"],
factor=image_factor,
)
else:
num_frames, height, width, channels = video.shape
min_pixels = ele.get("min_pixels", VIDEO_MIN_PIXELS)
total_pixels = ele.get("total_pixels", VIDEO_TOTAL_PIXELS)
max_pixels = max(total_pixels / num_frames * FRAME_FACTOR, int(min_pixels * 1.05))
resized_height, resized_width = smart_resize(
height, width,
factor=image_factor, min_pixels=min_pixels, max_pixels=max_pixels,
)
print(f"fetch_video: {smp_fps=}, {num_frames=}, ({height}, {width}) => ({resized_height}, {resized_width})")
else:
if 'frames_list://' in ele["video"]:
ele["video"] = ele["video"].removeprefix('frames_list://')
ele["video"] = json.loads(ele["video"])
if len(ele["video"])%2 == 1:
ele["video"].append(ele["video"][-1]) # only support even frames
assert isinstance(ele["video"], (list, tuple))
process_info = ele.copy()
process_info.pop("type", None)
process_info.pop("video", None)
total_frames=len(ele['video'])
sample_method = ele.get("sample", "sequence")
smp_fps = process_info.pop("max_video_fps", FPS)
video = [
torch.from_numpy(np.array(Image.open(video_element).convert("RGB")))
for video_element in ele["video"]
]
frame_indices = v1_sample_frames(
num_frames=total_frames, total_frames=total_frames, sample=sample_method
)
if "resized_height" in ele and "resized_width" in ele:
resized_height, resized_width = smart_resize(
ele["resized_height"],
ele["resized_width"],
factor=image_factor,
)
else:
height, width, _ = video[1].shape
min_pixels = ele.get("min_pixels", VIDEO_MIN_PIXELS)
total_pixels = ele.get("total_pixels", VIDEO_TOTAL_PIXELS)
max_pixels = max(total_pixels / total_frames * FRAME_FACTOR, int(min_pixels * 1.05))
resized_height, resized_width = smart_resize(
height, width,
factor=image_factor, min_pixels=min_pixels, max_pixels=max_pixels,
)
# 3) gather shapes of the sampled frames
shapes = [ video[i].shape[:2] for i in frame_indices ] # list of (H,W)
# pick the shape that occurs most often
major_shape, _ = Counter(shapes).most_common(1)[0]
major_h, major_w = major_shape
# VNIAH could have one outlier shape
for idx in frame_indices:
h, w = video[idx].shape[:2]
if (h, w) != (major_h, major_w):
# re-open, resize via PIL + TF, then to tensor
img = Image.open(ele["video"][idx]).convert("RGB")
img = v2.functional.resize(
img,
[major_h, major_w],
interpolation=v2.InterpolationMode.BICUBIC,
antialias=True,
)
arr = np.array(img)
video[idx] = torch.from_numpy(arr)
video = torch.stack(video, dim=0)
logger.info(f"video-in: num_frames={video.shape[0]}, {resized_height=}, {resized_width=}")
video = batched_resize(video, resized_width, resized_height, method=1, interp='bicubic', channel_first=False)
video = video.permute(0, 3, 1, 2)
resmp_fps = smp_fps
resmp_ts = np.array([i / smp_fps for i in range(video.shape[0])])
num_pad_frames = FRAME_FACTOR - (len(video) % FRAME_FACTOR)
if num_pad_frames < FRAME_FACTOR:
video = torch.cat([video] + [video[-1:]] * num_pad_frames, dim=0)
resmp_ts = np.concatenate(
[resmp_ts, np.array([resmp_ts[-1] * num_pad_frames])],
)
if return_metadata:
metadata = {
"resmp_ts": resmp_ts,
"resmp_fps": resmp_fps,
}
return video, metadata
if return_video_timestamp:
return video, resmp_ts
if return_video_sample_fps:
return video, resmp_fps
return video
########
# V2 parameters
IMAGE_FACTOR = 16 * 2
FRAME_FACTOR = int(os.environ.get('FRAME_FACTOR', 2))
MAX_FPS = float(os.environ.get('MAX_FPS', 2.0))
MIN_PATCHES_PER_FRAME = int(os.environ.get('MIN_PATCHES_PER_FRAME', 144))
MAX_PATCHES_PER_FRAME = int(os.environ.get('MAX_PATCHES_PER_FRAME', 3072))
MAX_TOKENS = int(os.environ.get('MAX_TOKENS', 3072))
MAX_PATCHES = MAX_TOKENS * FRAME_FACTOR
MAX_PATCHES_OS = MAX_PATCHES * int(os.environ.get('MAX_PATCHES_OS_MULTIPLIER', 4))
MAX_FRAMES_OS = MAX_PATCHES_OS // MIN_PATCHES_PER_FRAME
SUBSAMPLE_PIXELS = 16384
SAD_MEAN_ANCHOR = SUBSAMPLE_PIXELS * int(os.environ.get('SAD_MEAN_ANCHOR_MULTIPLIER', 20))
SAD_SUM_RATIO_SCALE = 1.0
SAD_SUM_RATIO_OFFSET = 0.0
MAX_SAMPLE_INTERVAL = float(os.environ.get('MAX_SAMPLE_INTERVAL', 4.0))
########
# V2 sample
def v2_sample_video(
src_fps: float,
src_frames: int,
) -> Tuple[int, float]:
"""calculate the number of frames and fps for video used for model inputs.
Args:
ele (dict): a dict contains the configuration of video.
src_fps (float): the original fps of the video.
src_frames (int): the original total number of frames of the video.
Returns:
Tuple[int, float]: the number of frames and fps for video used for model inputs.
"""
# init sample params
smp_fps = min(MAX_FPS, src_fps)
smp_frames = max(1, min(MAX_FRAMES_OS, int(src_frames / src_fps * smp_fps + 0.5)))
smp_fps = smp_frames / max(src_frames / src_fps, 1e-6)
print(f"v2_sample_video: {src_fps=} -> {smp_fps=}, {src_frames=} -> {smp_frames=}")
frame_indices = np.linspace(0, src_frames - 1, smp_frames, dtype=int)
return smp_frames, frame_indices
########
# V2 pre-process
def ts2cv(imgs_ts: torch.Tensor, channel_first=False, dtype=None) -> np.ndarray:
if channel_first:
imgs_ts = imgs_ts.permute(0, 2, 3, 1)
imgs_cv = imgs_ts.to(dtype=dtype, device="cpu").numpy() # shape (N, H, W, C)
return imgs_cv
def cv2ts(imgs_cv: np.ndarray, channel_first=False, dtype=None, device=None) -> torch.Tensor:
if not channel_first:
imgs_cv = imgs_cv.transpose(0, 3, 1, 2) # shape (N, C, H, W)
imgs_ts = torch.from_numpy(imgs_cv).to(dtype=dtype, device=device)
return imgs_ts
def cv_cvtColor(imgs: torch.Tensor, code: int=cv2.COLOR_RGB2GRAY) -> torch.Tensor:
# using OpenCV for RGB to Gray conversion
gray_imgs = np.array([cv2.cvtColor(img, code) for img in imgs]) # shape (N, H, W, C)
if len(gray_imgs.shape) == 3:
gray_imgs = gray_imgs[:, :, :, np.newaxis] # shape (N, H, W, 1)
return gray_imgs
def cv_resize(imgs: np.ndarray, width: int, height: int, interp='bicubic') -> np.ndarray:
cv_interp = dict(
bicubic=cv2.INTER_CUBIC,
bilinear=cv2.INTER_LINEAR,
nearest=cv2.INTER_NEAREST,
area=cv2.INTER_AREA,
lanczos=cv2.INTER_LANCZOS4,
)[interp]
N, H, W, C = imgs.shape
imgs_resized = np.empty_like(imgs, shape=(N, height, width, C))
for i in range(N):
cv2.resize(
imgs[i], (width, height), imgs_resized[i],
interpolation=cv_interp
)
return imgs_resized
def cv_frames_SAD(frames: np.ndarray) -> np.ndarray:
"""calculate the sum of absolute differences (SAD) between consecutive frames.
Args:
frames (np.ndarray): the input video frames, shape (T, H, W, C).
Returns:
np.ndarray: the SAD values between consecutive frames, shape (T-1,).
"""
diffs = cv2.absdiff(frames[1:], frames[:-1]) # shape (T-1, H, W, C)
sad_values = diffs.sum(axis=(1, 2, 3)) # shape (T-1,)
return sad_values
def compute_sad_sum_ratio(x: np.ndarray, anchor, scale, offset) -> float:
y = np.log(x + 1) - np.log(anchor)
y = np.tanh(y) * (0.5 * scale) - 0.5
y = np.clip(offset - y, 0, 1)
return y
def resample_SAD(frames: torch.Tensor, smp_fps: float) -> Tuple[List[int], np.ndarray]:
"""resample video frames based on SAD values.
Args:
frames (torch.Tensor): the input video frames, shape (T, C, H, W).
Returns:
Tuple[List[int], np.ndarray]: the resampled video frame indices and SAD values.
"""
if len(frames) <= 1:
return list(range(len(frames))), np.array([])
max_sample_interval = max(1, int(MAX_SAMPLE_INTERVAL * smp_fps + 0.5))
# compute SAD values
_frames = ts2cv(frames)
scale_ratio = SUBSAMPLE_PIXELS / (_frames.shape[-3] * _frames.shape[-2])
sub_width = int(_frames.shape[-2] * scale_ratio ** 0.5 + 0.5)
sub_height = int(_frames.shape[-3] * scale_ratio ** 0.5 + 0.5)
_frames = cv_resize(_frames, width=sub_width, height=sub_height, interp='bilinear')
_frames = cv_cvtColor(_frames, cv2.COLOR_RGB2Lab)
sad_values = cv_frames_SAD(_frames)
# compute sorted SAD and its integral
sad_sorted = np.sort(sad_values)
sad_integral = np.cumsum(sad_sorted)
sad_sum = sad_integral[-1]
sad_mean = sad_sum / len(sad_values)
# compute dynamic SAD threshold
sad_sum_ratio = compute_sad_sum_ratio(
sad_mean,
anchor=SAD_MEAN_ANCHOR,
scale=SAD_SUM_RATIO_SCALE,
offset=SAD_SUM_RATIO_OFFSET,
)
print(f"{sad_mean=}, {sad_sum_ratio=}")
# compute SAD threshold
sad_sum_thr = sad_sum * sad_sum_ratio
sad_idx = np.searchsorted(sad_integral, sad_sum_thr)
sad_value_thr = sad_sorted[sad_idx]
print(f"{sad_sum=}, {sad_sum_thr=}, {sad_idx=}, {sad_value_thr=}")
# filter frames based on SAD threshold
key_frame_indices = [0]
for i, sad in enumerate(sad_values):
if sad >= sad_value_thr:
key_frame_indices.append(i + 1)
if key_frame_indices[-1] <= len(sad_values) - max_sample_interval:
key_frame_indices.append(len(sad_values))
# ensure max sample interval
key_frame_indices_new = [key_frame_indices[0]]
for i in range(1, len(key_frame_indices)):
sample_interval = key_frame_indices[i] - key_frame_indices[i - 1]
if sample_interval >= max_sample_interval:
num_additional_frames = sample_interval // max_sample_interval
additional_frames_interval = sample_interval / (num_additional_frames + 1)
for j in range(1, num_additional_frames + 1):
new_idx = key_frame_indices[i - 1] + int(j * additional_frames_interval + 0.5)
key_frame_indices_new.append(new_idx)
if key_frame_indices_new[-1] != key_frame_indices[i]:
key_frame_indices_new.append(key_frame_indices[i])
key_frame_indices = key_frame_indices_new
print(f"num_key_frames={len(key_frame_indices)}, {key_frame_indices=}, ")
return key_frame_indices, sad_values
########
# V2 fetch video
def v2_fetch_video(
ele: dict, image_factor: int = IMAGE_FACTOR,
return_video_sample_fps: bool = False,
return_video_timestamp: bool = False,
return_metadata: bool = False,
slice_frames: slice | None = None,
) -> torch.Tensor | list[Image.Image]:
# load video
video, smp_fps = load_video(ele["video"], sampler=v2_sample_video)
if slice_frames is not None:
video = video[slice_frames]
smp_video = video
# resample frames
smp_frames = len(video)
smp_tokens = (video.shape[-3] // image_factor) * (video.shape[-2] // image_factor) * (smp_frames // FRAME_FACTOR)
resmp_indices, sad_values = resample_SAD(video, smp_fps)
resmp_ts = np.array(resmp_indices) / smp_fps
video = video[resmp_indices]
resmp_video = video
# duplicate last frame to make it divisible by FRAME_FACTOR
num_pad_frames = FRAME_FACTOR - (len(video) % FRAME_FACTOR)
if num_pad_frames < FRAME_FACTOR:
video = torch.cat([video] + [video[-1:]] * num_pad_frames, dim=0)
resmp_fps = len(video) / smp_frames * smp_fps if smp_frames > 0 else smp_fps
# resize frames
resmp_frames, src_h, src_w, channels = video.shape
src_patches = (src_h * src_w) / (image_factor * image_factor)
patches_per_frame = max(MIN_PATCHES_PER_FRAME, min(MAX_PATCHES_PER_FRAME, MAX_PATCHES / resmp_frames))
scale_ratio = (patches_per_frame / src_patches) ** 0.5
dst_w = int(src_w * scale_ratio / image_factor + 0.5) * image_factor
dst_h = int(src_h * scale_ratio / image_factor + 0.5) * image_factor
resmp_tokens = (dst_h // image_factor) * (dst_w // image_factor) * (resmp_frames // FRAME_FACTOR)
print(f"fetch_video_v2: {smp_tokens=} -> {resmp_tokens=}, {smp_fps=} -> {resmp_fps=}, "
f"{smp_frames=} -> {resmp_frames=}, ({src_w}, {src_h}) => ({dst_w}, {dst_h})")
video = batched_resize(video, dst_w, dst_h, method=1, interp='bicubic', channel_first=False)
video = video.permute(0, 3, 1, 2)
if return_metadata:
metadata = {
# "smp_fps": smp_fps,
# "smp_frames": smp_frames,
# "smp_tokens": smp_tokens,
# "smp_video": smp_video,
# "sad_values": sad_values,
# "resmp_indices": resmp_indices,
"resmp_ts": resmp_ts,
# "resmp_video": resmp_video,
"resmp_fps": resmp_fps,
# "resmp_frames": resmp_frames,
# "resmp_tokens": resmp_tokens,
}
return video, metadata
if return_video_timestamp:
return video, resmp_ts
if return_video_sample_fps:
return video, resmp_fps
return video
########
|