{ "cells": [ { "cell_type": "markdown", "metadata": {}, "source": [ "# GroundingDINO: Setup, Installation & Model Download\n", "\n", "## Notebook 09 of 14\n", "\n", "This notebook covers:\n", "- Environment setup\n", "- Installing GroundingDINO\n", "- Downloading model weights from HuggingFace\n", "- Verifying the installation\n", "\n", "---" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 1. Environment Setup" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import subprocess, sys, os\n", "\n", "# Check Python and CUDA\n", "print(f\"Python: {sys.version}\")\n", "try:\n", " import torch\n", " print(f\"PyTorch: {torch.__version__}\")\n", " print(f\"CUDA available: {torch.cuda.is_available()}\")\n", " if torch.cuda.is_available():\n", " print(f\"CUDA version: {torch.version.cuda}\")\n", " print(f\"GPU: {torch.cuda.get_device_name(0)}\")\n", " print(f\"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1e9:.1f} GB\")\n", "except ImportError:\n", " print(\"PyTorch not installed\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 2. Install GroundingDINO" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import os\n", "\n", "# Create model directory\n", "MODEL_DIR = \"/mnt/mydata/huggingface\"\n", "os.makedirs(MODEL_DIR, exist_ok=True)\n", "print(f\"Model directory: {MODEL_DIR}\")\n", "\n", "# Install GroundingDINO\n", "gdino_dir = os.path.join(MODEL_DIR, \"GroundingDINO\")\n", "\n", "if not os.path.exists(gdino_dir):\n", " print(\"Cloning GroundingDINO...\")\n", " os.system(f\"git clone https://github.com/IDEA-Research/GroundingDINO.git {gdino_dir}\")\n", "else:\n", " print(f\"GroundingDINO already cloned at {gdino_dir}\")\n", "\n", "# Install\n", "print(\"Installing GroundingDINO...\")\n", "result = os.system(f\"cd {gdino_dir} && pip install -e . -q\")\n", "print(f\"Installation result: {'Success' if result == 0 else 'Failed'}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Install additional dependencies\n", "deps = [\n", " \"transformers>=4.30.0\",\n", " \"timm\",\n", " \"supervision\",\n", " \"huggingface_hub\",\n", " \"pycocotools\",\n", "]\n", "\n", "for dep in deps:\n", " result = os.system(f\"pip install {dep} -q\")\n", " status = '✓' if result == 0 else '✗'\n", " print(f\" {status} {dep}\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 3. Download Model Weights from HuggingFace" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "from huggingface_hub import hf_hub_download, snapshot_download\n", "import os\n", "\n", "MODEL_DIR = \"/mnt/mydata/huggingface\"\n", "os.makedirs(MODEL_DIR, exist_ok=True)\n", "\n", "# Download GroundingDINO-T (Tiny) weights\n", "print(\"Downloading GroundingDINO-T weights...\")\n", "try:\n", " gdino_t_path = hf_hub_download(\n", " repo_id=\"ShilongLiu/GroundingDINO\",\n", " filename=\"groundingdino_swint_ogc.pth\",\n", " local_dir=MODEL_DIR\n", " )\n", " print(f\"✓ GroundingDINO-T saved to: {gdino_t_path}\")\n", " size_mb = os.path.getsize(gdino_t_path) / 1e6\n", " print(f\" File size: {size_mb:.1f} MB\")\n", "except Exception as e:\n", " print(f\"✗ Error: {e}\")\n", " print(\" Trying alternative download...\")\n", " # Alternative: direct URL\n", " url = \"https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha/groundingdino_swint_ogc.pth\"\n", " gdino_t_path = os.path.join(MODEL_DIR, \"groundingdino_swint_ogc.pth\")\n", " os.system(f\"wget -q {url} -O {gdino_t_path}\")\n", " if os.path.exists(gdino_t_path):\n", " print(f\"✓ Downloaded to {gdino_t_path}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Download GroundingDINO-B (Base) weights\n", "print(\"Downloading GroundingDINO-B weights...\")\n", "try:\n", " gdino_b_path = hf_hub_download(\n", " repo_id=\"ShilongLiu/GroundingDINO\",\n", " filename=\"groundingdino_swinb_cogcoor.pth\",\n", " local_dir=MODEL_DIR\n", " )\n", " print(f\"✓ GroundingDINO-B saved to: {gdino_b_path}\")\n", " size_mb = os.path.getsize(gdino_b_path) / 1e6\n", " print(f\" File size: {size_mb:.1f} MB\")\n", "except Exception as e:\n", " print(f\"✗ Error: {e}\")\n", " url = \"https://github.com/IDEA-Research/GroundingDINO/releases/download/v0.1.0-alpha2/groundingdino_swinb_cogcoor.pth\"\n", " gdino_b_path = os.path.join(MODEL_DIR, \"groundingdino_swinb_cogcoor.pth\")\n", " os.system(f\"wget -q {url} -O {gdino_b_path}\")\n", " if os.path.exists(gdino_b_path):\n", " print(f\"✓ Downloaded to {gdino_b_path}\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Download config files\n", "gdino_dir = os.path.join(MODEL_DIR, \"GroundingDINO\")\n", "\n", "config_t = os.path.join(gdino_dir, \"groundingdino/config/GroundingDINO_SwinT_OGC.py\")\n", "config_b = os.path.join(gdino_dir, \"groundingdino/config/GroundingDINO_SwinB_cfg.py\")\n", "\n", "print(\"Config files:\")\n", "for cfg, name in [(config_t, 'Swin-T config'), (config_b, 'Swin-B config')]:\n", " exists = os.path.exists(cfg)\n", " print(f\" {'✓' if exists else '✗'} {name}: {cfg}\")\n", "\n", "# List all downloaded files\n", "print(f\"\\nFiles in {MODEL_DIR}:\")\n", "for f in sorted(os.listdir(MODEL_DIR)):\n", " fpath = os.path.join(MODEL_DIR, f)\n", " if os.path.isfile(fpath):\n", " size = os.path.getsize(fpath) / 1e6\n", " print(f\" {f} ({size:.1f} MB)\")\n", " else:\n", " print(f\" {f}/ (directory)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 4. Load and Verify Model" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "import sys\n", "import torch\n", "\n", "MODEL_DIR = \"/mnt/mydata/huggingface\"\n", "gdino_dir = os.path.join(MODEL_DIR, \"GroundingDINO\")\n", "\n", "# Add to path\n", "if gdino_dir not in sys.path:\n", " sys.path.insert(0, gdino_dir)\n", "\n", "try:\n", " from groundingdino.util.inference import load_model, load_image, predict, annotate\n", " print(\"✓ GroundingDINO imported successfully\")\n", " \n", " # Load model\n", " config_path = os.path.join(gdino_dir, \"groundingdino/config/GroundingDINO_SwinT_OGC.py\")\n", " weights_path = os.path.join(MODEL_DIR, \"groundingdino_swint_ogc.pth\")\n", " \n", " if os.path.exists(weights_path) and os.path.exists(config_path):\n", " model = load_model(config_path, weights_path)\n", " model.eval()\n", " print(f\"✓ Model loaded successfully\")\n", " \n", " # Count parameters\n", " total_params = sum(p.numel() for p in model.parameters())\n", " trainable_params = sum(p.numel() for p in model.parameters() if p.requires_grad)\n", " print(f\" Total parameters: {total_params:,} ({total_params/1e6:.1f}M)\")\n", " print(f\" Trainable: {trainable_params:,} ({trainable_params/1e6:.1f}M)\")\n", " else:\n", " print(f\"✗ Model files not found\")\n", " print(f\" Config: {config_path} - {'exists' if os.path.exists(config_path) else 'MISSING'}\")\n", " print(f\" Weights: {weights_path} - {'exists' if os.path.exists(weights_path) else 'MISSING'}\")\n", " \n", "except ImportError as e:\n", " print(f\"✗ Import error: {e}\")\n", " print(\" Make sure GroundingDINO is installed: pip install -e /mnt/mydata/huggingface/GroundingDINO\")" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Quick sanity check - run inference on a test image\n", "import numpy as np\n", "from PIL import Image\n", "import matplotlib.pyplot as plt\n", "\n", "# Download a test image\n", "test_img_path = os.path.join(MODEL_DIR, \"test_image.jpg\")\n", "if not os.path.exists(test_img_path):\n", " os.system(f\"wget -q http://images.cocodataset.org/val2017/000000039769.jpg -O {test_img_path}\")\n", "\n", "if os.path.exists(test_img_path):\n", " img = Image.open(test_img_path)\n", " print(f\"Test image: {img.size} ({img.mode})\")\n", " \n", " plt.figure(figsize=(8, 6))\n", " plt.imshow(img)\n", " plt.title('Test Image for GroundingDINO', fontsize=12, fontweight='bold')\n", " plt.axis('off')\n", " plt.tight_layout()\n", " plt.savefig('test_image_display.png', dpi=150, bbox_inches='tight')\n", " plt.show()\n", "else:\n", " print(\"Could not download test image\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## 5. Model Architecture Summary" ] }, { "cell_type": "code", "execution_count": null, "metadata": {}, "outputs": [], "source": [ "# Print model architecture if loaded\n", "try:\n", " print(\"GroundingDINO Model Architecture:\")\n", " print(\"=\" * 60)\n", " \n", " # Print top-level modules\n", " for name, module in model.named_children():\n", " params = sum(p.numel() for p in module.parameters())\n", " print(f\" {name}: {type(module).__name__} ({params/1e6:.1f}M params)\")\n", " \n", " # Print sub-modules\n", " for sub_name, sub_module in module.named_children():\n", " sub_params = sum(p.numel() for p in sub_module.parameters())\n", " if sub_params > 0:\n", " print(f\" {sub_name}: {type(sub_module).__name__} ({sub_params/1e6:.2f}M)\")\n", " \n", "except NameError:\n", " print(\"Model not loaded. Run the previous cells first.\")\n", " print(\"\\nExpected architecture:\")\n", " print(\" backbone: SwinTransformer\")\n", " print(\" bert: BertModel\")\n", " print(\" feat_map: FeatureEnhancer\")\n", " print(\" transformer: GroundingDINOTransformer\")\n", " print(\" encoder: GroundingDINOTransformerEncoder\")\n", " print(\" decoder: GroundingDINOTransformerDecoder\")\n", " print(\" bbox_embed: MLP (box head)\")\n", " print(\" class_embed: Linear (contrastive head)\")" ] }, { "cell_type": "markdown", "metadata": {}, "source": [ "## Summary\n", "\n", "Setup checklist:\n", "- ✓ Clone GroundingDINO from GitHub\n", "- ✓ Install with `pip install -e .`\n", "- ✓ Download weights from HuggingFace to `/mnt/mydata/huggingface/`\n", "- ✓ Verify model loads correctly\n", "\n", "Model files:\n", "- `groundingdino_swint_ogc.pth` — Swin-T backbone (~172M params)\n", "- `groundingdino_swinb_cogcoor.pth` — Swin-B backbone (~253M params)\n", "\n", "**Next**: [Notebook 10 - Inference & Tensor Shapes](10_inference_tensor_shapes.ipynb)" ] } ], "metadata": { "kernelspec": { "display_name": "Python 3", "language": "python", "name": "python3" }, "language_info": { "name": "python", "version": "3.10.0" } }, "nbformat": 4, "nbformat_minor": 4 }