Git Product home page Git Product logo

frarch's Introduction

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

frarch's People

Contributors

victorbadenas avatar renovate[bot] avatar

Stargazers

Marc avatar  avatar

Watchers

 avatar

frarch's Issues

Dependency Dashboard

This issue lists Renovate updates and detected dependencies. Read the Dependency Dashboard docs to learn more.

Rate-Limited

These updates are currently rate-limited. Click on a checkbox below to force their creation now.

  • Update actions/setup-python action to v5
  • Update dependency black to v24
  • Update dependency numpy to v2
  • Update dependency watchdog to v5
  • Update peaceiris/actions-gh-pages action to v4
  • ๐Ÿ” Create all rate-limited PRs at once ๐Ÿ”

Open

These updates have all been created already. Click a checkbox below to force a retry/rebase of any.

Detected dependencies

github-actions
.github/workflows/ci.yml
  • actions/checkout v3
  • actions/setup-python v4
  • pre-commit/action v3.0.0
  • actions/checkout v3
  • actions/setup-python v4
  • actions/checkout v3
  • actions/setup-python v4
  • schneegans/dynamic-badges-action v1.6.0
  • actions/checkout v3
  • actions/setup-python v4
  • actions/checkout v3
  • actions/setup-python v4
  • frenck/action-setup-yq v1
  • actions/checkout v3
  • actions/setup-python v4
.github/workflows/docs.yaml
  • actions/checkout v3
  • actions/setup-python v4
  • peaceiris/actions-gh-pages v3
pep621
pyproject.toml
pip_requirements
docs/requirements.txt
setup-cfg
setup.cfg
  • hyperpyyaml ==1.1.0
  • numpy ==1.23.1
  • torch ==1.11.0
  • torchaudio ==0.11.0
  • torchvision ==0.12.0
  • tqdm ==4.64.1
  • setuptools >=46.4.0
  • black ==21.6b0
  • identify >=1.4.20
  • watchdog <1.0.0

  • Check this box to trigger a request for Renovate to run again on this repository

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.