Upload sd_token_similarity_calculator.ipynb
Browse files
sd_token_similarity_calculator.ipynb
CHANGED
@@ -55,10 +55,8 @@
|
|
55 |
"def absolute_value(x):\n",
|
56 |
" return max(x, -x)\n",
|
57 |
"\n",
|
58 |
-
"
|
59 |
-
"
|
60 |
-
" A = token[id_A]\n",
|
61 |
-
" B = token[id_B]\n",
|
62 |
" #Tensor vector length (2nd order, i.e (a^2 + b^2 + ....)^(1/2)\n",
|
63 |
" _A = LA.vector_norm(A, ord=2)\n",
|
64 |
" _B = LA.vector_norm(B, ord=2)\n",
|
@@ -69,6 +67,12 @@
|
|
69 |
" similarity_pcnt_aprox = round(similarity_pcnt, 3)\n",
|
70 |
" result = f'{similarity_pcnt_aprox} %'\n",
|
71 |
" return result\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
72 |
"#----#\n",
|
73 |
"\n",
|
74 |
"#print(vocab[8922]) #the vocab item for ID 8922\n",
|
@@ -86,7 +90,7 @@
|
|
86 |
{
|
87 |
"cell_type": "code",
|
88 |
"source": [
|
89 |
-
"# @title Tokenize prompt into IDs\n",
|
90 |
"from transformers import AutoTokenizer\n",
|
91 |
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
92 |
"\n",
|
@@ -95,6 +99,23 @@
|
|
95 |
"tokenizer_output = tokenizer(text = prompt)\n",
|
96 |
"input_ids = tokenizer_output['input_ids']\n",
|
97 |
"print(input_ids)\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
98 |
"id_A = input_ids[1]\n",
|
99 |
"A = token[id_A]\n",
|
100 |
"_A = LA.vector_norm(A, ord=2)\n",
|
@@ -108,36 +129,20 @@
|
|
108 |
" A = R*(_A/_R)\n",
|
109 |
"\n",
|
110 |
"#Save a copy of the tensor A\n",
|
111 |
-
"id_P =
|
112 |
-
"P =
|
113 |
-
"_P = LA.vector_norm(A, ord=2)\n"
|
114 |
-
"\n",
|
115 |
-
"#The prompt will be enclosed with the <|start-of-text|> and <|end-of-text|> tokens, which is why output will be [49406, ... , 49407].\n",
|
116 |
-
"\n",
|
117 |
-
"#You can leave the 'prompt' field empty to get a random value tensor. Since the tensor is random value, it will not correspond to any tensor in the vocab.json list , and this it will have no ID."
|
118 |
],
|
119 |
"metadata": {
|
120 |
-
"id": "
|
121 |
-
"colab": {
|
122 |
-
"base_uri": "https://localhost:8080/"
|
123 |
-
},
|
124 |
-
"outputId": "e335f5da-b26d-4eea-f854-fd646444ea14"
|
125 |
},
|
126 |
"execution_count": null,
|
127 |
-
"outputs": [
|
128 |
-
{
|
129 |
-
"output_type": "stream",
|
130 |
-
"name": "stdout",
|
131 |
-
"text": [
|
132 |
-
"[49406, 8922, 49407]\n"
|
133 |
-
]
|
134 |
-
}
|
135 |
-
]
|
136 |
},
|
137 |
{
|
138 |
"cell_type": "code",
|
139 |
"source": [
|
140 |
-
"# @title Take the ID at index 1 from above result and modify it (optional)\n",
|
141 |
"mix_with = \"\" # @param {type:'string'}\n",
|
142 |
"mix_method = \"None\" # @param [\"None\" , \"Average\", \"Subtract\"] {allow-input: true}\n",
|
143 |
"w = 0.5 # @param {type:\"slider\", min:0, max:1, step:0.01}\n",
|
@@ -193,7 +198,7 @@
|
|
193 |
"cell_type": "code",
|
194 |
"source": [
|
195 |
"\n",
|
196 |
-
"# @title Find Similiar Tokens to ID at index 1 from above result\n",
|
197 |
"dots = torch.zeros(NUM_TOKENS)\n",
|
198 |
"for index in range(NUM_TOKENS):\n",
|
199 |
" id_B = index\n",
|
@@ -233,7 +238,7 @@
|
|
233 |
{
|
234 |
"cell_type": "code",
|
235 |
"source": [
|
236 |
-
"# @title Print Result from the 'Similiar Tokens' list from above result\n",
|
237 |
"list_size = 100 # @param {type:'number'}\n",
|
238 |
"print_ID = False # @param {type:\"boolean\"}\n",
|
239 |
"print_Similarity = True # @param {type:\"boolean\"}\n",
|
@@ -264,7 +269,7 @@
|
|
264 |
"cell_type": "code",
|
265 |
"source": [
|
266 |
"\n",
|
267 |
-
"# @title Get similarity % of two token IDs\n",
|
268 |
"id_for_token_A = 4567 # @param {type:'number'}\n",
|
269 |
"id_for_token_B = 4343 # @param {type:'number'}\n",
|
270 |
"\n",
|
@@ -280,6 +285,45 @@
|
|
280 |
"execution_count": null,
|
281 |
"outputs": []
|
282 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
283 |
{
|
284 |
"cell_type": "markdown",
|
285 |
"source": [
|
@@ -316,7 +360,11 @@
|
|
316 |
"\n",
|
317 |
"Source: https://huggingface.co/docs/diffusers/main/en/using-diffusers/weighted_prompts*\n",
|
318 |
"\n",
|
319 |
-
"So TLDR; vector direction = “what to generate” , vector magnitude = “prompt weights
|
|
|
|
|
|
|
|
|
320 |
],
|
321 |
"metadata": {
|
322 |
"id": "njeJx_nSSA8H"
|
|
|
55 |
"def absolute_value(x):\n",
|
56 |
" return max(x, -x)\n",
|
57 |
"\n",
|
58 |
+
"\n",
|
59 |
+
"def token_similarity(A, B):\n",
|
|
|
|
|
60 |
" #Tensor vector length (2nd order, i.e (a^2 + b^2 + ....)^(1/2)\n",
|
61 |
" _A = LA.vector_norm(A, ord=2)\n",
|
62 |
" _B = LA.vector_norm(B, ord=2)\n",
|
|
|
67 |
" similarity_pcnt_aprox = round(similarity_pcnt, 3)\n",
|
68 |
" result = f'{similarity_pcnt_aprox} %'\n",
|
69 |
" return result\n",
|
70 |
+
"\n",
|
71 |
+
"def similarity(id_A , id_B):\n",
|
72 |
+
" #Tensors\n",
|
73 |
+
" A = token[id_A]\n",
|
74 |
+
" B = token[id_B]\n",
|
75 |
+
" return token_similarity(A, B)\n",
|
76 |
"#----#\n",
|
77 |
"\n",
|
78 |
"#print(vocab[8922]) #the vocab item for ID 8922\n",
|
|
|
90 |
{
|
91 |
"cell_type": "code",
|
92 |
"source": [
|
93 |
+
"# @title 📝 -> 🆔 Tokenize prompt into IDs\n",
|
94 |
"from transformers import AutoTokenizer\n",
|
95 |
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
96 |
"\n",
|
|
|
99 |
"tokenizer_output = tokenizer(text = prompt)\n",
|
100 |
"input_ids = tokenizer_output['input_ids']\n",
|
101 |
"print(input_ids)\n",
|
102 |
+
"\n",
|
103 |
+
"\n",
|
104 |
+
"#The prompt will be enclosed with the <|start-of-text|> and <|end-of-text|> tokens, which is why output will be [49406, ... , 49407].\n",
|
105 |
+
"\n",
|
106 |
+
"#You can leave the 'prompt' field empty to get a random value tensor. Since the tensor is random value, it will not correspond to any tensor in the vocab.json list , and this it will have no ID."
|
107 |
+
],
|
108 |
+
"metadata": {
|
109 |
+
"id": "RPdkYzT2_X85"
|
110 |
+
},
|
111 |
+
"execution_count": null,
|
112 |
+
"outputs": []
|
113 |
+
},
|
114 |
+
{
|
115 |
+
"cell_type": "code",
|
116 |
+
"source": [
|
117 |
+
"# @title 🆔->🥢 Take the ID at index 1 from above result and get its corresponding tensor value\n",
|
118 |
+
"\n",
|
119 |
"id_A = input_ids[1]\n",
|
120 |
"A = token[id_A]\n",
|
121 |
"_A = LA.vector_norm(A, ord=2)\n",
|
|
|
129 |
" A = R*(_A/_R)\n",
|
130 |
"\n",
|
131 |
"#Save a copy of the tensor A\n",
|
132 |
+
"id_P = id_A\n",
|
133 |
+
"P = A\n",
|
134 |
+
"_P = LA.vector_norm(A, ord=2)\n"
|
|
|
|
|
|
|
|
|
135 |
],
|
136 |
"metadata": {
|
137 |
+
"id": "YqdiF8DIz9Wu"
|
|
|
|
|
|
|
|
|
138 |
},
|
139 |
"execution_count": null,
|
140 |
+
"outputs": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
141 |
},
|
142 |
{
|
143 |
"cell_type": "code",
|
144 |
"source": [
|
145 |
+
"# @title 🥢 -> 🥢🔀 Take the ID at index 1 from above result and modify it (optional)\n",
|
146 |
"mix_with = \"\" # @param {type:'string'}\n",
|
147 |
"mix_method = \"None\" # @param [\"None\" , \"Average\", \"Subtract\"] {allow-input: true}\n",
|
148 |
"w = 0.5 # @param {type:\"slider\", min:0, max:1, step:0.01}\n",
|
|
|
198 |
"cell_type": "code",
|
199 |
"source": [
|
200 |
"\n",
|
201 |
+
"# @title 🥢->🧾🥢 Find Similiar Tokens to ID at index 1 from above result\n",
|
202 |
"dots = torch.zeros(NUM_TOKENS)\n",
|
203 |
"for index in range(NUM_TOKENS):\n",
|
204 |
" id_B = index\n",
|
|
|
238 |
{
|
239 |
"cell_type": "code",
|
240 |
"source": [
|
241 |
+
"# @title 🥢🧾 -> 🖨️ Print Result from the 'Similiar Tokens' list from above result\n",
|
242 |
"list_size = 100 # @param {type:'number'}\n",
|
243 |
"print_ID = False # @param {type:\"boolean\"}\n",
|
244 |
"print_Similarity = True # @param {type:\"boolean\"}\n",
|
|
|
269 |
"cell_type": "code",
|
270 |
"source": [
|
271 |
"\n",
|
272 |
+
"# @title 🆔 Get similarity % of two token IDs\n",
|
273 |
"id_for_token_A = 4567 # @param {type:'number'}\n",
|
274 |
"id_for_token_B = 4343 # @param {type:'number'}\n",
|
275 |
"\n",
|
|
|
285 |
"execution_count": null,
|
286 |
"outputs": []
|
287 |
},
|
288 |
+
{
|
289 |
+
"cell_type": "code",
|
290 |
+
"source": [
|
291 |
+
"# @title 💫 Compare Text encodings\n",
|
292 |
+
"\n",
|
293 |
+
"prompt_A = \"\" # @param {\"type\":\"string\",\"placeholder\":\"Write a prompt\"}\n",
|
294 |
+
"prompt_B = \"\" # @param {\"type\":\"string\",\"placeholder\":\"Write a prompt\"}\n",
|
295 |
+
"use_token_padding = False # @param {type:\"boolean\"}\n",
|
296 |
+
"\n",
|
297 |
+
"from transformers import CLIPProcessor, CLIPModel\n",
|
298 |
+
"\n",
|
299 |
+
"\n",
|
300 |
+
"processor = CLIPProcessor.from_pretrained(\"openai/clip-vit-large-patch14\" , clean_up_tokenization_spaces = True)\n",
|
301 |
+
"\n",
|
302 |
+
"model = CLIPModel.from_pretrained(\"openai/clip-vit-large-patch14\")\n",
|
303 |
+
"\n",
|
304 |
+
"ids_A = processor.tokenizer(text=prompt_A, padding=use_token_padding, return_tensors=\"pt\")\n",
|
305 |
+
"text_encoding_A = model.get_text_features(**ids_A)\n",
|
306 |
+
"\n",
|
307 |
+
"ids_B = processor.tokenizer(text=prompt_B, padding=use_token_padding, return_tensors=\"pt\")\n",
|
308 |
+
"text_encoding_B = model.get_text_features(**ids_B)\n",
|
309 |
+
"\n",
|
310 |
+
"similarity_str = 'The similarity between the text_encoding for A and B is ' + token_similarity(text_encoding_A[0] , text_encoding_B[0])\n",
|
311 |
+
"\n",
|
312 |
+
"\n",
|
313 |
+
"print(similarity_str)\n",
|
314 |
+
"#outputs = model(**inputs)\n",
|
315 |
+
"#logits_per_image = outputs.logits_per_image # this is the image-text similarity score\n",
|
316 |
+
"#probs = logits_per_image.softmax(dim=1) # we can take the softmax to get the label probabilities\n",
|
317 |
+
"\n",
|
318 |
+
"\n",
|
319 |
+
"\n"
|
320 |
+
],
|
321 |
+
"metadata": {
|
322 |
+
"id": "QQOjh5BvnG8M"
|
323 |
+
},
|
324 |
+
"execution_count": null,
|
325 |
+
"outputs": []
|
326 |
+
},
|
327 |
{
|
328 |
"cell_type": "markdown",
|
329 |
"source": [
|
|
|
360 |
"\n",
|
361 |
"Source: https://huggingface.co/docs/diffusers/main/en/using-diffusers/weighted_prompts*\n",
|
362 |
"\n",
|
363 |
+
"So TLDR; vector direction = “what to generate” , vector magnitude = “prompt weights”\n",
|
364 |
+
"\n",
|
365 |
+
"/---/\n",
|
366 |
+
"\n",
|
367 |
+
"Read more about CLIP here: https://huggingface.co/docs/transformers/model_doc/clip"
|
368 |
],
|
369 |
"metadata": {
|
370 |
"id": "njeJx_nSSA8H"
|