Upload sd_token_similarity_calculator.ipynb
Browse files
sd_token_similarity_calculator.ipynb
CHANGED
|
@@ -37,6 +37,9 @@
|
|
| 37 |
"%cd /content/sd_tokens\n",
|
| 38 |
"token = torch.load('sd15_tensors.pt', map_location=device, weights_only=True)\n",
|
| 39 |
"#-----#\n",
|
|
|
|
|
|
|
|
|
|
| 40 |
"\n",
|
| 41 |
"#Import the vocab.json\n",
|
| 42 |
"import json\n",
|
|
@@ -130,7 +133,9 @@
|
|
| 130 |
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
| 131 |
"\n",
|
| 132 |
"# @markdown Write name of token to match against\n",
|
| 133 |
-
"
|
|
|
|
|
|
|
| 134 |
"# @markdown (optional) Mix the token with something else\n",
|
| 135 |
"mix_with = \"\" # @param {\"type\":\"string\",\"placeholder\":\"leave empty for random value token\"}\n",
|
| 136 |
"mix_method = \"None\" # @param [\"None\" , \"Average\", \"Subtract\"] {allow-input: true}\n",
|
|
@@ -252,10 +257,23 @@
|
|
| 252 |
"\n",
|
| 253 |
"#The prompt will be enclosed with the <|start-of-text|> and <|end-of-text|> tokens, which is why output will be [49406, ... , 49407].\n",
|
| 254 |
"\n",
|
| 255 |
-
"#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
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 256 |
],
|
| 257 |
"metadata": {
|
| 258 |
-
"id": "iWeFnT1gAx6A"
|
|
|
|
| 259 |
},
|
| 260 |
"execution_count": null,
|
| 261 |
"outputs": []
|
|
@@ -268,7 +286,8 @@
|
|
| 268 |
"It takes a long while to check all the tokens (too long!) so this cell only samples a range of the 49K available tokens.\n",
|
| 269 |
"\n",
|
| 270 |
"You can run this cell, then paste the result into the 'must_contain' box , and then run the cell again.\n",
|
| 271 |
-
"\n"
|
|
|
|
| 272 |
],
|
| 273 |
"metadata": {
|
| 274 |
"id": "IUCuV9RtQpBn"
|
|
@@ -277,33 +296,12 @@
|
|
| 277 |
{
|
| 278 |
"cell_type": "code",
|
| 279 |
"source": [
|
| 280 |
-
"# @title
|
| 281 |
-
"import shelve\n",
|
| 282 |
-
"d = shelve.open('tokens_most_similiar_to_' + name_A.replace('</w>','').strip())\n",
|
| 283 |
-
"#NUM TOKENS == 49407\n",
|
| 284 |
-
"for index in range(NUM_TOKENS):\n",
|
| 285 |
-
" #print(d[f'{index}']) #<-----Use this to read values from the .db file\n",
|
| 286 |
-
" d[f'{index}']= vocab[indices[index].item()] #<---- write values to .db file\n",
|
| 287 |
-
"#----#\n",
|
| 288 |
-
"d.close() #close the file\n",
|
| 289 |
-
"# See this link for additional stuff to do with shelve: https://docs.python.org/3/library/shelve.html"
|
| 290 |
-
],
|
| 291 |
-
"metadata": {
|
| 292 |
-
"id": "qj888fPEbX8K"
|
| 293 |
-
},
|
| 294 |
-
"execution_count": 15,
|
| 295 |
-
"outputs": []
|
| 296 |
-
},
|
| 297 |
-
{
|
| 298 |
-
"cell_type": "code",
|
| 299 |
-
"source": [
|
| 300 |
-
"# @title 🪐🖼️ -> 📝 Token-Sampling Image interrogator\n",
|
| 301 |
-
"VOCAB_FILENAME = 'tokens_most_similiar_to_girl' #This vocab has been ordered where lowest index has the highest similarity to the reference vector \"girl</w>\". Feel free to create your own .db around a target token in above cells.\n",
|
| 302 |
"#-----#\n",
|
| 303 |
"import shelve\n",
|
| 304 |
"db_vocab = shelve.open(VOCAB_FILENAME)\n",
|
| 305 |
"# @markdown # What do you want to to mimic?\n",
|
| 306 |
-
"use = '
|
| 307 |
"# @markdown --------------------------\n",
|
| 308 |
"use_token_padding = True # param {type:\"boolean\"} <---- Enabled by default\n",
|
| 309 |
"prompt = \"photo of a banana\" # @param {\"type\":\"string\",\"placeholder\":\"Write a prompt\"}\n",
|
|
@@ -319,7 +317,7 @@
|
|
| 319 |
" return list(uploaded.keys())\n",
|
| 320 |
"#Get image\n",
|
| 321 |
"# You can use \"http://images.cocodataset.org/val2017/000000039769.jpg\" for testing\n",
|
| 322 |
-
"image_url = \"
|
| 323 |
"colab_image_path = \"\" # @param {\"type\":\"string\",\"placeholder\": \"eval. as '/content/sd_tokens/' + **your input**\"}\n",
|
| 324 |
"# @markdown --------------------------\n",
|
| 325 |
"from PIL import Image\n",
|
|
@@ -368,7 +366,7 @@
|
|
| 368 |
"# @markdown -----\n",
|
| 369 |
"# @markdown # Use a range of tokens from the vocab.json (slow method)\n",
|
| 370 |
"start_search_at_index = 1700 # @param {type:\"slider\", min:0, max: 49407, step:100}\n",
|
| 371 |
-
"# @markdown The lower the start_index, the more similiar the sampled tokens will be to the
|
| 372 |
"start_search_at_ID = start_search_at_index\n",
|
| 373 |
"search_range = 100 # @param {type:\"slider\", min:100, max: 2000, step:0}\n",
|
| 374 |
"restrictions = 'None' # @param [\"None\", \"Suffix only\", \"Prefix only\"]\n",
|
|
@@ -567,7 +565,22 @@
|
|
| 567 |
],
|
| 568 |
"metadata": {
|
| 569 |
"collapsed": true,
|
| 570 |
-
"id": "fi0jRruI0-tu"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 571 |
},
|
| 572 |
"execution_count": null,
|
| 573 |
"outputs": []
|
|
|
|
| 37 |
"%cd /content/sd_tokens\n",
|
| 38 |
"token = torch.load('sd15_tensors.pt', map_location=device, weights_only=True)\n",
|
| 39 |
"#-----#\n",
|
| 40 |
+
"VOCAB_FILENAME = 'tokens_most_similiar_to_girl'\n",
|
| 41 |
+
"ACTIVE_IMG = ''\n",
|
| 42 |
+
"#-----#\n",
|
| 43 |
"\n",
|
| 44 |
"#Import the vocab.json\n",
|
| 45 |
"import json\n",
|
|
|
|
| 133 |
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
| 134 |
"\n",
|
| 135 |
"# @markdown Write name of token to match against\n",
|
| 136 |
+
"token_name = \"banana\" # @param {type:'string',\"placeholder\":\"leave empty for random value token\"}\n",
|
| 137 |
+
"\n",
|
| 138 |
+
"prompt = token_name\n",
|
| 139 |
"# @markdown (optional) Mix the token with something else\n",
|
| 140 |
"mix_with = \"\" # @param {\"type\":\"string\",\"placeholder\":\"leave empty for random value token\"}\n",
|
| 141 |
"mix_method = \"None\" # @param [\"None\" , \"Average\", \"Subtract\"] {allow-input: true}\n",
|
|
|
|
| 257 |
"\n",
|
| 258 |
"#The prompt will be enclosed with the <|start-of-text|> and <|end-of-text|> tokens, which is why output will be [49406, ... , 49407].\n",
|
| 259 |
"\n",
|
| 260 |
+
"#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.\n",
|
| 261 |
+
"\n",
|
| 262 |
+
"# Save results as .db file\n",
|
| 263 |
+
"import shelve\n",
|
| 264 |
+
"VOCAB_FILENAME = 'tokens_most_similiar_to_' + name_A.replace('</w>','').strip()\n",
|
| 265 |
+
"d = shelve.open(VOCAB_FILENAME)\n",
|
| 266 |
+
"#NUM TOKENS == 49407\n",
|
| 267 |
+
"for index in range(NUM_TOKENS):\n",
|
| 268 |
+
" #print(d[f'{index}']) #<-----Use this to read values from the .db file\n",
|
| 269 |
+
" d[f'{index}']= vocab[indices[index].item()] #<---- write values to .db file\n",
|
| 270 |
+
"#----#\n",
|
| 271 |
+
"d.close() #close the file\n",
|
| 272 |
+
"# See this link for additional stuff to do with shelve: https://docs.python.org/3/library/shelve.html"
|
| 273 |
],
|
| 274 |
"metadata": {
|
| 275 |
+
"id": "iWeFnT1gAx6A",
|
| 276 |
+
"cellView": "form"
|
| 277 |
},
|
| 278 |
"execution_count": null,
|
| 279 |
"outputs": []
|
|
|
|
| 286 |
"It takes a long while to check all the tokens (too long!) so this cell only samples a range of the 49K available tokens.\n",
|
| 287 |
"\n",
|
| 288 |
"You can run this cell, then paste the result into the 'must_contain' box , and then run the cell again.\n",
|
| 289 |
+
"\n",
|
| 290 |
+
"Check the sd_tokens folder for stored .db files from running the '⚡ Get similiar tokens' cell. These can be used in the ⚡+🖼️ -> 📝 Token-Sampling Image interrogator cell\n"
|
| 291 |
],
|
| 292 |
"metadata": {
|
| 293 |
"id": "IUCuV9RtQpBn"
|
|
|
|
| 296 |
{
|
| 297 |
"cell_type": "code",
|
| 298 |
"source": [
|
| 299 |
+
"# @title ⚡+🖼️ -> 📝 Token-Sampling Image interrogator\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 300 |
"#-----#\n",
|
| 301 |
"import shelve\n",
|
| 302 |
"db_vocab = shelve.open(VOCAB_FILENAME)\n",
|
| 303 |
"# @markdown # What do you want to to mimic?\n",
|
| 304 |
+
"use = '🖼️image_encoding from image' # @param ['📝text_encoding from prompt', '🖼️image_encoding from image']\n",
|
| 305 |
"# @markdown --------------------------\n",
|
| 306 |
"use_token_padding = True # param {type:\"boolean\"} <---- Enabled by default\n",
|
| 307 |
"prompt = \"photo of a banana\" # @param {\"type\":\"string\",\"placeholder\":\"Write a prompt\"}\n",
|
|
|
|
| 317 |
" return list(uploaded.keys())\n",
|
| 318 |
"#Get image\n",
|
| 319 |
"# You can use \"http://images.cocodataset.org/val2017/000000039769.jpg\" for testing\n",
|
| 320 |
+
"image_url = \"\" # @param {\"type\":\"string\",\"placeholder\":\"leave empty for local upload (scroll down to see it)\"}\n",
|
| 321 |
"colab_image_path = \"\" # @param {\"type\":\"string\",\"placeholder\": \"eval. as '/content/sd_tokens/' + **your input**\"}\n",
|
| 322 |
"# @markdown --------------------------\n",
|
| 323 |
"from PIL import Image\n",
|
|
|
|
| 366 |
"# @markdown -----\n",
|
| 367 |
"# @markdown # Use a range of tokens from the vocab.json (slow method)\n",
|
| 368 |
"start_search_at_index = 1700 # @param {type:\"slider\", min:0, max: 49407, step:100}\n",
|
| 369 |
+
"# @markdown The lower the start_index, the more similiar the sampled tokens will be to the target token assigned in the '⚡ Get similiar tokens' cell\"\n",
|
| 370 |
"start_search_at_ID = start_search_at_index\n",
|
| 371 |
"search_range = 100 # @param {type:\"slider\", min:100, max: 2000, step:0}\n",
|
| 372 |
"restrictions = 'None' # @param [\"None\", \"Suffix only\", \"Prefix only\"]\n",
|
|
|
|
| 565 |
],
|
| 566 |
"metadata": {
|
| 567 |
"collapsed": true,
|
| 568 |
+
"id": "fi0jRruI0-tu",
|
| 569 |
+
"cellView": "form"
|
| 570 |
+
},
|
| 571 |
+
"execution_count": null,
|
| 572 |
+
"outputs": []
|
| 573 |
+
},
|
| 574 |
+
{
|
| 575 |
+
"cell_type": "code",
|
| 576 |
+
"source": [
|
| 577 |
+
"# @title (Optional) ⚡Actively set which Vocab list to use for the interrogator\n",
|
| 578 |
+
"token_name = \"\" # @param {\"type\":\"string\",\"placeholder\":\"Write a token_name used earlier\"}\n",
|
| 579 |
+
"VOCAB_FILENAME = 'tokens_most_similiar_to_' + token_name.replace('</w>','').strip()\n",
|
| 580 |
+
"print(f'Using a vocab ordered to most similiar to the token {token_name}')"
|
| 581 |
+
],
|
| 582 |
+
"metadata": {
|
| 583 |
+
"id": "FYa96UCQuE1U"
|
| 584 |
},
|
| 585 |
"execution_count": null,
|
| 586 |
"outputs": []
|