Skip to main content

Open-source Python package for image classification and regression built on timm + torchvision

Project description

FujiCV

Open-source Python package for image classification and regression, built on timm + torchvision.

CI Python 3.9+ License: MIT


Features

  • Backbone factory — instantly load any timm or torchvision model with a single call; classifier head auto-stripped, output dimension auto-detected.
  • Task heads — ClassificationHead, RegressionHead, MultiLabelHead.
  • Custom layers — LinearBNDropout, GeM pooling, AttentionPool, SqueezeExcite.
  • ModelBuilder — assemble backbone + custom layers + head; dummy forward pass validates shapes at build time.
  • Albumentations pipelines — light / medium / heavy augmentation presets, ImageNet normalisation, deterministic val transforms.
  • CSVImageDataset — reads any CSV-driven image dataset; pre-validates files, skips missing with a warning; supports classification, regression, multilabel.
  • Loss functions — 13 losses across classification, regression, and multilabel tasks, all registered in LOSS_REGISTRY.
  • Metrics — 16 metrics across all tasks, registered in METRIC_REGISTRY.
  • Trainer — AMP, gradient clipping, early stopping, best/last checkpointing, history CSV.
  • WandbLogger — W&B integration via env-var only (WANDB_API_KEY); graceful no-op when W&B is absent.
  • Evaluation — confusion matrix, ROC/PR curves, t-SNE, Grad-CAM (CNN), attention rollout (ViT).
  • InferencePredictor.from_checkpoint for single image and batch inference.
  • ONNX export — export and numeric verification.

Installation

# Core (CPU)
pip install fujicv

# With W&B logging
pip install "fujicv[wandb]"

# With ONNX export
pip install "fujicv[onnx]"

# Dev / testing
pip install "fujicv[dev]"

PyTorch is not included as a transitive dependency on PyPI. Install it separately following the official instructions to pick the right CUDA version.


Quick Start

1. Classification

from fujicv.models.builder import ModelBuilder
from fujicv.losses import get_loss
from fujicv.metrics import get_metric
from fujicv.engine.trainer import Trainer
from fujicv.data import build_splits, build_dataloaders
from fujicv.utils import load_config, set_seed

set_seed(42)
cfg = load_config("examples/configs/classification_example.yaml")

train_df, val_df, test_df = build_splits(cfg["dataset"])
train_loader, val_loader, _ = build_dataloaders(
    train_df, val_df, test_df, cfg["dataset"], cfg["augmentation"]
)

model = ModelBuilder(
    backbone_name="resnet50",
    task="classification",
    num_outputs=10,
    pretrained=True,
).build()

import torch.optim as optim

trainer = Trainer(
    model=model,
    train_loader=train_loader,
    val_loader=val_loader,
    loss_fn=get_loss("LabelSmoothingCE", {"smoothing": 0.1}),
    metrics={"Accuracy": get_metric("Accuracy"), "F1": get_metric("F1")},
    optimizer=optim.AdamW(model.parameters(), lr=3e-4),
    epochs=30,
    task="classification",
    output_dir="outputs/",
)
history = trainer.train()

2. Using the example scripts

# Train
python examples/train.py --config examples/configs/classification_example.yaml

# Evaluate (with attention maps)
python examples/evaluate.py \
    --config examples/configs/classification_example.yaml \
    --checkpoint outputs/classification/best.pt \
    --with-attention-maps

3. Inference

from fujicv.inference import Predictor
from fujicv.models.builder import ModelBuilder

model_skeleton = ModelBuilder(
    backbone_name="resnet50", task="classification", num_outputs=10, pretrained=False
).build()

predictor = Predictor.from_checkpoint("outputs/classification/best.pt", model=model_skeleton)
label, confidence = predictor.predict("path/to/image.jpg")
print(f"Predicted: {label}  ({confidence:.1%})")

4. ONNX Export

from fujicv.export import to_onnx, verify_onnx

to_onnx(model, "model.onnx")
verify_onnx(model, "model.onnx")

Package Layout

fujicv/
  models/       backbone factory, heads, custom layers, ModelBuilder
  data/         CSVImageDataset, transforms, dataloader factory
  losses/       13 loss functions + registry
  metrics/      16 metric callables + registry
  engine/       Trainer, WandbLogger, callbacks
  eval/         plots, report, ROC/PR curves, t-SNE, attention maps
  utils/        Registry, set_seed, config loader
  inference/    Predictor
  export/       ONNX export & verification
tests/
examples/
  configs/      YAML configs for each task type
  train.py
  evaluate.py

Supported Tasks

Task Loss examples Metric examples
Classification / Multiclass CrossEntropyLoss, FocalLoss, LabelSmoothingCE Accuracy, F1, AUROC
Regression MSELoss, HuberLoss, QuantileLoss MAE, RMSE, R2Score
Multi-label BCEWithLogitsLoss, AsymmetricLoss, FocalBCELoss HammingLoss, mAP, PerLabelAUROC

Security

See SECURITY.md for the full policy. Key points:

  • No hardcoded credentials anywhere in the codebase.
  • W&B API key read from WANDB_API_KEY environment variable only.
  • Loss/metric functions are pure tensor operations with no network calls.

Contributing

  1. Fork the repository and create a feature branch.
  2. Install in editable mode: pip install -e ".[dev]"
  3. Run the checks locally:
    ruff check fujicv/ tests/
    mypy fujicv/
    pytest tests/
    detect-secrets scan
    
  4. Open a pull request — CI will run automatically.

License

MIT — Copyright (c) 2024 FujiCV Contributors.

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

fujicv-1.0.0.tar.gz (49.7 kB view details)

Uploaded Source

Built Distribution

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

fujicv-1.0.0-py3-none-any.whl (55.1 kB view details)

Uploaded Python 3

File details

Details for the file fujicv-1.0.0.tar.gz.

File metadata

  • Download URL: fujicv-1.0.0.tar.gz
  • Upload date:
  • Size: 49.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for fujicv-1.0.0.tar.gz
Algorithm Hash digest
SHA256 b747497d89e3eee65cc0829f35ffc97d1509891d415e364716b6f3564bc47b97
MD5 159631822708aa22939b4a30dfcd876a
BLAKE2b-256 64b3af74d554e96515d62c4a04bc3056d0a63fd71157625724c135f798f0163b

See more details on using hashes here.

File details

Details for the file fujicv-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: fujicv-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 55.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.10.11

File hashes

Hashes for fujicv-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 79430ccbee179cc2a5550edb72276a7aed4b11ca7ee4b0013daf2c18cfaae53b
MD5 427d2ab2a79a18d6f2683c5728aeff0b
BLAKE2b-256 137a48e053a646b851852a7e7095a609f36f90003e03033fc002785d6a3b87cf

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