Spaces:
Runtime error
Runtime error
File size: 5,240 Bytes
ebb9c75 |
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 |
from __future__ import annotations
import datetime
import os
import pathlib
import shlex
import shutil
import subprocess
import sys
import gradio as gr
import slugify
import torch
import numpy as np
import huggingface_hub
from huggingface_hub import HfApi
from omegaconf import OmegaConf
from segment_anything import sam_model_registry
from dinov2.models import vision_transformer as vits
import dinov2.utils.utils as dinov2_utils
from gradio_demo.oss_ops_inference import main_oss_ops
ORIGINAL_SPACE_ID = ''
SPACE_ID = os.getenv('SPACE_ID', ORIGINAL_SPACE_ID)
class Runner:
def __init__(self, hf_token: str | None = None):
self.hf_token = hf_token
# self.checkpoint_dir = pathlib.Path('checkpoints')
# self.checkpoint_dir.mkdir(exist_ok=True)
# oss, ops
self.prompt_res_g = None
self.prompt_mask_g = None
self.tar1_res_g = None
self.tar2_res_g = None
self.version = 1
self.pred_masks = None
self.pred_mask_lists = None
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
sam_checkpoint = "models/sam_vit_h_4b8939.pth"
model_type = "default"
self.sam = sam_model_registry[model_type](checkpoint=sam_checkpoint)
self.sam.to(device=device)
dinov2_kwargs = dict(
img_size=518,
patch_size=14,
init_values=1e-5,
ffn_layer='mlp',
block_chunks=0,
qkv_bias=True,
proj_bias=True,
ffn_bias=True,
)
dinov2 = vits.__dict__["vit_large"](**dinov2_kwargs)
dinov2_utils.load_pretrained_weights(dinov2, "models/dinov2_vitl14_pretrain.pth", "teacher")
dinov2.eval()
dinov2.to(device=device)
self.dinov2 = dinov2
def inference_oss_ops(self, prompt, target1, target2, version):
if version == 'version 1 (πΊ multiple instances π» whole, π» part)':
self.prompt_res_g, self.tar1_res_g, self.tar2_res_g = prompt['image'], target1, target2
self.prompt_mask_g = (prompt['mask'][..., 0] != 0)[None, ...] # 1, H, w
self.version = 1
elif version == 'version 2 (π» multiple instances πΊ whole, π» part)':
self.prompt_res_g, self.tar1_res_g, self.tar2_res_g = prompt['image'], target1, target2
self.prompt_mask_g = (prompt['mask'][..., 0] != 0)[None, ...] # 1, H, w
self.version = 2
else:
self.prompt_res_g, self.tar1_res_g, self.tar2_res_g = prompt['image'], target1, target2
self.prompt_mask_g = (prompt['mask'][..., 0] != 0)[None, ...] # 1, H, w
self.version = 3
self.pred_masks, self.pred_mask_lists = main_oss_ops(
sam=self.sam,
dinov2=self.dinov2,
support_img=self.prompt_res_g,
support_mask=self.prompt_mask_g,
query_img_1=self.tar1_res_g,
query_img_2=self.tar2_res_g,
version=self.version
)
text = "Process Successful!"
return text
def clear_fn(self):
self.prompt_res_g, self.tar1_res_g, self.tar2_res_g, self.prompt_mask_g = None, None, None, None
self.version = 1
self.pred_masks = None
self.pred_mask_lists = None
return [None] * 7
def controllable_mask_output(self, k):
color = np.array([30, 144, 255])
if self.version != 1:
prompt_mask_res, tar1_mask_res, tar2_mask_res = self.pred_masks
h, w = prompt_mask_res.shape[-2:]
prompt_mask_img = prompt_mask_res.reshape(h, w, 1) * color.reshape(1, 1, -1)
prompt_mask_res = self.prompt_res_g * 0.5 + prompt_mask_img * 0.5
h, w = tar1_mask_res.shape[-2:]
tar1_mask_img = tar1_mask_res.reshape(h, w, 1) * color.reshape(1, 1, -1)
tar1_mask_res = self.tar1_res_g * 0.5 + tar1_mask_img * 0.5
h, w = tar2_mask_res.shape[-2:]
tar2_mask_img = tar2_mask_res.reshape(h, w, 1) * color.reshape(1, 1, -1)
tar2_mask_res = self.tar2_res_g * 0.5 + tar2_mask_img * 0.5
else:
prompt_mask_res = self.pred_masks[0]
tar1_mask_res, tar2_mask_res = self.pred_mask_lists[1:]
tar1_mask_res = tar1_mask_res[:min(k, len(tar1_mask_res))].sum(0)>0
tar2_mask_res = tar2_mask_res[:min(k, len(tar2_mask_res))].sum(0) > 0
h, w = prompt_mask_res.shape[-2:]
prompt_mask_img = prompt_mask_res.reshape(h, w, 1) * color.reshape(1, 1, -1)
prompt_mask_res = self.prompt_res_g * 0.5 + prompt_mask_img * 0.5
h, w = tar1_mask_res.shape[-2:]
tar1_mask_img = tar1_mask_res.reshape(h, w, 1) * color.reshape(1, 1, -1)
tar1_mask_res = self.tar1_res_g * 0.5 + tar1_mask_img * 0.5
h, w = tar2_mask_res.shape[-2:]
tar2_mask_img = tar2_mask_res.reshape(h, w, 1) * color.reshape(1, 1, -1)
tar2_mask_res = self.tar2_res_g * 0.5 + tar2_mask_img * 0.5
return prompt_mask_res/255, tar1_mask_res/255, tar2_mask_res/255
def inference_vos(self, prompt_vid, vid):
raise NotImplementedError
|