🇨🇳 中文
Image Offline Distillation
🇺🇸 English | 🇮🇳 हिंदी | 🇯🇵 日本語 | 🇨🇳 简体中文 | 🇪🇸 Español | 🇧🇷 Português (Brasil) | 🇰🇷 한국어 | 🇩🇪 Deutsch | 🇫🇷 Français
一个小型 GitHub-ready template,用于体验从公开图像模型进行 offline knowledge distillation。
该项目保持简单的 full-stack 结构:
- Backend: FastAPI + PyTorch + torchvision
- Frontend: Expo / React Native Web
- Container: Docker Compose
- No Makefile
本项目学习什么
这个 repository 不会训练大型 diffusion model。它通过 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。它会被 train 来模仿 teacher 的 softened probability distribution。
为什么这是 offline distillation
关键 artifact 是:
artifacts/teacher_logits_train.pt
创建这个 file 之后,就可以在不再次 call teacher model 的情况下 train student。
Dataset modes
| Dataset | Purpose |
|---|---|
fake |
Smoke test。不需要真实 images。 |
cifar10 |
下载 CIFAR-10,并将其 resize 到 ImageNet input size。 |
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 访问。这适用于 Linux Docker environments。
Open:
Frontend: http://localhost:8081
Frontend direct Metro: http://localhost:8081
Backend: http://localhost:8000/docs
Frontend note
Frontend 仍然基于 Expo。Docker 会运行 expo export --platform web,然后在 0.0.0.0:19006 上 serve exported web build。这样可以避免 interactive Expo dev server 带来的 Docker networking problems,同时仍然使用 Expo 生成 web build。
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_agreementdistillation_klstudent_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
Distilling Stable Diffusion 等 text-to-image diffusion models 是更重的 task。它通常涉及 latent-space objectives、scheduler changes、multi-step teacher sampling,以及 GPU-heavy training。
这个 repository 是第一阶段:它通过 public image models 教你 offline logits-cache pattern。这个流程跑通之后,下一步是使用 LCM-LoRA 或 teacher latent predictions 创建 diffusion-specific branch。