a fast prototyping library for DL research
Project description
homura
master | dev |
---|---|
homura is a fast prototyping library for DL research.
🔥🔥🔥🔥 homura (焰) is flame or blaze in Japanese. 🔥🔥🔥🔥
Requirements
Minimal requirements
Python>=3.9
PyTorch>=1.9.0
torchvision>=0.10.0
Installation
pip install -U homura-core
or
pip uninstall homura
pip install -U git+https://github.com/moskomule/homura
Optional
faiss (for faster kNN)
accimage (for faster image pre-processing)
cupy (homura)
opt_einsum (for accelerated einsum)
test
pytest .
APIs
Basics
homura
aims abstract (e.g., device-agnostic) simple prototyping.
from homura import optim, lr_scheduler
from homura import trainers, reporters
from homura.vision import MODEL_REGISTRY, DATASET_REGISTRY
from torch.nn import functional as F
train_loader, test_loader, num_classes = DATASET_REGISTRY('dataset_name')(...)
# User does not need to care about the device
model = MODEL_REGISTRY('model_name')(num_classes=num_classes)
# Model is registered in optimizer lazily. This is convenient for distributed training and other complicated scenes.
optimizer = optim.SGD(lr=0.1, momentum=0.9)
scheduler = lr_scheduler.MultiStepLR(milestones=[30, 80], gamma=0.1)
with trainers.SupervisedTrainer(model,
optimizer,
F.cross_entropy,
reporters=[reporters.TensorboardReporter(...)],
scheduler=scheduler) as trainer:
# epoch-based training
for _ in trainer.epoch_iterator(num_epochs):
trainer.train(train_loader)
trainer.scheduler.step()
trainer.test(test_loader)
trainer.scheduler.step()
# otherwise, iteration-based training
trainer.run(train_loader, test_loader,
total_iterations=1_000, val_intervals=10)
print(f"Max Accuracy={max(trainer.history['accuracy']['tests'])}")
You can customize iteration
of trainer
as follows.
from homura.trainers import TrainerBase, SupervisedTrainer
from homura.metrics import accuracy
trainer = SupervisedTrainer(...)
# from v2020.08, iteration is much simpler
def iteration(trainer: TrainerBase,
data: Tuple[torch.Tensor, torch.Tensor]
) -> None:
input, target = data
output = trainer.model(input)
loss = trainer.loss_f(output, target)
trainer.reporter.add('loss', loss.detach())
trainer.reporter.add('accuracy', accuracy(input, target))
trainer.reporter.add('')
if trainer.is_train:
trainer.optimizer.zero_grad()
loss.backward()
trainer.optimizer.step()
# in case schedule is step-wise
trainer.scheduler.step()
SupervisedTrainer.iteration = iteration
# or
trainer.update_iteration(iteration)
dict
of models, optimizers, loss functions are supported. This is useful for GANs, for example.
trainer = CustomTrainer({"generator": generator, "discriminator": discriminator},
{"generator": gen_opt, "discriminator": dis_opt},
{"reconstruction": recon_loss, "generator": gen_loss},
**kwargs)
reporter
internally tracks the values during each epoch and reduces after every epoch. Therefore, users can compute
mIoU, for example, as
from homura.metrics import confusion_matrix
def cm_to_miou(cms: List[torch.Tensor]) -> torch.Tensor:
# cms: list of confusion matrices
cm = sum(cms).float()
miou = cm.diag() / (cm.sum(0) + cm.sum(1) - cm.diag())
return miou.mean().item()
def iteration(trainer: TrainerBase,
data: Tuple[torch.Tensor, torch.Tensor]
) -> None:
input, target = data
output = trainer.model(input)
trainer.reporter.add('miou', confusion_matrix(output, target), reduction=cm_to_miou)
...
Distributed training
Distributed training is complicated at glance. homura
has simple APIs, to hide the messy codes for DDP, such
as homura.init_distributed
for the initialization and homura.is_master
for checking if the process is master or not.
For details, see examples/imagenet.py
.
Reproducibility
These methods make randomness deterministic in its context.
from homura.utils.reproducibility import set_deterministic, set_seed
with set_deterministic(seed):
# suppress nondeterministic computation
# but will affect the performance
something()
with set_seed(seed):
# only set random seed of Python, PyTorch and Numpy
other_thing()
Registry System
Following major libraries, homura
also has a simple registry system.
from homura import Registry
MODEL_REGISTRY = Registry("language_models")
@MODEL_REGISTRY.register
class Transformer(nn.Module):
...
# or
MODEL_REGISTRY.register(bert_model, 'bert')
# magic
MODEL_REGISTRY.import_modules(".")
transformer = MODEL_REGISTRY('Transformer')(...)
# or
bert = MODEL_REGISTRY('bert', ...)
Examples
See examples.
Citing
@misc{homura,
author = {Ryuichiro Hataya},
title = {homura},
year = {2018},
howpublished = {\url{https:/github.com/moskomule/homura}},
}
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 homura-core-2021.12.1.tar.gz
.
File metadata
- Download URL: homura-core-2021.12.1.tar.gz
- Upload date:
- Size: 53.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | faeff121f83623c7fae9d9700af732b37c89a1eab4aba892adcf53b8455b7e97 |
|
MD5 | 283e3e237dddbdefb0c2d649923707b9 |
|
BLAKE2b-256 | 54cd302f95e1c7c087a7a9b5b1882a7d6472aa27b92e978359bcef325aa19446 |
File details
Details for the file homura_core-2021.12.1-py3-none-any.whl
.
File metadata
- Download URL: homura_core-2021.12.1-py3-none-any.whl
- Upload date:
- Size: 67.2 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.7.1 importlib_metadata/4.10.0 pkginfo/1.8.2 requests/2.26.0 requests-toolbelt/0.9.1 tqdm/4.62.3 CPython/3.9.9
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | b3cccecf934241f54fa63dee70b4eca29d3a0638fb0eca4952be7d959256f679 |
|
MD5 | 3696ddf170ac363c4ffe997e8275974c |
|
BLAKE2b-256 | fcb628d49fefc3edbed53e3c942066b84749c5f9144f0239e489fb7cae189686 |