Skip to main content

Frarch drawing

Pytorch python

CI docs Coverage Badge

PyPI version fury.io license

Frarch is a Framework for Pytorch experiments inspired by speechbrain's workflow using hyperpyyaml configuration files. Frarch aims to minimize the code needed to perform an experiment while organizing the output models and the log files for the experiment as well as the configuration files used to train them in an organised manner.

Features

  • CPU and CUDA computations. Note that CUDA must be installed for Pytorch and as such frarch to compute in an NVIDIA GPU. Multi-GPU is not supported at the moment, but will be supported in the future.
  • Minimize the size of training scripts.
  • Support for Python's 3.8 and 3.9 versions
  • yaml definition of training hyperparameters.
  • organisation of output models and their hyperparameters, training scripts and logs.

Quick installation

The frarch package is evolving and not yet in a stable release. Documentation will be added as the package progresses. The package can be installed via PyPI or via github for the users that what to modify the contents of the package.

PyPI installation

Once the python environment has been created, you can install frarch by executing:

pip install frarch

Then frarch can be used in a python script using:

import frarch as fr

Github install

Once the python environment has been created, you can install frarch by executing:

git clone https://github.com/victorbadenas/frarch.git
cd frarch
pip install . # for enabling editable mode use the `-e` flag

for development instead of the last command, run pip install -e .[dev] to be able to hot reload changes to the package.

Test

To run the tests for the frarch package:

python -m unittest discover

or with coverage

coverage run -m unittest discover -s tests/unit
coverage report -m --omit='tests/unit/*'

Documentation

To create the documentation, run the following command:

make -C docs html
sensible-browser docs/_build/html/index.html
make -C docs latexpdf

Running an experiment

Frarch provides training classes such as ClassifierTrainer which provides methods to train a classifier model.

Example Python trainer script

In this example we present a sample training script for training the MNIST dataset.

from hyperpyyaml import load_hyperpyyaml

from frarch.parser import parse_arguments
from frarch.utils.data import build_experiment_structure
from frarch.utils.enums.stages import Stage
from frarch.train.classifier_trainer import ClassifierTrainer


class MNISTTrainer(ClassifierTrainer):
    def forward(self, batch, stage):
        inputs, _ = batch
        inputs = inputs.to(self.device)
        return self.modules.model(inputs)

    def compute_loss(self, predictions, batch, stage):
        _, labels = batch
        labels = labels.to(self.device)
        return self.hparams["loss"](predictions, labels)

    def on_stage_end(self, stage, loss=None, epoch=None):
        if stage == Stage.VALID:
            if self.checkpointer is not None:
                self.checkpointer.save(epoch=self.current_epoch, current_step=self.step)


if __name__ == "__main__":
    hparam_file, args = parse_arguments()

    with open(hparam_file, "r") as hparam_file_handler:
        hparams = load_hyperpyyaml(
            hparam_file_handler, args, overrides_must_match=False
        )

    build_experiment_structure(
        hparam_file,
        overrides=args,
        experiment_folder=hparams["experiment_folder"],
        debug=hparams["debug"],
    )

    trainer = MNISTTrainer(
        modules=hparams["modules"],
        opt_class=hparams["opt_class"],
        hparams=hparams,
        checkpointer=hparams["checkpointer"],
    )

    trainer.fit(
        train_set=hparams["train_dataset"],
        valid_set=hparams["valid_dataset"],
        train_loader_kwargs=hparams["dataloader_options"],
        valid_loader_kwargs=hparams["dataloader_options"],
    )

And the hparams yaml file used to configure the experiment:

# seeds
seed: 42
__set_seed: !apply:torch.manual_seed [!ref <seed>]
experiment_name: "mnist"
experiment_folder: "results/mnist_demo/"
device: "cpu"

# data folder
data_folder: /tmp/

# training parameters
epochs: 2
batch_size: 128
shuffle: True
num_clases: 10

transform_tensor: !new:torchvision.transforms.ToTensor
preprocessing: !new:torchvision.transforms.Compose
    transforms: [
        !ref <transform_tensor>,
    ]

# dataset object
train_dataset: !new:torchvision.datasets.MNIST
    root: !ref <data_folder>
    train: true
    download: true
    transform: !ref <preprocessing>

valid_dataset: !new:torchvision.datasets.MNIST
    root: !ref <data_folder>
    train: false
    download: true
    transform: !ref <preprocessing>

# dataloader options
dataloader_options:
    batch_size: !ref <batch_size>
    shuffle: !ref <shuffle>
    num_workers: 8

opt_class: !name:torch.optim.Adam
    lr: 0.001

loss: !new:torch.nn.CrossEntropyLoss

model: !apply:torchvision.models.vgg11
    pretrained: false

modules:
    model: !ref <model>

checkpointer: !new:frarch.modules.checkpointer.Checkpointer
    save_path: !ref <experiment_folder>
    modules: !ref <modules>

For the code execution run:

python train.py mnist.yaml

Metadata

Release files for frarch 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for frarch 0.1.6
File Size Uploaded Provenance
frarch-0.1.6.tar.gz 30.2 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for frarch 0.1.6
File Interpreter ABI Platform
frarch-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 69.9 kB

Release files / frarch-0.1.6.tar.gz

Download URL frarch-0.1.6.tar.gz
Size 30.2 kB
Tags Source
SHA-256 checksum
How to use checksums
f6486e1176843e59428bcfe5ac843d14c5db9820c4de42bc69b1fabc67ca3fcb
BLAKE2b-256 checksum
How to use checksums
b5691a8e1d25f4f094e0e06e5ae372312f989331e9c6088e86def3b8ba95f976
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Release files / frarch-0.1.6-py3-none-any.whl

Download URL frarch-0.1.6-py3-none-any.whl
Size 39.7 kB
Tags Python 3
SHA-256 checksum
How to use checksums
dc93bbcb22b2508a6ba7ffecbaabcb8ba4ece9916f4257ac68eb22a05b30bb1b
BLAKE2b-256 checksum
How to use checksums
2a021f7412dbeebd01d42d2393c87fe21e60d4045efe196ec86683c80792f193
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/4.0.1 CPython/3.9.13

Security for frarch 0.1.6

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page