Skip to content

ncdia.utils

ncdia.utils.metrics

ncdia.utils.metrics.accuracy

accuracy(output, target, topk=(1,))

Computes the accuracy over the k-top predictions for the specified values of k.

Parameters:

  • output (torch.Tensor): model output, shape (batch_size, num_classes)
  • target (torch.Tensor): target labels, shape (batch_size)
  • topk (tuple): top-k values, default is (1,)

Returns:

  • acc (list): accuracy values for each k in topk

per_class_accuracy(output, target, topk=(1, ))

Compute per class accuracy over the k-top predictions for the specified values of k

Parameters:

  • output (torch.Tensor): model output, shape (batch_size, num_classes)
  • target (torch.Tensor): target labels, shape (batch_size)
  • topk (tuple): top-k values, default is (1,)

Returns:

  • acc (list): accuracy values for each k in topk

ncdia.utils.metrics.meter

AverageMeter

Computes and stores the average and current value.

ncdia.utils.losses

CrossEntropyLoss

CrossEntropyLoss with label smoothing.

AngularPenaltySMLoss

Angular Penalty Softmax Loss. Three loss_types available: arcface, sphereface, cosface

ncdia.utils.cfg

Configs

Include implementation of setup and use of configs.

ncdia.utils.logger

Include implementation of loggers to write output to console and external text file.

ncdia.utils.registry

Registry

A registry to map strings to classes or functions.

Examples:

Text Only
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
>>> REGISTRY = Registry()
>>> @REGISTRY
>>> def foo():
>>>     return 'foo'
>>> @REGISTRY.register
>>> def bar():
>>>     return 'bar'

>>> print(REGISTRY['foo']())
foo
>>> print(REGISTRY['bar']())
bar

>>> print(REGISTRY)
{'foo': <function foo at 0x7f9b1c0e0d30>, 'bar': <function bar at 0x7f9b1c0e0e18>}
>>> print(REGISTRY['foo'])
<function foo at 0x7f9b1c0e0d30>
>>> print(REGISTRY['bar'])
<function bar at 0x7f9b1c0e0e18>

>>> print('foo' in REGISTRY)
True
>>> print('bar' in REGISTRY)
True
>>> print('foobar' in REGISTRY)
False

>>> print(REGISTRY.keys())
dict_keys(['foo', 'bar'])
>>> print(REGISTRY.values())
dict_values([<function foo at 0x7f9b1c0e0d30>, <function bar at 0x7f9b1c0e0e18>])
>>> print(REGISTRY.items())
dict_items([('foo', <function foo at 0x7f9b1c0e0d30>), ('bar', <function bar at 0x7f9b1c0e0e18>)])
>>> print(len(REGISTRY))
2

register_callable(self, target: callable)

Register a target.

Parameters:

  • target (callable): callable target to be registered.

register_dict(self, target)

Register a dict.

Parameters:

  • target (dict): A dict to be registered. All its values should be callable.

register(self, target)

Register a target.

Parameters:

  • target (callable | dict): target to be registered.

Returns:

  • target (object): Registered target.

build(self, target: dict | Configs, **kwargs)

Build a target with configs.

Parameters:

  • target (dict | Configs): A dict to be built. It should have a key 'type' to specify the target type. It may have other keys to specify the target configs.
  • kwargs (dict): Additional keyword arguments.

Returns:

  • target (object): A built target.

ncdia.utils.tools

mkdir_if_missing(dirname)

Create dirname if it is missing.

Parameters:

  • dirname (str): directory path

auto_device(device)

Automatically set the device for the input tensor.

Parameters:

  • device (str | torch.device): device name or device object. If None, return torch.device('cuda') if available, otherwise return torch.device('cpu').

set_random_seed(seed)

Set random seed for reproducibility.

Parameters:

  • seed (int): random seed