release infer and demo
This commit is contained in:
@@ -0,0 +1,33 @@
|
||||
import torch
|
||||
import functools
|
||||
|
||||
def get_default_device():
|
||||
if torch.cuda.is_available():
|
||||
return torch.device("cuda")
|
||||
elif torch.backends.mps.is_built() and torch.backends.mps.is_available():
|
||||
return torch.device("mps")
|
||||
else:
|
||||
return torch.device("cpu")
|
||||
|
||||
def safe_autocast_decorator(enabled=True):
|
||||
def decorator(func):
|
||||
@functools.wraps(func)
|
||||
def wrapper(*args, **kwargs):
|
||||
device = get_default_device()
|
||||
if device.type in ["cuda", "cpu"]:
|
||||
with torch.amp.autocast(device_type=device.type, enabled=enabled):
|
||||
return func(*args, **kwargs)
|
||||
else:
|
||||
return func(*args, **kwargs)
|
||||
return wrapper
|
||||
return decorator
|
||||
|
||||
import contextlib
|
||||
@contextlib.contextmanager
|
||||
def safe_autocast(enabled=True):
|
||||
device = get_default_device()
|
||||
if device.type in ["cuda", "cpu"]:
|
||||
with torch.amp.autocast(device_type=device.type, enabled=enabled):
|
||||
yield
|
||||
else:
|
||||
yield # MPS or other unsupported backends skip autocast
|
||||
@@ -0,0 +1,27 @@
|
||||
"""
|
||||
A helper function to get a default model for quick testing
|
||||
"""
|
||||
from omegaconf import open_dict
|
||||
from hydra import compose, initialize
|
||||
|
||||
import torch
|
||||
from matanyone2.model.matanyone2 import MatAnyone2
|
||||
|
||||
def get_matanyone2_model(ckpt_path, device=None) -> MatAnyone2:
|
||||
initialize(version_base='1.3.2', config_path="../config", job_name="eval_our_config")
|
||||
cfg = compose(config_name="eval_matanyone_config")
|
||||
|
||||
with open_dict(cfg):
|
||||
cfg['weights'] = ckpt_path
|
||||
|
||||
# Load the network weights
|
||||
if device is not None:
|
||||
matanyone2 = MatAnyone2(cfg, single_object=True).to(device).eval()
|
||||
model_weights = torch.load(cfg.weights, map_location=device)
|
||||
else: # if device is not specified, `.cuda()` by default
|
||||
matanyone2 = MatAnyone2(cfg, single_object=True).cuda().eval()
|
||||
model_weights = torch.load(cfg.weights)
|
||||
|
||||
matanyone2.load_weights(model_weights)
|
||||
|
||||
return matanyone2
|
||||
@@ -0,0 +1,54 @@
|
||||
import os
|
||||
import cv2
|
||||
import random
|
||||
import numpy as np
|
||||
|
||||
import torch
|
||||
import torchvision
|
||||
|
||||
IMAGE_EXTENSIONS = ('.jpg', '.jpeg', '.png', '.JPG', '.JPEG', '.PNG')
|
||||
VIDEO_EXTENSIONS = ('.mp4', '.mov', '.avi', '.MP4', '.MOV', '.AVI')
|
||||
|
||||
def read_frame_from_videos(frame_root):
|
||||
if frame_root.endswith(VIDEO_EXTENSIONS): # Video file path
|
||||
video_name = os.path.basename(frame_root)[:-4]
|
||||
frames, _, info = torchvision.io.read_video(filename=frame_root, pts_unit='sec', output_format='TCHW') # RGB
|
||||
fps = info['video_fps']
|
||||
else:
|
||||
video_name = os.path.basename(frame_root)
|
||||
frames = []
|
||||
fr_lst = sorted(os.listdir(frame_root))
|
||||
for fr in fr_lst:
|
||||
frame = cv2.imread(os.path.join(frame_root, fr))[...,[2,1,0]] # RGB, HWC
|
||||
frames.append(frame)
|
||||
fps = 24 # default
|
||||
frames = torch.Tensor(np.array(frames)).permute(0, 3, 1, 2).contiguous() # TCHW
|
||||
|
||||
length = frames.shape[0]
|
||||
|
||||
return frames, fps, length, video_name
|
||||
|
||||
def get_video_paths(input_root):
|
||||
video_paths = []
|
||||
for root, _, files in os.walk(input_root):
|
||||
for file in files:
|
||||
if file.lower().endswith(VIDEO_EXTENSIONS):
|
||||
video_paths.append(os.path.join(root, file))
|
||||
return sorted(video_paths)
|
||||
|
||||
def str_to_list(value):
|
||||
return list(map(int, value.split(',')))
|
||||
|
||||
def gen_dilate(alpha, min_kernel_size, max_kernel_size):
|
||||
kernel_size = random.randint(min_kernel_size, max_kernel_size)
|
||||
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size,kernel_size))
|
||||
fg_and_unknown = np.array(np.not_equal(alpha, 0).astype(np.float32))
|
||||
dilate = cv2.dilate(fg_and_unknown, kernel, iterations=1)*255
|
||||
return dilate.astype(np.float32)
|
||||
|
||||
def gen_erosion(alpha, min_kernel_size, max_kernel_size):
|
||||
kernel_size = random.randint(min_kernel_size, max_kernel_size)
|
||||
kernel = cv2.getStructuringElement(cv2.MORPH_ELLIPSE, (kernel_size,kernel_size))
|
||||
fg = np.array(np.equal(alpha, 255).astype(np.float32))
|
||||
erode = cv2.erode(fg, kernel, iterations=1)*255
|
||||
return erode.astype(np.float32)
|
||||
@@ -0,0 +1,62 @@
|
||||
from typing import List, Iterable
|
||||
import torch
|
||||
import torch.nn.functional as F
|
||||
from matanyone2.utils.device import safe_autocast
|
||||
|
||||
# STM
|
||||
def pad_divide_by(in_img: torch.Tensor, d: int) -> (torch.Tensor, Iterable[int]):
|
||||
h, w = in_img.shape[-2:]
|
||||
|
||||
if h % d > 0:
|
||||
new_h = h + d - h % d
|
||||
else:
|
||||
new_h = h
|
||||
if w % d > 0:
|
||||
new_w = w + d - w % d
|
||||
else:
|
||||
new_w = w
|
||||
lh, uh = int((new_h - h) / 2), int(new_h - h) - int((new_h - h) / 2)
|
||||
lw, uw = int((new_w - w) / 2), int(new_w - w) - int((new_w - w) / 2)
|
||||
pad_array = (int(lw), int(uw), int(lh), int(uh))
|
||||
out = F.pad(in_img, pad_array)
|
||||
return out, pad_array
|
||||
|
||||
|
||||
def unpad(img: torch.Tensor, pad: Iterable[int]) -> torch.Tensor:
|
||||
if len(img.shape) == 4:
|
||||
if pad[2] + pad[3] > 0:
|
||||
img = img[:, :, pad[2]:-pad[3], :]
|
||||
if pad[0] + pad[1] > 0:
|
||||
img = img[:, :, :, pad[0]:-pad[1]]
|
||||
elif len(img.shape) == 3:
|
||||
if pad[2] + pad[3] > 0:
|
||||
img = img[:, pad[2]:-pad[3], :]
|
||||
if pad[0] + pad[1] > 0:
|
||||
img = img[:, :, pad[0]:-pad[1]]
|
||||
elif len(img.shape) == 5:
|
||||
if pad[2] + pad[3] > 0:
|
||||
img = img[:, :, :, pad[2]:-pad[3], :]
|
||||
if pad[0] + pad[1] > 0:
|
||||
img = img[:, :, :, :, pad[0]:-pad[1]]
|
||||
else:
|
||||
raise NotImplementedError
|
||||
return img
|
||||
|
||||
|
||||
# @torch.jit.script
|
||||
def aggregate(prob: torch.Tensor, dim: int) -> torch.Tensor:
|
||||
with safe_autocast(enabled=False):
|
||||
prob = prob.float()
|
||||
new_prob = torch.cat([torch.prod(1 - prob, dim=dim, keepdim=True), prob],
|
||||
dim).clamp(1e-7, 1 - 1e-7)
|
||||
logits = torch.log((new_prob / (1 - new_prob))) # (0, 1) --> (-inf, inf)
|
||||
|
||||
return logits
|
||||
|
||||
|
||||
# @torch.jit.script
|
||||
def cls_to_one_hot(cls_gt: torch.Tensor, num_objects: int) -> torch.Tensor:
|
||||
# cls_gt: B*1*H*W
|
||||
B, _, H, W = cls_gt.shape
|
||||
one_hot = torch.zeros(B, num_objects + 1, H, W, device=cls_gt.device).scatter_(1, cls_gt, 1)
|
||||
return one_hot
|
||||
Reference in New Issue
Block a user