Upload 2 files
Browse files- sd_token_similarity_calculator.ipynb +114 -116
- vocab.json +0 -0
sd_token_similarity_calculator.ipynb
CHANGED
|
@@ -41,107 +41,90 @@
|
|
| 41 |
"ACTIVE_IMG = ''\n",
|
| 42 |
"#-----#\n",
|
| 43 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 44 |
"#Import the vocab.json\n",
|
| 45 |
"import json\n",
|
| 46 |
"import pandas as pd\n",
|
| 47 |
-
"
|
|
|
|
|
|
|
| 48 |
" data = json.load(f)\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 49 |
"\n",
|
|
|
|
|
|
|
|
|
|
| 50 |
"_df = pd.DataFrame({'count': data})['count']\n",
|
|
|
|
|
|
|
|
|
|
| 51 |
"\n",
|
| 52 |
-
"
|
| 53 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
| 54 |
"}\n",
|
|
|
|
| 55 |
"#-----#\n",
|
| 56 |
"\n",
|
| 57 |
-
"# Define functions/constants\n",
|
| 58 |
-
"NUM_TOKENS = 49407\n",
|
| 59 |
"\n",
|
| 60 |
-
"
|
| 61 |
-
"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
"\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
"\n",
|
| 64 |
-
"def token_similarity(A, B):\n",
|
| 65 |
"\n",
|
| 66 |
-
"
|
| 67 |
-
" _A = LA.vector_norm(A, ord=2)\n",
|
| 68 |
-
" _B = LA.vector_norm(B, ord=2)\n",
|
| 69 |
"\n",
|
| 70 |
-
"
|
| 71 |
-
"
|
| 72 |
-
"
|
| 73 |
-
"
|
| 74 |
-
"
|
| 75 |
-
"
|
| 76 |
-
"
|
| 77 |
-
"\n",
|
| 78 |
-
"\n",
|
| 79 |
-
"
|
| 80 |
-
"
|
| 81 |
-
" A = token[id_A]\n",
|
| 82 |
-
" B = token[id_B]\n",
|
| 83 |
-
" return token_similarity(A, B)\n",
|
| 84 |
-
"#----#\n",
|
| 85 |
"\n",
|
| 86 |
-
"#print(
|
| 87 |
-
"#print(token[8922].shape) #dimension of the token\n",
|
| 88 |
-
"\n",
|
| 89 |
-
"mix_with = \"\"\n",
|
| 90 |
-
"mix_method = \"None\"\n",
|
| 91 |
-
"\n",
|
| 92 |
-
"#-------------#\n",
|
| 93 |
-
"# UNUSED\n",
|
| 94 |
-
"\n",
|
| 95 |
-
"# Get the 10 lowest values from a tensor as a string\n",
|
| 96 |
-
"def get_valleys (A):\n",
|
| 97 |
-
" sorted, indices = torch.sort(A,dim=0 , descending=False)\n",
|
| 98 |
-
" result = \"{\"\n",
|
| 99 |
-
" for index in range(10):\n",
|
| 100 |
-
" id = indices[index].item()\n",
|
| 101 |
-
" result = result + f\"{id}\"\n",
|
| 102 |
-
" if(index<9):\n",
|
| 103 |
-
" result = result + \",\"\n",
|
| 104 |
-
" result = result + \"}\"\n",
|
| 105 |
-
" return result\n",
|
| 106 |
-
"\n",
|
| 107 |
-
"# Get the 10 highest values from a tensor as a string\n",
|
| 108 |
-
"def get_peaks (A):\n",
|
| 109 |
-
" sorted, indices = torch.sort(A,dim=0 , descending=True)\n",
|
| 110 |
-
" result = \"{\"\n",
|
| 111 |
-
" for index in range(10):\n",
|
| 112 |
-
" id = indices[index].item()\n",
|
| 113 |
-
" result = result + f\"{id}\"\n",
|
| 114 |
-
" if(index<9):\n",
|
| 115 |
-
" result = result + \",\"\n",
|
| 116 |
-
" result = result + \"}\"\n",
|
| 117 |
-
" return result"
|
| 118 |
],
|
| 119 |
"metadata": {
|
| 120 |
"id": "Ch9puvwKH1s3",
|
| 121 |
-
"collapsed": true
|
| 122 |
-
"outputId": "033c251a-2043-40e7-9500-4da870ffa7fd",
|
| 123 |
-
"colab": {
|
| 124 |
-
"base_uri": "https://localhost:8080/"
|
| 125 |
-
},
|
| 126 |
-
"cellView": "form"
|
| 127 |
},
|
| 128 |
"execution_count": null,
|
| 129 |
-
"outputs": [
|
| 130 |
-
{
|
| 131 |
-
"output_type": "stream",
|
| 132 |
-
"name": "stdout",
|
| 133 |
-
"text": [
|
| 134 |
-
"Cloning into 'sd_tokens'...\n",
|
| 135 |
-
"remote: Enumerating objects: 20, done.\u001b[K\n",
|
| 136 |
-
"remote: Counting objects: 100% (17/17), done.\u001b[K\n",
|
| 137 |
-
"remote: Compressing objects: 100% (17/17), done.\u001b[K\n",
|
| 138 |
-
"remote: Total 20 (delta 4), reused 0 (delta 0), pack-reused 3 (from 1)\u001b[K\n",
|
| 139 |
-
"Unpacking objects: 100% (20/20), 310.37 KiB | 2.10 MiB/s, done.\n",
|
| 140 |
-
"Filtering content: 100% (3/3), 160.82 MiB | 26.64 MiB/s, done.\n",
|
| 141 |
-
"/content/sd_tokens\n"
|
| 142 |
-
]
|
| 143 |
-
}
|
| 144 |
-
]
|
| 145 |
},
|
| 146 |
{
|
| 147 |
"cell_type": "code",
|
|
@@ -201,11 +184,11 @@
|
|
| 201 |
"\n",
|
| 202 |
"name_A = \"A of random type\"\n",
|
| 203 |
"if (id_A>-1):\n",
|
| 204 |
-
" name_A = vocab
|
| 205 |
"\n",
|
| 206 |
"name_C = \"token C of random type\"\n",
|
| 207 |
"if (id_C>-1):\n",
|
| 208 |
-
" name_C = vocab
|
| 209 |
"\n",
|
| 210 |
"print(f\"The similarity between A '{name_A}' and C '{name_C}' is {round(sim_AC.item()*100,2)} %\")\n",
|
| 211 |
"\n",
|
|
@@ -264,7 +247,7 @@
|
|
| 264 |
"for index in range(list_size):\n",
|
| 265 |
" id = indices[index].item()\n",
|
| 266 |
" if (print_Name):\n",
|
| 267 |
-
" print(f'{vocab
|
| 268 |
" if (print_ID):\n",
|
| 269 |
" print(f'ID = {id}') # IDs\n",
|
| 270 |
" if (print_Similarity):\n",
|
|
@@ -285,14 +268,13 @@
|
|
| 285 |
"#NUM TOKENS == 49407\n",
|
| 286 |
"for index in range(NUM_TOKENS):\n",
|
| 287 |
" #print(d[f'{index}']) #<-----Use this to read values from the .db file\n",
|
| 288 |
-
" d[f'{index}']= vocab
|
| 289 |
"#----#\n",
|
| 290 |
"d.close() #close the file\n",
|
| 291 |
"# See this link for additional stuff to do with shelve: https://docs.python.org/3/library/shelve.html"
|
| 292 |
],
|
| 293 |
"metadata": {
|
| 294 |
-
"id": "iWeFnT1gAx6A"
|
| 295 |
-
"cellView": "form"
|
| 296 |
},
|
| 297 |
"execution_count": null,
|
| 298 |
"outputs": []
|
|
@@ -315,20 +297,7 @@
|
|
| 315 |
{
|
| 316 |
"cell_type": "code",
|
| 317 |
"source": [
|
| 318 |
-
"# @title
|
| 319 |
-
"#-----#\n",
|
| 320 |
-
"NUM_TOKENS = 49407\n",
|
| 321 |
-
"import shelve\n",
|
| 322 |
-
"db_vocab = shelve.open(VOCAB_FILENAME)\n",
|
| 323 |
-
"print(f'using the tokens found in {VOCAB_FILENAME}.db as the vocab')\n",
|
| 324 |
-
"# @markdown # What do you want to to mimic?\n",
|
| 325 |
-
"use = '🖼️image_encoding from image' # @param ['📝text_encoding from prompt', '🖼️image_encoding from image']\n",
|
| 326 |
-
"# @markdown --------------------------\n",
|
| 327 |
-
"use_token_padding = True # param {type:\"boolean\"} <---- Enabled by default\n",
|
| 328 |
-
"prompt = \"photo of a banana\" # @param {\"type\":\"string\",\"placeholder\":\"Write a prompt\"}\n",
|
| 329 |
-
"#-----#\n",
|
| 330 |
-
"prompt_A = prompt\n",
|
| 331 |
-
"#-----#\n",
|
| 332 |
"from google.colab import files\n",
|
| 333 |
"def upload_files():\n",
|
| 334 |
" from google.colab import files\n",
|
|
@@ -338,28 +307,58 @@
|
|
| 338 |
" return list(uploaded.keys())\n",
|
| 339 |
"#Get image\n",
|
| 340 |
"# You can use \"http://images.cocodataset.org/val2017/000000039769.jpg\" for testing\n",
|
| 341 |
-
"image_url = \"
|
| 342 |
"colab_image_path = \"\" # @param {\"type\":\"string\",\"placeholder\": \"eval. as '/content/sd_tokens/' + **your input**\"}\n",
|
| 343 |
"# @markdown --------------------------\n",
|
|
|
|
|
|
|
|
|
|
| 344 |
"from PIL import Image\n",
|
| 345 |
"import requests\n",
|
| 346 |
"image_A = \"\"\n",
|
| 347 |
"#----#\n",
|
| 348 |
-
"if
|
| 349 |
-
"
|
| 350 |
-
"
|
| 351 |
-
"
|
| 352 |
-
"
|
| 353 |
-
"
|
| 354 |
-
"
|
| 355 |
-
"
|
| 356 |
-
"
|
| 357 |
-
"
|
| 358 |
-
" else:\n",
|
| 359 |
-
" image_A = cv2.imread(\"/content/sd_tokens/\" + colab_image_path)\n",
|
| 360 |
" else:\n",
|
| 361 |
-
" image_A =
|
|
|
|
|
|
|
| 362 |
"#------#\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 363 |
"from transformers import AutoTokenizer\n",
|
| 364 |
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
| 365 |
"from transformers import CLIPProcessor, CLIPModel\n",
|
|
@@ -652,8 +651,7 @@
|
|
| 652 |
],
|
| 653 |
"metadata": {
|
| 654 |
"collapsed": true,
|
| 655 |
-
"id": "fi0jRruI0-tu"
|
| 656 |
-
"cellView": "form"
|
| 657 |
},
|
| 658 |
"execution_count": null,
|
| 659 |
"outputs": []
|
|
|
|
| 41 |
"ACTIVE_IMG = ''\n",
|
| 42 |
"#-----#\n",
|
| 43 |
"\n",
|
| 44 |
+
"# Define functions/constants\n",
|
| 45 |
+
"NUM_TOKENS = 49407\n",
|
| 46 |
+
"NUM_PREFIX = 13662\n",
|
| 47 |
+
"NUM_SUFFIX = 32901\n",
|
| 48 |
+
"\n",
|
| 49 |
+
"loaded_Image_A = False\n",
|
| 50 |
+
"\n",
|
| 51 |
"#Import the vocab.json\n",
|
| 52 |
"import json\n",
|
| 53 |
"import pandas as pd\n",
|
| 54 |
+
"\n",
|
| 55 |
+
"# Read suffix.json\n",
|
| 56 |
+
"with open('suffix.json', 'r') as f:\n",
|
| 57 |
" data = json.load(f)\n",
|
| 58 |
+
"_df = pd.DataFrame({'count': data})['count']\n",
|
| 59 |
+
"suffix = {\n",
|
| 60 |
+
" key : value for key, value in _df.items()\n",
|
| 61 |
+
"}\n",
|
| 62 |
+
"# Read prefix json\n",
|
| 63 |
+
"with open('prefix.json', 'r') as f:\n",
|
| 64 |
+
" data = json.load(f)\n",
|
| 65 |
+
"_df = pd.DataFrame({'count': data})['count']\n",
|
| 66 |
+
"prefix = {\n",
|
| 67 |
+
" key : value for key, value in _df.items()\n",
|
| 68 |
+
"}\n",
|
| 69 |
"\n",
|
| 70 |
+
"# Read to_suffix.json\n",
|
| 71 |
+
"with open('to_suffix.json', 'r') as f:\n",
|
| 72 |
+
" data = json.load(f)\n",
|
| 73 |
"_df = pd.DataFrame({'count': data})['count']\n",
|
| 74 |
+
"suffix_to_vocab = {\n",
|
| 75 |
+
" key : value for key, value in _df.items()\n",
|
| 76 |
+
"}\n",
|
| 77 |
"\n",
|
| 78 |
+
"# Read to_prefix.json\n",
|
| 79 |
+
"with open('to_prefix.json', 'r') as f:\n",
|
| 80 |
+
" data = json.load(f)\n",
|
| 81 |
+
"_df = pd.DataFrame({'count': data})['count']\n",
|
| 82 |
+
"prefix_to_vocab = {\n",
|
| 83 |
+
" key : value for key, value in _df.items()\n",
|
| 84 |
"}\n",
|
| 85 |
+
"\n",
|
| 86 |
"#-----#\n",
|
| 87 |
"\n",
|
|
|
|
|
|
|
| 88 |
"\n",
|
| 89 |
+
"# Read to_suffix.json (reversing key and value)\n",
|
| 90 |
+
"with open('to_suffix.json', 'r') as f:\n",
|
| 91 |
+
" data = json.load(f)\n",
|
| 92 |
+
"_df = pd.DataFrame({'count': data})['count']\n",
|
| 93 |
+
"vocab_to_suffix = {\n",
|
| 94 |
+
" value : key for key, value in _df.items()\n",
|
| 95 |
+
"}\n",
|
| 96 |
"\n",
|
| 97 |
+
"# Read to_prefix.json (reversing key and value)\n",
|
| 98 |
+
"with open('to_prefix.json', 'r') as f:\n",
|
| 99 |
+
" data = json.load(f)\n",
|
| 100 |
+
"_df = pd.DataFrame({'count': data})['count']\n",
|
| 101 |
+
"vocab_to_prefix = {\n",
|
| 102 |
+
" value : key for key, value in _df.items()\n",
|
| 103 |
+
"}\n",
|
| 104 |
"\n",
|
|
|
|
| 105 |
"\n",
|
| 106 |
+
"#-----#\n",
|
|
|
|
|
|
|
| 107 |
"\n",
|
| 108 |
+
"#get token from id (excluding tokens with special symbols)\n",
|
| 109 |
+
"def vocab(id):\n",
|
| 110 |
+
" _id = f'{id}'\n",
|
| 111 |
+
" if _id in vocab_to_suffix:\n",
|
| 112 |
+
" _id = vocab_to_suffix[_id]\n",
|
| 113 |
+
" return suffix[_id]\n",
|
| 114 |
+
" if _id in vocab_to_prefix:\n",
|
| 115 |
+
" _id = vocab_to_prefix[_id]\n",
|
| 116 |
+
" return prefix[_id]\n",
|
| 117 |
+
" return ' ' #<---- return whitespace if other id like emojis etc.\n",
|
| 118 |
+
"#--------#\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
| 119 |
"\n",
|
| 120 |
+
"#print(get_token(35894))\n"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
],
|
| 122 |
"metadata": {
|
| 123 |
"id": "Ch9puvwKH1s3",
|
| 124 |
+
"collapsed": true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 125 |
},
|
| 126 |
"execution_count": null,
|
| 127 |
+
"outputs": []
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 128 |
},
|
| 129 |
{
|
| 130 |
"cell_type": "code",
|
|
|
|
| 184 |
"\n",
|
| 185 |
"name_A = \"A of random type\"\n",
|
| 186 |
"if (id_A>-1):\n",
|
| 187 |
+
" name_A = vocab(id_A)\n",
|
| 188 |
"\n",
|
| 189 |
"name_C = \"token C of random type\"\n",
|
| 190 |
"if (id_C>-1):\n",
|
| 191 |
+
" name_C = vocab(id_C)\n",
|
| 192 |
"\n",
|
| 193 |
"print(f\"The similarity between A '{name_A}' and C '{name_C}' is {round(sim_AC.item()*100,2)} %\")\n",
|
| 194 |
"\n",
|
|
|
|
| 247 |
"for index in range(list_size):\n",
|
| 248 |
" id = indices[index].item()\n",
|
| 249 |
" if (print_Name):\n",
|
| 250 |
+
" print(f'{vocab(id)}') # vocab item\n",
|
| 251 |
" if (print_ID):\n",
|
| 252 |
" print(f'ID = {id}') # IDs\n",
|
| 253 |
" if (print_Similarity):\n",
|
|
|
|
| 268 |
"#NUM TOKENS == 49407\n",
|
| 269 |
"for index in range(NUM_TOKENS):\n",
|
| 270 |
" #print(d[f'{index}']) #<-----Use this to read values from the .db file\n",
|
| 271 |
+
" d[f'{index}']= vocab(indices[index].item()) #<---- write values to .db file\n",
|
| 272 |
"#----#\n",
|
| 273 |
"d.close() #close the file\n",
|
| 274 |
"# See this link for additional stuff to do with shelve: https://docs.python.org/3/library/shelve.html"
|
| 275 |
],
|
| 276 |
"metadata": {
|
| 277 |
+
"id": "iWeFnT1gAx6A"
|
|
|
|
| 278 |
},
|
| 279 |
"execution_count": null,
|
| 280 |
"outputs": []
|
|
|
|
| 297 |
{
|
| 298 |
"cell_type": "code",
|
| 299 |
"source": [
|
| 300 |
+
"# @title 🖼️ Upload an image\n",
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 301 |
"from google.colab import files\n",
|
| 302 |
"def upload_files():\n",
|
| 303 |
" from google.colab import files\n",
|
|
|
|
| 307 |
" return list(uploaded.keys())\n",
|
| 308 |
"#Get image\n",
|
| 309 |
"# You can use \"http://images.cocodataset.org/val2017/000000039769.jpg\" for testing\n",
|
| 310 |
+
"image_url = \"\" # @param {\"type\":\"string\",\"placeholder\":\"leave empty for local upload (scroll down to see it)\"}\n",
|
| 311 |
"colab_image_path = \"\" # @param {\"type\":\"string\",\"placeholder\": \"eval. as '/content/sd_tokens/' + **your input**\"}\n",
|
| 312 |
"# @markdown --------------------------\n",
|
| 313 |
+
"\n",
|
| 314 |
+
"image_path = \"\"\n",
|
| 315 |
+
"\n",
|
| 316 |
"from PIL import Image\n",
|
| 317 |
"import requests\n",
|
| 318 |
"image_A = \"\"\n",
|
| 319 |
"#----#\n",
|
| 320 |
+
"if image_url == \"\":\n",
|
| 321 |
+
" import cv2\n",
|
| 322 |
+
" from google.colab.patches import cv2_imshow\n",
|
| 323 |
+
" # Open the image.\n",
|
| 324 |
+
" if colab_image_path == \"\":\n",
|
| 325 |
+
" keys = upload_files()\n",
|
| 326 |
+
" for key in keys:\n",
|
| 327 |
+
" image_A = cv2.imread(\"/content/sd_tokens/\" + key)\n",
|
| 328 |
+
" colab_image_path = \"/content/sd_tokens/\" + key\n",
|
| 329 |
+
" image_path = \"/content/sd_tokens/\" + key\n",
|
|
|
|
|
|
|
| 330 |
" else:\n",
|
| 331 |
+
" image_A = cv2.imread(\"/content/sd_tokens/\" + colab_image_path)\n",
|
| 332 |
+
"else:\n",
|
| 333 |
+
" image_A = Image.open(requests.get(image_url, stream=True).raw)\n",
|
| 334 |
"#------#\n",
|
| 335 |
+
"\n"
|
| 336 |
+
],
|
| 337 |
+
"metadata": {
|
| 338 |
+
"id": "ke6mZ1RZDOeB"
|
| 339 |
+
},
|
| 340 |
+
"execution_count": null,
|
| 341 |
+
"outputs": []
|
| 342 |
+
},
|
| 343 |
+
{
|
| 344 |
+
"cell_type": "code",
|
| 345 |
+
"source": [
|
| 346 |
+
"# @title ⚡+🖼️ -> 📝 Token-Sampling Image interrogator\n",
|
| 347 |
+
"#-----#\n",
|
| 348 |
+
"NUM_TOKENS = 49407\n",
|
| 349 |
+
"import shelve\n",
|
| 350 |
+
"db_vocab = shelve.open(VOCAB_FILENAME)\n",
|
| 351 |
+
"print(f'using the tokens found in {VOCAB_FILENAME}.db as the vocab')\n",
|
| 352 |
+
"# @markdown # What do you want to to mimic?\n",
|
| 353 |
+
"use = '🖼️image_encoding from image' # @param ['📝text_encoding from prompt', '🖼️image_encoding from image']\n",
|
| 354 |
+
"# @markdown --------------------------\n",
|
| 355 |
+
"use_token_padding = True # param {type:\"boolean\"} <---- Enabled by default\n",
|
| 356 |
+
"prompt = \"photo of a banana\" # @param {\"type\":\"string\",\"placeholder\":\"Write a prompt\"}\n",
|
| 357 |
+
"#-----#\n",
|
| 358 |
+
"prompt_A = prompt\n",
|
| 359 |
+
"if(image_path != \"\") : image_A = cv2.imread(\"/content/sd_tokens/\" + image_path)\n",
|
| 360 |
+
"#-----#\n",
|
| 361 |
+
"\n",
|
| 362 |
"from transformers import AutoTokenizer\n",
|
| 363 |
"tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
|
| 364 |
"from transformers import CLIPProcessor, CLIPModel\n",
|
|
|
|
| 651 |
],
|
| 652 |
"metadata": {
|
| 653 |
"collapsed": true,
|
| 654 |
+
"id": "fi0jRruI0-tu"
|
|
|
|
| 655 |
},
|
| 656 |
"execution_count": null,
|
| 657 |
"outputs": []
|
vocab.json
ADDED
|
The diff for this file is too large to render.
See raw diff
|
|
|