Utilities for training models in pytorch
Project description
xt-training
Description
This repo contains utilities for training deep learning models in pytorch, developed by Xtract AI.
Installation
From PyPI:
pip install xt-training
From source:
git clone https://github.com/XtractTech/xt-training.git
pip install ./xt-training
Usage
See specific help on a class or function using help
. E.g., help(Runner)
.
Training a model
Using xt-training (High Level)
First, you must define a config file with the necessary items. To generate a template config file, run:
python -m xt_training template path/to/save/dir
To generate template files for nni, add the --nni
flag
Instructions for defining a valid config file can be seen at the top of the config file.
After defining a valid config file, you can train your model by running:
python -m xt_training train path/to/config.py /path/to/save_dir
You can test the model by running
python -m xt_training test path/to/config.py /path/to/save_dir
Using Runner (Low Level)
If you want a little more control and want to define the trianing code yourself, you can utilize the Runner like so:
from xt_training import Runner, metrics
from torch.utils.tensorboard import SummaryWriter
# Here, define class instances for the required objects
# model =
# optimizer =
# scheduler =
# loss_fn =
# Define metrics - each of these will be printed for each iteration
# Either per-batch or running-average values can be printed
batch_metrics = {
'eps': metrics.EPS(),
'acc': metrics.Accuracy(),
'kappa': metrics.Kappa(),
'cm': metrics.ConfusionMatrix()
}
# Define tensorboard writer
writer = SummaryWriter()
# Create runner
runner = Runner(
model=model,
loss_fn=loss_fn,
optimizer=optimizer,
scheduler=scheduler,
batch_metrics=batch_metrics,
device='cuda:0',
writer=writer
)
# Define dataset and loaders
# dataset =
# train_loader =
# val_loader =
# Train
model.train()
runner(train_loader)
batch_metrics['cm'].print()
# Evaluate
model.eval()
runner(val_loader)
batch_metrics['cm'].print()
# Print training and evaluation history
print(runner)
Scoring a model
import torch
from xt_training import Runner
# Here, define the model
# model =
# model.load_state_dict(torch.load(<checkpoint file>))
# Create runner
# (alternatively, can use a fully-specified training runner as in the example above)
runner = Runner(model=model, device='cuda:0')
# Define dataset and loaders
# dataset =
# test_loader =
# Score
model.eval()
y_pred, y = runner(test_loader, return_preds=True)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file xt-training-1.13.0.tar.gz
.
File metadata
- Download URL: xt-training-1.13.0.tar.gz
- Upload date:
- Size: 16.2 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ce58a9f2d798ac812ce4bbbe020cfe85ff4dc3d339b6c8548937a9cc9aa0ba6b |
|
MD5 | 9850fbae46396243f7be9f73dc367ddf |
|
BLAKE2b-256 | 67c31ddc67186e2cfcde092ad0f099cff4c90554ac55c6b436de2fed9e282f61 |
File details
Details for the file xt_training-1.13.0-py3-none-any.whl
.
File metadata
- Download URL: xt_training-1.13.0-py3-none-any.whl
- Upload date:
- Size: 19.8 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1.post20200622 requests-toolbelt/0.9.1 tqdm/4.45.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3c2b826838e91392600cdb314600aed865d1e606dc85ebfeb3b0e0e66f90b1ee |
|
MD5 | c7ec08886ea2b6a136f652f60b8db317 |
|
BLAKE2b-256 | 2453c6ac7a3745c6d2e06d1c059957c920b0b3c40a989e26dbc40aeca739a8b5 |