Upload sd_token_similarity_calculator.ipynb
Browse files- sd_token_similarity_calculator.ipynb +194 -153
sd_token_similarity_calculator.ipynb
CHANGED
|
@@ -160,6 +160,114 @@
|
|
| 160 |
"execution_count": null,
|
| 161 |
"outputs": []
|
| 162 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 163 |
{
|
| 164 |
"cell_type": "code",
|
| 165 |
"source": [
|
|
@@ -313,119 +421,6 @@
|
|
| 313 |
"execution_count": null,
|
| 314 |
"outputs": []
|
| 315 |
},
|
| 316 |
-
{
|
| 317 |
-
"cell_type": "code",
|
| 318 |
-
"source": [
|
| 319 |
-
"# @title 📝 Prompt similarity: Order pre-made text_encodings\n",
|
| 320 |
-
"prompt = \"photo of a banana\" # @param {\"type\":\"string\",\"placeholder\":\"Write a prompt\"}\n",
|
| 321 |
-
"from transformers import AutoTokenizer\n",
|
| 322 |
-
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
| 323 |
-
"from transformers import CLIPProcessor, CLIPModel\n",
|
| 324 |
-
"processor = CLIPProcessor.from_pretrained(\"openai/clip-vit-large-patch14\" , clean_up_tokenization_spaces = True)\n",
|
| 325 |
-
"model = CLIPModel.from_pretrained(\"openai/clip-vit-large-patch14\")\n",
|
| 326 |
-
"\n",
|
| 327 |
-
"# Get text features for user input\n",
|
| 328 |
-
"inputs = tokenizer(text = prompt, padding=True, return_tensors=\"pt\")\n",
|
| 329 |
-
"text_features_A = model.get_text_features(**inputs)\n",
|
| 330 |
-
"text_features_A = text_features_A/text_features_A.norm(p=2, dim=-1, keepdim=True)\n",
|
| 331 |
-
"name_A = prompt\n",
|
| 332 |
-
"#------#\n",
|
| 333 |
-
"\n",
|
| 334 |
-
"# Load the .db file for prefix encodings\n",
|
| 335 |
-
"import shelve\n",
|
| 336 |
-
"_iters = -1\n",
|
| 337 |
-
"RANGE = NUM_PREFIX\n",
|
| 338 |
-
"NUM_PREFIX_LISTS = 1\n",
|
| 339 |
-
"dots = results_sim = torch.zeros(RANGE*NUM_PREFIX_LISTS)\n",
|
| 340 |
-
"for _PREFIX_ENC_VOCAB in PREFIX_ENC_VOCAB:\n",
|
| 341 |
-
" _iters = _iters + 1\n",
|
| 342 |
-
" d = shelve.open(_PREFIX_ENC_VOCAB)\n",
|
| 343 |
-
" for _index in range(RANGE):\n",
|
| 344 |
-
" index = _iters*RANGE + _index\n",
|
| 345 |
-
" text_features = d[f'{_index}']\n",
|
| 346 |
-
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
|
| 347 |
-
" sim = torch.nn.functional.cosine_similarity(text_features, text_features_A)\n",
|
| 348 |
-
" dots[index] = sim\n",
|
| 349 |
-
" #----#\n",
|
| 350 |
-
" d.close() #close the file\n",
|
| 351 |
-
"#------#\n",
|
| 352 |
-
"prefix_sorted, prefix_indices = torch.sort(dots,dim=0 , descending=True)\n",
|
| 353 |
-
"#------#\n",
|
| 354 |
-
"\n",
|
| 355 |
-
"# Load the .db file for prefix encodings\n",
|
| 356 |
-
"import shelve\n",
|
| 357 |
-
"_iters = -1\n",
|
| 358 |
-
"RANGE = NUM_SUFFIX\n",
|
| 359 |
-
"dots = results_sim = torch.zeros(RANGE*NUM_SUFFIX_LISTS)\n",
|
| 360 |
-
"for _SUFFIX_ENC_VOCAB in SUFFIX_ENC_VOCAB:\n",
|
| 361 |
-
" _iters = _iters + 1\n",
|
| 362 |
-
" d = shelve.open(_SUFFIX_ENC_VOCAB)\n",
|
| 363 |
-
" for _index in range(RANGE):\n",
|
| 364 |
-
" index = _iters*RANGE + _index\n",
|
| 365 |
-
" text_features = d[f'{_index}']\n",
|
| 366 |
-
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
|
| 367 |
-
" sim = torch.nn.functional.cosine_similarity(text_features, text_features_A)\n",
|
| 368 |
-
" dots[index] = sim\n",
|
| 369 |
-
" #----#\n",
|
| 370 |
-
" d.close() #close the file\n",
|
| 371 |
-
"#------#\n",
|
| 372 |
-
"suffix_sorted, suffix_indices = torch.sort(dots,dim=0 , descending=True)\n",
|
| 373 |
-
"#------#\n",
|
| 374 |
-
"\n",
|
| 375 |
-
"#Print the results\n",
|
| 376 |
-
"#'from_-encoded_suffix',\n",
|
| 377 |
-
"#'a_-_encoded_suffix' ,\n",
|
| 378 |
-
"#'by_-encoded_suffix' ,\n",
|
| 379 |
-
"#'encoded_suffix-_like'\n",
|
| 380 |
-
"\n",
|
| 381 |
-
"# title Show the 100 most similiar suffix and prefix text-encodings to the text encoding\n",
|
| 382 |
-
"RANGE = 100\n",
|
| 383 |
-
"_suffixes = '{'\n",
|
| 384 |
-
"_sims = '{'\n",
|
| 385 |
-
"for index in range(RANGE):\n",
|
| 386 |
-
" id = int(suffix_indices[index])\n",
|
| 387 |
-
" ahead = \"from \"\n",
|
| 388 |
-
" behind = \"\"\n",
|
| 389 |
-
" if(id>NUM_SUFFIX*1):\n",
|
| 390 |
-
" ahead = \"a \"\n",
|
| 391 |
-
" if(id>NUM_SUFFIX*2):\n",
|
| 392 |
-
" ahead = \"by \"\n",
|
| 393 |
-
" if(id>NUM_SUFFIX*3):\n",
|
| 394 |
-
" ahead = \"\"\n",
|
| 395 |
-
" behind = \"like\"\n",
|
| 396 |
-
" id = _modulus(id,NUM_SUFFIX)\n",
|
| 397 |
-
" #------#\n",
|
| 398 |
-
" sim = suffix_sorted[index].item()\n",
|
| 399 |
-
" name = ahead + get_suffix(id) + behind\n",
|
| 400 |
-
" if(get_suffix(id) == ' '): name = ahead + f'{id}' + behind\n",
|
| 401 |
-
" _suffixes = _suffixes + name + '|'\n",
|
| 402 |
-
" _sims = _sims + f'{round(sim*100,2)} %' + '|'\n",
|
| 403 |
-
"#------#\n",
|
| 404 |
-
"_suffixes = (_suffixes + '}').replace('|}', '}')\n",
|
| 405 |
-
"_sims = (_sims + '}').replace('|}', '}')\n",
|
| 406 |
-
"\n",
|
| 407 |
-
"print('most similiar suffix items to prompt : ' + _suffixes)\n",
|
| 408 |
-
"print('similarity % for suffix items : ' + _sims)\n",
|
| 409 |
-
"print('')\n",
|
| 410 |
-
"\n",
|
| 411 |
-
"#-------#\n",
|
| 412 |
-
"\n",
|
| 413 |
-
"_prefixes = '{'\n",
|
| 414 |
-
"for index in range(RANGE):\n",
|
| 415 |
-
" id = f'{prefix_indices[index]}'\n",
|
| 416 |
-
" #sim = prefix_sorted[index]\n",
|
| 417 |
-
" name = get_prefix(id)\n",
|
| 418 |
-
" _prefixes = _prefixes + name + '|'\n",
|
| 419 |
-
"#------#\n",
|
| 420 |
-
"_prefixes = (_prefixes + '}').replace('|}', '}')\n",
|
| 421 |
-
"print('most similiar prefix suffix to image : ' + _prefixes)\n"
|
| 422 |
-
],
|
| 423 |
-
"metadata": {
|
| 424 |
-
"id": "xc-PbIYF428y"
|
| 425 |
-
},
|
| 426 |
-
"execution_count": null,
|
| 427 |
-
"outputs": []
|
| 428 |
-
},
|
| 429 |
{
|
| 430 |
"cell_type": "markdown",
|
| 431 |
"source": [
|
|
@@ -479,13 +474,13 @@
|
|
| 479 |
],
|
| 480 |
"metadata": {
|
| 481 |
"id": "ke6mZ1RZDOeB",
|
| 482 |
-
"outputId": "
|
| 483 |
"colab": {
|
| 484 |
"base_uri": "https://localhost:8080/",
|
| 485 |
"height": 1000
|
| 486 |
}
|
| 487 |
},
|
| 488 |
-
"execution_count":
|
| 489 |
"outputs": [
|
| 490 |
{
|
| 491 |
"output_type": "display_data",
|
|
@@ -502,7 +497,7 @@
|
|
| 502 |
{
|
| 503 |
"cell_type": "code",
|
| 504 |
"source": [
|
| 505 |
-
"
|
| 506 |
"from transformers import AutoTokenizer\n",
|
| 507 |
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
| 508 |
"from transformers import CLIPProcessor, CLIPModel\n",
|
|
@@ -514,37 +509,96 @@
|
|
| 514 |
"image_features = model.get_image_features(**inputs)\n",
|
| 515 |
"image_features = image_features / image_features.norm(p=2, dim=-1, keepdim=True)\n",
|
| 516 |
"name_A = \"the image\"\n",
|
|
|
|
| 517 |
"\n",
|
| 518 |
"# Load the .db file for prefix encodings\n",
|
| 519 |
"import shelve\n",
|
| 520 |
-
"
|
| 521 |
-
"
|
| 522 |
-
"
|
| 523 |
-
"
|
| 524 |
-
"
|
| 525 |
-
"
|
| 526 |
-
"
|
| 527 |
-
"
|
| 528 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 529 |
"prefix_sorted, prefix_indices = torch.sort(dots,dim=0 , descending=True)\n",
|
| 530 |
-
"
|
| 531 |
"\n",
|
| 532 |
-
"# Load the .db file for
|
| 533 |
"import shelve\n",
|
| 534 |
-
"
|
| 535 |
-
"
|
| 536 |
-
"
|
| 537 |
-
"
|
| 538 |
-
"
|
| 539 |
-
"
|
| 540 |
-
"
|
| 541 |
-
"
|
| 542 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 543 |
"suffix_sorted, suffix_indices = torch.sort(dots,dim=0 , descending=True)\n",
|
| 544 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 545 |
],
|
| 546 |
"metadata": {
|
| 547 |
-
"id": "
|
| 548 |
},
|
| 549 |
"execution_count": null,
|
| 550 |
"outputs": []
|
|
@@ -577,23 +631,10 @@
|
|
| 577 |
"print('most similiar prefix tokens to image : ' + _prefixes)\n"
|
| 578 |
],
|
| 579 |
"metadata": {
|
| 580 |
-
"id": "eZqMUhP0qYaK"
|
| 581 |
-
"outputId": "4801cded-e73c-4c0b-eb6e-608ed899ff49",
|
| 582 |
-
"colab": {
|
| 583 |
-
"base_uri": "https://localhost:8080/"
|
| 584 |
-
}
|
| 585 |
},
|
| 586 |
"execution_count": null,
|
| 587 |
-
"outputs": [
|
| 588 |
-
{
|
| 589 |
-
"output_type": "stream",
|
| 590 |
-
"name": "stdout",
|
| 591 |
-
"text": [
|
| 592 |
-
"most similiar suffix tokens to image : {vfx |cleanup |warcraft |defend |avatar |wall |blu |indigo |dfs |bluetooth |orian |alliance |defence |defenses |defense |guardians |descendants |navis |raid |avengersendgame }\n",
|
| 593 |
-
"most similiar prefix tokens to image : {imperi-|blue-|bluec-|war-|blau-|veer-|blu-|vau-|bloo-|taun-|kavan-|kair-|storm-|anarch-|purple-|honor-|spartan-|swar-|raun-|andor-}\n"
|
| 594 |
-
]
|
| 595 |
-
}
|
| 596 |
-
]
|
| 597 |
},
|
| 598 |
{
|
| 599 |
"cell_type": "code",
|
|
|
|
| 160 |
"execution_count": null,
|
| 161 |
"outputs": []
|
| 162 |
},
|
| 163 |
+
{
|
| 164 |
+
"cell_type": "code",
|
| 165 |
+
"source": [
|
| 166 |
+
"# @title 📝 Prompt similarity: Order pre-made text_encodings\n",
|
| 167 |
+
"prompt = \" a fast car on the road \" # @param {\"type\":\"string\",\"placeholder\":\"Write a prompt\"}\n",
|
| 168 |
+
"from transformers import AutoTokenizer\n",
|
| 169 |
+
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
| 170 |
+
"from transformers import CLIPProcessor, CLIPModel\n",
|
| 171 |
+
"processor = CLIPProcessor.from_pretrained(\"openai/clip-vit-large-patch14\" , clean_up_tokenization_spaces = True)\n",
|
| 172 |
+
"model = CLIPModel.from_pretrained(\"openai/clip-vit-large-patch14\")\n",
|
| 173 |
+
"\n",
|
| 174 |
+
"# Get text features for user input\n",
|
| 175 |
+
"inputs = tokenizer(text = prompt, padding=True, return_tensors=\"pt\")\n",
|
| 176 |
+
"text_features_A = model.get_text_features(**inputs)\n",
|
| 177 |
+
"text_features_A = text_features_A/text_features_A.norm(p=2, dim=-1, keepdim=True)\n",
|
| 178 |
+
"name_A = prompt\n",
|
| 179 |
+
"#------#\n",
|
| 180 |
+
"\n",
|
| 181 |
+
"# Load the .db file for prefix encodings\n",
|
| 182 |
+
"import shelve\n",
|
| 183 |
+
"_iters = -1\n",
|
| 184 |
+
"RANGE = NUM_PREFIX\n",
|
| 185 |
+
"NUM_PREFIX_LISTS = 1\n",
|
| 186 |
+
"dots = results_sim = torch.zeros(RANGE*NUM_PREFIX_LISTS)\n",
|
| 187 |
+
"for _PREFIX_ENC_VOCAB in PREFIX_ENC_VOCAB:\n",
|
| 188 |
+
" _iters = _iters + 1\n",
|
| 189 |
+
" d = shelve.open(_PREFIX_ENC_VOCAB)\n",
|
| 190 |
+
" for _index in range(RANGE):\n",
|
| 191 |
+
" index = _iters*RANGE + _index\n",
|
| 192 |
+
" text_features = d[f'{_index}']\n",
|
| 193 |
+
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
|
| 194 |
+
" sim = torch.nn.functional.cosine_similarity(text_features, text_features_A)\n",
|
| 195 |
+
" dots[index] = sim\n",
|
| 196 |
+
" #----#\n",
|
| 197 |
+
" d.close() #close the file\n",
|
| 198 |
+
"#------#\n",
|
| 199 |
+
"prefix_sorted, prefix_indices = torch.sort(dots,dim=0 , descending=True)\n",
|
| 200 |
+
"#------#\n",
|
| 201 |
+
"\n",
|
| 202 |
+
"# Load the .db file for prefix encodings\n",
|
| 203 |
+
"import shelve\n",
|
| 204 |
+
"_iters = -1\n",
|
| 205 |
+
"RANGE = NUM_SUFFIX\n",
|
| 206 |
+
"dots = results_sim = torch.zeros(RANGE*NUM_SUFFIX_LISTS)\n",
|
| 207 |
+
"for _SUFFIX_ENC_VOCAB in SUFFIX_ENC_VOCAB:\n",
|
| 208 |
+
" _iters = _iters + 1\n",
|
| 209 |
+
" d = shelve.open(_SUFFIX_ENC_VOCAB)\n",
|
| 210 |
+
" for _index in range(RANGE):\n",
|
| 211 |
+
" index = _iters*RANGE + _index\n",
|
| 212 |
+
" text_features = d[f'{_index}']\n",
|
| 213 |
+
" text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True)\n",
|
| 214 |
+
" sim = torch.nn.functional.cosine_similarity(text_features, text_features_A)\n",
|
| 215 |
+
" dots[index] = sim\n",
|
| 216 |
+
" #----#\n",
|
| 217 |
+
" d.close() #close the file\n",
|
| 218 |
+
"#------#\n",
|
| 219 |
+
"suffix_sorted, suffix_indices = torch.sort(dots,dim=0 , descending=True)\n",
|
| 220 |
+
"#------#\n",
|
| 221 |
+
"\n",
|
| 222 |
+
"#Print the results\n",
|
| 223 |
+
"# title Show the 100 most similiar suffix and prefix text-encodings to the text encoding\n",
|
| 224 |
+
"RANGE = 30\n",
|
| 225 |
+
"_suffixes = '{'\n",
|
| 226 |
+
"_sims = '{'\n",
|
| 227 |
+
"for index in range(RANGE):\n",
|
| 228 |
+
" id = int(suffix_indices[index])\n",
|
| 229 |
+
" ahead = \"from \"\n",
|
| 230 |
+
" behind = \"\"\n",
|
| 231 |
+
" if(id>NUM_SUFFIX*1):\n",
|
| 232 |
+
" ahead = \"a \"\n",
|
| 233 |
+
" if(id>NUM_SUFFIX*2):\n",
|
| 234 |
+
" ahead = \"by \"\n",
|
| 235 |
+
" if(id>NUM_SUFFIX*3):\n",
|
| 236 |
+
" ahead = \"\"\n",
|
| 237 |
+
" behind = \"like\"\n",
|
| 238 |
+
" id = _modulus(id,NUM_SUFFIX)\n",
|
| 239 |
+
" #------#\n",
|
| 240 |
+
" sim = suffix_sorted[index].item()\n",
|
| 241 |
+
" name = ahead + get_suffix(id) + behind\n",
|
| 242 |
+
" if(get_suffix(id) == ' '): name = ahead + f'{id}' + behind\n",
|
| 243 |
+
" _suffixes = _suffixes + name + '|'\n",
|
| 244 |
+
" _sims = _sims + f'{round(sim*100,2)} %' + '|'\n",
|
| 245 |
+
"#------#\n",
|
| 246 |
+
"_suffixes = (_suffixes + '}').replace('|}', '}')\n",
|
| 247 |
+
"_sims = (_sims + '}').replace('|}', '}')\n",
|
| 248 |
+
"\n",
|
| 249 |
+
"print('most similiar suffix items to prompt : ' + _suffixes)\n",
|
| 250 |
+
"print('similarity % for suffix items : ' + _sims)\n",
|
| 251 |
+
"print('')\n",
|
| 252 |
+
"\n",
|
| 253 |
+
"#-------#\n",
|
| 254 |
+
"\n",
|
| 255 |
+
"_prefixes = '{'\n",
|
| 256 |
+
"for index in range(RANGE):\n",
|
| 257 |
+
" id = f'{prefix_indices[index]}'\n",
|
| 258 |
+
" #sim = prefix_sorted[index]\n",
|
| 259 |
+
" name = get_prefix(id)\n",
|
| 260 |
+
" _prefixes = _prefixes + name + '|'\n",
|
| 261 |
+
"#------#\n",
|
| 262 |
+
"_prefixes = (_prefixes + '}').replace('|}', '}')\n",
|
| 263 |
+
"print('most similiar prefix suffix to image : ' + _prefixes)\n"
|
| 264 |
+
],
|
| 265 |
+
"metadata": {
|
| 266 |
+
"id": "xc-PbIYF428y"
|
| 267 |
+
},
|
| 268 |
+
"execution_count": null,
|
| 269 |
+
"outputs": []
|
| 270 |
+
},
|
| 271 |
{
|
| 272 |
"cell_type": "code",
|
| 273 |
"source": [
|
|
|
|
| 421 |
"execution_count": null,
|
| 422 |
"outputs": []
|
| 423 |
},
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 424 |
{
|
| 425 |
"cell_type": "markdown",
|
| 426 |
"source": [
|
|
|
|
| 474 |
],
|
| 475 |
"metadata": {
|
| 476 |
"id": "ke6mZ1RZDOeB",
|
| 477 |
+
"outputId": "f98f9ea5-32d1-4cf7-b523-1c6b6e6792a2",
|
| 478 |
"colab": {
|
| 479 |
"base_uri": "https://localhost:8080/",
|
| 480 |
"height": 1000
|
| 481 |
}
|
| 482 |
},
|
| 483 |
+
"execution_count": 2,
|
| 484 |
"outputs": [
|
| 485 |
{
|
| 486 |
"output_type": "display_data",
|
|
|
|
| 497 |
{
|
| 498 |
"cell_type": "code",
|
| 499 |
"source": [
|
| 500 |
+
"\n",
|
| 501 |
"from transformers import AutoTokenizer\n",
|
| 502 |
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
| 503 |
"from transformers import CLIPProcessor, CLIPModel\n",
|
|
|
|
| 509 |
"image_features = model.get_image_features(**inputs)\n",
|
| 510 |
"image_features = image_features / image_features.norm(p=2, dim=-1, keepdim=True)\n",
|
| 511 |
"name_A = \"the image\"\n",
|
| 512 |
+
"#-----#\n",
|
| 513 |
"\n",
|
| 514 |
"# Load the .db file for prefix encodings\n",
|
| 515 |
"import shelve\n",
|
| 516 |
+
"_iters = -1\n",
|
| 517 |
+
"RANGE = NUM_PREFIX\n",
|
| 518 |
+
"NUM_PREFIX_LISTS = 1\n",
|
| 519 |
+
"dots = results_sim = torch.zeros(RANGE*NUM_PREFIX_LISTS)\n",
|
| 520 |
+
"for _PREFIX_ENC_VOCAB in PREFIX_ENC_VOCAB:\n",
|
| 521 |
+
" _iters = _iters + 1\n",
|
| 522 |
+
" d = shelve.open(_PREFIX_ENC_VOCAB)\n",
|
| 523 |
+
" for _index in range(RANGE):\n",
|
| 524 |
+
" index = _iters*RANGE + _index\n",
|
| 525 |
+
" text_features = d[f'{_index}']\n",
|
| 526 |
+
" logit_scale = model.logit_scale.exp()\n",
|
| 527 |
+
" torch.matmul(text_features, image_features.t()) * logit_scale\n",
|
| 528 |
+
" sim = torch.nn.functional.cosine_similarity(text_features, image_features) * logit_scale\n",
|
| 529 |
+
" dots[index] = sim\n",
|
| 530 |
+
" #----#\n",
|
| 531 |
+
" d.close() #close the file\n",
|
| 532 |
+
"#------#\n",
|
| 533 |
"prefix_sorted, prefix_indices = torch.sort(dots,dim=0 , descending=True)\n",
|
| 534 |
+
"#------#\n",
|
| 535 |
"\n",
|
| 536 |
+
"# Load the .db file for prefix encodings\n",
|
| 537 |
"import shelve\n",
|
| 538 |
+
"_iters = -1\n",
|
| 539 |
+
"RANGE = NUM_SUFFIX\n",
|
| 540 |
+
"dots = results_sim = torch.zeros(RANGE*NUM_SUFFIX_LISTS)\n",
|
| 541 |
+
"for _SUFFIX_ENC_VOCAB in SUFFIX_ENC_VOCAB:\n",
|
| 542 |
+
" _iters = _iters + 1\n",
|
| 543 |
+
" d = shelve.open(_SUFFIX_ENC_VOCAB)\n",
|
| 544 |
+
" for _index in range(RANGE):\n",
|
| 545 |
+
" index = _iters*RANGE + _index\n",
|
| 546 |
+
" text_features = d[f'{_index}']\n",
|
| 547 |
+
" logit_scale = model.logit_scale.exp()\n",
|
| 548 |
+
" torch.matmul(text_features, image_features.t()) * logit_scale\n",
|
| 549 |
+
" sim = torch.nn.functional.cosine_similarity(text_features, image_features) * logit_scale\n",
|
| 550 |
+
" dots[index] = sim\n",
|
| 551 |
+
" #----#\n",
|
| 552 |
+
" d.close() #close the file\n",
|
| 553 |
+
"#------#\n",
|
| 554 |
"suffix_sorted, suffix_indices = torch.sort(dots,dim=0 , descending=True)\n",
|
| 555 |
+
"#------#\n",
|
| 556 |
+
"\n",
|
| 557 |
+
"#Print the results\n",
|
| 558 |
+
"# title Show the 100 most similiar suffix and prefix text-encodings to the text encoding\n",
|
| 559 |
+
"RANGE = 30\n",
|
| 560 |
+
"_suffixes = '{'\n",
|
| 561 |
+
"_sims = '{'\n",
|
| 562 |
+
"for index in range(RANGE):\n",
|
| 563 |
+
" id = int(suffix_indices[index])\n",
|
| 564 |
+
" ahead = \"from \"\n",
|
| 565 |
+
" behind = \"\"\n",
|
| 566 |
+
" if(id>NUM_SUFFIX*1):\n",
|
| 567 |
+
" ahead = \"a \"\n",
|
| 568 |
+
" if(id>NUM_SUFFIX*2):\n",
|
| 569 |
+
" ahead = \"by \"\n",
|
| 570 |
+
" if(id>NUM_SUFFIX*3):\n",
|
| 571 |
+
" ahead = \"\"\n",
|
| 572 |
+
" behind = \"like\"\n",
|
| 573 |
+
" id = _modulus(id,NUM_SUFFIX)\n",
|
| 574 |
+
" #------#\n",
|
| 575 |
+
" sim = suffix_sorted[index].item()\n",
|
| 576 |
+
" name = ahead + get_suffix(id) + behind\n",
|
| 577 |
+
" if(get_suffix(id) == ' '): name = ahead + f'{id}' + behind\n",
|
| 578 |
+
" _suffixes = _suffixes + name + '|'\n",
|
| 579 |
+
" _sims = _sims + f'{round(sim*100,2)} %' + '|'\n",
|
| 580 |
+
"#------#\n",
|
| 581 |
+
"_suffixes = (_suffixes + '}').replace('|}', '}')\n",
|
| 582 |
+
"_sims = (_sims + '}').replace('|}', '}')\n",
|
| 583 |
+
"\n",
|
| 584 |
+
"print('most similiar suffix items to prompt : ' + _suffixes)\n",
|
| 585 |
+
"print('similarity % for suffix items : ' + _sims)\n",
|
| 586 |
+
"print('')\n",
|
| 587 |
+
"\n",
|
| 588 |
+
"#-------#\n",
|
| 589 |
+
"\n",
|
| 590 |
+
"_prefixes = '{'\n",
|
| 591 |
+
"for index in range(RANGE):\n",
|
| 592 |
+
" id = f'{prefix_indices[index]}'\n",
|
| 593 |
+
" #sim = prefix_sorted[index]\n",
|
| 594 |
+
" name = get_prefix(id)\n",
|
| 595 |
+
" _prefixes = _prefixes + name + '|'\n",
|
| 596 |
+
"#------#\n",
|
| 597 |
+
"_prefixes = (_prefixes + '}').replace('|}', '}')\n",
|
| 598 |
+
"print('most similiar prefix suffix to image : ' + _prefixes)\n"
|
| 599 |
],
|
| 600 |
"metadata": {
|
| 601 |
+
"id": "rebogpoyOG8k"
|
| 602 |
},
|
| 603 |
"execution_count": null,
|
| 604 |
"outputs": []
|
|
|
|
| 631 |
"print('most similiar prefix tokens to image : ' + _prefixes)\n"
|
| 632 |
],
|
| 633 |
"metadata": {
|
| 634 |
+
"id": "eZqMUhP0qYaK"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 635 |
},
|
| 636 |
"execution_count": null,
|
| 637 |
+
"outputs": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 638 |
},
|
| 639 |
{
|
| 640 |
"cell_type": "code",
|