|
| 1 | +# Few-Shot Classifier Library |
| 2 | + |
| 3 | +A lightweight Python library for image classification using few-shot learning. Given a small number of support images per class, this library classifies query images based on visual similarity using learnable embeddings. |
| 4 | + |
| 5 | +## Features |
| 6 | + |
| 7 | +- No training required — works directly with pretrained backbones |
| 8 | +- Supports multiple popular models: ResNet, MobileNet, EfficientNet, DenseNet, and more |
| 9 | +- Accepts individual images or batches for prediction |
| 10 | +- **Automatically detects image type:** handles RGB and Grayscale images as required by the model |
| 11 | +- Returns class indices or original labels (if provided) |
| 12 | +- Easy integration with PyTorch pipelines |
| 13 | + |
| 14 | +## Installation |
| 15 | + |
| 16 | +Install directly from PyPI: |
| 17 | + |
| 18 | +``` |
| 19 | +
|
| 20 | +pip install fewshotlib |
| 21 | +
|
| 22 | +``` |
| 23 | + |
| 24 | +Or, if not yet published, install locally from source: |
| 25 | + |
| 26 | +``` |
| 27 | +
|
| 28 | +git clone https://github.com/RohitXJ/few-shot-lib.git |
| 29 | +cd fewshotlib |
| 30 | +pip install . |
| 31 | +
|
| 32 | +``` |
| 33 | + |
| 34 | +## How to Use |
| 35 | + |
| 36 | +``` |
| 37 | +
|
| 38 | +from fewshotlib import FewShotClassifier |
| 39 | +from PIL import Image |
| 40 | +
|
| 41 | +# Load a model checkpoint (must be generated using our model creation utility) |
| 42 | +
|
| 43 | +model_path = 'model.pt' |
| 44 | +classifier = FewShotClassifier(model_path) |
| 45 | +
|
| 46 | +# Predict from a single image file |
| 47 | +
|
| 48 | +result = classifier.predict('test_image.png') |
| 49 | +print(result) |
| 50 | +
|
| 51 | +# Predict from a batch of image files |
| 52 | +
|
| 53 | +results = classifier.predict(['img1.png', 'img2.png']) |
| 54 | +print(results) |
| 55 | +
|
| 56 | +# Predict and return the original class label (if available) |
| 57 | +
|
| 58 | +result = classifier.predict('test_image.png') |
| 59 | +print(result['label']) \# outputs the original label, if present |
| 60 | +
|
| 61 | +``` |
| 62 | + |
| 63 | +## Notes |
| 64 | + |
| 65 | +- If your `model.pt` file contains labels from the prototype creation step, you will receive the corresponding original label for each prediction. |
| 66 | +- The library automatically handles input images in RGB or Grayscale formats and applies the necessary preprocessing for the chosen backbone model. |
| 67 | +- Supports file paths to images (recommended). For power users, you may load a PIL image and pass it directly, but file path input is default. |
| 68 | + |
| 69 | +## Generate `model.pt` |
| 70 | + |
| 71 | +To create a compatible `model.pt` file for this library, use our companion tool (coming soon): |
| 72 | + |
| 73 | +``` |
| 74 | +
|
| 75 | +https://github.com/RohitXJ/few-shot-web |
| 76 | +
|
| 77 | +``` |
| 78 | + |
| 79 | +This tool lets you select a few images per class and generates a `.pt` file storing class prototypes and labels. |
| 80 | + |
| 81 | +## License |
| 82 | + |
| 83 | +This project is licensed under a **Custom Research and Educational Use License**. |
| 84 | + |
| 85 | +You are permitted to use, copy, modify, and distribute this software **only for research and educational purposes**. **Commercial use is strictly prohibited** without prior written permission from the author. |
| 86 | + |
| 87 | +See the [LICENSE](./LICENSE) file for full terms. |
| 88 | + |
| 89 | +For commercial inquiries, contact: [[email protected]](mailto:[email protected]) |
| 90 | + |
0 commit comments