Image Offline Distillation

License OS Python

CodeQL Advanced Python Lint CI Pytest

Python Pytest FastAPI React Native TypeScript Jest Expo

🇺🇸 English | 🇮🇳 हिंदी | 🇯🇵 日本語 | 🇨🇳 简体中文 | 🇪🇸 Español | 🇧🇷 Português (Brasil) | 🇰🇷 한국어 | 🇩🇪 Deutsch | 🇫🇷 Français

सार्वजनिक इमेज मॉडल से offline knowledge distillation का अनुभव करने के लिए एक छोटा, GitHub-ready template।

यह प्रोजेक्ट एक सरल full-stack संरचना रखता है:

  • Backend: FastAPI + PyTorch + torchvision
  • Frontend: Expo / React Native Web
  • Container: Docker Compose
  • No Makefile

यह क्या सिखाता है

यह repository कोई बड़ा diffusion model train नहीं करती। यह public image classifier के साथ offline distillation pattern सिखाती है:

public ImageNet teacher model
  -> run once on images
  -> save teacher logits
  -> train a small CNN student from cached logits
  -> compare teacher/student agreement

Default teacher public ImageNet weights वाला torchvision.models.resnet18 है। आप resnet50 या mobilenet_v3_large भी उपयोग कर सकते हैं।

Student एक tiny CNN है जो वही 1000 ImageNet logits output करता है। इसे teacher के softened probability distribution की नकल करने के लिए train किया जाता है।

यह offline distillation क्यों है

मुख्य artifact है:

artifacts/teacher_logits_train.pt

यह file बनने के बाद, student को teacher model को फिर से call किए बिना train किया जा सकता है।

Dataset modes

Dataset Purpose
fake Smoke test. Real images की आवश्यकता नहीं होती।
cifar10 CIFAR-10 download करता है और उसे ImageNet input size में resize करता है।
image_folder data/images के अंदर आपकी अपनी unlabeled images का उपयोग करता है।

Real experiment के लिए, images यहाँ रखें:

data/images/

Nested folders की अनुमति है। Labels आवश्यक नहीं हैं।

Start

docker compose down -v
docker compose down -v
docker compose up --build

Frontend service Docker network_mode: host के साथ Expo Web का उपयोग करती है ताकि expo start --web --localhost --port 8081 host browser से reachable रहे। यह Linux Docker environments के लिए intended है।

Open:

Frontend: http://localhost:8081
Frontend direct Metro: http://localhost:8081
Backend:  http://localhost:8000/docs

Frontend note

Frontend Expo-based ही रहता है। Docker expo export --platform web चलाता है और फिर exported web build को 0.0.0.0:19006 पर serve करता है। इससे interactive Expo dev server के साथ Docker networking problems से बचा जाता है, जबकि web build के लिए Expo का उपयोग जारी रहता है।

Run from API

curl -X POST http://localhost:8000/api/v1/distillation/run-all   -H 'Content-Type: application/json'   -d '{
    "teacher": "resnet18",
    "dataset": "fake",
    "samples": 128,
    "batch_size": 16,
    "epochs": 2,
    "learning_rate": 0.001,
    "temperature": 3.0,
    "device": "cpu"
  }'

Run from CLI

docker compose run --rm backend python /app/cli.py run-all   --teacher resnet18   --dataset fake   --samples 128   --batch-size 16   --epochs 2   --temperature 3.0   --device cpu

अपना image folder उपयोग करने के लिए:

docker compose run --rm backend python /app/cli.py run-all   --teacher resnet18   --dataset image_folder   --samples 256   --epochs 3   --device cpu

Outputs

artifacts/
├── teacher_logits_train.pt
├── teacher_cache_metadata.json
├── student_model.pt
└── report.json

report.json में शामिल है:

  • teacher_student_top1_agreement
  • distillation_kl
  • student_parameters
  • training loss history

Tests

docker compose -f docker-compose.test.yml run --rm backend_test
docker compose -f docker-compose.yml -f docker-compose.test.yml run --rm frontend_test

Diffusion models के बारे में notes

Stable Diffusion जैसे text-to-image diffusion models को distill करना अधिक भारी task है। इसमें आम तौर पर latent-space objectives, scheduler changes, multi-step teacher sampling, और GPU-heavy training शामिल होते हैं।

यह repository पहला stage है: यह public image models के साथ offline logits-cache pattern सिखाती है। यह काम करने के बाद, अगला step LCM-LoRA या teacher latent predictions का उपयोग करके diffusion-specific branch बनाना है।