# torchpack
[](https://pypi.python.org/pypi/torchpack)
Torchpack is a set of interfaces to simplify the usage of PyTorch.
Documentation is ongoing.
## Installation
- Install with pip.
```
pip install torchpack
```
- Install from source.
```
git clone https://github.com/hellock/torchpack.git
cd torchpack
python setup.py install
```
**Note**: If you want to use tensorboard to visualize the training process, you need to
install tensorflow([`installation guide`](https://www.tensorflow.org/install/install_linux)) and tensorboardX(`pip install tensorboardX`).
## What can torchpack do
Torchpack aims to help users to start training with less code, while stays
flexible and configurable. It provides a `Runner` with lots of `Hook`s.
## Example
```python
######################## file1: config.py #######################
work_dir = './demo' # dir to save log file and checkpoints
optimizer = dict(
algorithm='SGD', args=dict(lr=0.001, momentum=0.9, weight_decay=5e-4))
workflow = [('train', 2), ('val', 1)] # train 2 epochs and then validate 1 epochs, iteratively
max_epoch = 16
lr_policy = dict(policy='step', step=12) # decrese learning rate by 10 every 12 epochs
checkpoint_cfg = dict(interval=1) # save checkpoint at every epoch
log_cfg = dict(
# log at every 50 iterations
interval=50,
# two logging hooks, one for printing in terminal and one for tensorboard visualization
hooks=[
('TextLoggerHook', {}),
('TensorboardLoggerHook', dict(log_dir=work_dir + '/log'))
])
######################### file2: main.py ########################
import torch
from torchpack import Config, Runner
from collections import OrderedDict
# define how to process a batch and return a dict
def batch_processor(model, data, train_mode):
img, label = data
volatile = False if train_mode else True
img_var = torch.autograd.Variable(img, volatile=volatile)
label_var = torch.autograd.Variable(label, requires_grad=False)
pred = model(img)
loss = F.cross_entropy(pred, label_var)
accuracy = get_accuracy(pred, label_var)
log_vars = OrderedDict()
log_vars['loss'] = loss.data[0]
log_vars['accuracy'] = accuracy.data[0]
outputs = dict(
loss=loss, log_vars=log_vars, num_samples=img.size(0))
return outputs
cfg = Config.from_file('config.py') # or config.yaml/config.json
model = resnet18()
runner = Runner(model, cfg.optimizer, batch_processor, cfg.work_dir)
runner.register_default_hooks(cfg.lr_policy, cfg.checkpoint_cfg, cfg.log_cfg)
runner.run([train_loader, val_loader], cfg.workflow, cfg.max_epoch)
```
[](https://pypi.python.org/pypi/torchpack)
Torchpack is a set of interfaces to simplify the usage of PyTorch.
Documentation is ongoing.
## Installation
- Install with pip.
```
pip install torchpack
```
- Install from source.
```
git clone https://github.com/hellock/torchpack.git
cd torchpack
python setup.py install
```
**Note**: If you want to use tensorboard to visualize the training process, you need to
install tensorflow([`installation guide`](https://www.tensorflow.org/install/install_linux)) and tensorboardX(`pip install tensorboardX`).
## What can torchpack do
Torchpack aims to help users to start training with less code, while stays
flexible and configurable. It provides a `Runner` with lots of `Hook`s.
## Example
```python
######################## file1: config.py #######################
work_dir = './demo' # dir to save log file and checkpoints
optimizer = dict(
algorithm='SGD', args=dict(lr=0.001, momentum=0.9, weight_decay=5e-4))
workflow = [('train', 2), ('val', 1)] # train 2 epochs and then validate 1 epochs, iteratively
max_epoch = 16
lr_policy = dict(policy='step', step=12) # decrese learning rate by 10 every 12 epochs
checkpoint_cfg = dict(interval=1) # save checkpoint at every epoch
log_cfg = dict(
# log at every 50 iterations
interval=50,
# two logging hooks, one for printing in terminal and one for tensorboard visualization
hooks=[
('TextLoggerHook', {}),
('TensorboardLoggerHook', dict(log_dir=work_dir + '/log'))
])
######################### file2: main.py ########################
import torch
from torchpack import Config, Runner
from collections import OrderedDict
# define how to process a batch and return a dict
def batch_processor(model, data, train_mode):
img, label = data
volatile = False if train_mode else True
img_var = torch.autograd.Variable(img, volatile=volatile)
label_var = torch.autograd.Variable(label, requires_grad=False)
pred = model(img)
loss = F.cross_entropy(pred, label_var)
accuracy = get_accuracy(pred, label_var)
log_vars = OrderedDict()
log_vars['loss'] = loss.data[0]
log_vars['accuracy'] = accuracy.data[0]
outputs = dict(
loss=loss, log_vars=log_vars, num_samples=img.size(0))
return outputs
cfg = Config.from_file('config.py') # or config.yaml/config.json
model = resnet18()
runner = Runner(model, cfg.optimizer, batch_processor, cfg.work_dir)
runner.register_default_hooks(cfg.lr_policy, cfg.checkpoint_cfg, cfg.log_cfg)
runner.run([train_loader, val_loader], cfg.workflow, cfg.max_epoch)
```
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
torchpack-0.0.5.tar.gz
(11.0 kB
view details)
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
torchpack-0.0.5-py3-none-any.whl
(17.5 kB
view details)
File details
Details for the file torchpack-0.0.5.tar.gz.
File metadata
- Download URL: torchpack-0.0.5.tar.gz
- Upload date:
- Size: 11.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
9baa4f31bb50f98aaa8562af744cad62513dfe38216d175eb8b27b1401af6ad9
|
|
| MD5 |
f19f1179294751fd62656bc26796e745
|
|
| BLAKE2b-256 |
7008088b89ce4a270b7afc02c14f61df975fc7de59b507a11fc5c26a70908e0f
|
File details
Details for the file torchpack-0.0.5-py3-none-any.whl.
File metadata
- Download URL: torchpack-0.0.5-py3-none-any.whl
- Upload date:
- Size: 17.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
47a4a12ac6df84d3751f19c34e59ef75ee321cfe06dac1ccfcad63f37977dcca
|
|
| MD5 |
f564d480221e2673fe49691b55207210
|
|
| BLAKE2b-256 |
33dd4af0b8a3608344417ac60d54f28a47e6fda47ed73aa6602474c7a0f6e6c2
|