Ejemplo n.º 1
0
    def compute(self) -> Tuple[torch.Tensor, torch.Tensor, torch.Tensor]:
        if len(self._predictions) < 1 or len(self._targets) < 1:
            raise NotComputableError("PrecisionRecallCurve must have at least one example before it can be computed.")

        _prediction_tensor = torch.cat(self._predictions, dim=0)
        _target_tensor = torch.cat(self._targets, dim=0)

        ws = idist.get_world_size()
        if ws > 1 and not self._is_reduced:
            # All gather across all processes
            _prediction_tensor = cast(torch.Tensor, idist.all_gather(_prediction_tensor))
            _target_tensor = cast(torch.Tensor, idist.all_gather(_target_tensor))
        self._is_reduced = True

        if idist.get_rank() == 0:
            # Run compute_fn on zero rank only
            precision, recall, thresholds = self.compute_fn(_prediction_tensor, _target_tensor)
            precision = torch.tensor(precision)
            recall = torch.tensor(recall)
            # thresholds can have negative strides, not compatible with torch tensors
            # https://discuss.pytorch.org/t/negative-strides-in-tensor-error/134287/2
            thresholds = torch.tensor(thresholds.copy())
        else:
            precision, recall, thresholds = None, None, None

        if ws > 1:
            # broadcast result to all processes
            precision = idist.broadcast(precision, src=0, safe_mode=True)
            recall = idist.broadcast(recall, src=0, safe_mode=True)
            thresholds = idist.broadcast(thresholds, src=0, safe_mode=True)

        return precision, recall, thresholds
Ejemplo n.º 2
0
 def compute(self) -> torch.Tensor:
     if self._num_examples == 0:
         raise NotComputableError(
             "SSIM must have at least one example before it can be computed."
         )
     return torch.sum(self._sum_of_batchwise_ssim /
                      self._num_examples)  # type: ignore[arg-type]
Ejemplo n.º 3
0
 def compute(self):
     if self._num_distributions.eq(0):
         raise NotComputableError(
             'Accuracy must have at least one example before it can be computed'
         )
     ppl = self._perplexities_sum / self._num_distributions
     return ppl
Ejemplo n.º 4
0
    def compute(self):
        if (self._num_examples['combined_loss'] == 0):
            raise NotComputableError(
                'Loss must have at least one example before it can be computed.'
                + str(self._num_examples))

        combined_loss = self._sum['combined_loss'] / self._num_examples[
            'combined_loss']
        relevance_loss = self._sum['relevance_loss'] / self._num_examples[
            'relevance_loss']

        results = {
            'combined_loss': combined_loss,
            'relevance_loss': relevance_loss
        }

        if "diversity_loss" in self._sum:
            diversity_loss = self._sum['diversity_loss'] / self._num_examples[
                'diversity_loss']
            results["diversity_loss"] = diversity_loss

        if "context_loss" in self._sum:
            context_loss = self._sum['context_loss'] / self._num_examples[
                'context_loss']
            results["context_loss"] = context_loss

        return results
Ejemplo n.º 5
0
 def compute(self):
     if self._num_examples == 0:
         raise NotComputableError(
             'MeanSquaredError must have at least one example before it can be computed.'
         )
     return torch.mean(
         torch.div(self._sum_of_squared_errors, self._num_examples))
Ejemplo n.º 6
0
    def compute(self) -> float:
        if len(self._probs) < 1:
            raise NotComputableError(
                "Inception score must have at least one example before it can be computed."
            )

        ws = idist.get_world_size()
        _probs_tensor = torch.cat(self._probs, dim=0)

        if ws > 1 and not self._is_reduced:
            _probs_tensor = cast(torch.Tensor, idist.all_gather(_probs_tensor))
        self._is_reduced = True

        result = 0.0
        if idist.get_rank() == 0:
            N = _probs_tensor.shape[0]
            scores = torch.zeros((self.n_splits, ))
            for i in range(self.n_splits):
                part = _probs_tensor[i * (N // self.n_splits):(i + 1) *
                                     (N // self.n_splits)]
                kl = part * (torch.log(part) -
                             torch.log(torch.mean(part, dim=0)))
                kl = torch.mean(torch.sum(kl, dim=1))
                scores[i] = torch.exp(kl)
            result = torch.mean(scores).item()
        if ws > 1:
            result = cast(float, idist.broadcast(result, src=0))

        return result
Ejemplo n.º 7
0
 def compute(self):
     """ """
     if self._num_examples == 0:
         raise NotComputableError(
             "Accuracy must have at least one example before it can be computed."
         )
     return self._num_correct / self._num_examples
Ejemplo n.º 8
0
    def compute(self):
        if self._num_examples == 0:
            raise NotComputableError(
                "CustomAccuracy must have at least one example before it can be computed."
            )

        return (self._sum_percentages / self._num_examples) * 100
Ejemplo n.º 9
0
    def compute_impl(self):
        if not self._preds:
            raise NotComputableError("NRMSE must have at least one example before it can be computed.")

        pred = torch.stack(self._preds).numpy()
        tgt = torch.stack(self._tgts).numpy()
        return nrmse_skimage(pred, tgt, norm_type=self.norm_type)
Ejemplo n.º 10
0
    def compute(self) -> Union[Any, torch.Tensor, numbers.Number]:
        if self.num_examples < 1:
            raise NotComputableError(
                "{} must have at least one example before" " it can be computed.".format(self.__class__.__name__)
            )

        return self.accumulator / self.num_examples
Ejemplo n.º 11
0
    def compute(self) -> torch.Tensor:
        if self.num_examples < 1:
            raise NotComputableError(
                "{} must have at least one example before" " it can be computed.".format(self.__class__.__name__)
            )

        return torch.exp(self.accumulator / self.num_examples)
Ejemplo n.º 12
0
    def _compute_macro(self) -> torch.Tensor:
        if self._num_sentences == 0:
            raise NotComputableError(
                "Bleu must have at least one example before it can be computed."
            )

        return self._sum_of_bleu / self._num_sentences
Ejemplo n.º 13
0
 def compute(self):
     if self.false_negatives + self.true_positives == 0:
         raise NotComputableError(
             'Accuracy must have at least one example before it can be computed.'
         )
     return float(self.true_positives) / float(self.false_negatives +
                                               self.true_positives)
Ejemplo n.º 14
0
 def compute(self):
     """ """
     if self._num_examples == 0:
         raise NotComputableError(
             "Loss must have at least one example before it can be computed."
         )
     return self._sum_generator_loss / self._num_examples, self._sum_discriminator_loss / self._num_examples
Ejemplo n.º 15
0
    def compute(self) -> Union[torch.Tensor, float]:
        is_scalar = not isinstance(self._positives,
                                   torch.Tensor) or self._positives.ndim == 0
        if is_scalar and self._positives == 0:
            raise NotComputableError(
                f"{self.__class__.__name__} must have at least one example before it can be computed."
            )
        if not self._is_reduced:
            if not (self._type == "multilabel" and not self._average):
                self._true_positives = idist.all_reduce(
                    self._true_positives)  # type: ignore[assignment]
                self._positives = idist.all_reduce(
                    self._positives)  # type: ignore[assignment]
            else:
                self._true_positives = cast(
                    torch.Tensor, idist.all_gather(self._true_positives))
                self._positives = cast(torch.Tensor,
                                       idist.all_gather(self._positives))
            self._is_reduced = True  # type: bool

        result = self._true_positives / (self._positives + self.eps)

        if self._average:
            return cast(torch.Tensor, result).mean().item()
        else:
            return result
Ejemplo n.º 16
0
 def compute(self):
     if self._num_examples == 0:
         raise NotComputableError(
             "R2Score must have at least one example before it can be computed."
         )
     return 1 - self._sum_of_errors / (
         self._y_sq_sum - (self._y_sum**2) / self._num_examples)
Ejemplo n.º 17
0
    def compute_impl(self):
        if self._num_examples == 0:
            raise NotComputableError(
                "PSNR must have at least one example before it can be computed."
            )

        return self._sum / self._num_examples
 def _update(self, output):
     y_pred, y = output
     if (y == 0).any():
         raise NotComputableError("The ground truth has 0.")
     absolute_error = torch.abs(y_pred - y.view_as(y_pred)) / torch.abs(y.view_as(y_pred))
     self._sum_of_absolute_relative_errors += torch.sum(absolute_error).item()
     self._num_samples += y.size()[0]
Ejemplo n.º 19
0
 def compute(self):
     if self.all_embeddings is None:
         raise NotComputableError(
             'EmbeddingInfo must have at least one example before it can be computed.'
         )
     return self.all_embeddings.cpu(), self.all_image_data.cpu(
     ), self.all_targets
Ejemplo n.º 20
0
    def compute(self) -> float:
        if len(self._predictions) < 1 or len(self._targets) < 1:
            raise NotComputableError(
                "EpochMetric must have at least one example before it can be computed."
            )

        _prediction_tensor = torch.cat(self._predictions, dim=0)
        _target_tensor = torch.cat(self._targets, dim=0)

        ws = idist.get_world_size()

        if ws > 1 and not self._is_reduced:
            # All gather across all processes
            _prediction_tensor = cast(torch.Tensor,
                                      idist.all_gather(_prediction_tensor))
            _target_tensor = cast(torch.Tensor,
                                  idist.all_gather(_target_tensor))
        self._is_reduced = True

        result = 0.0
        if idist.get_rank() == 0:
            # Run compute_fn on zero rank only
            result = self.compute_fn(_prediction_tensor, _target_tensor)

        if ws > 1:
            # broadcast result to all processes
            result = cast(float, idist.broadcast(result, src=0))

        return result
Ejemplo n.º 21
0
    def compute(self) -> Union[float, torch.Tensor]:
        if self.num_examples < 1:
            raise NotComputableError(
                f"{self.__class__.__name__} must have at least one example before it can be computed."
            )

        return self.accumulator / self.num_examples
Ejemplo n.º 22
0
 def compute(self):
     """ """
     if self._num_examples_positive == 0 or self._num_examples_negative == 0:
         raise NotComputableError("AverageDistances needs at least one example per target to be computed.")
     return (
         self._sum_positive_distances / self._num_examples_positive,
         self._sum_negative_distances / self._num_examples_negative,
     )
Ejemplo n.º 23
0
 def compute(self, prefix=None) -> dict:
     if self._num_examples == 0:
         raise NotComputableError("TopKCategoricalAccuracy must have at"
                                  "least one example before it can be computed.")
     if prefix is None:
         return {f"top_k@{k}": self._num_correct[k] / self._num_examples for k in self.k_s}
     else:
         return {f"{prefix}top_k@{k}": self._num_correct[k] / self._num_examples for k in self.k_s}
Ejemplo n.º 24
0
 def compute(self):
     if self._num_examples == 0:
         raise NotComputableError(
             'Accuracy must have at least one example before it can be computed.'
         )
     return self._l1_loss_age / self._num_examples,\
            self._num_correct[0] / self._num_examples,\
            self._num_correct[1] / self._num_examples
Ejemplo n.º 25
0
    def compute(self):
        if self._num_examples == 0:
            raise NotComputableError(
                'RootMeanSquaredError must have at least one example before it can be computed.'
            )
        mse = self._sum_of_squared_errors / self._num_examples

        return torch.sqrt(mse).cpu().numpy()
Ejemplo n.º 26
0
 def compute(self) -> float:
     if self._num_examples == 0:
         raise NotComputableError(
             "GeometricMeanAbsoluteError must have at least one example before it can be computed."
         )
     return torch.exp(
         cast(torch.Tensor, self._sum_of_errors) /
         self._num_examples).item()
Ejemplo n.º 27
0
 def compute(self):
     if self._num_examples == 0:
         raise NotComputableError(
             'Confusion matrix must have at least one example before it can be computed.'
         )
     if self.average_samples:
         return self.confusion_matrix / self._num_examples
     return self.confusion_matrix.cpu()
Ejemplo n.º 28
0
    def compute(self):
        for k in self.kv_sum_metric.keys():
            if self.kv_sum_inst[k] == 0:
                raise NotComputableError('Accuracy must have at least one example before it can be computed')

            metric_value = self.kv_sum_metric[k] / self.kv_sum_inst[k]
            self.kv_metric[k] = metric_value.item()

        return self.kv_metric
Ejemplo n.º 29
0
    def _update(self, output):
        y_pred, y = output

        if (y == 0).any():
            raise NotComputableError("The ground truth has 0.")

        errors = (y.view_as(y_pred) - y_pred) / y
        self._sum_of_errors += torch.sum(errors).item()
        self._num_examples += y.shape[0]
Ejemplo n.º 30
0
 def compute(self):
     if self._actual is None:
         raise NotComputableError('Recall must have at least one example before it can be computed')
     result = self._true_positives / self._actual
     result[result != result] = 0.0
     if self._average:
         return result.mean().item()
     else:
         return result