From 7a74516dc58b4b9e10eb507a478a72ae400e9918 Mon Sep 17 00:00:00 2001 From: Scott Register Date: Sat, 26 Jul 2025 07:35:44 -0700 Subject: [PATCH] commit2 runpod --- Dockerfile | 39 ++++++++ RUNPOD_DEPLOYMENT.md | 216 +++++++++++++++++++++++++++++++++++++++++++ config_runpod.yaml | 26 ++++++ docker-compose.yml | 17 ++++ requirements.txt | 2 +- runpod_setup.sh | 71 ++++++++++++++ setup.py | 5 +- test_installation.py | 64 +++++++++++++ 8 files changed, 438 insertions(+), 2 deletions(-) create mode 100644 Dockerfile create mode 100644 RUNPOD_DEPLOYMENT.md create mode 100644 config_runpod.yaml create mode 100644 docker-compose.yml create mode 100644 runpod_setup.sh create mode 100644 test_installation.py diff --git a/Dockerfile b/Dockerfile new file mode 100644 index 0000000..6a36a00 --- /dev/null +++ b/Dockerfile @@ -0,0 +1,39 @@ +FROM runpod/pytorch:2.4.0-py3.11-cuda12.4.1-devel-ubuntu22.04 + +# Set working directory +WORKDIR /app + +# Install system dependencies +RUN apt-get update && apt-get install -y \ + ffmpeg \ + libsm6 \ + libxext6 \ + libxrender-dev \ + libglib2.0-0 \ + git \ + && rm -rf /var/lib/apt/lists/* + +# Copy requirements first for better caching +COPY requirements.txt . + +# Install Python dependencies +RUN pip install --no-cache-dir -r requirements.txt + +# Install SAM2 (if not in requirements.txt) +RUN pip install git+https://github.com/facebookresearch/segment-anything-2.git + +# Copy project files +COPY . . + +# Install project in development mode +RUN pip install -e . + +# Create directories for models and data +RUN mkdir -p /app/models /app/data /app/output + +# Set environment variables +ENV PYTHONPATH=/app +ENV CUDA_VISIBLE_DEVICES=0 + +# Default command +CMD ["/bin/bash"] \ No newline at end of file diff --git a/RUNPOD_DEPLOYMENT.md b/RUNPOD_DEPLOYMENT.md new file mode 100644 index 0000000..3ac2914 --- /dev/null +++ b/RUNPOD_DEPLOYMENT.md @@ -0,0 +1,216 @@ +# RunPod Deployment Guide + +## Quick Start (Recommended) + +### 1. Create RunPod Instance +- **Template**: `runpod/pytorch:2.4.0-py3.11-cuda12.4.1-devel-ubuntu22.04` +- **GPU**: NVIDIA A40 (48GB VRAM) +- **Storage**: 50GB+ (for videos and models) +- **Persistent Storage**: Recommended for model caching + +### 2. Connect and Setup +```bash +# After SSH/Terminal access +cd /workspace + +# Clone your repository +git clone https://github.com/YOUR_USERNAME/sam2e.git +cd sam2e + +# Make setup script executable and run +chmod +x runpod_setup.sh +./runpod_setup.sh + +# Install SAM2 separately (not on PyPI) +pip install git+https://github.com/facebookresearch/segment-anything-2.git +``` + +### 3. Upload Your Video +```bash +# Option 1: wget from URL +wget -O /workspace/data/myvideo.mp4 "https://your-video-url.com/video.mp4" + +# Option 2: Use RunPod's file browser +# Upload to /workspace/data/ + +# Option 3: rclone from cloud storage +rclone copy remote:path/to/video.mp4 /workspace/data/ +``` + +### 4. Configure and Run +```bash +# Use RunPod-optimized config +cp config_runpod.yaml config.yaml + +# Edit video path +nano config.yaml # Update video_path + +# Run processing +vr180-matting config.yaml +``` + +## Advanced Docker Deployment + +### Option 1: Pre-built Docker Image +```bash +# Build and push to Docker Hub (do this locally first) +docker build -t yourusername/vr180-matting:latest . +docker push yourusername/vr180-matting:latest + +# On RunPod, use your image as template +``` + +### Option 2: Build on RunPod +```bash +cd /workspace/sam2e +docker build -t vr180-matting . +docker run --gpus all -v /workspace/data:/app/data -v /workspace/output:/app/output -it vr180-matting +``` + +## Performance Tips for A40 + +### Optimal Settings +```yaml +processing: + scale_factor: 0.75 # A40 can handle higher resolution + chunk_size: 0 # Let it auto-calculate for 48GB + +detection: + model: "yolov8m" # or yolov8l for better accuracy + +matting: + memory_offload: false # Plenty of VRAM + fp16: true # Still use FP16 for speed +``` + +### Expected Performance +- **50% scale**: ~10-15 FPS processing +- **75% scale**: ~6-10 FPS processing +- **100% scale**: ~4-6 FPS processing + +### Memory Monitoring +```bash +# Watch GPU usage while processing +watch -n 1 nvidia-smi + +# Or in another terminal +nvidia-smi dmon -s um +``` + +## Batch Processing Script + +Create `batch_process.py` for multiple videos: +```python +import os +import sys +from pathlib import Path +from vr180_matting.config import VR180Config +from vr180_matting.vr180_processor import VR180Processor + +# Directory setup +input_dir = Path("/workspace/data/videos") +output_dir = Path("/workspace/output") +base_config = "config_runpod.yaml" + +for video_file in input_dir.glob("*.mp4"): + print(f"Processing: {video_file.name}") + + # Load base config + config = VR180Config.from_yaml(base_config) + + # Update paths + config.input.video_path = str(video_file) + config.output.path = str(output_dir / f"matted_{video_file.stem}.mp4") + + # Process + processor = VR180Processor(config) + processor.process_video() + + print(f"Completed: {video_file.name}\n") +``` + +## Cost Optimization + +### RunPod Spot Instances +- A40 spot: ~$0.44/hour vs $0.79/hour on-demand +- Perfect for batch processing +- Add persistence: $0.10/GB/month for model storage + +### Processing Time Estimates +- 30s clip @ 50% scale: ~10 minutes = ~$0.07 +- 1 hour video @ 50% scale: ~12 hours = ~$5.28 +- 1 hour video @ 25% scale: ~6 hours = ~$2.64 + +### Auto-shutdown Script +```bash +# Add to end of processing script +echo "Processing complete, shutting down in 60 seconds..." +sleep 60 +runpodctl stop instance +``` + +## Troubleshooting + +### SAM2 Model Download Issues +```bash +# Manual download +cd /workspace/sam2e/models +wget https://dl.fbaipublicfiles.com/segment_anything_2/sam2_hiera_large.pt +``` + +### CUDA Version Mismatch +```bash +# Check CUDA version +nvcc --version +python -c "import torch; print(torch.version.cuda)" + +# Reinstall PyTorch if needed +pip install torch torchvision --index-url https://download.pytorch.org/whl/cu121 +``` + +### Out of Memory on A40 +```yaml +# Unlikely, but if it happens: +processing: + scale_factor: 0.25 + chunk_size: 300 +matting: + memory_offload: true +``` + +## Monitoring Dashboard + +Create `monitor.py` for real-time stats: +```python +import psutil +import GPUtil +import time + +while True: + # GPU stats + gpus = GPUtil.getGPUs() + for gpu in gpus: + print(f"GPU: {gpu.memoryUsed}MB / {gpu.memoryTotal}MB ({gpu.memoryUtil*100:.1f}%)") + + # CPU/RAM stats + print(f"RAM: {psutil.virtual_memory().percent}%") + print(f"CPU: {psutil.cpu_percent()}%") + print("-" * 40) + time.sleep(2) +``` + +## Quick Commands Reference + +```bash +# Test on short clip +vr180-matting config.yaml --dry-run + +# Process with monitoring +vr180-matting config.yaml --verbose + +# Override settings +vr180-matting config.yaml --scale 0.25 --format greenscreen + +# Generate config +vr180-matting --generate-config my_config.yaml +``` \ No newline at end of file diff --git a/config_runpod.yaml b/config_runpod.yaml new file mode 100644 index 0000000..ae13aa5 --- /dev/null +++ b/config_runpod.yaml @@ -0,0 +1,26 @@ +input: + video_path: "/workspace/data/input_video.mp4" + +processing: + scale_factor: 0.5 # A40 can handle 0.5 well + chunk_size: 0 # Auto-calculate based on A40's 48GB VRAM + overlap_frames: 60 + +detection: + confidence_threshold: 0.7 + model: "yolov8m" # Use medium model on A40 + +matting: + use_disparity_mapping: true + memory_offload: false # A40 has enough VRAM + fp16: true + +output: + path: "/workspace/output/matted_video.mp4" + format: "alpha" + background_color: [0, 255, 0] + maintain_sbs: true + +hardware: + device: "cuda" + max_vram_gb: 45 # A40 has 48GB, leave headroom \ No newline at end of file diff --git a/docker-compose.yml b/docker-compose.yml new file mode 100644 index 0000000..7f32bbd --- /dev/null +++ b/docker-compose.yml @@ -0,0 +1,17 @@ +version: '3.8' + +services: + vr180-matting: + build: . + runtime: nvidia + environment: + - NVIDIA_VISIBLE_DEVICES=all + - CUDA_VISIBLE_DEVICES=0 + volumes: + - ./data:/app/data # Mount data directory + - ./output:/app/output # Mount output directory + - ./models:/app/models # Mount models directory + working_dir: /app + stdin_open: true + tty: true + command: /bin/bash \ No newline at end of file diff --git a/requirements.txt b/requirements.txt index 3765e22..cddad16 100644 --- a/requirements.txt +++ b/requirements.txt @@ -5,7 +5,7 @@ numpy>=1.24.0 pillow>=10.0.0 pyyaml>=6.0 ultralytics>=8.0.0 -sam2>=1.0.0 +# sam2>=1.0.0 # Install via git: pip install git+https://github.com/facebookresearch/segment-anything-2.git tqdm>=4.65.0 psutil>=5.9.0 ffmpeg-python>=0.2.0 \ No newline at end of file diff --git a/runpod_setup.sh b/runpod_setup.sh new file mode 100644 index 0000000..7f018d6 --- /dev/null +++ b/runpod_setup.sh @@ -0,0 +1,71 @@ +#!/bin/bash +# RunPod Quick Setup Script + +echo "๐Ÿš€ Setting up VR180 Matting on RunPod..." +echo "GPU: $(nvidia-smi --query-gpu=name --format=csv,noheader)" +echo "" + +# Update system +echo "๐Ÿ“ฆ Installing system dependencies..." +apt-get update && apt-get install -y ffmpeg git wget nano + +# Clone your repository (replace with your actual repo URL) +cd /workspace +if [ ! -d "sam2e" ]; then + echo "๐Ÿ“ฅ Cloning repository..." + git clone https://git.10n.app/scott/test2.git +fi +cd sam2e + +# Install Python dependencies +echo "๐Ÿ Installing Python dependencies..." +pip install --upgrade pip +pip install -r requirements.txt + +# Install SAM2 separately (not on PyPI) +echo "๐ŸŽฏ Installing SAM2..." +pip install git+https://github.com/facebookresearch/segment-anything-2.git + +# Install project +echo "๐Ÿ“ฆ Installing VR180 matting package..." +pip install -e . + +# Download models +echo "๐Ÿ“ฅ Downloading models..." +mkdir -p models + +# Download YOLOv8 models +python -c "from ultralytics import YOLO; YOLO('yolov8n.pt'); YOLO('yolov8m.pt')" + +# Download SAM2 checkpoint +cd models +if [ ! -f "sam2_hiera_large.pt" ]; then + echo "Downloading SAM2 model weights..." + wget -q --show-progress https://dl.fbaipublicfiles.com/segment_anything_2/sam2_hiera_large.pt +fi +cd .. + +# Create working directories +mkdir -p /workspace/data /workspace/output + +# Test installation +echo "" +echo "๐Ÿงช Testing installation..." +python test_installation.py + +echo "" +echo "โœ… Setup complete!" +echo "" +echo "๐Ÿ“ Quick start:" +echo "1. Upload your VR180 video to /workspace/data/" +echo " wget -O /workspace/data/video.mp4 'your-video-url'" +echo "" +echo "2. Use the RunPod optimized config:" +echo " cp config_runpod.yaml config.yaml" +echo " nano config.yaml # Update video path" +echo "" +echo "3. Run the matting:" +echo " vr180-matting config.yaml" +echo "" +echo "๐Ÿ’ก For A40 GPU, you can use higher quality settings:" +echo " vr180-matting config.yaml --scale 0.75" diff --git a/setup.py b/setup.py index 91b0e3c..14c52c0 100644 --- a/setup.py +++ b/setup.py @@ -13,11 +13,14 @@ setup( "pillow>=10.0.0", "pyyaml>=6.0", "ultralytics>=8.0.0", - "sam2>=1.0.0", + # "sam2>=1.0.0", # Install separately: pip install git+https://github.com/facebookresearch/segment-anything-2.git "tqdm>=4.65.0", "psutil>=5.9.0", "ffmpeg-python>=0.2.0", ], + dependency_links=[ + "git+https://github.com/facebookresearch/segment-anything-2.git#egg=sam2" + ], entry_points={ "console_scripts": [ "vr180-matting=vr180_matting.main:main", diff --git a/test_installation.py b/test_installation.py new file mode 100644 index 0000000..d59c2ed --- /dev/null +++ b/test_installation.py @@ -0,0 +1,64 @@ +#!/usr/bin/env python3 +"""Test script to verify installation and GPU setup""" + +import sys +import torch +import cv2 +import numpy as np + +print("VR180 Matting Installation Test") +print("=" * 50) + +# Check Python version +print(f"Python version: {sys.version}") + +# Check PyTorch and CUDA +print(f"\nPyTorch version: {torch.__version__}") +print(f"CUDA available: {torch.cuda.is_available()}") +if torch.cuda.is_available(): + print(f"CUDA version: {torch.version.cuda}") + print(f"GPU: {torch.cuda.get_device_name(0)}") + print(f"GPU Memory: {torch.cuda.get_device_properties(0).total_memory / 1024**3:.1f} GB") + +# Check OpenCV +print(f"\nOpenCV version: {cv2.__version__}") + +# Test imports +try: + from ultralytics import YOLO + print("\nโœ… YOLO import successful") +except ImportError as e: + print(f"\nโŒ YOLO import failed: {e}") + +try: + from sam2.build_sam import build_sam2_video_predictor + print("โœ… SAM2 import successful") +except ImportError as e: + print(f"โŒ SAM2 import failed: {e}") + print(" Install with: pip install git+https://github.com/facebookresearch/segment-anything-2.git") + +try: + from vr180_matting.config import VR180Config + from vr180_matting.detector import YOLODetector + from vr180_matting.sam2_wrapper import SAM2VideoMatting + from vr180_matting.memory_manager import VRAMManager + print("โœ… VR180 matting modules import successful") +except ImportError as e: + print(f"โŒ VR180 matting import failed: {e}") + print(" Make sure to run: pip install -e .") + +# Memory check +if torch.cuda.is_available(): + print(f"\n๐Ÿ“Š Current GPU Memory Usage:") + print(f" Allocated: {torch.cuda.memory_allocated() / 1024**3:.2f} GB") + print(f" Reserved: {torch.cuda.memory_reserved() / 1024**3:.2f} GB") + print(f" Free: {(torch.cuda.get_device_properties(0).total_memory - torch.cuda.memory_reserved()) / 1024**3:.2f} GB") + +print("\n" + "=" * 50) +print("Installation test complete!") +print("\nNext steps:") +print("1. If any imports failed, install missing dependencies") +print("2. Download SAM2 model weights if needed") +print("3. Run: vr180-matting --generate-config config.yaml") +print("4. Edit config.yaml with your video path") +print("5. Run: vr180-matting config.yaml") \ No newline at end of file