Matthew Tucker commited on
Commit ·
bd181aa
1
Parent(s): 24ea9ce
copy class dependencies to notebook
Browse filesCan't figure out how to run it in the cloud with local deps yet
- Cliptag.ipynb +151 -16
Cliptag.ipynb
CHANGED
|
@@ -2,7 +2,7 @@
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
-
"execution_count":
|
| 6 |
"metadata": {
|
| 7 |
"colab": {
|
| 8 |
"base_uri": "https://localhost:8080/"
|
|
@@ -10,10 +10,156 @@
|
|
| 10 |
"id": "WDAOVA8dtmR-",
|
| 11 |
"outputId": "ca96e5b3-5566-4264-d4e4-71b1e7ef8c49"
|
| 12 |
},
|
| 13 |
-
"outputs": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 14 |
"source": [
|
| 15 |
"%pip install torch\n",
|
| 16 |
-
"%pip install clip_interrogator"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 17 |
]
|
| 18 |
},
|
| 19 |
{
|
|
@@ -33,7 +179,7 @@
|
|
| 33 |
},
|
| 34 |
{
|
| 35 |
"cell_type": "code",
|
| 36 |
-
"execution_count":
|
| 37 |
"metadata": {
|
| 38 |
"colab": {
|
| 39 |
"base_uri": "https://localhost:8080/"
|
|
@@ -41,20 +187,9 @@
|
|
| 41 |
"id": "bJ1tJyL0-8qQ",
|
| 42 |
"outputId": "1313dc44-baba-4d49-c1ee-ebf6b0c351f6"
|
| 43 |
},
|
| 44 |
-
"outputs": [
|
| 45 |
-
{
|
| 46 |
-
"name": "stdout",
|
| 47 |
-
"output_type": "stream",
|
| 48 |
-
"text": [
|
| 49 |
-
"Using CPU\n",
|
| 50 |
-
"Loading CLIP model ViT-L-14/openai...\n",
|
| 51 |
-
"Loading CLIP model ViT-B-32/laion2b_s34b_b79k...\n"
|
| 52 |
-
]
|
| 53 |
-
}
|
| 54 |
-
],
|
| 55 |
"source": [
|
| 56 |
"from clip_interrogator import Config\n",
|
| 57 |
-
"from cliptag.cliptag import load_keywords_from_files, CIInterrogator\n",
|
| 58 |
"import torch \n",
|
| 59 |
"\n",
|
| 60 |
"if torch.cuda.is_available():\n",
|
|
|
|
| 2 |
"cells": [
|
| 3 |
{
|
| 4 |
"cell_type": "code",
|
| 5 |
+
"execution_count": 1,
|
| 6 |
"metadata": {
|
| 7 |
"colab": {
|
| 8 |
"base_uri": "https://localhost:8080/"
|
|
|
|
| 10 |
"id": "WDAOVA8dtmR-",
|
| 11 |
"outputId": "ca96e5b3-5566-4264-d4e4-71b1e7ef8c49"
|
| 12 |
},
|
| 13 |
+
"outputs": [
|
| 14 |
+
{
|
| 15 |
+
"name": "stdout",
|
| 16 |
+
"output_type": "stream",
|
| 17 |
+
"text": [
|
| 18 |
+
"Requirement already satisfied: torch in ./.venv/lib/python3.11/site-packages (2.0.0)\n",
|
| 19 |
+
"Requirement already satisfied: filelock in ./.venv/lib/python3.11/site-packages (from torch) (3.12.0)\n",
|
| 20 |
+
"Requirement already satisfied: typing-extensions in ./.venv/lib/python3.11/site-packages (from torch) (4.5.0)\n",
|
| 21 |
+
"Requirement already satisfied: sympy in ./.venv/lib/python3.11/site-packages (from torch) (1.11.1)\n",
|
| 22 |
+
"Requirement already satisfied: networkx in ./.venv/lib/python3.11/site-packages (from torch) (3.1)\n",
|
| 23 |
+
"Requirement already satisfied: jinja2 in ./.venv/lib/python3.11/site-packages (from torch) (3.1.2)\n",
|
| 24 |
+
"Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.11/site-packages (from jinja2->torch) (2.1.2)\n",
|
| 25 |
+
"Requirement already satisfied: mpmath>=0.19 in ./.venv/lib/python3.11/site-packages (from sympy->torch) (1.3.0)\n",
|
| 26 |
+
"\n",
|
| 27 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.1\u001b[0m\n",
|
| 28 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
|
| 29 |
+
"Note: you may need to restart the kernel to use updated packages.\n",
|
| 30 |
+
"Requirement already satisfied: clip_interrogator in ./.venv/lib/python3.11/site-packages (0.6.0)\n",
|
| 31 |
+
"Requirement already satisfied: torch in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (2.0.0)\n",
|
| 32 |
+
"Requirement already satisfied: torchvision in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (0.15.1)\n",
|
| 33 |
+
"Requirement already satisfied: Pillow in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (9.5.0)\n",
|
| 34 |
+
"Requirement already satisfied: requests in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (2.28.2)\n",
|
| 35 |
+
"Requirement already satisfied: safetensors in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (0.3.1)\n",
|
| 36 |
+
"Requirement already satisfied: tqdm in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (4.65.0)\n",
|
| 37 |
+
"Requirement already satisfied: open-clip-torch in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (2.17.1)\n",
|
| 38 |
+
"Requirement already satisfied: accelerate in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (0.18.0)\n",
|
| 39 |
+
"Requirement already satisfied: transformers>=4.27.1 in ./.venv/lib/python3.11/site-packages (from clip_interrogator) (4.28.1)\n",
|
| 40 |
+
"Requirement already satisfied: filelock in ./.venv/lib/python3.11/site-packages (from transformers>=4.27.1->clip_interrogator) (3.12.0)\n",
|
| 41 |
+
"Requirement already satisfied: huggingface-hub<1.0,>=0.11.0 in ./.venv/lib/python3.11/site-packages (from transformers>=4.27.1->clip_interrogator) (0.13.4)\n",
|
| 42 |
+
"Requirement already satisfied: numpy>=1.17 in ./.venv/lib/python3.11/site-packages (from transformers>=4.27.1->clip_interrogator) (1.24.2)\n",
|
| 43 |
+
"Requirement already satisfied: packaging>=20.0 in ./.venv/lib/python3.11/site-packages (from transformers>=4.27.1->clip_interrogator) (23.1)\n",
|
| 44 |
+
"Requirement already satisfied: pyyaml>=5.1 in ./.venv/lib/python3.11/site-packages (from transformers>=4.27.1->clip_interrogator) (6.0)\n",
|
| 45 |
+
"Requirement already satisfied: regex!=2019.12.17 in ./.venv/lib/python3.11/site-packages (from transformers>=4.27.1->clip_interrogator) (2023.3.23)\n",
|
| 46 |
+
"Requirement already satisfied: tokenizers!=0.11.3,<0.14,>=0.11.1 in ./.venv/lib/python3.11/site-packages (from transformers>=4.27.1->clip_interrogator) (0.13.3)\n",
|
| 47 |
+
"Requirement already satisfied: psutil in ./.venv/lib/python3.11/site-packages (from accelerate->clip_interrogator) (5.9.5)\n",
|
| 48 |
+
"Requirement already satisfied: typing-extensions in ./.venv/lib/python3.11/site-packages (from torch->clip_interrogator) (4.5.0)\n",
|
| 49 |
+
"Requirement already satisfied: sympy in ./.venv/lib/python3.11/site-packages (from torch->clip_interrogator) (1.11.1)\n",
|
| 50 |
+
"Requirement already satisfied: networkx in ./.venv/lib/python3.11/site-packages (from torch->clip_interrogator) (3.1)\n",
|
| 51 |
+
"Requirement already satisfied: jinja2 in ./.venv/lib/python3.11/site-packages (from torch->clip_interrogator) (3.1.2)\n",
|
| 52 |
+
"Requirement already satisfied: ftfy in ./.venv/lib/python3.11/site-packages (from open-clip-torch->clip_interrogator) (6.1.1)\n",
|
| 53 |
+
"Requirement already satisfied: sentencepiece in ./.venv/lib/python3.11/site-packages (from open-clip-torch->clip_interrogator) (0.1.98)\n",
|
| 54 |
+
"Requirement already satisfied: protobuf<4 in ./.venv/lib/python3.11/site-packages (from open-clip-torch->clip_interrogator) (3.20.3)\n",
|
| 55 |
+
"Requirement already satisfied: timm in ./.venv/lib/python3.11/site-packages (from open-clip-torch->clip_interrogator) (0.6.13)\n",
|
| 56 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in ./.venv/lib/python3.11/site-packages (from requests->clip_interrogator) (3.1.0)\n",
|
| 57 |
+
"Requirement already satisfied: idna<4,>=2.5 in ./.venv/lib/python3.11/site-packages (from requests->clip_interrogator) (3.4)\n",
|
| 58 |
+
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./.venv/lib/python3.11/site-packages (from requests->clip_interrogator) (1.26.15)\n",
|
| 59 |
+
"Requirement already satisfied: certifi>=2017.4.17 in ./.venv/lib/python3.11/site-packages (from requests->clip_interrogator) (2022.12.7)\n",
|
| 60 |
+
"Requirement already satisfied: wcwidth>=0.2.5 in ./.venv/lib/python3.11/site-packages (from ftfy->open-clip-torch->clip_interrogator) (0.2.6)\n",
|
| 61 |
+
"Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.11/site-packages (from jinja2->torch->clip_interrogator) (2.1.2)\n",
|
| 62 |
+
"Requirement already satisfied: mpmath>=0.19 in ./.venv/lib/python3.11/site-packages (from sympy->torch->clip_interrogator) (1.3.0)\n",
|
| 63 |
+
"\n",
|
| 64 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.1\u001b[0m\n",
|
| 65 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
|
| 66 |
+
"Note: you may need to restart the kernel to use updated packages.\n",
|
| 67 |
+
"Requirement already satisfied: open_clip_torch in ./.venv/lib/python3.11/site-packages (2.17.1)\n",
|
| 68 |
+
"Requirement already satisfied: torch>=1.9.0 in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (2.0.0)\n",
|
| 69 |
+
"Requirement already satisfied: torchvision in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (0.15.1)\n",
|
| 70 |
+
"Requirement already satisfied: regex in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (2023.3.23)\n",
|
| 71 |
+
"Requirement already satisfied: ftfy in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (6.1.1)\n",
|
| 72 |
+
"Requirement already satisfied: tqdm in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (4.65.0)\n",
|
| 73 |
+
"Requirement already satisfied: huggingface-hub in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (0.13.4)\n",
|
| 74 |
+
"Requirement already satisfied: sentencepiece in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (0.1.98)\n",
|
| 75 |
+
"Requirement already satisfied: protobuf<4 in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (3.20.3)\n",
|
| 76 |
+
"Requirement already satisfied: timm in ./.venv/lib/python3.11/site-packages (from open_clip_torch) (0.6.13)\n",
|
| 77 |
+
"Requirement already satisfied: filelock in ./.venv/lib/python3.11/site-packages (from torch>=1.9.0->open_clip_torch) (3.12.0)\n",
|
| 78 |
+
"Requirement already satisfied: typing-extensions in ./.venv/lib/python3.11/site-packages (from torch>=1.9.0->open_clip_torch) (4.5.0)\n",
|
| 79 |
+
"Requirement already satisfied: sympy in ./.venv/lib/python3.11/site-packages (from torch>=1.9.0->open_clip_torch) (1.11.1)\n",
|
| 80 |
+
"Requirement already satisfied: networkx in ./.venv/lib/python3.11/site-packages (from torch>=1.9.0->open_clip_torch) (3.1)\n",
|
| 81 |
+
"Requirement already satisfied: jinja2 in ./.venv/lib/python3.11/site-packages (from torch>=1.9.0->open_clip_torch) (3.1.2)\n",
|
| 82 |
+
"Requirement already satisfied: wcwidth>=0.2.5 in ./.venv/lib/python3.11/site-packages (from ftfy->open_clip_torch) (0.2.6)\n",
|
| 83 |
+
"Requirement already satisfied: requests in ./.venv/lib/python3.11/site-packages (from huggingface-hub->open_clip_torch) (2.28.2)\n",
|
| 84 |
+
"Requirement already satisfied: pyyaml>=5.1 in ./.venv/lib/python3.11/site-packages (from huggingface-hub->open_clip_torch) (6.0)\n",
|
| 85 |
+
"Requirement already satisfied: packaging>=20.9 in ./.venv/lib/python3.11/site-packages (from huggingface-hub->open_clip_torch) (23.1)\n",
|
| 86 |
+
"Requirement already satisfied: numpy in ./.venv/lib/python3.11/site-packages (from torchvision->open_clip_torch) (1.24.2)\n",
|
| 87 |
+
"Requirement already satisfied: pillow!=8.3.*,>=5.3.0 in ./.venv/lib/python3.11/site-packages (from torchvision->open_clip_torch) (9.5.0)\n",
|
| 88 |
+
"Requirement already satisfied: MarkupSafe>=2.0 in ./.venv/lib/python3.11/site-packages (from jinja2->torch>=1.9.0->open_clip_torch) (2.1.2)\n",
|
| 89 |
+
"Requirement already satisfied: charset-normalizer<4,>=2 in ./.venv/lib/python3.11/site-packages (from requests->huggingface-hub->open_clip_torch) (3.1.0)\n",
|
| 90 |
+
"Requirement already satisfied: idna<4,>=2.5 in ./.venv/lib/python3.11/site-packages (from requests->huggingface-hub->open_clip_torch) (3.4)\n",
|
| 91 |
+
"Requirement already satisfied: urllib3<1.27,>=1.21.1 in ./.venv/lib/python3.11/site-packages (from requests->huggingface-hub->open_clip_torch) (1.26.15)\n",
|
| 92 |
+
"Requirement already satisfied: certifi>=2017.4.17 in ./.venv/lib/python3.11/site-packages (from requests->huggingface-hub->open_clip_torch) (2022.12.7)\n",
|
| 93 |
+
"Requirement already satisfied: mpmath>=0.19 in ./.venv/lib/python3.11/site-packages (from sympy->torch>=1.9.0->open_clip_torch) (1.3.0)\n",
|
| 94 |
+
"\n",
|
| 95 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m A new release of pip available: \u001b[0m\u001b[31;49m22.3.1\u001b[0m\u001b[39;49m -> \u001b[0m\u001b[32;49m23.1\u001b[0m\n",
|
| 96 |
+
"\u001b[1m[\u001b[0m\u001b[34;49mnotice\u001b[0m\u001b[1;39;49m]\u001b[0m\u001b[39;49m To update, run: \u001b[0m\u001b[32;49mpip install --upgrade pip\u001b[0m\n",
|
| 97 |
+
"Note: you may need to restart the kernel to use updated packages.\n"
|
| 98 |
+
]
|
| 99 |
+
}
|
| 100 |
+
],
|
| 101 |
"source": [
|
| 102 |
"%pip install torch\n",
|
| 103 |
+
"%pip install clip_interrogator\n",
|
| 104 |
+
"%pip install open_clip_torch"
|
| 105 |
+
]
|
| 106 |
+
},
|
| 107 |
+
{
|
| 108 |
+
"cell_type": "code",
|
| 109 |
+
"execution_count": 2,
|
| 110 |
+
"metadata": {},
|
| 111 |
+
"outputs": [],
|
| 112 |
+
"source": [
|
| 113 |
+
"import torch\n",
|
| 114 |
+
"from clip_interrogator import Interrogator, LabelTable, Config\n",
|
| 115 |
+
"import open_clip\n",
|
| 116 |
+
"import os\n",
|
| 117 |
+
"\n",
|
| 118 |
+
"def load_keywords_from_files(directory):\n",
|
| 119 |
+
" keyword_list = []\n",
|
| 120 |
+
" for filename in os.listdir(directory):\n",
|
| 121 |
+
" # Check if the file has a .txt extension\n",
|
| 122 |
+
" if filename.endswith('.txt'):\n",
|
| 123 |
+
" file_path = os.path.join(directory, filename)\n",
|
| 124 |
+
" with open(file_path, 'r') as file:\n",
|
| 125 |
+
" # Read lines and strip leading/trailing whitespace\n",
|
| 126 |
+
" keywords = [line.strip() for line in file]\n",
|
| 127 |
+
" # Add keywords to the master keyword_list\n",
|
| 128 |
+
" keyword_list.extend(keywords)\n",
|
| 129 |
+
" print(f\"Loaded {len(keyword_list)} keywords...\")\n",
|
| 130 |
+
" return keyword_list\n",
|
| 131 |
+
"\n",
|
| 132 |
+
"class CIInterrogator(Interrogator):\n",
|
| 133 |
+
" def __init__(self, config: Config):\n",
|
| 134 |
+
" self.config = config\n",
|
| 135 |
+
" self.device = config.device\n",
|
| 136 |
+
" self.dtype = torch.float16 if self.device == 'cuda' else torch.float32\n",
|
| 137 |
+
" self.caption_offloaded = True\n",
|
| 138 |
+
" self.clip_offloaded = True\n",
|
| 139 |
+
" self.load_clip_model()\n",
|
| 140 |
+
"\n",
|
| 141 |
+
" def load_clip_model(self):\n",
|
| 142 |
+
" config = self.config\n",
|
| 143 |
+
"\n",
|
| 144 |
+
" clip_model_name, clip_model_pretrained_name = config.clip_model_name.split('/', 2)\n",
|
| 145 |
+
"\n",
|
| 146 |
+
" if config.clip_model is None:\n",
|
| 147 |
+
" if not config.quiet:\n",
|
| 148 |
+
" print(f\"Loading CLIP model {config.clip_model_name}...\")\n",
|
| 149 |
+
"\n",
|
| 150 |
+
" self.clip_model, _, self.clip_preprocess = open_clip.create_model_and_transforms(\n",
|
| 151 |
+
" clip_model_name, \n",
|
| 152 |
+
" pretrained=clip_model_pretrained_name, \n",
|
| 153 |
+
" precision='fp16' if config.device == 'cuda' else 'fp32',\n",
|
| 154 |
+
" device=config.device,\n",
|
| 155 |
+
" jit=False,\n",
|
| 156 |
+
" cache_dir=config.clip_model_path\n",
|
| 157 |
+
" )\n",
|
| 158 |
+
" self.clip_model.eval()\n",
|
| 159 |
+
" else:\n",
|
| 160 |
+
" self.clip_model = config.clip_model\n",
|
| 161 |
+
" self.clip_preprocess = config.clip_preprocess\n",
|
| 162 |
+
" self.tokenize = open_clip.get_tokenizer(clip_model_name)\n"
|
| 163 |
]
|
| 164 |
},
|
| 165 |
{
|
|
|
|
| 179 |
},
|
| 180 |
{
|
| 181 |
"cell_type": "code",
|
| 182 |
+
"execution_count": null,
|
| 183 |
"metadata": {
|
| 184 |
"colab": {
|
| 185 |
"base_uri": "https://localhost:8080/"
|
|
|
|
| 187 |
"id": "bJ1tJyL0-8qQ",
|
| 188 |
"outputId": "1313dc44-baba-4d49-c1ee-ebf6b0c351f6"
|
| 189 |
},
|
| 190 |
+
"outputs": [],
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 191 |
"source": [
|
| 192 |
"from clip_interrogator import Config\n",
|
|
|
|
| 193 |
"import torch \n",
|
| 194 |
"\n",
|
| 195 |
"if torch.cuda.is_available():\n",
|