Skip to main content

Stochastic-depth ResNet training library extracted from a thesis project

Project description

sdhd

sdhd stands for Stochastic Depth for High-Depth residual networks.

It is a reusable Python library extracted from my thesis codebase on stochastic-depth regularization for residual CNNs.

What the library provides

  • ResNet models without stochastic depth;
  • ResNet models with stochastic depth in two modes:
    • per-batch gating,
    • per-sample gating;
  • CIFAR-style residual networks with 3x3 basic blocks;
  • bottleneck residual networks (ResNet-50 / ResNet-101 style);
  • folder-based image loading;
  • CIFAR-100 loading;
  • LMDB-backed datasets for large-scale training;
  • a generic PyTorch training loop with checkpoint save/load support;
  • a small command-line interface for training experiments.

Installation

Local editable install

pip install -e ./sdhd

With LMDB support

pip install -e './sdhd[lmdb]'

Quick examples

Build a CIFAR-style model

from sdhd import StochasticDepthConfig, CIFARResNetConfig, build_cifar_resnet

sd = StochasticDepthConfig(enabled=True, mode="sample", final_survival_prob=0.5)
config = CIFARResNetConfig(depth=110, num_classes=100, stochastic_depth=sd)
model = build_cifar_resnet(config)

Build a bottleneck ResNet

from sdhd import StochasticDepthConfig, BottleneckResNetConfig, build_bottleneck_resnet

sd = StochasticDepthConfig(enabled=True, mode="batch", final_survival_prob=0.5)
config = BottleneckResNetConfig(depth=50, num_classes=365, stochastic_depth=sd)
model = build_bottleneck_resnet(config)

Create CIFAR-100 dataloaders

from sdhd import create_cifar100_dataloaders

train_loader, test_loader = create_cifar100_dataloaders("/path/to/cifar100", batch_size=256)

Train a built-in ResNet in one function call

from sdhd import TrainerConfig, train_resnet

trainer = TrainerConfig(epochs=5, lr=0.01, output_dir="runs/example", compile_model=False)
result = train_resnet(
    architecture="bottleneck",
    dataset_type="folder",
    data_dir="/path/to/dataset",
    depth=50,
    batch_size=8,
    trainer_config=trainer,
    enable_sd=True,
    sd_mode="sample",
)
print(result.best_val_accuracy)

Train your own custom PyTorch model

import torch.nn as nn
from sdhd import TrainerConfig, train_custom_model

model = nn.Sequential(
    nn.Flatten(),
    nn.Linear(3 * 224 * 224, 102),
)

trainer = TrainerConfig(epochs=5, lr=0.01, output_dir="runs/custom", compile_model=False)
result = train_custom_model(
    model,
    dataset_type="folder",
    data_dir="/path/to/dataset",
    batch_size=8,
    trainer_config=trainer,
)
print(result.best_val_accuracy)

Lower-level training API

from sdhd import TrainerConfig, train_classifier

trainer = TrainerConfig(epochs=100, lr=0.1, output_dir="runs/example")
result = train_classifier(model, train_loader, test_loader, trainer)
print(result.best_val_accuracy)

CLI

After installation, you can run:

sdhd-train --help

Or:

python -m sdhd --help

Thesis background

This package comes from a thesis project on stochastic depth for regularizing residual CNNs. The main thesis-specific contributions preserved in this library are:

  • explicit support for per-sample vs per-batch stochastic depth;
  • stochastic depth for bottleneck residual blocks;
  • support for large-scale LMDB datasets.

Full documentation

The package keeps its public-facing documentation concentrated in this README, in-code docstrings, and the printable LaTeX manual:

  • docs/tex/sdhd_manual.tex

Additional private/helper Markdown notes for workspace management are intentionally kept outside the standalone sdhd repository.

Release and maintenance

Use GitHub Releases together with Trusted Publishing to publish to PyPI.

Recommendation for the LaTeX manual

Keep the LaTeX source in the repository and optionally publish the compiled PDF through GitHub Releases. PyPI should stay focused on the concise package README.

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

sdhd-0.1.0.tar.gz (26.9 kB view details)

Uploaded Source

Built Distribution

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

sdhd-0.1.0-py3-none-any.whl (20.8 kB view details)

Uploaded Python 3

File details

Details for the file sdhd-0.1.0.tar.gz.

File metadata

  • Download URL: sdhd-0.1.0.tar.gz
  • Upload date:
  • Size: 26.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sdhd-0.1.0.tar.gz
Algorithm Hash digest
SHA256 d6613ca1b002d720ffed1500571f705e2313ddc558239c888c1e268812612c5b
MD5 a1674612bc68e6af212e2f0e62f8d102
BLAKE2b-256 60d1604ab72f3e089cbbfb3d31264bf54c8f5c6f6ecbd90aa6fbcf0932b37e98

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdhd-0.1.0.tar.gz:

Publisher: release.yml on gerageragera39/sdhd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file sdhd-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: sdhd-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 20.8 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.12

File hashes

Hashes for sdhd-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 596278e5f7765471fbd0b9fc1eca50dd0aad02729c5f3a91773b742b125aad9d
MD5 0a3e10b1d83edded230890898a9f6165
BLAKE2b-256 bd10a292fe5067226716d31af9c00da6a0d1790328653df1ff031f8b862d4aaa

See more details on using hashes here.

Provenance

The following attestation bundles were made for sdhd-0.1.0-py3-none-any.whl:

Publisher: release.yml on gerageragera39/sdhd

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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