Datasets:
Tags:
Not-For-All-Audiences
Upload dino_tagger.ipynb
Browse files- dino_tagger.ipynb +1 -0
dino_tagger.ipynb
ADDED
|
@@ -0,0 +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":["# =============================================\n","# Image Tagger - lodestones/tagger-experiment\n","# Upload images → Get zip with images + tags\n","# =============================================\n","\n","# 1. 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 os\n","import zipfile\n","from pathlib import Path\n","from google.colab import files\n","\n","# 2. Download the model files from Hugging Face\n","print(\"Downloading model files...\")\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","# 3. Upload images\n","print(\"\\nPlease upload your images (you can select multiple)...\")\n","uploaded = files.upload()\n","\n","# Get list of uploaded image files\n","image_paths = [f for f in uploaded.keys() if f.lower().endswith(('.png', '.jpg', '.jpeg', '.webp', '.bmp'))]\n","\n","if not image_paths:\n"," print(\"❌ No image files uploaded.\")\n","else:\n"," print(f\"✅ Uploaded {len(image_paths)} images.\")\n","\n","# 4. Prepare output directory\n","output_dir = Path(\"/content/output\")\n","output_dir.mkdir(exist_ok=True)\n","\n","# 5. Load the tagger\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=\"cuda\" if torch.cuda.is_available() else \"cpu\",\n",")\n","\n","print(f\"✅ Tagger loaded on {tagger.device}\")\n","\n","# 6. Process each image\n","print(\"\\nProcessing images...\")\n","\n","for i, img_path in enumerate(image_paths, start=1):\n"," print(f\"[{i}/{len(image_paths)}] Processing: {img_path}\")\n","\n"," # Predict tags (using top-k or threshold - adjust as needed)\n"," tags = tagger.predict(img_path, topk=50) # Returns list of (tag, prob) tuples\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"," # Save tags as text file (one tag per line, highest confidence first)\n"," tag_file = output_dir / f\"{i}.txt\"\n"," with open(tag_file, \"w\", encoding=\"utf-8\") as f:\n"," for tag, prob in tags:\n"," f.write(f\"{tag}\\n\") # You can also do f\"{tag} {prob:.4f}\\n\" if you want scores\n","\n"," print(f\" → Saved {i}.jpg and {i}.txt\")\n","\n","# 7. Create zip file\n","zip_path = \"/content/output.zip\"\n","\n","with zipfile.ZipFile(zip_path, \"w\") as zipf:\n"," for file in output_dir.iterdir():\n"," zipf.write(file, file.name)\n","\n","print(f\"\\n✅ All done! Output zip created at: {zip_path}\")\n","\n","# 8. Download the zip\n","files.download(zip_path)"],"metadata":{"id":"4lsdCf8NE2zp","outputId":"d96de383-f13f-43dc-fae7-cd680044d255","colab":{"base_uri":"https://localhost:8080/"}},"execution_count":null,"outputs":[{"metadata":{"tags":null},"name":"stdout","output_type":"stream","text":["Downloading model files...\n"]}]}],"metadata":{"colab":{"provenance":[{"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}
|