A semi-supervised learning library using iterative pseudo-labeling.
Project description
SOSLR: Semi-Orchestration-Supervised Learning
A simple and effective semi-supervised learning library for image classification, built with PyTorch.
SOSLR implements an iterative pseudo-labeling approach. Starting with a small set of labeled images and a large pool of unlabeled images, it trains a model, uses it to predict "pseudo-labels" for chunks of the unlabeled data, and then retrains on the combined set. This process repeats, improving the model with each round.
Table of Contents
Features
- Simple API: A clean, function-based API with
trainandpredict. - Iterative Pseudo-Labeling: Leverages unlabeled data to improve model accuracy.
- Transfer Learning: Uses pretrained backbones from
torchvision(e.g., DenseNet, ResNet). - Automatic Artifacts: Automatically saves the best model, class mappings, and transforms for easy prediction.
- Early Stopping: Monitors validation accuracy to prevent overfitting and stops when a target is reached.
Project & Data Structure
To use soslr, we recommend the following folder structure for your project:
your-project/
│
├── data/
│ ├── labeled/ # Your initial labeled images
│ │ ├── class_a/
│ │ │ ├── img1.jpg
│ │ │ └── img2.png
│ │ └── class_b/
│ │ └── img3.jpg
│ │
│ └── unlabeled/ # Your pool of unlabeled images
│ ├── img_x.jpg
│ └── img_y.png
│
├── images_to_predict/ # New images for prediction later
│ ├── new_img1.jpg
│ └── new_img2.png
│
└── sos_model/ # OUTPUT: Where the model will be saved
├── best_model.pth
└── class_mapping.json
Installation
You can install it using pip:
pip install soslr
Quick Start
Here’s a complete example from training to prediction.
1. Training
Create a Python script (e.g., run_training.py) to start the training process. The library will use your labeled and unlabeled data, evaluate performance on a validation set, and save the best-performing model to the output_dir.
# run_training.py
import soslr
# Point to your data directories
LABELED_DATA_DIR = 'data/labeled'
UNLABELED_DATA_DIR = 'data/unlabeled'
MODEL_OUTPUT_DIR = 'sos_model'
print("Starting training...")
# This will run the full training loop and save the best model
final_accuracy = soslr.train(
labeled_dir=LABELED_DATA_DIR,
unlabeled_dir=UNLABELED_DATA_DIR,
output_dir=MODEL_OUTPUT_DIR,
model_name='resnet50', # Use a ResNet-50 backbone
k=5, # Split unlabeled data into 5 chunks per round
pseudo_epochs=1, # Train for 1 epoch on each pseudo-labeled chunk
target_acc=0.90 # Stop if validation accuracy reaches 90%
)
print(f"Training complete. Final test accuracy: {final_accuracy:.4f}")
2. Prediction
After training, you can use the saved model in sos_model/ to make predictions on new, unseen images.
Create another script (e.g., run_prediction.py):
# run_prediction.py
import soslr
import pprint
# Point to the new images and the saved model directory
IMAGES_TO_PREDICT_DIR = 'images_to_predict'
MODEL_DIR = 'sos_model'
print(f"Loading model from '{MODEL_DIR}' to predict images in '{IMAGES_TO_PREDICT_DIR}'...")
# Get predictions
predictions = soslr.predict(
images_dir=IMAGES_TO_PREDICT_DIR,
model_dir=MODEL_DIR,
model_name='resnet50' # Must match the model used for training
)
print("\nPrediction Results:")
pprint.pprint(predictions)
# Example Output:
# {'new_img1.jpg': 'class_a',
# 'new_img2.png': 'class_b'}
API Reference
soslr.train(...)
Trains a semi-supervised model and saves the training artifacts.
| Argument | Type | Default | Description |
|---|---|---|---|
labeled_dir |
str |
Required | Path to the directory with labeled data, organized in class subfolders. |
unlabeled_dir |
str |
Required | Path to the directory with unlabeled images. |
output_dir |
str |
'sos_model' |
Directory to save the trained model and class mapping. |
model_name |
str |
'densenet121' |
Name of the torchvision model to use (e.g., 'resnet50'). |
k |
int |
5 |
Number of chunks to split the unlabeled data into per round. |
pseudo_epochs |
int |
1 |
Number of epochs to train on each pseudo-labeled chunk. |
max_rounds |
int |
10 |
Maximum number of rounds to train over the unlabeled data. |
target_acc |
float |
0.95 |
Validation accuracy threshold to trigger early stopping. |
batch_size |
int |
64 |
The batch size for training and evaluation. |
lr |
float |
1e-4 |
The learning rate for the Adam optimizer. |
val_split |
tuple |
(0.2, 0.2) |
Proportions of labeled data to use for validation and testing. |
Returns: float - The final test accuracy of the best model.
soslr.predict(...)
Makes predictions on a directory of images using a trained model.
| Argument | Type | Default | Description |
|---|---|---|---|
images_dir |
str |
Required | Path to the directory of images to predict. |
model_dir |
str |
'sos_model' |
Path to the directory containing the saved best_model.pth and class_mapping.json. |
model_name |
str |
'densenet121' |
The architecture name used during training. Must be the same. |
input_size |
int |
224 |
The image input size used during training. |
Returns: dict - A dictionary mapping each image filename to its predicted class name.
Contributing
Contributions are welcome! Please open an issue or submit a pull request on the GitHub repository.
License
This project is licensed under the MIT License. See the LICENSE file for details.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file soslr-0.1.1.tar.gz.
File metadata
- Download URL: soslr-0.1.1.tar.gz
- Upload date:
- Size: 10.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3809ba06cf0773ba812d17a4689e0546c6a634cc5817d73ecf0651ca7762db93
|
|
| MD5 |
60958aad9e554f1d928abe75acc81228
|
|
| BLAKE2b-256 |
b605e23faf7fd92fd5931b13a36cb6ec891e496714920c4f4d2208e73c0919d4
|
File details
Details for the file soslr-0.1.1-py3-none-any.whl.
File metadata
- Download URL: soslr-0.1.1-py3-none-any.whl
- Upload date:
- Size: 8.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/6.1.0 CPython/3.11.1
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
50a2f02bd714c3fd351443a8e2afb19a4f164f058feed7cf7bccbdfffda3d96d
|
|
| MD5 |
a7fb81c97e627dd3dca06cfc620967e3
|
|
| BLAKE2b-256 |
24d8d0c40c4d44753129c56545eb0ead0c9dcf4e2e33a305ae6e4a65bf3d9266
|