微调并评估图像分类模型
从数据去重与分组划分入手,编写分阶段训练代码,输出分类评估、错例分析和预测测试。
完整提示词
<context>
Fine-tuning a pretrained backbone is the right default for custom image classification, and most of the work is around the data. Near-duplicate photos (burst shots, the same object from slightly different angles, frames from one video) split randomly across train and test give scores that collapse on new images; split by source instead. Models also learn shortcuts: the camera, the background, a watermark or the hospital's label marker, which the misclassification review exposes. Augmentations must preserve the label: horizontal flips are wrong for text, digits or left-right anatomy, and heavy colour jitter is wrong when colour is the signal. Inputs must be resized and normalised the way the backbone was pretrained. With little data, train the head first and then unfreeze with a lower learning rate for the backbone. And EXIF orientation is often ignored, so some photos train sideways.
</context>
<task>
Fine-tune an image classifier for: {{task}}
Data: {{data}}
{{#deployment}}
Deployment: {{deployment}}
{{/deployment}}
1. Read the existing code and dependencies; use the image library already present (torchvision, timm or similar).
2. Check the data: counts per class, corrupt or unreadable files, EXIF orientation, very small images, exact and near duplicates (perceptual hash), and the source of each image. Choose a split grouped by source (video, patient, device, session or object) and stratified by class; lock the test set.
3. Pick a backbone that fits the deployment limits and the data size, use its expected input size and normalisation, and say why.
4. Set augmentation that preserves labels for this domain, and say which common augmentations were excluded and why.
5. Train in two phases: head only for a few epochs, then unfreeze with a lower backbone learning rate, cosine or step schedule, mixed precision where supported, early stopping on validation macro-F1 or balanced accuracy. Handle class imbalance with a weighted loss or sampler.
6. Evaluate on validation and, once, on test: per-class precision and recall, confusion matrix, and macro and overall scores against a simple baseline (for example a linear probe on frozen features).
7. Review misclassifications: a grid of the most confident errors, grouped by cause (label error, ambiguous image, shortcut such as background or device, true model error). Check one suspected shortcut with a quick test (for example cropping out the background or slicing by device).
8. Save the model with class names, input size and normalisation; write a prediction function and a test that loads the model and predicts a fixed image. Run the tests.
</task>
<constraints>
- No random split when the same source appears in several images.
- Do not report accuracy alone when classes are imbalanced.
- Do not start long GPU runs without stating the expected time; run a short smoke training first.
{{> guardrails/scope-discipline}}
{{> guardrails/verify-before-done}}
</constraints>
<output_format>
## Data check
Counts, duplicates, sources, split choice.
## Setup
Backbone, input size, augmentation kept and excluded, schedule.
## Code
Code blocks with file paths.
## Results
Table: class | precision | recall | support, plus overall against the baseline.
## Misclassification review
Error causes with counts and example file names.
## Check
Commands run and real results.
## Next steps
Up to three, ranked.
</output_format>可替换内容
- {{task}}
- {{data}}
- {{#deployment}}
- {{deployment}}
- {{/deployment}}
- {{> guardrails/scope-discipline}}
- {{> guardrails/verify-before-done}}
分类与标签
编程开发、图像分类、模型微调、数据划分、错例分析、模型评估