Datasets:
Tags:
Not-For-All-Audiences
Upload 2 files
Browse files- dino_tagger.ipynb +1 -1
- gemma4_batch_captioner.ipynb +1 -1
dino_tagger.ipynb
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"cells":[{"cell_type":"code","source":["from google.colab import drive\n","drive.mount('/content/drive')"],"metadata":{"id":"pb9bxY0OE81H"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown β
Huggingface download (fast if you have added HF_TOKEN to Colab Secrets in this instance)\n","# =============================================\n","# Cell 1: Setup - Dependencies, HF Hub Download & Tagger (GPU Optimized)\n","# =============================================\n","\n","# Install dependencies\n","!pip install -q torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118\n","!pip install -q huggingface_hub safetensors pillow\n","\n","import torch\n","from PIL import Image\n","import os\n","import zipfile\n","from pathlib import Path\n","from google.colab import files\n","from huggingface_hub import hf_hub_download\n","\n","# Check GPU availability\n","if torch.cuda.is_available():\n"," print(f\"β
GPU detected: {torch.cuda.get_device_name(0)}\")\n"," device = \"cuda\"\n","else:\n"," print(\"β οΈ No GPU found. Running on CPU (will be slower).\")\n"," device = \"cpu\"\n","\n","print(\"Downloading model files via Hugging Face Hub (faster & resumable)...\")\n","\n","# Download using hf_hub_download (recommended way)\n","hf_hub_download(\n"," repo_id=\"lodestones/tagger-experiment\",\n"," filename=\"tagger_proto.safetensors\",\n"," local_dir=\"/content\",\n"," local_dir_use_symlinks=False\n",")\n","\n","hf_hub_download(\n"," repo_id=\"lodestones/tagger-experiment\",\n"," filename=\"tagger_vocab_with_categories.json\",\n"," local_dir=\"/content\",\n"," local_dir_use_symlinks=False\n",")\n","\n","hf_hub_download(\n"," repo_id=\"lodestones/tagger-experiment\",\n"," filename=\"inference_tagger_standalone.py\",\n"," local_dir=\"/content\",\n"," local_dir_use_symlinks=False\n",")\n","\n","print(\"β
Model files downloaded successfully via Hugging Face Hub.\")\n","\n","# Load the Tagger\n","from inference_tagger_standalone import Tagger\n","\n","tagger = Tagger(\n"," checkpoint_path=\"/content/tagger_proto.safetensors\",\n"," vocab_path=\"/content/tagger_vocab_with_categories.json\",\n"," device=device,\n"," dtype=torch.bfloat16 if device == \"cuda\" else torch.float32,\n"," max_size=1024\n",")\n","\n","print(f\"β
Tagger loaded successfully on {tagger.device}\")"],"metadata":{"id":"880QrkhOZLM3","cellView":"form"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# =============================================\n","# Cell 2: Process Images from Upload or ZIP + Custom Output\n","# =============================================\n","\n","# @title βοΈ Run the tagger (Manual upload)\n","\n","# === Input Mode ===\n","use_manual_upload = True #param {type:\"boolean\"}\n","zip_file_path = \"\" # param {type:\"string\"}\n","\n","# === Tagging Settings ===\n","threshold_percent = 90 # param {type:\"slider\", min:1, max:95, step:1}\n","max_tags = 500 # param {type:\"slider\", min:5, max:150, step:5}\n","use_max_tags = False # param {type:\"boolean\"}\n","add_commas = True # param {type:\"boolean\"}\n","\n","# === Output Settings ===\n","output_zip_path = \"/content/dino_tagger_output.zip\" # @param {type:\"string\"}\n","auto_download = True #param {type:\"boolean\"}\n","\n","print(f\"Input β Manual Upload: {use_manual_upload} | ZIP: '{zip_file_path}'\")\n","print(f\"Output β Save to: '{output_zip_path}' | Auto-download: {auto_download}\")\n","print(f\"Tagging β Threshold: {threshold_percent}% | Max tags: {max_tags} | Force max: {use_max_tags} | Commas: {add_commas}\\n\")\n","\n","# ------------------- Prepare images -------------------\n","output_dir = Path(\"/content/output\")\n","output_dir.mkdir(exist_ok=True)\n","\n","image_files = []\n","\n","if use_manual_upload:\n"," print(\"Please upload your images (multiple files supported)...\")\n"," uploaded = files.upload()\n"," image_files = [f for f in uploaded.keys()\n"," if f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp', '.bmp'))]\n"," print(f\"β
Uploaded {len(image_files)} image(s) manually.\")\n","\n","else:\n"," if not zip_file_path or not Path(zip_file_path).exists():\n"," print(f\"β ZIP file not found at: {zip_file_path}\")\n"," else:\n"," print(f\"Extracting images from ZIP: {zip_file_path}\")\n"," extract_dir = Path(\"/content/extracted_images\")\n"," extract_dir.mkdir(exist_ok=True)\n","\n"," with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n"," zip_ref.extractall(extract_dir)\n","\n"," for ext in ['.png', '.jpg', '.jpeg', '.webp', '.bmp']:\n"," image_files.extend(list(extract_dir.rglob(f\"*{ext}\")))\n"," image_files.extend(list(extract_dir.rglob(f\"*{ext.upper()}\")))\n","\n"," image_files = sorted(set(str(p) for p in image_files))\n"," print(f\"β
Found {len(image_files)} image(s) in the ZIP file.\")\n","\n","# ------------------- Process images -------------------\n","if not image_files:\n"," print(\"β No images found to process.\")\n","else:\n"," print(f\"\\nStarting tagging for {len(image_files)} image(s) on {tagger.device}...\\n\")\n","\n"," threshold = threshold_percent / 100.0\n","\n"," for i, img_path in enumerate(image_files, start=1):\n"," img_name = Path(img_path).name\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_name}\")\n","\n"," # Use GPU-accelerated prediction\n"," if use_max_tags:\n"," tags_list = tagger.predict(img_path, topk=max_tags, threshold=None)\n"," else:\n"," tags_list = tagger.predict(img_path, topk=None, threshold=threshold)\n"," if len(tags_list) > max_tags:\n"," tags_list = tags_list[:max_tags]\n","\n"," # Save image as numbered JPG\n"," img = Image.open(img_path).convert(\"RGB\")\n"," new_img_path = output_dir / f\"{i}.jpg\"\n"," img.save(new_img_path, \"JPEG\", quality=95)\n","\n"," # Prepare single-line tag string\n"," tags_only = [tag for tag, prob in tags_list]\n"," tag_text = \" , \".join(tags_only) if add_commas else \" \".join(tags_only)\n","\n"," # Save tags\n"," tag_file = output_dir / f\"{i}.txt\"\n"," with open(tag_file, \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved {i}.jpg + {i}.txt ({len(tags_list)} tags)\")\n","\n"," # Create output zip at custom path\n"," final_zip_path = Path(output_zip_path)\n"," final_zip_path.parent.mkdir(parents=True, exist_ok=True)\n","\n"," with zipfile.ZipFile(final_zip_path, \"w\") as zipf:\n"," for file in sorted(output_dir.iterdir()):\n"," zipf.write(file, file.name)\n","\n"," print(f\"\\nβ
Processing complete! Output ZIP saved to: {final_zip_path}\")\n","\n"," # Auto-download if enabled\n"," if auto_download:\n"," files.download(str(final_zip_path))\n"," print(\"π₯ Auto-download triggered.\")\n"," else:\n"," print(\"β
Auto-download disabled. You can manually download the file from the path above.\")"],"metadata":{"id":"aR-oik8TTpNU","cellView":"form"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# =============================================\n","# Cell 3: Unload previous tagger + Run WD-VIT Tagger + Prepend Tags\n","# =============================================\n","\n","#@markdown π§ WD-VIT Tagger v3 - Prepend Tags to Existing Files\n","\n","import torch\n","import gc\n","import os\n","from pathlib import Path\n","import zipfile\n","from google.colab import files\n","import timm\n","import torchvision.transforms as transforms\n","from PIL import Image\n","import pandas as pd\n","import requests\n","from io import StringIO\n","\n","print(\"π§Ή Unloading previous tagger from VRAM...\")\n","\n","# Unload previous model to free VRAM\n","if 'tagger' in globals():\n"," del tagger\n"," print(\" β Deleted tagger object\")\n","\n","if torch.cuda.is_available():\n"," torch.cuda.empty_cache()\n"," torch.cuda.synchronize()\n"," gc.collect()\n"," print(f\" β VRAM cleared. Current memory: {torch.cuda.memory_allocated()/1024**2:.1f} MB\")\n","\n","print(\"\\nπ₯ Loading WD-VIT Tagger v3...\")\n","\n","# ==================== Setup WD Tagger ====================\n","\n","# Download tags\n","tags_url = \"https://huggingface.co/SmilingWolf/wd-vit-tagger-v3/resolve/main/selected_tags.csv\"\n","response = requests.get(tags_url)\n","if response.status_code != 200:\n"," raise Exception(f\"Failed to download selected_tags.csv: {response.status_code}\")\n","\n","tags_df = pd.read_csv(StringIO(response.text))\n","tags_list = tags_df['name'].tolist()\n","print(f\"β
Loaded {len(tags_list)} tags from selected_tags.csv\")\n","\n","# Load model\n","model = timm.create_model(\"hf_hub:SmilingWolf/wd-vit-tagger-v3\", pretrained=True)\n","model.eval()\n","device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n","model = model.to(device)\n","print(f\"β
WD-VIT Tagger loaded on {device}\")\n","\n","# Preprocessing\n","preprocess = transforms.Compose([\n"," transforms.Resize((448, 448)),\n"," transforms.ToTensor(),\n"," transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n","])\n","\n","# ==================== Settings ====================\n","\n","threshold = 0.7 # @param {type:\"slider\", min:0.1, max:0.95, step:0.01}\n","add_commas = True # @param {type:\"boolean\"}\n","output_zip_path = \"/content/dino_wd_combined_output.zip\" # @param {type:\"string\"}\n","auto_download = True # @param {type:\"boolean\"}\n","\n","print(f\"\\nSettings β WD Threshold: {threshold} | Add commas: {add_commas}\")\n","print(f\"Output β {output_zip_path} | Auto-download: {auto_download}\\n\")\n","\n","# ==================== Process Images ====================\n","\n","input_dir = Path(\"/content/output\") # From Cell 2\n","output_dir = Path(\"/content/wd_combined_output\")\n","output_dir.mkdir(exist_ok=True)\n","\n","image_files = sorted(list(input_dir.glob(\"*.jpg\")))\n","\n","if not image_files:\n"," print(\"β No images found in /content/output/\")\n","else:\n"," print(f\"Starting WD tagging for {len(image_files)} images...\\n\")\n","\n"," for i, img_path in enumerate(image_files, start=1):\n"," img_name = img_path.name\n"," txt_path = input_dir / f\"{i}.txt\" # Original tags from Cell 2\n","\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_name}\")\n","\n"," # Load and preprocess image\n"," image = Image.open(img_path).convert(\"RGB\")\n"," input_tensor = preprocess(image).unsqueeze(0).to(device)\n","\n"," # Inference\n"," with torch.no_grad():\n"," logits = model(input_tensor)\n"," probs = torch.sigmoid(logits).cpu().numpy()[0]\n","\n"," # Get WD tags above threshold\n"," wd_tags = [tags_list[j] for j, prob in enumerate(probs) if prob > threshold]\n","\n"," # Read existing tags from Cell 2\n"," if txt_path.exists():\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," existing_text = f.read().strip()\n"," existing_tags = [t.strip() for t in existing_text.replace(\",\", \" \").split() if t.strip()]\n"," else:\n"," existing_tags = []\n","\n"," # Combine: WD tags first, then existing tags\n"," combined_tags = wd_tags + existing_tags\n","\n"," # Remove possible duplicates while preserving order\n"," seen = set()\n"," final_tags = []\n"," for tag in combined_tags:\n"," if tag not in seen:\n"," seen.add(tag)\n"," final_tags.append(tag)\n","\n"," # Create tag string\n"," tag_text = \" , \".join(final_tags) if add_commas else \" \".join(final_tags)\n","\n"," # Save updated files\n"," new_img_path = output_dir / f\"{i}.jpg\"\n"," new_txt_path = output_dir / f\"{i}.txt\"\n","\n"," image.save(new_img_path, \"JPEG\", quality=95)\n","\n"," with open(new_txt_path, \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved {i}.jpg + {i}.txt (WD: {len(wd_tags)} | Total: {len(final_tags)})\")\n","\n"," # ==================== Create Final ZIP ====================\n","\n"," final_zip = Path(output_zip_path)\n"," final_zip.parent.mkdir(parents=True, exist_ok=True)\n","\n"," with zipfile.ZipFile(final_zip, \"w\") as zipf:\n"," for file in sorted(output_dir.iterdir()):\n"," zipf.write(file, file.name)\n","\n"," print(f\"\\nβ
All done! Combined output saved to: {final_zip}\")\n","\n"," if auto_download:\n"," files.download(str(final_zip))\n"," print(\"π₯ Auto-download triggered.\")\n"," else:\n"," print(\"β
Auto-download disabled.\")"],"metadata":{"cellView":"form","id":"58THbptXvXIZ"},"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"cellView":"form","id":"d4c348b4"},"source":["# =============================================\n","# Cell 2: Process Images from Upload or ZIP + Custom Output\n","# =============================================\n","\n","# @title π¦ βοΈ Run the tagger using .zip file in Drive + custom options\n","\n","# === Input Mode ===\n","use_manual_upload = False #param {type:\"boolean\"}\n","zip_file_path = \"/content/drive/MyDrive/fetch.zip\" # @param {type:\"string\"}\n","\n","# === Tagging Settings ===\n","threshold_percent = 65 # @param {type:\"slider\", min:1, max:95, step:1}\n","max_tags = 300 # @param {type:\"slider\", min:5, max:500, step:5}\n","use_max_tags = False # @param {type:\"boolean\"}\n","add_commas = True # @param {type:\"boolean\"}\n","\n","# === Output Settings ===\n","output_zip_path = \"/content/drive/MyDrive/dino_tagger_output.zip\" # @param {type:\"string\"}\n","auto_download = False #param {type:\"boolean\"}\n","\n","print(f\"Input β Manual Upload: {use_manual_upload} | ZIP: '{zip_file_path}'\")\n","print(f\"Output β Save to: '{output_zip_path}' | Auto-download: {auto_download}\")\n","print(f\"Tagging β Threshold: {threshold_percent}% | Max tags: {max_tags} | Force max: {use_max_tags} | Commas: {add_commas}\\n\")\n","\n","# ------------------- Prepare images -------------------\n","output_dir = Path(\"/content/output\")\n","output_dir.mkdir(exist_ok=True)\n","\n","image_files = []\n","\n","if use_manual_upload:\n"," print(\"Please upload your images (multiple files supported)...\")\n"," uploaded = files.upload()\n"," image_files = [f for f in uploaded.keys()\n"," if f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp', '.bmp'))]\n"," print(f\"β
Uploaded {len(image_files)} image(s) manually.\")\n","\n","else:\n"," if not zip_file_path or not Path(zip_file_path).exists():\n"," print(f\"β ZIP file not found at: {zip_file_path}\")\n"," else:\n"," print(f\"Extracting images from ZIP: {zip_file_path}\")\n"," extract_dir = Path(\"/content/extracted_images\")\n"," extract_dir.mkdir(exist_ok=True)\n","\n"," with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n"," zip_ref.extractall(extract_dir)\n","\n"," temp_image_files = []\n"," for ext in ['.png', '.jpg', '.jpeg', '.webp', '.bmp']:\n"," temp_image_files.extend(list(extract_dir.rglob(f\"*{ext}\")))\n"," temp_image_files.extend(list(extract_dir.rglob(f\"*{ext.upper()}\")))\n","\n"," # Filter out files from __MACOSX directories that cause issues\n"," image_files = sorted(set(str(p) for p in temp_image_files if \"__MACOSX\" not in str(p)))\n"," print(f\"β
Found {len(image_files)} image(s) in the ZIP file (excluding macOS metadata files).\")\n","\n","# ------------------- Process images -------------------\n","if not image_files:\n"," print(\"β No images found to process.\")\n","else:\n"," print(f\"\\nStarting tagging for {len(image_files)} image(s) on {tagger.device}...\\n\")\n","\n"," threshold = threshold_percent / 100.0\n","\n"," for i, img_path in enumerate(image_files, start=1):\n"," img_name = Path(img_path).name\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_name}\")\n","\n"," # Use GPU-accelerated prediction\n"," if use_max_tags:\n"," tags_list = tagger.predict(img_path, topk=max_tags, threshold=None)\n"," else:\n"," tags_list = tagger.predict(img_path, topk=None, threshold=threshold)\n"," if len(tags_list) > max_tags:\n"," tags_list = tags_list[:max_tags]\n","\n"," # Save image as numbered JPG\n"," img = Image.open(img_path).convert(\"RGB\")\n"," new_img_path = output_dir / f\"{i}.jpg\"\n"," img.save(new_img_path, \"JPEG\", quality=95)\n","\n"," # Prepare single-line tag string\n"," tags_only = [tag for tag, prob in tags_list]\n"," tag_text = \" , \".join(tags_only) if add_commas else \" \".join(tags_only)\n","\n"," # Save tags\n"," tag_file = output_dir / f\"{i}.txt\"\n"," with open(tag_file, \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved {i}.jpg + {i}.txt ({len(tags_list)} tags)\")\n","\n"," # Create output zip at custom path\n"," final_zip_path = Path(output_zip_path)\n"," final_zip_path.parent.mkdir(parents=True, exist_ok=True)\n","\n"," with zipfile.ZipFile(final_zip_path, \"w\") as zipf:\n"," for file in sorted(output_dir.iterdir()):\n"," zipf.write(file, file.name)\n","\n"," print(f\"\\nβ
Processing complete! Output ZIP saved to: {final_zip_path}\")\n","\n"," # Auto-download if enabled\n"," if auto_download:\n"," files.download(str(final_zip_path))\n"," print(\"π₯ Auto-download triggered.\")\n"," else:\n"," print(\"β
Auto-download disabled. You can manually download the file from the path above.\")"],"execution_count":null,"outputs":[]},{"cell_type":"code","source":["# =============================================\n","# Cell 3: Disconnect Runtime After Processing (Optional)\n","# =============================================\n","\n","# @title Disconnect from Session After Completion\n","\n","disconnect_after_run = True # @param {type:\"boolean\"}\n","\n","if disconnect_after_run:\n"," print(\"π Disconnecting Colab runtime in 10 seconds...\")\n"," print(\"This will stop the current session and free up GPU resources.\")\n","\n"," import time\n"," time.sleep(10)\n","\n"," from google.colab import runtime\n"," runtime.unassign()\n","\n"," print(\"β
Runtime disconnected.\")\n","else:\n"," print(\"β
Disconnect option is disabled.\")\n"," print(\"You can manually disconnect via Runtime β Disconnect and delete runtime if needed.\")"],"metadata":{"id":"byGffTyyPgEY","cellView":"form"},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":["# Image Tagger using lodestones/tagger-experiment\n","\n","This Colab notebook lets you automatically generate **tags** for a batch of images using the [lodestones/tagger-experiment](https://huggingface.co/lodestones/tagger-experiment) model.\n","\n","### Features:\n","- Process images either by **manual upload** or by providing a **ZIP file** containing your images\n","- Adjustable **probability threshold** (only tags above this % are kept when not forcing max tags)\n","- Control the **maximum number of tags** per image\n","- Option to **force the top N tags** (ignores threshold) or strictly respect the probability threshold\n","- Output format: Images saved as `1.jpg`, `2.jpg`, ... and corresponding tags as `1.txt`, `2.txt`, ...\n","- Tags in each `.txt` file are saved as a **single line** β either space-separated or comma-separated\n","- Final result is packaged into a downloadable `output.zip` file\n","\n","### How to use:\n","1. Run **Cell 1** first (installs dependencies and loads the model)\n","2. In **Cell 2**, configure your settings:\n"," - Choose between manual upload or ZIP file input\n"," - Adjust threshold, max tags, and output style using the sliders and checkboxes\n","3. Run **Cell 2** β upload images or point to your ZIP file\n","4. Wait for processing and download the resulting `output.zip`\n","\n","The notebook preserves the original image quality (saved as high-quality JPG) and organizes everything with simple numbered filenames for easy use in training pipelines."],"metadata":{"id":"q-fVFdZqMxRA"}},{"cell_type":"code","source":["#@markdown Download method using wget (backup method)\n","# =============================================\n","# Cell 1: Setup - Dependencies, Model & Tagger (GPU Optimized)\n","# =============================================\n","\n","# Install dependencies\n","!pip install -q torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118\n","!pip install -q safetensors pillow\n","\n","import torch\n","from PIL import Image\n","import os\n","import zipfile\n","from pathlib import Path\n","from google.colab import files\n","\n","# Check GPU availability\n","if torch.cuda.is_available():\n"," print(f\"β
GPU detected: {torch.cuda.get_device_name(0)}\")\n"," device = \"cuda\"\n","else:\n"," print(\"β οΈ No GPU found. Running on CPU (will be slower).\")\n"," device = \"cpu\"\n","\n","print(\"Downloading model files...\")\n","\n","!wget -q https://huggingface.co/lodestones/tagger-experiment/resolve/main/tagger_proto.safetensors -O tagger_proto.safetensors\n","!wget -q https://huggingface.co/lodestones/tagger-experiment/resolve/main/tagger_vocab_with_categories.json -O tagger_vocab_with_categories.json\n","!wget -q https://huggingface.co/lodestones/tagger-experiment/resolve/main/inference_tagger_standalone.py -O inference_tagger_standalone.py\n","\n","print(\"β
Model files downloaded.\")\n","\n","# Load the Tagger with GPU preference\n","from inference_tagger_standalone import Tagger\n","\n","tagger = Tagger(\n"," checkpoint_path=\"tagger_proto.safetensors\",\n"," vocab_path=\"tagger_vocab_with_categories.json\",\n"," device=device, # Explicit GPU if available\n"," dtype=torch.bfloat16 if device == \"cuda\" else torch.float32,\n"," max_size=1024\n",")\n","\n","print(f\"β
Tagger loaded successfully on {tagger.device}\")"],"metadata":{"id":"qEQZt_7nPffG","cellView":"form"},"execution_count":null,"outputs":[]}],"metadata":{"colab":{"provenance":[{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1775233835241},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1775233735620},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1775159289229},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1774964848181},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1774906863352},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1774901938471},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1774884683620},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1774883563998},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1774882033159},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/dino_tagger.ipynb","timestamp":1774881268156},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Drive to WebP.ipynb","timestamp":1774879795776},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Drive to WebP.ipynb","timestamp":1763646205520},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/Drive to WebP.ipynb","timestamp":1760993725927},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1760450712160},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1756712618300},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1747490904984},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1740037333374},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1736477078136},{"file_id":"https://huggingface.co/codeShare/JupyterNotebooks/blob/main/YT-playlist-to-mp3.ipynb","timestamp":1725365086834}],"gpuType":"T4"},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"},"accelerator":"GPU"},"nbformat":4,"nbformat_minor":0}
|
|
|
|
| 1 |
+
{"cells":[{"cell_type":"code","source":["#@markdown # Cell 0: Mount Google Drive & Prepare HF_TOKEN for faster HF downloads (UPDATED)\n","\n","from google.colab import drive\n","from google.colab import userdata\n","import os\n","\n","# Mount Google Drive (still used for output ZIPs/results only)\n","print(\"Mounting Google Drive...\")\n","drive.mount('/content/drive', force_remount=True)\n","\n","# Create a working directory on Drive (for final outputs/ZIPs only)\n","WORKING_DIR = \"/content/drive/MyDrive/DinoTaggerPipeline\"\n","os.makedirs(WORKING_DIR, exist_ok=True)\n","print(f\"β
Working directory set to: {WORKING_DIR} (outputs only)\")\n","\n","# === NEW: Local model directory on /content/ (CPU storage only) ===\n","# Checkpoints will ONLY go here. Nothing is saved to Drive.\n","MODEL_DIR = \"/content/models\"\n","os.makedirs(MODEL_DIR, exist_ok=True)\n","print(f\"β
Model directory set to: {MODEL_DIR} (local /content/ storage only)\")\n","print(\" β Checkpoint models (safetensors, vocab, etc.) will be saved here only.\")\n","\n","# HF_TOKEN from Colab Secrets\n","hf_token = userdata.get('HF_TOKEN')\n","if hf_token:\n"," os.environ[\"HF_TOKEN\"] = hf_token\n"," print(\"β
HF_TOKEN loaded from Colab Secrets and set as environment variable.\")\n"," print(\" β This enables authenticated + faster/resumable Hugging Face downloads.\")\n","else:\n"," print(\"β οΈ HF_TOKEN not found in Colab Secrets.\")\n"," print(\" β Add it via the key icon (left sidebar) β Secrets β Name: HF_TOKEN\")\n"," print(\" β Some downloads may be slower or fail if the repo requires login.\")\n","\n","print(\"\\nβ
Cell 0 complete. Models will now save to local /content/models only.\")"],"metadata":{"cellView":"form","id":"rtukY485TQeT"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 1: Download All Models to Disk (CPU-only, No VRAM Load) (UPDATED)\n","\n","import torch\n","from huggingface_hub import hf_hub_download, snapshot_download\n","from pathlib import Path\n","import os\n","\n","print(\"Downloading models to local /content/models (CPU-only, resumable)...\")\n","\n","# 1. DINO Tagger (lodestones/tagger-experiment) β saved to MODEL_DIR only\n","print(\"\\n1. Downloading DINO Tagger files...\")\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"tagger_proto.safetensors\", local_dir=MODEL_DIR, local_dir_use_symlinks=False)\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"tagger_vocab_with_categories.json\", local_dir=MODEL_DIR, local_dir_use_symlinks=False)\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"inference_tagger_standalone.py\", local_dir=MODEL_DIR, local_dir_use_symlinks=False)\n","\n","# 2. WD-VIT Tagger (selected_tags.csv) β saved to MODEL_DIR only\n","print(\"\\n2. Downloading WD-VIT Tagger assets...\")\n","tags_path = Path(MODEL_DIR) / \"selected_tags.csv\"\n","if not tags_path.exists():\n"," import requests\n"," from io import StringIO\n"," import pandas as pd\n"," response = requests.get(\"https://huggingface.co/SmilingWolf/wd-vit-tagger-v3/resolve/main/selected_tags.csv\")\n"," tags_df = pd.read_csv(StringIO(response.text))\n"," tags_df.to_csv(tags_path, index=False)\n"," print(f\" β Saved {len(tags_df)} tags to selected_tags.csv in {MODEL_DIR}\")\n","\n","# 3. Gemma-4-E2B-Heretic (processor + model files will be cached by transformers on local storage)\n","print(\"\\n3. Gemma-4-E2B-Heretic will be cached on first load in Cell 7 (large model - stored locally).\")\n","\n","print(f\"\\nβ
All lightweight models downloaded/cached to: {MODEL_DIR} (local /content/ only)\")\n","print(\" β No checkpoint models were saved to Google Drive.\")\n","print(\" Heavy models (Gemma) will download during loading in Cell 7.\")"],"metadata":{"cellView":"form","id":"a5CohOa3TTcy"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 2: Extract Images from ZIP (unchanged - no model changes)\n","\n","from pathlib import Path\n","import zipfile\n","\n","# === Settings ===\n","zip_file_path = \"/content/drive/MyDrive/Archive.zip\" #@param {type:\"string\"}\n","# Full path on Drive, e.g. /content/drive/MyDrive/my_images.zip\n","\n","extract_dir = Path(\"/content/extracted_images\")\n","extract_dir.mkdir(exist_ok=True)\n","\n","if not zip_file_path or not Path(zip_file_path).exists():\n"," print(f\"β ZIP file not found at: {zip_file_path}\")\n"," print(\" β Please provide a valid path to your image ZIP.\")\n","else:\n"," print(f\"Extracting images from: {zip_file_path}\")\n"," with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n"," # Filter out macOS specific files (e.g., __MACOSX directory, .DS_Store)\n"," members = [m for m in zip_ref.namelist() if not m.startswith('__MACOSX/') and not m.endswith('/.DS_Store')]\n"," for member in members:\n"," zip_ref.extract(member, extract_dir)\n"," print(\" β Ignored __MACOSX/ and .DS_Store files during extraction.\")\n","\n"," image_files = []\n"," for ext in ['.png', '.jpg', '.jpeg', '.webp', '.avif','.bmp']:\n"," image_files.extend(list(extract_dir.rglob(f\"*{ext}\")))\n"," image_files.extend(list(extract_dir.rglob(f\"*{ext.upper()}\")))\n","\n"," image_files = sorted(set(str(p) for p in image_files))\n"," print(f\"β
Extracted and found {len(image_files)} image(s).\")\n"," print(\" Images are ready in /content/extracted_images/\")"],"metadata":{"cellView":"form","id":"wBm5MDYETXMa"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 3: Clear VRAM + Load WD-VIT Tagger v3 (UPDATED)\n","\n","import torch\n","import gc\n","import timm\n","import torchvision.transforms as transforms\n","import pandas as pd\n","from pathlib import Path\n","\n","# Clear VRAM\n","print(\"π§Ή Clearing VRAM...\")\n","if 'model' in globals(): del model\n","if 'tagger' in globals(): del tagger\n","torch.cuda.empty_cache()\n","gc.collect()\n","print(\" β VRAM cleared.\")\n","\n","# Load WD-VIT Tagger\n","print(\"\\nπ₯ Loading WD-VIT Tagger v3...\")\n","# UPDATED: Use MODEL_DIR (local only)\n","tags_path = Path(MODEL_DIR) / \"selected_tags.csv\"\n","tags_df = pd.read_csv(tags_path)\n","tags_list = tags_df['name'].tolist()\n","print(f\" β Loaded {len(tags_list)} tags.\")\n","\n","model = timm.create_model(\"hf_hub:SmilingWolf/wd-vit-tagger-v3\", pretrained=True)\n","model.eval()\n","device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n","model = model.to(device)\n","\n","preprocess = transforms.Compose([\n"," transforms.Resize((448, 448)),\n"," transforms.ToTensor(),\n"," transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n","])\n","\n","print(f\"β
WD-VIT Tagger loaded on {device}\")"],"metadata":{"cellView":"form","id":"gRW5abcETaXd"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 4: Tag Images with WD-VIT Tagger (FIXED) (unchanged)\n","\n","from PIL import Image\n","from pathlib import Path\n","\n","threshold = 0.7 #@param {type:\"slider\", min:0.1, max:0.95, step:0.01}\n","add_commas = True #@param {type:\"boolean\"}\n","\n","input_dir = Path(\"/content/extracted_images\")\n","output_dir = Path(\"/content/wd_tags\")\n","output_dir.mkdir(exist_ok=True)\n","\n","# FIXED: Clean glob pattern (was broken with Chinese text garbage)\n","image_files = sorted(list(input_dir.glob(\"*.*\")))\n","image_files = [f for f in image_files if f.suffix.lower() in {'.png','.jpg','.jpeg','.webp','.bmp','.avif'}]\n","\n","print(f\"Starting WD tagging for {len(image_files)} images...\")\n","\n","for i, img_path in enumerate(image_files, start=1):\n"," img_name = img_path.name\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_name}\")\n","\n"," # Load image once\n"," image = Image.open(img_path).convert(\"RGB\")\n","\n"," # Prepare for model\n"," input_tensor = preprocess(image).unsqueeze(0).to(device)\n","\n"," with torch.no_grad():\n"," logits = model(input_tensor)\n"," probs = torch.sigmoid(logits).cpu().numpy()[0]\n","\n"," wd_tags = [tags_list[j] for j, prob in enumerate(probs) if prob > threshold]\n","\n"," tag_text = \" , \".join(wd_tags) if add_commas else \" \".join(wd_tags)\n","\n"," base_name = f\"{i:04d}\" # numbered for consistency\n","\n"," # FIXED: Reuse already-loaded image instead of opening twice\n"," image.save(output_dir / f\"{base_name}.jpg\", \"JPEG\", quality=95)\n","\n"," with open(output_dir / f\"{base_name}.txt\", \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved {base_name}.jpg + {base_name}.txt ({len(wd_tags)} WD tags)\")\n","\n","print(\"\\nβ
WD tagging complete. Tags saved in /content/wd_tags/\")"],"metadata":{"cellView":"form","id":"IvF3aMsSTkpG"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 5: Clear VRAM + Load DINO Tagger (UPDATED)\n","\n","import torch\n","import gc\n","import sys\n","from pathlib import Path\n","\n","# UPDATED: Add MODEL_DIR to sys.path so Python can find the module (inference_tagger_standalone.py is now in local /content/models)\n","if str(MODEL_DIR) not in sys.path:\n"," sys.path.insert(0, str(MODEL_DIR))\n","\n","from inference_tagger_standalone import Tagger # from Cell 1 download (now in MODEL_DIR)\n","\n","# Clear previous model\n","print(\"π§Ή Clearing VRAM...\")\n","if 'model' in globals(): del model\n","torch.cuda.empty_cache()\n","gc.collect()\n","\n","# Load DINO Tagger\n","print(\"\\nπ₯ Loading DINO Tagger...\")\n","# UPDATED: Load checkpoint & vocab from local MODEL_DIR only\n","tagger = Tagger(\n"," checkpoint_path=str(Path(MODEL_DIR) / \"tagger_proto.safetensors\"),\n"," vocab_path=str(Path(MODEL_DIR) / \"tagger_vocab_with_categories.json\"),\n"," device=\"cuda\" if torch.cuda.is_available() else \"cpu\",\n"," dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,\n"," max_size=1024\n",")\n","\n","print(f\"β
DINO Tagger loaded on {tagger.device} (checkpoint from local /content/models)\")"],"metadata":{"cellView":"form","id":"7AAhPUI5TnAh"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 6.a: Tag with DINO Tagger + Append to .txt Files (unchanged)\n","\n","threshold_percent = 83 #@param {type:\"slider\", min:1, max:95, step:1}\n","max_tags = 100 #@param {type:\"slider\", min:5, max:150, step:5}\n","use_max_tags = False #@param {type:\"boolean\"}\n","\n","wd_dir = Path(\"/content/wd_tags\")\n","dino_dir = Path(\"/content/dino_combined\")\n","dino_dir.mkdir(exist_ok=True)\n","\n","image_files = sorted(list(wd_dir.glob(\"*.jpg\")))\n","\n","threshold = threshold_percent / 100.0\n","\n","print(f\"Starting DINO tagging for {len(image_files)} images...\")\n","\n","for i, img_path in enumerate(image_files, start=1):\n"," base_name = img_path.stem\n"," txt_path = wd_dir / f\"{base_name}.txt\"\n","\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_path.name}\")\n","\n"," if use_max_tags:\n"," tags_list = tagger.predict(str(img_path), topk=max_tags, threshold=None)\n"," else:\n"," tags_list = tagger.predict(str(img_path), topk=None, threshold=threshold)\n"," if len(tags_list) > max_tags:\n"," tags_list = tags_list[:max_tags]\n","\n"," dino_tags = [tag for tag, prob in tags_list]\n","\n"," # Read WD tags\n"," if txt_path.exists():\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," existing_text = f.read().strip()\n"," existing_tags = [t.strip() for t in existing_text.replace(\",\", \" \").split() if t.strip()]\n"," else:\n"," existing_tags = []\n","\n"," # Combine: WD first, then DINO (or reverse if preferred)\n"," combined = existing_tags + dino_tags\n"," seen = set()\n"," final_tags = [tag for tag in combined if not (tag in seen or seen.add(tag))]\n","\n"," tag_text = \" , \".join(final_tags) if add_commas else \" \".join(final_tags) # add_commas from previous cell if defined\n","\n"," # Save combined\n"," new_img = dino_dir / f\"{base_name}.jpg\"\n"," new_txt = dino_dir / f\"{base_name}.txt\"\n","\n"," Image.open(img_path).convert(\"RGB\").save(new_img, \"JPEG\", quality=95)\n"," with open(new_txt, \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved combined tags ({len(final_tags)} total)\")\n","\n","print(\"\\nβ
DINO tags appended. Combined tags in /content/dino_combined/\")"],"metadata":{"cellView":"form","id":"T_0lkZ95Tqzv"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 6.b: Save DINO-Tagged Results to Google Drive (Safe to Disconnect) (unchanged)\n","\n","from pathlib import Path\n","import zipfile\n","import shutil\n","\n","print(\"πΎ Saving all DINO-tagged results (images + combined tags) to Google Drive as a ZIP...\")\n","\n","dino_dir = Path(\"/content/dino_combined\")\n","if not dino_dir.exists() or not list(dino_dir.glob(\"*.jpg\")):\n"," print(\"β No /content/dino_combined folder found. Please run Cell 6 first.\")\n","else:\n"," # Create a ZIP for easy download / archive directly from dino_dir\n"," zip_path = Path(WORKING_DIR) / \"dino_tagged_results.zip\" # Changed zip name for clarity\n"," num_files_to_zip = len(list(dino_dir.iterdir()))\n"," print(f\" Zipping {num_files_to_zip} image+tag pairs...\")\n","\n"," with zipfile.ZipFile(zip_path, \"w\") as zipf:\n"," for file in dino_dir.iterdir():\n"," zipf.write(file, file.name) # Write directly from dino_dir, preserving original file names in zip\n","\n"," print(f\"\\nβ
ZIP creation complete!\")\n"," print(f\" β ZIP archive saved to: {zip_path}\")\n"," print(\"\\nYou can now safely disconnect / restart the runtime.\")\n"," print(\" When you come back, run Cell 6.c to restore everything.\")"],"metadata":{"cellView":"form","id":"DchQX0ugTu0b"},"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"38fc3364","cellView":"form"},"source":["#@markdown # Cell 6.c Save all prompts to combined txt file {item1|item2|...} (optional)\n","from google.colab import files\n","import zipfile\n","import os\n","from pathlib import Path\n","import shutil # Import shutil for copying files\n","\n","# --- Configuration for Drive path ---\n","# The path to the ZIP file on Google Drive\n","zip_file_on_drive_path = Path(WORKING_DIR) / \"dino_tagged_results.zip\" # Changed to process the zip from Cell 6.b\n","# The directory on Google Drive where the output .txt file should be saved\n","drive_target_output_dir = Path(zip_file_on_drive_path).parent # Get the parent directory\n","drive_target_output_dir.mkdir(parents=True, exist_ok=True) # Ensure the directory exists\n","\n","print(f\"π Processing ZIP from Drive: {zip_file_on_drive_path}\")\n","\n","# Local temporary directory for extraction\n","extract_dir = \"/content/extracted_txt_files_drive_processing\"\n","\n","# --- Added: Clear the directory if it exists ---\n","if os.path.exists(extract_dir):\n"," shutil.rmtree(extract_dir)\n","# ---\n","os.makedirs(extract_dir, exist_ok=True)\n","\n","# Extract the ZIP file from Drive to the temporary folder\n","try:\n"," with zipfile.ZipFile(zip_file_on_drive_path, 'r') as zip_ref:\n"," zip_ref.extractall(extract_dir)\n"," print(f\"β
ZIP extracted from Drive to {extract_dir}\")\n","except FileNotFoundError:\n"," print(f\"β Error: ZIP file not found at {zip_file_on_drive_path}. Please ensure it exists and Drive is mounted.\")\n"," # For simplicity, we'll let it proceed, but subsequent steps might fail if files are missing.\n"," pass\n","\n","# Get all .txt files and sort them numerically\n","txt_paths = sorted(\n"," list(Path(extract_dir).glob(\"*.txt\")),\n"," key=lambda p: f\"{int(p.stem):05d}\" if p.stem.isdigit() else p.stem.lower()\n",")\n","\n","cleaned_titles = []\n","if not txt_paths:\n"," print(\"β οΈ No .txt files found inside the ZIP after extraction! Generating empty combined file.\")\n"," combined_content = '{}'\n","else:\n"," print(f\"π Found {len(txt_paths)} title files. Processing...\")\n"," # Read, clean, and combine titles\n"," for txt_path in txt_paths:\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," title = f.read().strip()\n","\n"," # Remove the forbidden characters: ^ { } |\n"," cleaned = (title\n"," .replace('^', '')\n"," .replace('{', '')\n"," .replace('}', '')\n"," .replace('|', '')\n"," .replace('\"','')\n"," .replace('>', '')\n"," .replace('<','')) # Fixed syntax error here\n","\n"," cleaned_titles.append(cleaned)\n","\n"," # Build the exact format you want: {text1|text2|text3|...}\n"," combined_content = '{' + '|'.join(cleaned_titles) + '}'\n","\n","# --- Save to local content directory (for immediate download) ---\n","local_output_file_name = \"combined_titles_from_drive.txt\"\n","local_output_file_path = f\"/content/{local_output_file_name}\"\n","with open(local_output_file_path, \"w\", encoding=\"utf-8\") as f:\n"," f.write(combined_content)\n","\n","print(f\"\\nβ
Done! Combined {len(cleaned_titles)} titles into one line.\")\n","print(f\"π Local file saved as: {local_output_file_path}\")\n","print(f\" (First 100 characters: {combined_content[:100]}...)\")\n","\n","# --- Save to Google Drive in the same directory as the original ZIP ---\n","drive_final_output_path = drive_target_output_dir / local_output_file_name\n","shutil.copy(local_output_file_path, str(drive_final_output_path))\n","print(f\"βοΈ Also saved to Google Drive: {drive_final_output_path}\")\n","\n","# Auto-download the combined file from the local path\n","#files.download(local_output_file_path)\n","\n","#print(\"π₯ Download started for the local file. The file is also saved to Google Drive.\")"],"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FQF71-mvmlc1"},"outputs":[],"source":["# ================================================\n","# Auto Disconnect Colab Session\n","# ================================================\n","\n","print(\"π Disconnecting Colab session in 15 seconds...\")\n","import time\n","time.sleep(3)\n","\n","from google.colab import runtime\n","runtime.unassign()\n","\n","print(\"Session disconnected.\")"]}],"metadata":{"accelerator":"GPU","colab":{"gpuType":"T4","provenance":[{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/gemma4_batch_captioner.ipynb","timestamp":1775991040063},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/gemma4_batch_captioner.ipynb","timestamp":1775586753307},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/gemma4_batch_captioner.ipynb","timestamp":1775585424465}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
|
gemma4_batch_captioner.ipynb
CHANGED
|
@@ -1 +1 @@
|
|
| 1 |
-
{"cells":[{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"F5mVpzevnlmz"},"outputs":[],"source":["#@markdown # Cell 0: Mount Google Drive & Prepare HF_TOKEN for faster HF downloads\n","\n","from google.colab import drive\n","from google.colab import userdata\n","import os\n","\n","# Mount Google Drive\n","print(\"Mounting Google Drive...\")\n","drive.mount('/content/drive', force_remount=True)\n","\n","# Create a working directory on Drive (change if you want a different path)\n","WORKING_DIR = \"/content/drive/MyDrive/DinoTaggerPipeline\"\n","os.makedirs(WORKING_DIR, exist_ok=True)\n","print(f\"β
Working directory set to: {WORKING_DIR}\")\n","\n","# HF_TOKEN from Colab Secrets (recommended for private/gated models and faster downloads)\n","hf_token = userdata.get('HF_TOKEN')\n","if hf_token:\n"," os.environ[\"HF_TOKEN\"] = hf_token\n"," print(\"β
HF_TOKEN loaded from Colab Secrets and set as environment variable.\")\n"," print(\" β This enables authenticated + faster/resumable Hugging Face downloads.\")\n","else:\n"," print(\"β οΈ HF_TOKEN not found in Colab Secrets.\")\n"," print(\" β Add it via the key icon (left sidebar) β Secrets β Name: HF_TOKEN\")\n"," print(\" β Some downloads may be slower or fail if the repo requires login.\")\n","\n","print(\"\\nβ
Cell 0 complete. Ready for model downloads.\")"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"bpJTAMcxnnuY"},"outputs":[],"source":["#@markdown # Cell 1: Download All Models to Disk (CPU-only, No VRAM Load)\n","\n","import torch\n","from huggingface_hub import hf_hub_download, snapshot_download\n","from pathlib import Path\n","import os\n","\n","print(\"Downloading models to disk (CPU-only, resumable)...\")\n","\n","# 1. DINO Tagger (lodestones/tagger-experiment)\n","print(\"\\n1. Downloading DINO Tagger files...\")\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"tagger_proto.safetensors\", local_dir=WORKING_DIR, local_dir_use_symlinks=False)\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"tagger_vocab_with_categories.json\", local_dir=WORKING_DIR, local_dir_use_symlinks=False)\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"inference_tagger_standalone.py\", local_dir=WORKING_DIR, local_dir_use_symlinks=False)\n","\n","# 2. WD-VIT Tagger (selected_tags.csv + model via timm/hf_hub)\n","print(\"\\n2. Downloading WD-VIT Tagger assets...\")\n","tags_path = Path(WORKING_DIR) / \"selected_tags.csv\"\n","if not tags_path.exists():\n"," import requests\n"," from io import StringIO\n"," import pandas as pd\n"," response = requests.get(\"https://huggingface.co/SmilingWolf/wd-vit-tagger-v3/resolve/main/selected_tags.csv\")\n"," tags_df = pd.read_csv(StringIO(response.text))\n"," tags_df.to_csv(tags_path, index=False)\n"," print(f\" β Saved {len(tags_df)} tags to selected_tags.csv\")\n","\n","# 3. Gemma-4-E2B-Heretic (processor + model files will be cached by transformers)\n","print(\"\\n3. Gemma-4-E2B-Heretic will be cached on first load in Cell 7 (large model).\")\n","\n","print(f\"\\nβ
All lightweight models downloaded/cached to: {WORKING_DIR}\")\n","print(\" Heavy models (Gemma) will download during loading in Cell 7.\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"Arc9kCZKns2D","cellView":"form"},"outputs":[],"source":["from pathlib import Path\n","import zipfile\n","\n","# === Settings ===\n","zip_file_path = \"/content/drive/MyDrive/Archive.zip\" #@param {type:\"string\"}\n","# Full path on Drive, e.g. /content/drive/MyDrive/my_images.zip\n","\n","extract_dir = Path(\"/content/extracted_images\")\n","extract_dir.mkdir(exist_ok=True)\n","\n","if not zip_file_path or not Path(zip_file_path).exists():\n"," print(f\"β ZIP file not found at: {zip_file_path}\")\n"," print(\" β Please provide a valid path to your image ZIP.\")\n","else:\n"," print(f\"Extracting images from: {zip_file_path}\")\n"," with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n"," # Filter out macOS specific files (e.g., __MACOSX directory, .DS_Store)\n"," members = [m for m in zip_ref.namelist() if not m.startswith('__MACOSX/') and not m.endswith('/.DS_Store')]\n"," for member in members:\n"," zip_ref.extract(member, extract_dir)\n"," print(\" β Ignored __MACOSX/ and .DS_Store files during extraction.\")\n","\n"," image_files = []\n"," for ext in ['.png', '.jpg', '.jpeg', '.webp', '.avif','.bmp']:\n"," image_files.extend(list(extract_dir.rglob(f\"*{ext}\")))\n"," image_files.extend(list(extract_dir.rglob(f\"*{ext.upper()}\")))\n","\n"," image_files = sorted(set(str(p) for p in image_files))\n"," print(f\"β
Extracted and found {len(image_files)} image(s).\")\n"," print(\" Images are ready in /content/extracted_images/\")"]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"xHzgQ_u6nyhO"},"outputs":[],"source":["#@markdown # Cell 3: Clear VRAM + Load WD-VIT Tagger v3\n","\n","import torch\n","import gc\n","import timm\n","import torchvision.transforms as transforms\n","import pandas as pd\n","from pathlib import Path\n","\n","# Clear VRAM\n","print(\"π§Ή Clearing VRAM...\")\n","if 'model' in globals(): del model\n","if 'tagger' in globals(): del tagger\n","torch.cuda.empty_cache()\n","gc.collect()\n","print(\" β VRAM cleared.\")\n","\n","# Load WD-VIT Tagger\n","print(\"\\nπ₯ Loading WD-VIT Tagger v3...\")\n","tags_path = Path(WORKING_DIR) / \"selected_tags.csv\"\n","tags_df = pd.read_csv(tags_path)\n","tags_list = tags_df['name'].tolist()\n","print(f\" β Loaded {len(tags_list)} tags.\")\n","\n","model = timm.create_model(\"hf_hub:SmilingWolf/wd-vit-tagger-v3\", pretrained=True)\n","model.eval()\n","device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n","model = model.to(device)\n","\n","preprocess = transforms.Compose([\n"," transforms.Resize((448, 448)),\n"," transforms.ToTensor(),\n"," transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n","])\n","\n","print(f\"β
WD-VIT Tagger loaded on {device}\")"]},{"cell_type":"code","source":["#@markdown # Cell 4: Tag Images with WD-VIT Tagger (FIXED)\n","\n","from PIL import Image\n","from pathlib import Path\n","\n","threshold = 0.7 #@param {type:\"slider\", min:0.1, max:0.95, step:0.01}\n","add_commas = True #@param {type:\"boolean\"}\n","\n","input_dir = Path(\"/content/extracted_images\")\n","output_dir = Path(\"/content/wd_tags\")\n","output_dir.mkdir(exist_ok=True)\n","\n","# FIXED: Clean glob pattern (was broken with Chinese text garbage)\n","image_files = sorted(list(input_dir.glob(\"*.*\")))\n","image_files = [f for f in image_files if f.suffix.lower() in {'.png','.jpg','.jpeg','.webp','.bmp','.avif'}]\n","\n","print(f\"Starting WD tagging for {len(image_files)} images...\")\n","\n","for i, img_path in enumerate(image_files, start=1):\n"," img_name = img_path.name\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_name}\")\n","\n"," # Load image once\n"," image = Image.open(img_path).convert(\"RGB\")\n","\n"," # Prepare for model\n"," input_tensor = preprocess(image).unsqueeze(0).to(device)\n","\n"," with torch.no_grad():\n"," logits = model(input_tensor)\n"," probs = torch.sigmoid(logits).cpu().numpy()[0]\n","\n"," wd_tags = [tags_list[j] for j, prob in enumerate(probs) if prob > threshold]\n","\n"," tag_text = \" , \".join(wd_tags) if add_commas else \" \".join(wd_tags)\n","\n"," base_name = f\"{i:04d}\" # numbered for consistency\n","\n"," # FIXED: Reuse already-loaded image instead of opening twice\n"," image.save(output_dir / f\"{base_name}.jpg\", \"JPEG\", quality=95)\n","\n"," with open(output_dir / f\"{base_name}.txt\", \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved {base_name}.jpg + {base_name}.txt ({len(wd_tags)} WD tags)\")\n","\n","print(\"\\nβ
WD tagging complete. Tags saved in /content/wd_tags/\")"],"metadata":{"id":"Am3L3nYCOo6P","cellView":"form"},"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"ODhu29DFoC0c","cellView":"form"},"outputs":[],"source":["#@markdown # Cell 5: Clear VRAM + Load DINO Tagger\n","\n","import torch\n","import gc\n","import sys\n","from pathlib import Path\n","\n","# Add WORKING_DIR to sys.path so Python can find the module\n","if str(WORKING_DIR) not in sys.path:\n"," sys.path.insert(0, str(WORKING_DIR))\n","\n","from inference_tagger_standalone import Tagger # from Cell 1 download\n","\n","# Clear previous model\n","print(\"π§Ή Clearing VRAM...\")\n","if 'model' in globals(): del model\n","torch.cuda.empty_cache()\n","gc.collect()\n","\n","# Load DINO Tagger\n","print(\"\\nπ₯ Loading DINO Tagger...\")\n","tagger = Tagger(\n"," checkpoint_path=str(Path(WORKING_DIR) / \"tagger_proto.safetensors\"),\n"," vocab_path=str(Path(WORKING_DIR) / \"tagger_vocab_with_categories.json\"),\n"," device=\"cuda\" if torch.cuda.is_available() else \"cpu\",\n"," dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,\n"," max_size=1024\n",")\n","\n","print(f\"β
DINO Tagger loaded on {tagger.device}\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"s4_XYkSYoIlN","cellView":"form"},"outputs":[],"source":["#@markdown # Cell 6.a: Tag with DINO Tagger + Append to .txt Files\n","\n","threshold_percent = 83 #@param {type:\"slider\", min:1, max:95, step:1}\n","max_tags = 100 #@param {type:\"slider\", min:5, max:150, step:5}\n","use_max_tags = False #@param {type:\"boolean\"}\n","\n","wd_dir = Path(\"/content/wd_tags\")\n","dino_dir = Path(\"/content/dino_combined\")\n","dino_dir.mkdir(exist_ok=True)\n","\n","image_files = sorted(list(wd_dir.glob(\"*.jpg\")))\n","\n","threshold = threshold_percent / 100.0\n","\n","print(f\"Starting DINO tagging for {len(image_files)} images...\")\n","\n","for i, img_path in enumerate(image_files, start=1):\n"," base_name = img_path.stem\n"," txt_path = wd_dir / f\"{base_name}.txt\"\n","\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_path.name}\")\n","\n"," if use_max_tags:\n"," tags_list = tagger.predict(str(img_path), topk=max_tags, threshold=None)\n"," else:\n"," tags_list = tagger.predict(str(img_path), topk=None, threshold=threshold)\n"," if len(tags_list) > max_tags:\n"," tags_list = tags_list[:max_tags]\n","\n"," dino_tags = [tag for tag, prob in tags_list]\n","\n"," # Read WD tags\n"," if txt_path.exists():\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," existing_text = f.read().strip()\n"," existing_tags = [t.strip() for t in existing_text.replace(\",\", \" \").split() if t.strip()]\n"," else:\n"," existing_tags = []\n","\n"," # Combine: WD first, then DINO (or reverse if preferred)\n"," combined = existing_tags + dino_tags\n"," seen = set()\n"," final_tags = [tag for tag in combined if not (tag in seen or seen.add(tag))]\n","\n"," tag_text = \" , \".join(final_tags) if add_commas else \" \".join(final_tags) # add_commas from previous cell if defined\n","\n"," # Save combined\n"," new_img = dino_dir / f\"{base_name}.jpg\"\n"," new_txt = dino_dir / f\"{base_name}.txt\"\n","\n"," Image.open(img_path).convert(\"RGB\").save(new_img, \"JPEG\", quality=95)\n"," with open(new_txt, \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved combined tags ({len(final_tags)} total)\")\n","\n","print(\"\\nβ
DINO tags appended. Combined tags in /content/dino_combined/\")"]},{"cell_type":"code","source":["#@markdown # Cell 6.b: Save DINO-Tagged Results to Google Drive (Safe to Disconnect)\n","\n","from pathlib import Path\n","import zipfile\n","import shutil\n","\n","print(\"πΎ Saving all DINO-tagged results (images + combined tags) to Google Drive as a ZIP...\")\n","\n","dino_dir = Path(\"/content/dino_combined\")\n","if not dino_dir.exists() or not list(dino_dir.glob(\"*.jpg\")):\n"," print(\"β No /content/dino_combined folder found. Please run Cell 6 first.\")\n","else:\n"," # Create a ZIP for easy download / archive directly from dino_dir\n"," zip_path = Path(WORKING_DIR) / \"dino_tagged_results.zip\" # Changed zip name for clarity\n"," num_files_to_zip = len(list(dino_dir.iterdir()))\n"," print(f\" Zipping {num_files_to_zip} image+tag pairs...\")\n","\n"," with zipfile.ZipFile(zip_path, \"w\") as zipf:\n"," for file in dino_dir.iterdir():\n"," zipf.write(file, file.name) # Write directly from dino_dir, preserving original file names in zip\n","\n"," print(f\"\\nβ
ZIP creation complete!\")\n"," print(f\" β ZIP archive saved to: {zip_path}\")\n"," print(\"\\nYou can now safely disconnect / restart the runtime.\")\n"," print(\" When you come back, run Cell 6.c to restore everything.\")"],"metadata":{"id":"lKhaoG11wm4T","cellView":"form"},"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"38fc3364","cellView":"form"},"source":["#@markdown # Cell 6.c Save all prompts to combined txt file {item1|item2|...} (optional)\n","from google.colab import files\n","import zipfile\n","import os\n","from pathlib import Path\n","import shutil # Import shutil for copying files\n","\n","# --- Configuration for Drive path ---\n","# The path to the ZIP file on Google Drive\n","zip_file_on_drive_path = Path(WORKING_DIR) / \"dino_tagged_results.zip\" # Changed to process the zip from Cell 6.b\n","# The directory on Google Drive where the output .txt file should be saved\n","drive_target_output_dir = Path(zip_file_on_drive_path).parent # Get the parent directory\n","drive_target_output_dir.mkdir(parents=True, exist_ok=True) # Ensure the directory exists\n","\n","print(f\"π Processing ZIP from Drive: {zip_file_on_drive_path}\")\n","\n","# Local temporary directory for extraction\n","extract_dir = \"/content/extracted_txt_files_drive_processing\"\n","\n","# --- Added: Clear the directory if it exists ---\n","if os.path.exists(extract_dir):\n"," shutil.rmtree(extract_dir)\n","# ---\n","os.makedirs(extract_dir, exist_ok=True)\n","\n","# Extract the ZIP file from Drive to the temporary folder\n","try:\n"," with zipfile.ZipFile(zip_file_on_drive_path, 'r') as zip_ref:\n"," zip_ref.extractall(extract_dir)\n"," print(f\"β
ZIP extracted from Drive to {extract_dir}\")\n","except FileNotFoundError:\n"," print(f\"β Error: ZIP file not found at {zip_file_on_drive_path}. Please ensure it exists and Drive is mounted.\")\n"," # For simplicity, we'll let it proceed, but subsequent steps might fail if files are missing.\n"," pass\n","\n","# Get all .txt files and sort them numerically\n","txt_paths = sorted(\n"," list(Path(extract_dir).glob(\"*.txt\")),\n"," key=lambda p: f\"{int(p.stem):05d}\" if p.stem.isdigit() else p.stem.lower()\n",")\n","\n","cleaned_titles = []\n","if not txt_paths:\n"," print(\"β οΈ No .txt files found inside the ZIP after extraction! Generating empty combined file.\")\n"," combined_content = '{}'\n","else:\n"," print(f\"π Found {len(txt_paths)} title files. Processing...\")\n"," # Read, clean, and combine titles\n"," for txt_path in txt_paths:\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," title = f.read().strip()\n","\n"," # Remove the forbidden characters: ^ { } |\n"," cleaned = (title\n"," .replace('^', '')\n"," .replace('{', '')\n"," .replace('}', '')\n"," .replace('|', '')\n"," .replace('\"','')\n"," .replace('>', '')\n"," .replace('<','')) # Fixed syntax error here\n","\n"," cleaned_titles.append(cleaned)\n","\n"," # Build the exact format you want: {text1|text2|text3|...}\n"," combined_content = '{' + '|'.join(cleaned_titles) + '}'\n","\n","# --- Save to local content directory (for immediate download) ---\n","local_output_file_name = \"combined_titles_from_drive.txt\"\n","local_output_file_path = f\"/content/{local_output_file_name}\"\n","with open(local_output_file_path, \"w\", encoding=\"utf-8\") as f:\n"," f.write(combined_content)\n","\n","print(f\"\\nβ
Done! Combined {len(cleaned_titles)} titles into one line.\")\n","print(f\"π Local file saved as: {local_output_file_path}\")\n","print(f\" (First 100 characters: {combined_content[:100]}...)\")\n","\n","# --- Save to Google Drive in the same directory as the original ZIP ---\n","drive_final_output_path = drive_target_output_dir / local_output_file_name\n","shutil.copy(local_output_file_path, str(drive_final_output_path))\n","print(f\"βοΈ Also saved to Google Drive: {drive_final_output_path}\")\n","\n","# Auto-download the combined file from the local path\n","#files.download(local_output_file_path)\n","\n","#print(\"π₯ Download started for the local file. The file is also saved to Google Drive.\")"],"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FQF71-mvmlc1"},"outputs":[],"source":["# ================================================\n","# Auto Disconnect Colab Session\n","# ================================================\n","\n","print(\"π Disconnecting Colab session in 15 seconds...\")\n","import time\n","time.sleep(3)\n","\n","from google.colab import runtime\n","runtime.unassign()\n","\n","print(\"Session disconnected.\")"]},{"cell_type":"markdown","source":["Disconnect the drive. We retrieve values from drive. Google colab needs fresh session for gemma4 since its a fresh model (April 2 2026 released model) hence why you need to disconnect the Colab like this."],"metadata":{"id":"SI-pmpU1wyy8"}},{"cell_type":"code","source":["#@markdown # Cell 7.a: Reconnect Drive + Restore HF_TOKEN + Install Gemma-4 Dependencies\n","\n","from google.colab import drive\n","from google.colab import userdata\n","from pathlib import Path\n","import zipfile\n","import shutil\n","import os\n","\n","print(\"π Reconnecting to Google Drive...\")\n","\n","# Mount Drive again\n","drive.mount('/content/drive', force_remount=True)\n","\n","# Restore working directory\n","WORKING_DIR = \"/content/drive/MyDrive/DinoTaggerPipeline\"\n","os.makedirs(WORKING_DIR, exist_ok=True)\n","print(f\"β
Working directory restored: {WORKING_DIR}\")\n","\n","# Restore HF_TOKEN\n","hf_token = userdata.get('HF_TOKEN')\n","if hf_token:\n"," os.environ[\"HF_TOKEN\"] = hf_token\n"," print(\"β
HF_TOKEN restored from Colab Secrets.\")\n","else:\n"," print(\"β οΈ HF_TOKEN not found in secrets. Add it via the key icon if Gemma-4 fails to download.\")\n","\n","# Restore DINO-tagged files from backup (if they exist)\n","backup_dir = Path(WORKING_DIR) / \"dino_tagged_backup\"\n","dino_dir = Path(\"/content/dino_combined\")\n","dino_dir.mkdir(exist_ok=True)\n","\n","if backup_dir.exists() and list(backup_dir.glob(\"*.jpg\")):\n"," print(f\"π¦ Restoring {len(list(backup_dir.glob('*.jpg')))} tagged image pairs...\")\n"," for item in backup_dir.iterdir():\n"," shutil.copy2(item, dino_dir / item.name)\n"," print(\"β
DINO-tagged files restored to /content/dino_combined\")\n","elif (Path(WORKING_DIR) / \"dino_tagged_backup.zip\").exists():\n"," print(\"π¦ Unzipping backup ZIP...\")\n"," with zipfile.ZipFile(Path(WORKING_DIR) / \"dino_tagged_backup.zip\", 'r') as zip_ref:\n"," zip_ref.extractall(dino_dir)\n"," print(\"β
Files restored from ZIP.\")\n","else:\n"," print(\"β οΈ No backup found on Drive. Make sure you ran Cell 6.b before disconnecting.\")\n","\n","# Install Gemma-4 dependencies EXACTLY as in the working Cell 1a (no git, no restart needed)\n","print(\"\\nπ Installing Gemma-4 dependencies (safe & official method from model card)...\")\n","!pip install -q --upgrade --force-reinstall \"pillow<12.0\"\n","!pip install -q --upgrade transformers accelerate bitsandbytes\n","\n","print(\"\\nβ
Dependencies installed.\")\n","print(\" β Ready to load Gemma-4 in Cell 7.\")\n","print(\"\\nCell 7.a complete. Run Cell 7 next.\")"],"metadata":{"id":"2lmjE8GWwxrK"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 7.b: Clear VRAM + Load Gemma-4-E2B-Heretic (Merged β Safe Official Method)\n","\n","import torch\n","import gc\n","from google.colab import userdata\n","from transformers import AutoProcessor, AutoModelForMultimodalLM\n","\n","# Aggressive VRAM cleanup\n","print(\"π§Ή Clearing VRAM...\")\n","if 'tagger' in globals():\n"," del tagger\n","if 'model' in globals():\n"," del model\n","if 'processor' in globals():\n"," del processor\n","\n","torch.cuda.empty_cache()\n","gc.collect()\n","\n","if torch.cuda.is_available():\n"," print(f\" VRAM before loading: {torch.cuda.memory_allocated() / 1024**3:.2f} GB\")\n","else:\n"," print(\" β οΈ No GPU detected β loading on CPU (slow).\")\n","\n","# HF Token (already restored in Cell 6.c)\n","hf_token = userdata.get('HF_TOKEN')\n","if hf_token:\n"," print(\"β
HF_TOKEN loaded.\")\n","else:\n"," print(\"β οΈ HF_TOKEN not found.\")\n","\n","model_id = \"coder3101/gemma-4-E2B-it-heretic\"\n","\n","print(f\"\\nπ½ Loading Gemma-4-E2B-Heretic **EXACTLY** as recommended on model card...\")\n","\n","processor = AutoProcessor.from_pretrained(model_id, token=hf_token)\n","\n","# Official recommended loading (fixes vision hangs + gray-image bug)\n","model = AutoModelForMultimodalLM.from_pretrained(\n"," model_id,\n"," token=hf_token,\n"," dtype=\"auto\", # β model card uses \"dtype\"\n"," device_map=\"auto\", # β vision + text on GPU automatically\n"," low_cpu_mem_usage=True,\n",")\n","\n","print(\"\\nβ
Gemma-4-E2B-Heretic loaded successfully!\")\n","print(f\" Device: {model.device}\")\n","print(f\" Dtype: {model.dtype}\")\n","print(f\" VRAM used: {torch.cuda.memory_allocated() / 1024**3:.2f} GB\")\n","print(\" Vision encoder fully initialized β ready for captions.\")\n","\n","# Quick confirmation\n","print(f\"\\n Transformers version: {__import__('transformers').__version__}\")\n","print(f\" Model type: {model.config.model_type if hasattr(model.config, 'model_type') else 'N/A'}\")\n","\n","print(\"\\nβ
Cell 7 complete β ready for Cell 8 (caption generation with tags).\")"],"metadata":{"id":"2bEySax4xLlM"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["from PIL import Image\n","import base64\n","from pathlib import Path\n","import torch\n","import io\n","\n","input_dir = Path(\"/content/dino_combined\")\n","output_dir = Path(\"/content/final_captions\")\n","output_dir.mkdir(exist_ok=True)\n","\n","image_files = sorted(list(input_dir.glob(\"*.jpg\")))\n","\n","# Determine the optimal resolution for Gemma-4's vision encoder (improves latent features)\n","target_max_dim = 1024\n","if hasattr(processor, \"image_processor\") and hasattr(processor.image_processor, \"size\"):\n"," size_cfg = processor.image_processor.size\n"," if isinstance(size_cfg, dict):\n"," dims = [v for v in size_cfg.values() if isinstance(v, (int, float))]\n"," if dims:\n"," target_max_dim = int(max(dims))\n"," elif isinstance(size_cfg, (int, float)):\n"," target_max_dim = int(size_cfg)\n","\n","print(f\"π§ Using optimal vision resolution: max {target_max_dim}px (matched to processor for better latent representation)\")\n","\n","prompt_base = \"Describe this image in detail, including key objects, scene, colors, mood, lighting, and any visible text. The description text should be long, roughly 400 words in size. Be accurate and comprehensive. Do not use newlines, bold font, or itemized lists. Output a block of text.\"\n","\n","print(f\"Generating detailed captions for {len(image_files)} images with improved latent image representation...\")\n","\n","def image_to_data_url(img: Image.Image) -> str:\n"," \"\"\"Lossless PNG data URL β cleanest possible pixel data for the vision encoder\"\"\"\n"," buffered = io.BytesIO()\n"," img.save(buffered, format=\"PNG\") # lossless, no compression artifacts\n"," img_str = base64.b64encode(buffered.getvalue()).decode(\"utf-8\")\n"," return f\"data:image/png;base64,{img_str}\"\n","\n","for i, img_path in enumerate(image_files, start=1):\n"," base_name = img_path.stem\n"," txt_path = input_dir / f\"{base_name}.txt\"\n","\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_path.name}\")\n","\n"," # Load existing DINO tags\n"," tags = \"\"\n"," if txt_path.exists():\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," tags = f.read().strip()\n","\n"," # === OPTIMAL INPUT FOR LATENT REPRESENTATION ===\n"," image = Image.open(img_path).convert(\"RGB\")\n","\n"," # Resize to the processor's preferred size (best latent quality)\n"," if max(image.size) != target_max_dim or min(image.size) < 512:\n"," scale = target_max_dim / max(image.size)\n"," new_size = (int(image.width * scale), int(image.height * scale))\n"," image = image.resize(new_size, Image.LANCZOS)\n","\n"," # Lossless data URL (direct pixel data β stronger latent features)\n"," image_url = image_to_data_url(image)\n","\n"," full_prompt = f\"{prompt_base}\\n\\nThe description must include these words : {tags}\"\n","\n"," messages = [{\n"," \"role\": \"user\",\n"," \"content\": [\n"," {\"type\": \"image\", \"url\": image_url}, # lossless PNG data\n"," {\"type\": \"text\", \"text\": full_prompt}\n"," ]\n"," }]\n","\n"," inputs = processor.apply_chat_template(\n"," messages, add_generation_prompt=True, tokenize=True, return_tensors=\"pt\", return_dict=True\n"," ).to(model.device)\n","\n"," with torch.no_grad():\n"," outputs = model.generate(\n"," **inputs,\n"," max_new_tokens=768,\n"," do_sample=True,\n"," temperature=0.65,\n"," top_p=0.9,\n"," )\n","\n"," input_len = inputs[\"input_ids\"].shape[-1]\n"," raw = processor.decode(outputs[0][input_len:], skip_special_tokens=False)\n"," caption = processor.parse_response(raw)[\"content\"] if hasattr(processor, \"parse_response\") else raw.strip()\n","\n"," # Save the (optimally resized) image + caption\n"," image.save(output_dir / f\"{base_name}.jpg\", \"JPEG\", quality=95)\n"," with open(output_dir / f\"{base_name}.txt\", \"w\", encoding=\"utf-8\") as f:\n"," f.write(caption)\n","\n"," print(f\" β Caption generated ({len(caption.split())} words) β latent features improved\")\n","\n","print(\"\\nβ
Gemma-4 captions complete with **improved latent image representation**!\")\n","print(\"Final pairs saved in /content/final_captions/\")"],"metadata":{"id":"gZk3qzO13bU8"},"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"qxd0tFpmosVI"},"outputs":[],"source":["#@markdown # Cell 9: Save Final Image-Text Pairs as ZIP on Google Drive\n","\n","from pathlib import Path\n","import zipfile\n","from google.colab import files\n","\n","final_dir = Path(\"/content/final_captions\")\n","zip_name = \"final_image_text_pairs.zip\"\n","zip_path = Path(WORKING_DIR) / zip_name\n","\n","print(f\"Creating ZIP: {zip_path}\")\n","\n","with zipfile.ZipFile(zip_path, 'w') as zipf:\n"," for file in final_dir.iterdir():\n"," zipf.write(file, file.name)\n","\n","print(f\"β
ZIP saved to Google Drive at: {zip_path}\")\n","\n","# Optional: Auto-download to local machine\n","auto_download = True #@param {type:\"boolean\"}\n","if auto_download:\n"," files.download(str(zip_path))\n"," print(\"π₯ Auto-download triggered.\")\n","else:\n"," print(\" β You can download the ZIP manually from Google Drive.\")\n","\n","print(\"\\nπ Pipeline complete! All steps finished.\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TZ4h8CWIy4uM"},"outputs":[],"source":["# ================================================\n","# Auto Disconnect Colab Session\n","# ================================================\n","\n","print(\"π Disconnecting Colab session in 15 seconds...\")\n","import time\n","time.sleep(3)\n","\n","from google.colab import runtime\n","runtime.unassign()\n","\n","print(\"Session disconnected.\")"]}],"metadata":{"accelerator":"GPU","colab":{"gpuType":"T4","provenance":[{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/gemma4_batch_captioner.ipynb","timestamp":1775586753307},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/gemma4_batch_captioner.ipynb","timestamp":1775585424465}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
|
|
|
|
| 1 |
+
{"cells":[{"cell_type":"code","source":["#@markdown # Cell 0: Mount Google Drive & Prepare HF_TOKEN for faster HF downloads (UPDATED)\n","\n","from google.colab import drive\n","from google.colab import userdata\n","import os\n","\n","# Mount Google Drive (still used for output ZIPs/results only)\n","print(\"Mounting Google Drive...\")\n","drive.mount('/content/drive', force_remount=True)\n","\n","# Create a working directory on Drive (for final outputs/ZIPs only)\n","WORKING_DIR = \"/content/drive/MyDrive/DinoTaggerPipeline\"\n","os.makedirs(WORKING_DIR, exist_ok=True)\n","print(f\"β
Working directory set to: {WORKING_DIR} (outputs only)\")\n","\n","# === NEW: Local model directory on /content/ (CPU storage only) ===\n","# Checkpoints will ONLY go here. Nothing is saved to Drive.\n","MODEL_DIR = \"/content/models\"\n","os.makedirs(MODEL_DIR, exist_ok=True)\n","print(f\"β
Model directory set to: {MODEL_DIR} (local /content/ storage only)\")\n","print(\" β Checkpoint models (safetensors, vocab, etc.) will be saved here only.\")\n","\n","# HF_TOKEN from Colab Secrets\n","hf_token = userdata.get('HF_TOKEN')\n","if hf_token:\n"," os.environ[\"HF_TOKEN\"] = hf_token\n"," print(\"β
HF_TOKEN loaded from Colab Secrets and set as environment variable.\")\n"," print(\" β This enables authenticated + faster/resumable Hugging Face downloads.\")\n","else:\n"," print(\"β οΈ HF_TOKEN not found in Colab Secrets.\")\n"," print(\" β Add it via the key icon (left sidebar) β Secrets β Name: HF_TOKEN\")\n"," print(\" β Some downloads may be slower or fail if the repo requires login.\")\n","\n","print(\"\\nβ
Cell 0 complete. Models will now save to local /content/models only.\")"],"metadata":{"cellView":"form","id":"rtukY485TQeT"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 1: Download All Models to Disk (CPU-only, No VRAM Load) (UPDATED)\n","\n","import torch\n","from huggingface_hub import hf_hub_download, snapshot_download\n","from pathlib import Path\n","import os\n","\n","print(\"Downloading models to local /content/models (CPU-only, resumable)...\")\n","\n","# 1. DINO Tagger (lodestones/tagger-experiment) β saved to MODEL_DIR only\n","print(\"\\n1. Downloading DINO Tagger files...\")\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"tagger_proto.safetensors\", local_dir=MODEL_DIR, local_dir_use_symlinks=False)\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"tagger_vocab_with_categories.json\", local_dir=MODEL_DIR, local_dir_use_symlinks=False)\n","hf_hub_download(repo_id=\"lodestones/tagger-experiment\", filename=\"inference_tagger_standalone.py\", local_dir=MODEL_DIR, local_dir_use_symlinks=False)\n","\n","# 2. WD-VIT Tagger (selected_tags.csv) β saved to MODEL_DIR only\n","print(\"\\n2. Downloading WD-VIT Tagger assets...\")\n","tags_path = Path(MODEL_DIR) / \"selected_tags.csv\"\n","if not tags_path.exists():\n"," import requests\n"," from io import StringIO\n"," import pandas as pd\n"," response = requests.get(\"https://huggingface.co/SmilingWolf/wd-vit-tagger-v3/resolve/main/selected_tags.csv\")\n"," tags_df = pd.read_csv(StringIO(response.text))\n"," tags_df.to_csv(tags_path, index=False)\n"," print(f\" β Saved {len(tags_df)} tags to selected_tags.csv in {MODEL_DIR}\")\n","\n","# 3. Gemma-4-E2B-Heretic (processor + model files will be cached by transformers on local storage)\n","print(\"\\n3. Gemma-4-E2B-Heretic will be cached on first load in Cell 7 (large model - stored locally).\")\n","\n","print(f\"\\nβ
All lightweight models downloaded/cached to: {MODEL_DIR} (local /content/ only)\")\n","print(\" β No checkpoint models were saved to Google Drive.\")\n","print(\" Heavy models (Gemma) will download during loading in Cell 7.\")"],"metadata":{"cellView":"form","id":"a5CohOa3TTcy"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 2: Extract Images from ZIP (unchanged - no model changes)\n","\n","from pathlib import Path\n","import zipfile\n","\n","# === Settings ===\n","zip_file_path = \"/content/drive/MyDrive/Archive.zip\" #@param {type:\"string\"}\n","# Full path on Drive, e.g. /content/drive/MyDrive/my_images.zip\n","\n","extract_dir = Path(\"/content/extracted_images\")\n","extract_dir.mkdir(exist_ok=True)\n","\n","if not zip_file_path or not Path(zip_file_path).exists():\n"," print(f\"β ZIP file not found at: {zip_file_path}\")\n"," print(\" β Please provide a valid path to your image ZIP.\")\n","else:\n"," print(f\"Extracting images from: {zip_file_path}\")\n"," with zipfile.ZipFile(zip_file_path, 'r') as zip_ref:\n"," # Filter out macOS specific files (e.g., __MACOSX directory, .DS_Store)\n"," members = [m for m in zip_ref.namelist() if not m.startswith('__MACOSX/') and not m.endswith('/.DS_Store')]\n"," for member in members:\n"," zip_ref.extract(member, extract_dir)\n"," print(\" β Ignored __MACOSX/ and .DS_Store files during extraction.\")\n","\n"," image_files = []\n"," for ext in ['.png', '.jpg', '.jpeg', '.webp', '.avif','.bmp']:\n"," image_files.extend(list(extract_dir.rglob(f\"*{ext}\")))\n"," image_files.extend(list(extract_dir.rglob(f\"*{ext.upper()}\")))\n","\n"," image_files = sorted(set(str(p) for p in image_files))\n"," print(f\"β
Extracted and found {len(image_files)} image(s).\")\n"," print(\" Images are ready in /content/extracted_images/\")"],"metadata":{"cellView":"form","id":"wBm5MDYETXMa"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 3: Clear VRAM + Load WD-VIT Tagger v3 (UPDATED)\n","\n","import torch\n","import gc\n","import timm\n","import torchvision.transforms as transforms\n","import pandas as pd\n","from pathlib import Path\n","\n","# Clear VRAM\n","print(\"π§Ή Clearing VRAM...\")\n","if 'model' in globals(): del model\n","if 'tagger' in globals(): del tagger\n","torch.cuda.empty_cache()\n","gc.collect()\n","print(\" β VRAM cleared.\")\n","\n","# Load WD-VIT Tagger\n","print(\"\\nπ₯ Loading WD-VIT Tagger v3...\")\n","# UPDATED: Use MODEL_DIR (local only)\n","tags_path = Path(MODEL_DIR) / \"selected_tags.csv\"\n","tags_df = pd.read_csv(tags_path)\n","tags_list = tags_df['name'].tolist()\n","print(f\" β Loaded {len(tags_list)} tags.\")\n","\n","model = timm.create_model(\"hf_hub:SmilingWolf/wd-vit-tagger-v3\", pretrained=True)\n","model.eval()\n","device = torch.device(\"cuda\" if torch.cuda.is_available() else \"cpu\")\n","model = model.to(device)\n","\n","preprocess = transforms.Compose([\n"," transforms.Resize((448, 448)),\n"," transforms.ToTensor(),\n"," transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),\n","])\n","\n","print(f\"β
WD-VIT Tagger loaded on {device}\")"],"metadata":{"cellView":"form","id":"gRW5abcETaXd"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 4: Tag Images with WD-VIT Tagger (FIXED) (unchanged)\n","\n","from PIL import Image\n","from pathlib import Path\n","\n","threshold = 0.7 #@param {type:\"slider\", min:0.1, max:0.95, step:0.01}\n","add_commas = True #@param {type:\"boolean\"}\n","\n","input_dir = Path(\"/content/extracted_images\")\n","output_dir = Path(\"/content/wd_tags\")\n","output_dir.mkdir(exist_ok=True)\n","\n","# FIXED: Clean glob pattern (was broken with Chinese text garbage)\n","image_files = sorted(list(input_dir.glob(\"*.*\")))\n","image_files = [f for f in image_files if f.suffix.lower() in {'.png','.jpg','.jpeg','.webp','.bmp','.avif'}]\n","\n","print(f\"Starting WD tagging for {len(image_files)} images...\")\n","\n","for i, img_path in enumerate(image_files, start=1):\n"," img_name = img_path.name\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_name}\")\n","\n"," # Load image once\n"," image = Image.open(img_path).convert(\"RGB\")\n","\n"," # Prepare for model\n"," input_tensor = preprocess(image).unsqueeze(0).to(device)\n","\n"," with torch.no_grad():\n"," logits = model(input_tensor)\n"," probs = torch.sigmoid(logits).cpu().numpy()[0]\n","\n"," wd_tags = [tags_list[j] for j, prob in enumerate(probs) if prob > threshold]\n","\n"," tag_text = \" , \".join(wd_tags) if add_commas else \" \".join(wd_tags)\n","\n"," base_name = f\"{i:04d}\" # numbered for consistency\n","\n"," # FIXED: Reuse already-loaded image instead of opening twice\n"," image.save(output_dir / f\"{base_name}.jpg\", \"JPEG\", quality=95)\n","\n"," with open(output_dir / f\"{base_name}.txt\", \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved {base_name}.jpg + {base_name}.txt ({len(wd_tags)} WD tags)\")\n","\n","print(\"\\nβ
WD tagging complete. Tags saved in /content/wd_tags/\")"],"metadata":{"cellView":"form","id":"IvF3aMsSTkpG"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 5: Clear VRAM + Load DINO Tagger (UPDATED)\n","\n","import torch\n","import gc\n","import sys\n","from pathlib import Path\n","\n","# UPDATED: Add MODEL_DIR to sys.path so Python can find the module (inference_tagger_standalone.py is now in local /content/models)\n","if str(MODEL_DIR) not in sys.path:\n"," sys.path.insert(0, str(MODEL_DIR))\n","\n","from inference_tagger_standalone import Tagger # from Cell 1 download (now in MODEL_DIR)\n","\n","# Clear previous model\n","print(\"π§Ή Clearing VRAM...\")\n","if 'model' in globals(): del model\n","torch.cuda.empty_cache()\n","gc.collect()\n","\n","# Load DINO Tagger\n","print(\"\\nπ₯ Loading DINO Tagger...\")\n","# UPDATED: Load checkpoint & vocab from local MODEL_DIR only\n","tagger = Tagger(\n"," checkpoint_path=str(Path(MODEL_DIR) / \"tagger_proto.safetensors\"),\n"," vocab_path=str(Path(MODEL_DIR) / \"tagger_vocab_with_categories.json\"),\n"," device=\"cuda\" if torch.cuda.is_available() else \"cpu\",\n"," dtype=torch.bfloat16 if torch.cuda.is_available() else torch.float32,\n"," max_size=1024\n",")\n","\n","print(f\"β
DINO Tagger loaded on {tagger.device} (checkpoint from local /content/models)\")"],"metadata":{"cellView":"form","id":"7AAhPUI5TnAh"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 6.a: Tag with DINO Tagger + Append to .txt Files (unchanged)\n","\n","threshold_percent = 83 #@param {type:\"slider\", min:1, max:95, step:1}\n","max_tags = 100 #@param {type:\"slider\", min:5, max:150, step:5}\n","use_max_tags = False #@param {type:\"boolean\"}\n","\n","wd_dir = Path(\"/content/wd_tags\")\n","dino_dir = Path(\"/content/dino_combined\")\n","dino_dir.mkdir(exist_ok=True)\n","\n","image_files = sorted(list(wd_dir.glob(\"*.jpg\")))\n","\n","threshold = threshold_percent / 100.0\n","\n","print(f\"Starting DINO tagging for {len(image_files)} images...\")\n","\n","for i, img_path in enumerate(image_files, start=1):\n"," base_name = img_path.stem\n"," txt_path = wd_dir / f\"{base_name}.txt\"\n","\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_path.name}\")\n","\n"," if use_max_tags:\n"," tags_list = tagger.predict(str(img_path), topk=max_tags, threshold=None)\n"," else:\n"," tags_list = tagger.predict(str(img_path), topk=None, threshold=threshold)\n"," if len(tags_list) > max_tags:\n"," tags_list = tags_list[:max_tags]\n","\n"," dino_tags = [tag for tag, prob in tags_list]\n","\n"," # Read WD tags\n"," if txt_path.exists():\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," existing_text = f.read().strip()\n"," existing_tags = [t.strip() for t in existing_text.replace(\",\", \" \").split() if t.strip()]\n"," else:\n"," existing_tags = []\n","\n"," # Combine: WD first, then DINO (or reverse if preferred)\n"," combined = existing_tags + dino_tags\n"," seen = set()\n"," final_tags = [tag for tag in combined if not (tag in seen or seen.add(tag))]\n","\n"," tag_text = \" , \".join(final_tags) if add_commas else \" \".join(final_tags) # add_commas from previous cell if defined\n","\n"," # Save combined\n"," new_img = dino_dir / f\"{base_name}.jpg\"\n"," new_txt = dino_dir / f\"{base_name}.txt\"\n","\n"," Image.open(img_path).convert(\"RGB\").save(new_img, \"JPEG\", quality=95)\n"," with open(new_txt, \"w\", encoding=\"utf-8\") as f:\n"," f.write(tag_text)\n","\n"," print(f\" β Saved combined tags ({len(final_tags)} total)\")\n","\n","print(\"\\nβ
DINO tags appended. Combined tags in /content/dino_combined/\")"],"metadata":{"cellView":"form","id":"T_0lkZ95Tqzv"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 6.b: Save DINO-Tagged Results to Google Drive (Safe to Disconnect) (unchanged)\n","\n","from pathlib import Path\n","import zipfile\n","import shutil\n","\n","print(\"πΎ Saving all DINO-tagged results (images + combined tags) to Google Drive as a ZIP...\")\n","\n","dino_dir = Path(\"/content/dino_combined\")\n","if not dino_dir.exists() or not list(dino_dir.glob(\"*.jpg\")):\n"," print(\"β No /content/dino_combined folder found. Please run Cell 6 first.\")\n","else:\n"," # Create a ZIP for easy download / archive directly from dino_dir\n"," zip_path = Path(WORKING_DIR) / \"dino_tagged_results.zip\" # Changed zip name for clarity\n"," num_files_to_zip = len(list(dino_dir.iterdir()))\n"," print(f\" Zipping {num_files_to_zip} image+tag pairs...\")\n","\n"," with zipfile.ZipFile(zip_path, \"w\") as zipf:\n"," for file in dino_dir.iterdir():\n"," zipf.write(file, file.name) # Write directly from dino_dir, preserving original file names in zip\n","\n"," print(f\"\\nβ
ZIP creation complete!\")\n"," print(f\" β ZIP archive saved to: {zip_path}\")\n"," print(\"\\nYou can now safely disconnect / restart the runtime.\")\n"," print(\" When you come back, run Cell 6.c to restore everything.\")"],"metadata":{"cellView":"form","id":"DchQX0ugTu0b"},"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"38fc3364","cellView":"form"},"source":["#@markdown # Cell 6.c Save all prompts to combined txt file {item1|item2|...} (optional)\n","from google.colab import files\n","import zipfile\n","import os\n","from pathlib import Path\n","import shutil # Import shutil for copying files\n","\n","# --- Configuration for Drive path ---\n","# The path to the ZIP file on Google Drive\n","zip_file_on_drive_path = Path(WORKING_DIR) / \"dino_tagged_results.zip\" # Changed to process the zip from Cell 6.b\n","# The directory on Google Drive where the output .txt file should be saved\n","drive_target_output_dir = Path(zip_file_on_drive_path).parent # Get the parent directory\n","drive_target_output_dir.mkdir(parents=True, exist_ok=True) # Ensure the directory exists\n","\n","print(f\"π Processing ZIP from Drive: {zip_file_on_drive_path}\")\n","\n","# Local temporary directory for extraction\n","extract_dir = \"/content/extracted_txt_files_drive_processing\"\n","\n","# --- Added: Clear the directory if it exists ---\n","if os.path.exists(extract_dir):\n"," shutil.rmtree(extract_dir)\n","# ---\n","os.makedirs(extract_dir, exist_ok=True)\n","\n","# Extract the ZIP file from Drive to the temporary folder\n","try:\n"," with zipfile.ZipFile(zip_file_on_drive_path, 'r') as zip_ref:\n"," zip_ref.extractall(extract_dir)\n"," print(f\"β
ZIP extracted from Drive to {extract_dir}\")\n","except FileNotFoundError:\n"," print(f\"β Error: ZIP file not found at {zip_file_on_drive_path}. Please ensure it exists and Drive is mounted.\")\n"," # For simplicity, we'll let it proceed, but subsequent steps might fail if files are missing.\n"," pass\n","\n","# Get all .txt files and sort them numerically\n","txt_paths = sorted(\n"," list(Path(extract_dir).glob(\"*.txt\")),\n"," key=lambda p: f\"{int(p.stem):05d}\" if p.stem.isdigit() else p.stem.lower()\n",")\n","\n","cleaned_titles = []\n","if not txt_paths:\n"," print(\"β οΈ No .txt files found inside the ZIP after extraction! Generating empty combined file.\")\n"," combined_content = '{}'\n","else:\n"," print(f\"π Found {len(txt_paths)} title files. Processing...\")\n"," # Read, clean, and combine titles\n"," for txt_path in txt_paths:\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," title = f.read().strip()\n","\n"," # Remove the forbidden characters: ^ { } |\n"," cleaned = (title\n"," .replace('^', '')\n"," .replace('{', '')\n"," .replace('}', '')\n"," .replace('|', '')\n"," .replace('\"','')\n"," .replace('>', '')\n"," .replace('<','')) # Fixed syntax error here\n","\n"," cleaned_titles.append(cleaned)\n","\n"," # Build the exact format you want: {text1|text2|text3|...}\n"," combined_content = '{' + '|'.join(cleaned_titles) + '}'\n","\n","# --- Save to local content directory (for immediate download) ---\n","local_output_file_name = \"combined_titles_from_drive.txt\"\n","local_output_file_path = f\"/content/{local_output_file_name}\"\n","with open(local_output_file_path, \"w\", encoding=\"utf-8\") as f:\n"," f.write(combined_content)\n","\n","print(f\"\\nβ
Done! Combined {len(cleaned_titles)} titles into one line.\")\n","print(f\"π Local file saved as: {local_output_file_path}\")\n","print(f\" (First 100 characters: {combined_content[:100]}...)\")\n","\n","# --- Save to Google Drive in the same directory as the original ZIP ---\n","drive_final_output_path = drive_target_output_dir / local_output_file_name\n","shutil.copy(local_output_file_path, str(drive_final_output_path))\n","print(f\"βοΈ Also saved to Google Drive: {drive_final_output_path}\")\n","\n","# Auto-download the combined file from the local path\n","#files.download(local_output_file_path)\n","\n","#print(\"π₯ Download started for the local file. The file is also saved to Google Drive.\")"],"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"id":"FQF71-mvmlc1"},"outputs":[],"source":["# ================================================\n","# Auto Disconnect Colab Session\n","# ================================================\n","\n","print(\"π Disconnecting Colab session in 15 seconds...\")\n","import time\n","time.sleep(3)\n","\n","from google.colab import runtime\n","runtime.unassign()\n","\n","print(\"Session disconnected.\")"]},{"cell_type":"markdown","source":["Disconnect the drive. We retrieve values from drive. Google colab needs fresh session for gemma4 since its a fresh model (April 2 2026 released model) hence why you need to disconnect the Colab like this."],"metadata":{"id":"SI-pmpU1wyy8"}},{"cell_type":"code","source":["#@markdown # Cell 7.a: Reconnect Drive + Restore HF_TOKEN + Install Gemma-4 Dependencies\n","\n","from google.colab import drive\n","from google.colab import userdata\n","from pathlib import Path\n","import zipfile\n","import shutil\n","import os\n","\n","print(\"π Reconnecting to Google Drive...\")\n","\n","# Mount Drive again\n","drive.mount('/content/drive', force_remount=True)\n","\n","# Restore working directory\n","WORKING_DIR = \"/content/drive/MyDrive/DinoTaggerPipeline\"\n","os.makedirs(WORKING_DIR, exist_ok=True)\n","print(f\"β
Working directory restored: {WORKING_DIR}\")\n","\n","# Restore HF_TOKEN\n","hf_token = userdata.get('HF_TOKEN')\n","if hf_token:\n"," os.environ[\"HF_TOKEN\"] = hf_token\n"," print(\"β
HF_TOKEN restored from Colab Secrets.\")\n","else:\n"," print(\"β οΈ HF_TOKEN not found in secrets. Add it via the key icon if Gemma-4 fails to download.\")\n","\n","# Restore DINO-tagged files from backup (if they exist)\n","backup_dir = Path(WORKING_DIR) / \"dino_tagged_backup\"\n","dino_dir = Path(\"/content/dino_combined\")\n","dino_dir.mkdir(exist_ok=True)\n","\n","if backup_dir.exists() and list(backup_dir.glob(\"*.jpg\")):\n"," print(f\"π¦ Restoring {len(list(backup_dir.glob('*.jpg')))} tagged image pairs...\")\n"," for item in backup_dir.iterdir():\n"," shutil.copy2(item, dino_dir / item.name)\n"," print(\"β
DINO-tagged files restored to /content/dino_combined\")\n","elif (Path(WORKING_DIR) / \"dino_tagged_backup.zip\").exists():\n"," print(\"π¦ Unzipping backup ZIP...\")\n"," with zipfile.ZipFile(Path(WORKING_DIR) / \"dino_tagged_backup.zip\", 'r') as zip_ref:\n"," zip_ref.extractall(dino_dir)\n"," print(\"β
Files restored from ZIP.\")\n","else:\n"," print(\"β οΈ No backup found on Drive. Make sure you ran Cell 6.b before disconnecting.\")\n","\n","# Install Gemma-4 dependencies EXACTLY as in the working Cell 1a (no git, no restart needed)\n","print(\"\\nπ Installing Gemma-4 dependencies (safe & official method from model card)...\")\n","!pip install -q --upgrade --force-reinstall \"pillow<12.0\"\n","!pip install -q --upgrade transformers accelerate bitsandbytes\n","\n","print(\"\\nβ
Dependencies installed.\")\n","print(\" β Ready to load Gemma-4 in Cell 7.\")\n","print(\"\\nCell 7.a complete. Run Cell 7 next.\")"],"metadata":{"id":"2lmjE8GWwxrK"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["#@markdown # Cell 7.b: Clear VRAM + Load Gemma-4-E2B-Heretic (Merged β Safe Official Method)\n","\n","import torch\n","import gc\n","from google.colab import userdata\n","from transformers import AutoProcessor, AutoModelForMultimodalLM\n","\n","# Aggressive VRAM cleanup\n","print(\"π§Ή Clearing VRAM...\")\n","if 'tagger' in globals():\n"," del tagger\n","if 'model' in globals():\n"," del model\n","if 'processor' in globals():\n"," del processor\n","\n","torch.cuda.empty_cache()\n","gc.collect()\n","\n","if torch.cuda.is_available():\n"," print(f\" VRAM before loading: {torch.cuda.memory_allocated() / 1024**3:.2f} GB\")\n","else:\n"," print(\" β οΈ No GPU detected β loading on CPU (slow).\")\n","\n","# HF Token (already restored in Cell 6.c)\n","hf_token = userdata.get('HF_TOKEN')\n","if hf_token:\n"," print(\"β
HF_TOKEN loaded.\")\n","else:\n"," print(\"β οΈ HF_TOKEN not found.\")\n","\n","model_id = \"coder3101/gemma-4-E2B-it-heretic\"\n","\n","print(f\"\\nπ½ Loading Gemma-4-E2B-Heretic **EXACTLY** as recommended on model card...\")\n","\n","processor = AutoProcessor.from_pretrained(model_id, token=hf_token)\n","\n","# Official recommended loading (fixes vision hangs + gray-image bug)\n","model = AutoModelForMultimodalLM.from_pretrained(\n"," model_id,\n"," token=hf_token,\n"," dtype=\"auto\", # β model card uses \"dtype\"\n"," device_map=\"auto\", # β vision + text on GPU automatically\n"," low_cpu_mem_usage=True,\n",")\n","\n","print(\"\\nβ
Gemma-4-E2B-Heretic loaded successfully!\")\n","print(f\" Device: {model.device}\")\n","print(f\" Dtype: {model.dtype}\")\n","print(f\" VRAM used: {torch.cuda.memory_allocated() / 1024**3:.2f} GB\")\n","print(\" Vision encoder fully initialized β ready for captions.\")\n","\n","# Quick confirmation\n","print(f\"\\n Transformers version: {__import__('transformers').__version__}\")\n","print(f\" Model type: {model.config.model_type if hasattr(model.config, 'model_type') else 'N/A'}\")\n","\n","print(\"\\nβ
Cell 7 complete β ready for Cell 8 (caption generation with tags).\")"],"metadata":{"id":"2bEySax4xLlM"},"execution_count":null,"outputs":[]},{"cell_type":"code","source":["from PIL import Image\n","import base64\n","from pathlib import Path\n","import torch\n","import io\n","\n","input_dir = Path(\"/content/dino_combined\")\n","output_dir = Path(\"/content/final_captions\")\n","output_dir.mkdir(exist_ok=True)\n","\n","image_files = sorted(list(input_dir.glob(\"*.jpg\")))\n","\n","# Determine the optimal resolution for Gemma-4's vision encoder (improves latent features)\n","target_max_dim = 1024\n","if hasattr(processor, \"image_processor\") and hasattr(processor.image_processor, \"size\"):\n"," size_cfg = processor.image_processor.size\n"," if isinstance(size_cfg, dict):\n"," dims = [v for v in size_cfg.values() if isinstance(v, (int, float))]\n"," if dims:\n"," target_max_dim = int(max(dims))\n"," elif isinstance(size_cfg, (int, float)):\n"," target_max_dim = int(size_cfg)\n","\n","print(f\"π§ Using optimal vision resolution: max {target_max_dim}px (matched to processor for better latent representation)\")\n","\n","prompt_base = \"Describe this image in detail, including key objects, scene, colors, mood, lighting, and any visible text. The description text should be long, roughly 400 words in size. Be accurate and comprehensive. Do not use newlines, bold font, or itemized lists. Output a block of text.\"\n","\n","print(f\"Generating detailed captions for {len(image_files)} images with improved latent image representation...\")\n","\n","def image_to_data_url(img: Image.Image) -> str:\n"," \"\"\"Lossless PNG data URL β cleanest possible pixel data for the vision encoder\"\"\"\n"," buffered = io.BytesIO()\n"," img.save(buffered, format=\"PNG\") # lossless, no compression artifacts\n"," img_str = base64.b64encode(buffered.getvalue()).decode(\"utf-8\")\n"," return f\"data:image/png;base64,{img_str}\"\n","\n","for i, img_path in enumerate(image_files, start=1):\n"," base_name = img_path.stem\n"," txt_path = input_dir / f\"{base_name}.txt\"\n","\n"," print(f\"[{i}/{len(image_files)}] Processing: {img_path.name}\")\n","\n"," # Load existing DINO tags\n"," tags = \"\"\n"," if txt_path.exists():\n"," with open(txt_path, \"r\", encoding=\"utf-8\") as f:\n"," tags = f.read().strip()\n","\n"," # === OPTIMAL INPUT FOR LATENT REPRESENTATION ===\n"," image = Image.open(img_path).convert(\"RGB\")\n","\n"," # Resize to the processor's preferred size (best latent quality)\n"," if max(image.size) != target_max_dim or min(image.size) < 512:\n"," scale = target_max_dim / max(image.size)\n"," new_size = (int(image.width * scale), int(image.height * scale))\n"," image = image.resize(new_size, Image.LANCZOS)\n","\n"," # Lossless data URL (direct pixel data β stronger latent features)\n"," image_url = image_to_data_url(image)\n","\n"," full_prompt = f\"{prompt_base}\\n\\nThe description must include these words : {tags}\"\n","\n"," messages = [{\n"," \"role\": \"user\",\n"," \"content\": [\n"," {\"type\": \"image\", \"url\": image_url}, # lossless PNG data\n"," {\"type\": \"text\", \"text\": full_prompt}\n"," ]\n"," }]\n","\n"," inputs = processor.apply_chat_template(\n"," messages, add_generation_prompt=True, tokenize=True, return_tensors=\"pt\", return_dict=True\n"," ).to(model.device)\n","\n"," with torch.no_grad():\n"," outputs = model.generate(\n"," **inputs,\n"," max_new_tokens=768,\n"," do_sample=True,\n"," temperature=0.65,\n"," top_p=0.9,\n"," )\n","\n"," input_len = inputs[\"input_ids\"].shape[-1]\n"," raw = processor.decode(outputs[0][input_len:], skip_special_tokens=False)\n"," caption = processor.parse_response(raw)[\"content\"] if hasattr(processor, \"parse_response\") else raw.strip()\n","\n"," # Save the (optimally resized) image + caption\n"," image.save(output_dir / f\"{base_name}.jpg\", \"JPEG\", quality=95)\n"," with open(output_dir / f\"{base_name}.txt\", \"w\", encoding=\"utf-8\") as f:\n"," f.write(caption)\n","\n"," print(f\" β Caption generated ({len(caption.split())} words) β latent features improved\")\n","\n","print(\"\\nβ
Gemma-4 captions complete with **improved latent image representation**!\")\n","print(\"Final pairs saved in /content/final_captions/\")"],"metadata":{"id":"gZk3qzO13bU8"},"execution_count":null,"outputs":[]},{"cell_type":"code","execution_count":null,"metadata":{"cellView":"form","id":"qxd0tFpmosVI"},"outputs":[],"source":["#@markdown # Cell 9: Save Final Image-Text Pairs as ZIP on Google Drive\n","\n","from pathlib import Path\n","import zipfile\n","from google.colab import files\n","\n","final_dir = Path(\"/content/final_captions\")\n","zip_name = \"final_image_text_pairs.zip\"\n","zip_path = Path(WORKING_DIR) / zip_name\n","\n","print(f\"Creating ZIP: {zip_path}\")\n","\n","with zipfile.ZipFile(zip_path, 'w') as zipf:\n"," for file in final_dir.iterdir():\n"," zipf.write(file, file.name)\n","\n","print(f\"β
ZIP saved to Google Drive at: {zip_path}\")\n","\n","# Optional: Auto-download to local machine\n","auto_download = True #@param {type:\"boolean\"}\n","if auto_download:\n"," files.download(str(zip_path))\n"," print(\"π₯ Auto-download triggered.\")\n","else:\n"," print(\" β You can download the ZIP manually from Google Drive.\")\n","\n","print(\"\\nπ Pipeline complete! All steps finished.\")"]},{"cell_type":"code","execution_count":null,"metadata":{"id":"TZ4h8CWIy4uM"},"outputs":[],"source":["# ================================================\n","# Auto Disconnect Colab Session\n","# ================================================\n","\n","print(\"π Disconnecting Colab session in 15 seconds...\")\n","import time\n","time.sleep(3)\n","\n","from google.colab import runtime\n","runtime.unassign()\n","\n","print(\"Session disconnected.\")"]}],"metadata":{"accelerator":"GPU","colab":{"gpuType":"T4","provenance":[{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/gemma4_batch_captioner.ipynb","timestamp":1775990959106},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/gemma4_batch_captioner.ipynb","timestamp":1775586753307},{"file_id":"https://huggingface.co/datasets/codeShare/lora-training-data/blob/main/gemma4_batch_captioner.ipynb","timestamp":1775585424465}]},"kernelspec":{"display_name":"Python 3","name":"python3"},"language_info":{"name":"python"}},"nbformat":4,"nbformat_minor":0}
|