Skip to main content

Active learning for edge vision.

Project description

Python Version License PyPI Downloads

active-vision

Active learning at the edge for computer vision.

The goal of this project is to create a framework for the active learning loop for computer vision deployed on edge devices.

Installation

I recommend using uv to set up a virtual environment and install the package. You can also use other virtual env of your choice.

If you're using uv:

uv venv
uv sync

Once the virtual environment is created, you can install the package using pip.

Get a release from PyPI

pip install active-vision

Install from source

git clone https://github.com/dnth/active-vision.git
cd active-vision
pip install -e .

[!TIP] If you're using uv add a uv before the pip install command to install into your virtual environment. Eg:

uv pip install active-vision

Usage

See the notebook for a complete example.

Be sure to prepared 3 datasets:

  • train: A dataframe of an existing labeled training dataset.
  • unlabeled: A dataframe of unlabeled data which we will sample from using active learning.
  • eval: A dataframe of labeled data which we will use to evaluate the performance of the model. (Optional)
from active_vision import ActiveLearner
import pandas as pd

# Create an active learner instance with a model
al = ActiveLearner("resnet18")

# Load dataset 
train_df = pd.read_parquet("training_samples.parquet")
al.load_dataset(df, filepath_col="filepath", label_col="label")

# Train model
al.train(epochs=3, lr=1e-3)

# Evaluate the model on a *labeled* evaluation set
accuracy = al.evaluate(eval_df, filepath_col="filepath", label_col="label")

# Get predictions from an *unlabeled* set
pred_df = al.predict(filepaths)

# Sample low confidence predictions from unlabeled set
uncertain_df = al.sample_uncertain(pred_df, num_samples=10)

# Launch a Gradio UI to label the low confidence samples
al.label(uncertain_df, output_filename="uncertain")

Gradio UI

Once complete, the labeled samples will be save into a new df. We can now add the newly labeled data to the training set.

# Add newly labeled data to training set and save as a new file active_labeled
al.add_to_train_set(labeled_df, output_filename="active_labeled")

Repeat the process until the model is good enough. Use the dataset to train a larger model and deploy.

Workflow

There are two workflows for active learning at the edge that we can use depending on the availability of labeled data.

With unlabeled data

If we have no labeled data, we can use active learning to iteratively improve the model and build a labeled dataset.

  1. Load a small proxy model.
  2. Label an initial dataset.
  3. Train the proxy model on the labeled dataset.
  4. Run inference on the unlabeled dataset.
  5. Evaluate the performance of the proxy model on the unlabeled dataset.
  6. Is model good enough?
    • Yes: Save the proxy model and the dataset.
    • No: Select the most informative images to label using active learning.
  7. Label the most informative images and add them to the dataset.
  8. Repeat steps 3-6.
  9. Save the proxy model and the dataset.
  10. Train a larger model on the saved dataset.
graph TD
    A[Load a small proxy model] --> B[Label an initial dataset]
    B --> C[Train proxy model on labeled dataset]
    C --> D[Run inference on unlabeled dataset]
    D --> E[Evaluate proxy model performance]
    E --> F{Model good enough?}
    F -->|Yes| G[Save proxy model and dataset]
    G --> H[Train and deploy a larger model]
    F -->|No| I[Select informative images using active learning]
    I --> J[Label selected images]
    J --> C

With labeled data

If we have a labeled dataset, we can use active learning to iteratively improve the dataset and the model by fixing the most important label errors.

  1. Load a small proxy model.
  2. Train the proxy model on the labeled dataset.
  3. Run inference on the entire labeled dataset.
  4. Get the most important label errors with active learning.
  5. Fix the label errors.
  6. Repeat steps 2-5 until the dataset is good enough.
  7. Save the labeled dataset.
  8. Train a larger model on the saved labeled dataset.
graph TD
    A[Load a small proxy model] --> B[Train proxy model on labeled dataset]
    B --> C[Run inference on labeled dataset]
    C --> D[Get important label errors using active learning]
    D --> E[Fix label errors]
    E --> F{Dataset good enough?}
    F -->|No| B
    F -->|Yes| G[Save cleaned dataset]
    G --> H[Train and deploy larger model]

Methodology

To test out the workflows we will use the imagenette dataset. But this will be applicable to any dataset.

Imagenette is a subset of the ImageNet dataset with 10 classes. We will use this dataset to test out the workflows. Additionally, Imagenette has an existing leaderboard which we can use to evaluate the performance of the models.

Step 1: Download the dataset

Download the imagenette dataset. The imagenette dataset has a train and validation split. Since the leaderboard is based on the validation set, we will evalutate the performance of our model on the validation set to make it easier to compare to the leaderboard.

We will treat the imagenette train set as a unlabeled set and iteratively sample from it while monitoring the performance on the validation set. Ideally we will be able to get to a point where the performance on the validation set is close to the leaderboard with minimal number of labeled images.

I've processed the imagenette dataset and uploaded it to the hub. You can download it from here.

To load the dataset, you can use the following code:

from datasets import load_dataset

unlabeled_dataset = load_dataset("dnth/active-learning-imagenette", "unlabeled")
eval_dataset = load_dataset("dnth/active-learning-imagenette", "evaluation")

Step 2: Initial Sampling

Label an initial dataset of 10 images from each class. This will give us a small proxy dataset to train our model on. The sampling will be done randomly. There are more intelligent sampling strategies but we will start with random sampling.

Step 3: Training the proxy model

Train a proxy model on the initial dataset. The proxy model will be a small model that is easy to train and deploy. We will use the fastai framework to train the model. We will use the resnet18 architecture as a starting point. Once training is complete, compute the accuracy of the proxy model on the validation set and compare it to the leaderboard.

[!TIP] With the initial model we got 91.24% accuracy on the validation set. See the notebook for more details.

Train Epochs Number of Images Validation Accuracy Source
10 100 91.24% Initial sampling notebook
80 9469 94.90% fastai
200 9469 95.11% fastai

Step 4: Inference on the unlabeled dataset

Run inference on the unlabeled dataset (the remaining imagenette train set) and evaluate the performance of the proxy model.

Step 5: Active learning

Use active learning to select the most informative images to label from the unlabeled set. Pick the top 10 images from the unlabeled set that the proxy model is least confident about and label them.

Step 6: Repeat

Repeat step 3 - 5 until the performance on the validation set is close to the leaderboard. Note the number of labeled images vs the performance on the validation set. Ideally we want to get to a point where the performance on the validation set is close to the leaderboard with minimal number of labeled images.

After the first iteration we got 94.57% accuracy on the validation set. See the notebook for more details.

[!TIP]

Train Epochs Number of Images Validation Accuracy Source
10 200 94.57% First relabeling notebook

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

active_vision-0.0.3.tar.gz (13.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

active_vision-0.0.3-py3-none-any.whl (11.4 kB view details)

Uploaded Python 3

File details

Details for the file active_vision-0.0.3.tar.gz.

File metadata

  • Download URL: active_vision-0.0.3.tar.gz
  • Upload date:
  • Size: 13.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for active_vision-0.0.3.tar.gz
Algorithm Hash digest
SHA256 d0d7a3855fcc0304a04fd6ae73a9db2408ed4ad20a62a69cf6395e4bcc0666e9
MD5 1e930fbc7c0e08eb620a7d8d1240c163
BLAKE2b-256 465a86d11c70a09be9ae5f5558f94a8ee609dfdfa8836270f2ff4ad5a9ae1661

See more details on using hashes here.

File details

Details for the file active_vision-0.0.3-py3-none-any.whl.

File metadata

  • Download URL: active_vision-0.0.3-py3-none-any.whl
  • Upload date:
  • Size: 11.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.0.1 CPython/3.12.8

File hashes

Hashes for active_vision-0.0.3-py3-none-any.whl
Algorithm Hash digest
SHA256 6b4ed5984ac1a9e98d46fa8c5566a729efb3384887228599308116dc98484cf3
MD5 00790366479cfbe0a70b86f239f8b121
BLAKE2b-256 eb9f15659d3729e45cee109027c3d6fc0c560a84aa05a61c9ed9ff56287c75c3

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page