catalyst-team / catalyst

Accelerated deep learning R&D
https://catalyst-team.com
Apache License 2.0
3.3k stars 390 forks source link

AccuracyCallback does not work if 1 is not in topk_args #1160

Closed PeYceBall closed 3 years ago

PeYceBall commented 3 years ago

šŸ› Bug Report

Apparently AccuracyCallback raises an error when 1 is not in tuple passed to AccuracyCallback as topk_args.

How To Reproduce

I ran code snippet from https://catalyst-team.github.io/catalyst/#getting-started on Google Colab while only changing topk_args in AccuracyCallback.

#### Code sample ```python import os from torch import nn, optim from torch.utils.data import DataLoader from catalyst import dl, utils from catalyst.data.transforms import ToTensor from catalyst.contrib.datasets import MNIST model = nn.Sequential(nn.Flatten(), nn.Linear(28 * 28, 10)) criterion = nn.CrossEntropyLoss() optimizer = optim.Adam(model.parameters(), lr=0.02) loaders = { "train": DataLoader(MNIST(os.getcwd(), train=True, download=True, transform=ToTensor()), batch_size=32), "valid": DataLoader(MNIST(os.getcwd(), train=False, download=True, transform=ToTensor()), batch_size=32), } runner = dl.SupervisedRunner(input_key="features", output_key="logits", target_key="targets", loss_key="loss") # model training runner.train( model=model, criterion=criterion, optimizer=optimizer, loaders=loaders, num_epochs=1, callbacks=[ dl.AccuracyCallback(input_key="logits", target_key="targets", topk_args=(2, 3)), # catalyst[ml] required dl.ConfusionMatrixCallback(input_key="logits", target_key="targets", num_classes=10), ], logdir="./logs", valid_loader="valid", valid_metric="loss", minimize_valid_metric=True, verbose=True, load_best_on_end=True, ) # model inference for prediction in runner.predict_loader(loader=loaders["valid"]): assert prediction["logits"].detach().cpu().numpy().shape[-1] == 10 features_batch = next(iter(loaders["valid"]))[0] # model stochastic weight averaging model.load_state_dict(utils.get_averaged_weights_by_path_mask(logdir="./logs", path_mask="*.pth")) # model tracing utils.trace_model(model=runner.model, batch=features_batch) # model quantization utils.quantize_model(model=runner.model) # model pruning utils.prune_model(model=runner.model, pruning_fn="l1_unstructured", amount=0.8) # onnx export utils.onnx_export(model=runner.model, batch=features_batch, file="./logs/mnist.onnx", verbose=True) ```

Expected behavior

AccuracyCallback should work with any tuple of positive integers.

Environment

Catalyst version: 21.03.2
PyTorch version: 1.8.1+cu101
Is debug build: No
CUDA used to build PyTorch: 10.1
TensorFlow version: 2.4.1
TensorBoard version: 2.4.1

OS: Ubuntu 18.04.5 LTS
GCC version: (Ubuntu 7.5.0-3ubuntu1~18.04) 7.5.0
CMake version: version 3.12.0

Python version: 3.7
Is CUDA available: No
CUDA runtime version: No CUDA
GPU models and configuration: No CUDA
Nvidia driver version: No CUDA
cuDNN version: No CUDA

Versions of relevant libraries:
[pip3] catalyst==21.3.2
[pip3] numpy==1.19.5
[pip3] tensorboard==2.4.1
[pip3] tensorboard-plugin-wit==1.8.0
[pip3] tensorboardX==2.2
[pip3] tensorflow==2.4.1
[pip3] tensorflow-datasets==4.0.1
[pip3] tensorflow-estimator==2.4.0
[pip3] tensorflow-gcs-config==2.4.0
[pip3] tensorflow-hub==0.11.0
[pip3] tensorflow-metadata==0.29.0
[pip3] tensorflow-probability==0.12.1
[pip3] torch==1.8.1+cu101
[pip3] torchsummary==1.5.1
[pip3] torchtext==0.9.1
[pip3] torchvision==0.9.1+cu101
[conda] Could not collect

Additional context

From what I understood, bug appears because of this line https://github.com/catalyst-team/catalyst/blob/2ff687e802250772f8614583af933d6613f87788/catalyst/metrics/_accuracy.py#L82 ("01" after "accuracy" in string).

Checklist

github-actions[bot] commented 3 years ago

Hi! Thank you for your contribution! Please re-check all issue template checklists - unfilled issues would be closed automatically. And do not forget to join our slack for collaboration.

Scitator commented 3 years ago

hi, yeah, that's correct, Catalyst suppose to have top1 accuracy as the default alias :) as a possible solution we could always add (1, ) to the topk_args, but have not implemented it yet nevertheless, it would be a valuable contribution if you could do so šŸš€ The AccuracyMetric code is very straightforward.

stale[bot] commented 3 years ago

This issue has been automatically marked as stale because it has not had recent activity. It will be closed if no further activity occurs. Thank you for your contributions.