Spaces:
Sleeping
Sleeping
File size: 11,565 Bytes
71bb5e9 9145aca 71bb5e9 9fe654e 39570c3 daed59e 71bb5e9 39570c3 b0a14dc 71bb5e9 39570c3 64796cc fbeb0dc 64796cc fbeb0dc 64796cc a8e5fc3 71bb5e9 39570c3 9fe654e 71bb5e9 104f14f fbeb0dc 5d090b9 104f14f 39570c3 104f14f fbeb0dc 5d090b9 104f14f a8e5fc3 39570c3 a8e5fc3 e03c2d8 a8e5fc3 39570c3 a8e5fc3 39570c3 a8e5fc3 39570c3 a8e5fc3 39570c3 a8e5fc3 71bb5e9 a8e5fc3 9fe654e 39570c3 e0eb046 39570c3 e0eb046 39570c3 e0eb046 64fa430 a8e5fc3 5d090b9 0c9a1d7 a8e5fc3 fbeb0dc a8e5fc3 fbeb0dc 104f14f a8e5fc3 104f14f a8e5fc3 104f14f a8e5fc3 104f14f a8e5fc3 104f14f a8e5fc3 104f14f a8e5fc3 104f14f a8e5fc3 104f14f a8e5fc3 e0eb046 a8e5fc3 104f14f 39570c3 e0eb046 a8e5fc3 e0eb046 a8e5fc3 104f14f 39570c3 a8e5fc3 39570c3 a8e5fc3 64fa430 |
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 |
import sys
import threading
import streamlit as st
import numpy
import torch
import openshape
import transformers
from PIL import Image
from huggingface_hub import HfFolder, snapshot_download
from demo_support import retrieval, generation, utils, lvis
from collections import OrderedDict
@st.cache_resource
def load_openclip():
sys.clip_move_lock = threading.Lock()
clip_model, clip_prep = transformers.CLIPModel.from_pretrained(
"laion/CLIP-ViT-bigG-14-laion2B-39B-b160k",
low_cpu_mem_usage=True, torch_dtype=half,
offload_state_dict=True
), transformers.CLIPProcessor.from_pretrained("laion/CLIP-ViT-bigG-14-laion2B-39B-b160k")
if torch.cuda.is_available():
with sys.clip_move_lock:
clip_model.cuda()
return clip_model, clip_prep
@st.cache_resource
def load_openshape(name, to_cpu=False):
pce = openshape.load_pc_encoder(name)
if to_cpu:
pce = pce.cpu()
return pce
def load_tripletmix(name, to_cpu=False):
pce = openshape.load_pc_encoder_mix(name)
if to_cpu:
pce = pce.cpu()
return pce
def retrieval_filter_expand():
sim_th = st.sidebar.slider("Similarity Threshold", 0.05, 0.5, 0.1, key='rsimth')
tag = ""
face_min = 0
face_max = 34985808
anim_min = 0
anim_max = 563
tag_n = not bool(tag.strip())
anim_n = not (anim_min > 0 or anim_max < 563)
face_n = not (face_min > 0 or face_max < 34985808)
filter_fn = lambda x: (
(anim_n or anim_min <= x['anims'] <= anim_max)
and (face_n or face_min <= x['faces'] <= face_max)
and (tag_n or tag in x['tags'])
)
return sim_th, filter_fn
def retrieval_results(results):
st.caption("Click the link to view the 3D shape")
for i in range(len(results) // 4):
cols = st.columns(4)
for j in range(4):
idx = i * 4 + j
if idx >= len(results):
continue
entry = results[idx]
with cols[j]:
ext_link = f"https://objaverse.allenai.org/explore/?query={entry['u']}"
st.image(entry['img'])
# st.markdown(f"[![thumbnail {entry['desc'].replace('\n', ' ')}]({entry['img']})]({ext_link})")
# st.text(entry['name'])
quote_name = entry['name'].replace('[', '\\[').replace(']', '\\]').replace('\n', ' ')
st.markdown(f"[{quote_name}]({ext_link})")
def classification_lvis(load_data):
pc = load_data(prog)
col2 = utils.render_pc(pc)
prog.progress(0.5, "Running Classification")
ref_dev = next(model_classification.parameters()).device
enc = model_classification(torch.tensor(pc[:, [0, 2, 1, 3, 4, 5]].T[None], device=ref_dev))
sim = torch.matmul(torch.nn.functional.normalize(lvis.feats, dim=-1), torch.nn.functional.normalize(enc.cpu(), dim=-1).squeeze())
argsort = torch.argsort(sim, descending=True)
pred = OrderedDict((lvis.categories[i], sim[i]) for i in argsort if i < len(lvis.categories))
with col2:
for i, (cat, sim) in zip(range(5), pred.items()):
st.text(cat)
st.caption("Similarity %.4f" % sim)
prog.progress(1.0, "Idle")
def classification_custom(load_data, cats):
pc = load_data(prog)
col2 = utils.render_pc(pc)
prog.progress(0.5, "Computing Category Embeddings")
device = clip_model.device
tn = clip_prep(text=cats, return_tensors='pt', truncation=True, max_length=76, padding=True).to(device)
feats = clip_model.get_text_features(**tn).float().cpu()
prog.progress(0.5, "Running Classification")
ref_dev = next(model_classification.parameters()).device
enc = model_classification(torch.tensor(pc[:, [0, 2, 1, 3, 4, 5]].T[None], device=ref_dev))
sim = torch.matmul(torch.nn.functional.normalize(feats, dim=-1), torch.nn.functional.normalize(enc.cpu(), dim=-1).squeeze())
argsort = torch.argsort(sim, descending=True)
pred = OrderedDict((cats[i], sim[i]) for i in argsort if i < len(cats))
with col2:
for i, (cat, sim) in zip(range(5), pred.items()):
st.text(cat)
st.caption("Similarity %.4f" % sim)
prog.progress(1.0, "Idle")
def retrieval_pc(load_data, k, sim_th, filter_fn):
pc = load_data(prog)
prog.progress(0.5, "Computing Embeddings")
col2 = utils.render_pc(pc)
ref_dev = next(model_g14.parameters()).device
enc = model_g14(torch.tensor(pc[:, [0, 2, 1, 3, 4, 5]].T[None], device=ref_dev))
if model_name == "pb-sn-M":
enc = pc_adapter(enc)
sim = torch.matmul(torch.nn.functional.normalize(lvis.feats, dim=-1), torch.nn.functional.normalize(enc.cpu(), dim=-1).squeeze())
argsort = torch.argsort(sim, descending=True)
pred = OrderedDict((lvis.categories[i], sim[i]) for i in argsort if i < len(lvis.categories))
with col2:
for i, (cat, sim) in zip(range(5), pred.items()):
st.text(cat)
st.caption("Similarity %.4f" % sim)
prog.progress(0.7, "Running Retrieval")
retrieval_results(retrieval.retrieve(enc, k, sim_th, filter_fn))
prog.progress(1.0, "Idle")
def retrieval_img(pic, k, sim_th, filter_fn):
img = Image.open(pic)
prog.progress(0.5, "Computing Embeddings")
st.image(img)
device = clip_model.device
tn = clip_prep(images=[img], return_tensors="pt").to(device)
enc = clip_model.get_image_features(pixel_values=tn['pixel_values'].type(half)).float().cpu()
prog.progress(0.7, "Running Retrieval")
retrieval_results(retrieval.retrieve(enc, k, sim_th, filter_fn))
prog.progress(1.0, "Idle")
def retrieval_text(text, k, sim_th, filter_fn):
prog.progress(0.5, "Computing Embeddings")
device = clip_model.device
tn = clip_prep(text=[text], return_tensors='pt', truncation=True, max_length=76).to(device)
enc = clip_model.get_text_features(**tn).float().cpu()
prog.progress(0.7, "Running Retrieval")
retrieval_results(retrieval.retrieve(enc, k, sim_th, filter_fn))
prog.progress(1.0, "Idle")
def generation_img(load_data, prompt, noise_scale, cfg_scale, steps):
pc = load_data(prog)
prog.progress(0.5, "Running Generation")
col2 = utils.render_pc(pc)
if torch.cuda.is_available():
with sys.clip_move_lock:
clip_model.cpu()
width = 640
height = 640
img = generation.pc_to_image(
model_g14, pc, prompt, noise_scale, width, height, cfg_scale, steps,
lambda i, t, _: prog.progress(0.49 + i / (steps + 1) / 2, "Running Diffusion Step %d" % i)
)
if torch.cuda.is_available():
with sys.clip_move_lock:
clip_model.cuda()
with col2:
st.image(img)
prog.progress(1.0, "Idle")
def generation_text(load_data, cond_scale):
pc = load_data(prog)
prog.progress(0.5, "Running Generation")
col2 = utils.render_pc(pc)
cap = generation.pc_to_text(model_g14, pc, cond_scale)
st.text(cap)
prog.progress(1.0, "Idle")
try:
f32 = numpy.float32
half = torch.float16 if torch.cuda.is_available() else torch.bfloat16
clip_model, clip_prep = load_openclip()
#model_g14 = load_openshape('openshape-pointbert-vitg14-rgb')
#model_g14 = load_tripletmix('tripletmix-spconv-all')
st.caption("This demo presents three tasks: 3D classification, cross-modal retrieval, and cross-modal generation. Examples are provided for demonstration purposes. You're encouraged to fine-tune task parameters and upload files for customized testing as required.")
st.sidebar.title("TripletMix Demo Configuration Panel")
task = st.sidebar.selectbox(
'Task Selection',
("3D Classification", "Cross-modal retrieval", "Cross-modal generation")
)
if task == "3D Classification":
cls_mode = st.sidebar.selectbox(
'Choose the source of categories',
("LVIS Categories", "Custom Categories")
)
model_name = st.sidebar.selectbox(
'Model Selection',
("pb-Mix", "pb")
)
if model_name == "pb-Mix":
model_classification = load_tripletmix('tripletmix-pointbert-all-modelnet40')
elif model_name == "pb":
model_classification = load_openshape('openshape-pointbert-vitg14-rgb')
load_data = utils.input_3d_shape('rpcinput')
if cls_mode == "LVIS Categories":
st.title("Classification with LVIS Categories")
prog = st.progress(0.0, "Idle")
if st.sidebar.button("submit"):
classification_lvis(load_data)
elif cls_mode == "Custom Categories":
st.title("Classification with Custom Categories")
prog = st.progress(0.0, "Idle")
cats = st.sidebar.text_input("Custom Categories (64 max, separated with comma)")
cats = [a.strip() for a in cats.split(',')]
if len(cats) > 64:
st.error('Maximum 64 custom categories supported in the demo')
if st.sidebar.button("submit"):
classification_custom(load_data, cats)
elif task == "Cross-modal retrieval":
input_mode = st.sidebar.selectbox(
'Choose an input modality',
("Point Cloud", "Image", "Text")
)
k = st.sidebar.slider("Number of items to retrieve", 1, 100, 16, key='rnum')
sim_th, filter_fn = retrieval_filter_expand()
if input_mode == "Point Cloud":
st.title("Retrieval with Point Cloud")
prog = st.progress(0.0, "Idle")
load_data = utils.input_3d_shape('rpcinput')
if st.sidebar.button("submit"):
retrieval_pc(load_data, k, sim_th, filter_fn)
elif input_mode == "Image":
st.title("Retrieval with Image")
prog = st.progress(0.0, "Idle")
pic = st.sidebar.file_uploader("Upload an Image", key='rimageinput')
if st.sidebar.button("submit"):
retrieval_img(pic, k, sim_th, filter_fn)
elif input_mode == "Text":
st.title("Retrieval with Text")
prog = st.progress(0.0, "Idle")
text = st.sidebar.text_input("Input Text", key='rtextinput')
if st.sidebar.button("submit"):
retrieval_text(text, k, sim_th, filter_fn)
elif task == "Cross-modal generation":
generation_mode = st.sidebar.selectbox(
'Choose the mode of generation',
("PointCloud-to-Image", "PointCloud-to-Text")
)
load_data = utils.input_3d_shape('rpcinput')
if generation_mode == "PointCloud-to-Image":
st.title("Image Generation")
prog = st.progress(0.0, "Idle")
prompt = st.sidebar.text_input("Prompt (Optional)", key='gprompt')
noise_scale = st.sidebar.slider('Variation Level', 0, 5, 1)
cfg_scale = st.sidebar.slider('Guidance Scale', 0.0, 30.0, 10.0)
steps = st.sidebar.slider('Diffusion Steps', 8, 50, 25)
if st.sidebar.button("submit"):
generation_img(load_data, prompt, noise_scale, cfg_scale, steps)
elif generation_mode == "PointCloud-to-Text":
st.title("Text Generation")
prog = st.progress(0.0, "Idle")
cond_scale = st.sidebar.slider('Conditioning Scale', 0.0, 4.0, 2.0, 0.1, key='gcond')
if st.sidebar.button("submit"):
generation_text(load_data, cond_scale)
except Exception:
import traceback
st.error(traceback.format_exc().replace("\n", " \n"))
|