Spaces:
Running on Zero
Running on Zero
Deploy from 3dvalley spaces/material-maps
Browse files
app.py
CHANGED
|
@@ -79,13 +79,13 @@ with torch.no_grad():
|
|
| 79 |
_tok = CLIPTokenizer.from_pretrained(LOTUS_REPO, subfolder="tokenizer")
|
| 80 |
_enc = CLIPTextModel.from_pretrained(LOTUS_REPO, subfolder="text_encoder")
|
| 81 |
_ids = _tok([""], padding="max_length", max_length=_tok.model_max_length, return_tensors="pt").input_ids
|
| 82 |
-
EMPTY_PROMPT = _enc(_ids)[0].half().to(DEVICE)
|
| 83 |
del _tok, _enc
|
| 84 |
# The task embedding that selects the normal head (see Lotus's infer.py).
|
| 85 |
_task = torch.tensor([[1.0, 0.0]])
|
| 86 |
TASK_EMB = torch.cat([torch.sin(_task), torch.cos(_task)], dim=-1).half().to(DEVICE)
|
| 87 |
|
| 88 |
-
clip_model = CLIPModel.from_pretrained(CLIP_REPO
|
| 89 |
clip_processor = CLIPProcessor.from_pretrained(CLIP_REPO)
|
| 90 |
|
| 91 |
# (label, words for CLIP, roughness level, metal): "metal" is bare metal all
|
|
@@ -112,9 +112,12 @@ CLASSES = [
|
|
| 112 |
("polished metal", "a shiny polished metal, chrome, gold or copper texture", 0.15, "metal"),
|
| 113 |
("snow", "a snow or ice texture", 0.3, None),
|
| 114 |
]
|
|
|
|
|
|
|
| 115 |
with torch.no_grad():
|
| 116 |
-
_t = clip_processor(text=[c[1] for c in CLASSES], return_tensors="pt", padding=True)
|
| 117 |
-
CLASS_EMB = F.normalize(clip_model.get_text_features(**_t).float(), dim=-1)
|
|
|
|
| 118 |
_log("models ready")
|
| 119 |
|
| 120 |
|
|
|
|
| 79 |
_tok = CLIPTokenizer.from_pretrained(LOTUS_REPO, subfolder="tokenizer")
|
| 80 |
_enc = CLIPTextModel.from_pretrained(LOTUS_REPO, subfolder="text_encoder")
|
| 81 |
_ids = _tok([""], padding="max_length", max_length=_tok.model_max_length, return_tensors="pt").input_ids
|
| 82 |
+
EMPTY_PROMPT = _enc(_ids)[0].half().to(DEVICE) # computed on the CPU, like the CLIP classes below
|
| 83 |
del _tok, _enc
|
| 84 |
# The task embedding that selects the normal head (see Lotus's infer.py).
|
| 85 |
_task = torch.tensor([[1.0, 0.0]])
|
| 86 |
TASK_EMB = torch.cat([torch.sin(_task), torch.cos(_task)], dim=-1).half().to(DEVICE)
|
| 87 |
|
| 88 |
+
clip_model = CLIPModel.from_pretrained(CLIP_REPO).eval()
|
| 89 |
clip_processor = CLIPProcessor.from_pretrained(CLIP_REPO)
|
| 90 |
|
| 91 |
# (label, words for CLIP, roughness level, metal): "metal" is bare metal all
|
|
|
|
| 112 |
("polished metal", "a shiny polished metal, chrome, gold or copper texture", 0.15, "metal"),
|
| 113 |
("snow", "a snow or ice texture", 0.3, None),
|
| 114 |
]
|
| 115 |
+
# The class words are embedded once, on the CPU: ZeroGPU only lends a GPU
|
| 116 |
+
# inside a @spaces.GPU call, so nothing runs on "cuda" at start-up.
|
| 117 |
with torch.no_grad():
|
| 118 |
+
_t = clip_processor(text=[c[1] for c in CLASSES], return_tensors="pt", padding=True)
|
| 119 |
+
CLASS_EMB = F.normalize(clip_model.get_text_features(**_t).float(), dim=-1).to(DEVICE)
|
| 120 |
+
clip_model = clip_model.half().to(DEVICE)
|
| 121 |
_log("models ready")
|
| 122 |
|
| 123 |
|