forked from apple/ml-cvnets
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbase_criteria.py
43 lines (32 loc) · 1.37 KB
/
base_criteria.py
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
36
37
38
39
40
41
42
43
#
# For licensing see accompanying LICENSE file.
# Copyright (C) 2020 Apple Inc. All Rights Reserved.
#
import torch
from torch import nn, Tensor
import argparse
from typing import Any
class BaseCriteria(nn.Module):
def __init__(self, *args, **kwargs):
super(BaseCriteria, self).__init__()
self.eps = 1e-7
@classmethod
def add_arguments(cls, parser: argparse.ArgumentParser):
return parser
def forward(self, input_sample: Tensor, prediction: Any, target: Tensor) -> Tensor:
raise NotImplementedError
@staticmethod
def _class_weights(target: Tensor, n_classes: int, norm_val: float = 1.1) -> Tensor:
class_hist: Tensor = torch.histc(target.float(), bins=n_classes, min=0, max=n_classes - 1)
mask_indices = (class_hist == 0)
# normalize between 0 and 1 by dividing by the sum
norm_hist = torch.div(class_hist, class_hist.sum())
norm_hist = torch.add(norm_hist, norm_val)
# compute class weights..
# samples with more frequency will have less weight and vice-versa
class_wts = torch.div(torch.ones_like(class_hist), torch.log(norm_hist))
# mask the classes which do not have samples in the current batch
class_wts[mask_indices] = 0.0
return class_wts.to(device=target.device)
def __repr__(self):
return '{}'.format(self.__class__.__name__)