torch
torchvision
tqdm
This implementation is inspired by "Model-Agnostic Meta-Learning for Fast Adaptation of Deep Networks" (2017).
- task: classifying image with few dataset.
- dataset: downloaded from
torch
dataset library.
Model-Agnostic Meta-Learning for Few-Shot Image Classification
This repository implements a Model-Agnostic Meta-Learning (MAML) algorithm for few-shot image classification tasks using PyTorch. MAML is designed to address the challenge of adapting to new tasks with limited examples by learning an initialization that enables fast adaptation with minimal gradient steps.
Few-shot learning focuses on enabling models to generalize to new tasks with only a few labeled examples. MAML achieves this by optimizing for a set of parameters that can quickly adapt to new tasks through gradient-based updates, allowing the model to efficiently learn from limited data.
You can access the full documentation here: gitbook
You can access the test result on colab here: colab
Instruction
Organize your dataset into a structure compatible with PyTorch's ImageFolder:
dataset/
βββ class1/
β βββ img1.jpg
β βββ img2.jpg
β βββ ...
βββ class2/
β βββ img1.jpg
β βββ img2.jpg
β βββ ...
βββ ...
Training
Run the training script with desired parameters:
python run.py train --dataset_path path/to/your/dataset --save_to /path/to/save/model --n_way 5 --k_shot 2 --n_query 4 --epochs 1 --iters 4
path
: Path to your dataset.save_to
: path to save the trained model.n_way
: number of classes in each episode.k_shot
: Number of support samples per class.n-_query
: Number of query samples per class.
change training configuration from
config.py
Evaluation
python run.py --dataset path/to/your/dataset --model path/to/saved/model.pth --n_way 5
# output example:
# seen classes: [10, 11, 18, 1, 3]
# unseen classes: [19, 1, 4, 2, 13]
# accuracy: 1.0000(10/10)
model
: Path to your model.dataset
: Path to your dataset.
Download Omniglot Dataset
pyhton download --path ./somewhre/your/dataset/
path
: Path to your dataset.
More Explanation
Model-Agnostic Meta-Learning(MAML) is a powerful approach for few-shot learning, where the objective is to enable rapid adaptation to new tasks with very few labeled examples. The key idea behind MAML is to learn a model initialization that can be quickly fine-tuned on new tasks using only a small number of gradient updates, allowing efficient generalization to unseen data.
- Task-Specific Adaptation: Instead of learning fixed representations, MAML optimizes model parameters that can be rapidly adapted to different tasks with gradient-based updates.
- Inner-Loop Optimization: For each task, the model is fine-tuned on a small support set using a few gradient steps to minimize task-specific loss.
- Meta-Update (Outer Loop): After task-specific updates, gradients are computed based on query set performance, and the initial model parameters are updated to improve adaptability across tasks.
- Optimization: The model is trained using second-order gradient updates (or first-order approximations) to optimize for fast adaptation while maintaining generalization ability.