Actually works properly
Browse filesprior versions were generated using CLIPModel.
Turns out thats the WRONG model. Now using the same code that SD actually uses: CLIPTextModel
- generate-embedding.py +12 -9
generate-embedding.py
CHANGED
|
@@ -3,7 +3,6 @@
|
|
| 3 |
""" Work in progress
|
| 4 |
NB: This is COMPLETELY DIFFERENT from "generate-embeddings.py"!!!
|
| 5 |
|
| 6 |
-
|
| 7 |
Plan:
|
| 8 |
Take input for a single word or phrase.
|
| 9 |
Generate a embedding file, "generated.safetensors"
|
|
@@ -20,7 +19,7 @@ import sys
|
|
| 20 |
import json
|
| 21 |
import torch
|
| 22 |
from safetensors.torch import save_file
|
| 23 |
-
from transformers import CLIPProcessor,
|
| 24 |
|
| 25 |
import logging
|
| 26 |
# Turn off stupid mesages from CLIPModel.load
|
|
@@ -41,18 +40,18 @@ def init():
|
|
| 41 |
processor = CLIPProcessor.from_pretrained(clipsrc)
|
| 42 |
print("done",file=sys.stderr)
|
| 43 |
print("loading model from "+clipsrc,file=sys.stderr)
|
| 44 |
-
model =
|
| 45 |
print("done",file=sys.stderr)
|
| 46 |
|
| 47 |
model = model.to(device)
|
| 48 |
|
| 49 |
-
def
|
| 50 |
inputs = processor(text=text, return_tensors="pt")
|
| 51 |
inputs.to(device)
|
| 52 |
with torch.no_grad():
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
return
|
| 56 |
|
| 57 |
|
| 58 |
init()
|
|
@@ -60,11 +59,15 @@ init()
|
|
| 60 |
|
| 61 |
word = input("type a phrase to generate an embedding for: ")
|
| 62 |
|
| 63 |
-
emb =
|
| 64 |
-
embs=emb.unsqueeze(0) # stupid matrix magic to make the
|
|
|
|
| 65 |
|
| 66 |
print("Shape of result = ",embs.shape)
|
|
|
|
| 67 |
output = "generated.safetensors"
|
|
|
|
|
|
|
| 68 |
print(f"Saving to {output}...")
|
| 69 |
save_file({"emb_params": embs}, output)
|
| 70 |
|
|
|
|
| 3 |
""" Work in progress
|
| 4 |
NB: This is COMPLETELY DIFFERENT from "generate-embeddings.py"!!!
|
| 5 |
|
|
|
|
| 6 |
Plan:
|
| 7 |
Take input for a single word or phrase.
|
| 8 |
Generate a embedding file, "generated.safetensors"
|
|
|
|
| 19 |
import json
|
| 20 |
import torch
|
| 21 |
from safetensors.torch import save_file
|
| 22 |
+
from transformers import CLIPProcessor,CLIPTextModel
|
| 23 |
|
| 24 |
import logging
|
| 25 |
# Turn off stupid mesages from CLIPModel.load
|
|
|
|
| 40 |
processor = CLIPProcessor.from_pretrained(clipsrc)
|
| 41 |
print("done",file=sys.stderr)
|
| 42 |
print("loading model from "+clipsrc,file=sys.stderr)
|
| 43 |
+
model = CLIPTextModel.from_pretrained(clipsrc)
|
| 44 |
print("done",file=sys.stderr)
|
| 45 |
|
| 46 |
model = model.to(device)
|
| 47 |
|
| 48 |
+
def cliptextmodel_embed_calc(text):
|
| 49 |
inputs = processor(text=text, return_tensors="pt")
|
| 50 |
inputs.to(device)
|
| 51 |
with torch.no_grad():
|
| 52 |
+
outputs = model(**inputs)
|
| 53 |
+
embeddings = outputs.pooler_output
|
| 54 |
+
return embeddings
|
| 55 |
|
| 56 |
|
| 57 |
init()
|
|
|
|
| 59 |
|
| 60 |
word = input("type a phrase to generate an embedding for: ")
|
| 61 |
|
| 62 |
+
emb = cliptextmodel_embed_calc(word)
|
| 63 |
+
#embs=emb.unsqueeze(0) # stupid matrix magic to make it the required shape
|
| 64 |
+
embs=emb
|
| 65 |
|
| 66 |
print("Shape of result = ",embs.shape)
|
| 67 |
+
|
| 68 |
output = "generated.safetensors"
|
| 69 |
+
if all(char.isalpha() for char in word):
|
| 70 |
+
output=f"{word}.safetensors"
|
| 71 |
print(f"Saving to {output}...")
|
| 72 |
save_file({"emb_params": embs}, output)
|
| 73 |
|