codeShare commited on
Commit
a070d20
·
verified ·
1 Parent(s): 84d4abc

Upload sd_token_similarity_calculator.ipynb

Browse files
Files changed (1) hide show
  1. sd_token_similarity_calculator.ipynb +92 -109
sd_token_similarity_calculator.ipynb CHANGED
@@ -3,7 +3,8 @@
3
  "nbformat_minor": 0,
4
  "metadata": {
5
  "colab": {
6
- "provenance": []
 
7
  },
8
  "kernelspec": {
9
  "name": "python3",
@@ -11,7 +12,8 @@
11
  },
12
  "language_info": {
13
  "name": "python"
14
- }
 
15
  },
16
  "cells": [
17
  {
@@ -47,15 +49,14 @@
47
  "NUM_SUFFIX = 32901\n",
48
  "\n",
49
  "PREFIX_ENC_VOCAB = ['encoded_prefix_to_girl',]\n",
50
- "SUFFIX_ENC_VOCAB = [\n",
51
- " 'from_-encoded_suffix',\n",
52
- " 'a_-_encoded_suffix' ,\n",
53
- " 'by_-encoded_suffix' ,\n",
54
- " 'encoded_suffix-_like']\n",
55
  "\n",
56
  "# Make sure these match above results\n",
57
- "NUM_PREFIX_LISTS = 1\n",
58
- "NUM_SUFFIX_LISTS = 4\n",
59
  "#-----#\n",
60
  "\n",
61
  "\n",
@@ -155,10 +156,29 @@
155
  ],
156
  "metadata": {
157
  "id": "Ch9puvwKH1s3",
158
- "collapsed": true
 
 
 
 
159
  },
160
- "execution_count": null,
161
- "outputs": []
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
162
  },
163
  {
164
  "cell_type": "code",
@@ -308,7 +328,8 @@
308
  "# See this link for additional stuff to do with shelve: https://docs.python.org/3/library/shelve.html"
309
  ],
310
  "metadata": {
311
- "id": "iWeFnT1gAx6A"
 
312
  },
313
  "execution_count": null,
314
  "outputs": []
@@ -1076,48 +1097,19 @@
1076
  {
1077
  "cell_type": "code",
1078
  "source": [
1079
- "# @title Make your own text_encodings .db file for later use\n",
1080
  "\n",
1081
- "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
 
 
1082
  "\n",
1083
- "from transformers import AutoTokenizer\n",
1084
- "tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
1085
- "from transformers import CLIPProcessor, CLIPModel\n",
1086
- "processor = CLIPProcessor.from_pretrained(\"openai/clip-vit-large-patch14\" , clean_up_tokenization_spaces = True)\n",
1087
- "model = CLIPModel.from_pretrained(\"openai/clip-vit-large-patch14\").to(device)\n",
1088
- "\n",
1089
- "# Save results as .db file\n",
1090
- "import shelve\n",
1091
- "d = shelve.open('green_-encoded_suffix')\n",
1092
- "for index in range(NUM_SUFFIX):\n",
1093
- " inputs = tokenizer(text = 'green '+get_suffix(index), padding=True, return_tensors=\"pt\").to(device)\n",
1094
- " text_features = model.get_text_features(**inputs).to(device)\n",
1095
- " d[f'{index}'] = text_features.to('cpu')\n",
1096
- "#----#\n",
1097
- "d.close() #close the file\n",
1098
- "\n"
1099
- ],
1100
- "metadata": {
1101
- "id": "9ZiTsF9jV0TV"
1102
- },
1103
- "execution_count": null,
1104
- "outputs": []
1105
- },
1106
- {
1107
- "cell_type": "code",
1108
- "source": [
1109
- "# Save results as .db file\n",
1110
- "import shelve\n",
1111
- "d = shelve.open('encoded_suffix-_knee')\n",
1112
- "for index in range(NUM_SUFFIX):\n",
1113
- " inputs = tokenizer(text = get_suffix(index)+' knee', padding=True, return_tensors=\"pt\").to(device)\n",
1114
- " text_features = model.get_text_features(**inputs).to(device)\n",
1115
- " d[f'{index}'] = text_features.to('cpu')\n",
1116
- "#----#\n",
1117
- "d.close() #close the file"
1118
  ],
1119
  "metadata": {
1120
- "id": "9h6Rw72Pd307"
1121
  },
1122
  "execution_count": null,
1123
  "outputs": []
@@ -1125,56 +1117,53 @@
1125
  {
1126
  "cell_type": "code",
1127
  "source": [
1128
- "# Save results as .db file\n",
1129
- "import shelve\n",
1130
- "d = shelve.open('encoded_suffix-_nose')\n",
1131
- "for index in range(NUM_SUFFIX):\n",
1132
- " inputs = tokenizer(text = get_suffix(index)+' nose', padding=True, return_tensors=\"pt\").to(device)\n",
1133
- " text_features = model.get_text_features(**inputs).to(device)\n",
1134
- " d[f'{index}'] = text_features.to('cpu')\n",
1135
- "#----#\n",
1136
- "d.close() #close the file"
1137
- ],
1138
- "metadata": {
1139
- "id": "UG4M1-Tqd8Ly"
1140
- },
1141
- "execution_count": null,
1142
- "outputs": []
1143
- },
1144
- {
1145
- "cell_type": "code",
1146
- "source": [
1147
- "# Save results as .db file\n",
1148
- "import shelve\n",
1149
- "d = shelve.open('encoded_suffix-_hair')\n",
1150
- "for index in range(NUM_SUFFIX):\n",
1151
- " inputs = tokenizer(text = get_suffix(index)+' hair', padding=True, return_tensors=\"pt\").to(device)\n",
1152
- " text_features = model.get_text_features(**inputs).to(device)\n",
1153
- " d[f'{index}'] = text_features.to('cpu')\n",
1154
- "#----#\n",
1155
- "d.close() #close the file"
1156
- ],
1157
- "metadata": {
1158
- "id": "GxkmAz6-eJgY"
1159
- },
1160
- "execution_count": null,
1161
- "outputs": []
1162
- },
1163
- {
1164
- "cell_type": "code",
1165
- "source": [
1166
- "# Save results as .db file\n",
1167
- "import shelve\n",
1168
- "d = shelve.open('encoded_suffix-_background')\n",
1169
- "for index in range(NUM_SUFFIX):\n",
1170
- " inputs = tokenizer(text = get_suffix(index)+' background', padding=True, return_tensors=\"pt\").to(device)\n",
1171
- " text_features = model.get_text_features(**inputs).to(device)\n",
1172
- " d[f'{index}'] = text_features.to('cpu')\n",
1173
- "#----#\n",
1174
- "d.close() #close the file"
1175
  ],
1176
  "metadata": {
1177
- "id": "Sdi8Gw1BfL-0"
1178
  },
1179
  "execution_count": null,
1180
  "outputs": []
@@ -1182,18 +1171,12 @@
1182
  {
1183
  "cell_type": "code",
1184
  "source": [
1185
- "# Save results as .db file\n",
1186
- "import shelve\n",
1187
- "d = shelve.open('in_a_-encoded_suffix')\n",
1188
- "for index in range(NUM_SUFFIX):\n",
1189
- " inputs = tokenizer(text = 'in a '+get_suffix(index), padding=True, return_tensors=\"pt\").to(device)\n",
1190
- " text_features = model.get_text_features(**inputs).to(device)\n",
1191
- " d[f'{index}'] = text_features.to('cpu')\n",
1192
- "#----#\n",
1193
- "d.close() #close the file"
1194
  ],
1195
  "metadata": {
1196
- "id": "FztUKiyegN2F"
1197
  },
1198
  "execution_count": null,
1199
  "outputs": []
 
3
  "nbformat_minor": 0,
4
  "metadata": {
5
  "colab": {
6
+ "provenance": [],
7
+ "gpuType": "T4"
8
  },
9
  "kernelspec": {
10
  "name": "python3",
 
12
  },
13
  "language_info": {
14
  "name": "python"
15
+ },
16
+ "accelerator": "GPU"
17
  },
18
  "cells": [
19
  {
 
49
  "NUM_SUFFIX = 32901\n",
50
  "\n",
51
  "PREFIX_ENC_VOCAB = ['encoded_prefix_to_girl',]\n",
52
+ "SUFFIX_ENC_VOCAB = ['a_-_encoded_suffix' ,]\n",
53
+ " #'from_-encoded_suffix',\n",
54
+ " #'by_-encoded_suffix' ,\n",
55
+ " #'encoded_suffix-_like']\n",
 
56
  "\n",
57
  "# Make sure these match above results\n",
58
+ "NUM_PREFIX_LISTS = len(PREFIX_ENC_VOCAB)\n",
59
+ "NUM_SUFFIX_LISTS = len(SUFFIX_ENC_VOCAB)\n",
60
  "#-----#\n",
61
  "\n",
62
  "\n",
 
156
  ],
157
  "metadata": {
158
  "id": "Ch9puvwKH1s3",
159
+ "collapsed": true,
160
+ "colab": {
161
+ "base_uri": "https://localhost:8080/"
162
+ },
163
+ "outputId": "42e8d455-ca0a-4c78-dba7-a32d9dee9b41"
164
  },
165
+ "execution_count": 1,
166
+ "outputs": [
167
+ {
168
+ "output_type": "stream",
169
+ "name": "stdout",
170
+ "text": [
171
+ "Cloning into 'sd_tokens'...\n",
172
+ "remote: Enumerating objects: 99, done.\u001b[K\n",
173
+ "remote: Counting objects: 100% (96/96), done.\u001b[K\n",
174
+ "remote: Compressing objects: 100% (96/96), done.\u001b[K\n",
175
+ "remote: Total 99 (delta 34), reused 0 (delta 0), pack-reused 3 (from 1)\u001b[K\n",
176
+ "Unpacking objects: 100% (99/99), 1.35 MiB | 1.60 MiB/s, done.\n",
177
+ "Filtering content: 100% (22/22), 2.47 GiB | 36.54 MiB/s, done.\n",
178
+ "/content/sd_tokens\n"
179
+ ]
180
+ }
181
+ ]
182
  },
183
  {
184
  "cell_type": "code",
 
328
  "# See this link for additional stuff to do with shelve: https://docs.python.org/3/library/shelve.html"
329
  ],
330
  "metadata": {
331
+ "id": "iWeFnT1gAx6A",
332
+ "cellView": "form"
333
  },
334
  "execution_count": null,
335
  "outputs": []
 
1097
  {
1098
  "cell_type": "code",
1099
  "source": [
 
1100
  "\n",
1101
+ "# @title Import text-to-image-prompts .json files\n",
1102
+ "%cd /content/\n",
1103
+ "!git clone https://huggingface.co/datasets/codeShare/text-to-image-prompts\n",
1104
  "\n",
1105
+ "#Initialize\n",
1106
+ "import os\n",
1107
+ "def my_mkdirs(folder):\n",
1108
+ " if os.path.exists(folder)==False:\n",
1109
+ " os.makedirs(folder)"
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1110
  ],
1111
  "metadata": {
1112
+ "id": "Qy51FFu8aVNA"
1113
  },
1114
  "execution_count": null,
1115
  "outputs": []
 
1117
  {
1118
  "cell_type": "code",
1119
  "source": [
1120
+ "# @title Make your own text_encodings .db file for later use\n",
1121
+ "device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')\n",
1122
+ "from transformers import AutoTokenizer\n",
1123
+ "tokenizer = AutoTokenizer.from_pretrained(\"openai/clip-vit-large-patch14\", clean_up_tokenization_spaces = False)\n",
1124
+ "from transformers import CLIPProcessor, CLIPModel\n",
1125
+ "processor = CLIPProcessor.from_pretrained(\"openai/clip-vit-large-patch14\" , clean_up_tokenization_spaces = True)\n",
1126
+ "model = CLIPModel.from_pretrained(\"openai/clip-vit-large-patch14\").to(device)\n",
1127
+ "\n",
1128
+ "#Import the vocab.json\n",
1129
+ "import json\n",
1130
+ "import pandas as pd\n",
1131
+ "\n",
1132
+ "my_mkdirs('/content/text_encodings/')\n",
1133
+ "filename = ''\n",
1134
+ "\n",
1135
+ "for file_index in range(34):\n",
1136
+ " if file_index <1: continue\n",
1137
+ " filename = f'🦜 fusion-t2i-prompt-features-{file_index}'\n",
1138
+ " #🦜 fusion-t2i-prompt-features-1.json\n",
1139
+ "\n",
1140
+ " # Read suffix.json\n",
1141
+ " %cd /content/text-to-image-prompts/civitai-prompts/green/\n",
1142
+ " with open(filename + '.json', 'r') as f:\n",
1143
+ " data = json.load(f)\n",
1144
+ " _df = pd.DataFrame({'count': data})['count']\n",
1145
+ " prompts = {\n",
1146
+ " key : value for key, value in _df.items()\n",
1147
+ " }\n",
1148
+ " NUM_ITEMS = int(prompts[\"0\"])\n",
1149
+ " #------#\n",
1150
+ "\n",
1151
+ " # Calculate text_encoding for .json file contents and results as .db file\n",
1152
+ "\n",
1153
+ " %cd /content/text_encodings/\n",
1154
+ " import shelve\n",
1155
+ " d = shelve.open(filename)\n",
1156
+ " for index in range(NUM_ITEMS):\n",
1157
+ " inputs = tokenizer(text = '' + prompts[f'{index}'], padding=True, return_tensors=\"pt\").to(device)\n",
1158
+ " text_features = model.get_text_features(**inputs).to(device)\n",
1159
+ " text_features = text_features/text_features.norm(p=2, dim=-1, keepdim=True).to(device)\n",
1160
+ " d[f'{index}'] = text_features.to('cpu')\n",
1161
+ " #----#\n",
1162
+ " d.close() #close the file\n",
1163
+ "\n"
 
 
 
1164
  ],
1165
  "metadata": {
1166
+ "id": "9ZiTsF9jV0TV"
1167
  },
1168
  "execution_count": null,
1169
  "outputs": []
 
1171
  {
1172
  "cell_type": "code",
1173
  "source": [
1174
+ "# @title Download the created text_encodings as .zip file\n",
1175
+ "%cd /content/\n",
1176
+ "!zip -r /content/text-encodings.zip /content/text_encodings"
 
 
 
 
 
 
1177
  ],
1178
  "metadata": {
1179
+ "id": "gX-sHZPWj4Lt"
1180
  },
1181
  "execution_count": null,
1182
  "outputs": []