vissl/meters/accuracy_list_meter.py [73:166]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    @property
    def value(self):
        """
        Value of the meter globally synced. For each output, all the top-k values are
        returned. If there are several meters attached to the same layer
        name, a list of top-k values will be returned for that layer name meter.
        """
        val_dict = {}
        for ind, meter in enumerate(self._meters):
            meter_val = meter.value
            sample_count = meter._total_sample_count
            val_dict[ind] = {}
            val_dict[ind]["val"] = meter_val
            val_dict[ind]["sample_count"] = sample_count
        # also create dict w.r.t top-k
        output_dict = {}
        for k in self._topk_values:
            top_k_str = f"top_{k}"
            output_dict[top_k_str] = {}
            for ind in range(len(self._meters)):
                meter_name = (
                    self._meter_names[ind] if (len(self._meter_names) > 0) else ind
                )
                val = 100.0 * round(float(val_dict[ind]["val"][top_k_str]), 6)
                # we could have several meters with the same name. We append the result
                # to the dict.
                if meter_name not in output_dict[top_k_str]:
                    output_dict[top_k_str][meter_name] = [val]
                else:
                    output_dict[top_k_str][meter_name].append(val)
        for topk in output_dict:
            for k in output_dict[topk]:
                if len(output_dict[topk][k]) == 1:
                    output_dict[topk][k] = output_dict[topk][k][0]
        return output_dict

    def sync_state(self):
        """
        Globally syncing the state of each meter across all the trainers.
        """
        for _, meter in enumerate(self._meters):
            meter.sync_state()

    def get_classy_state(self):
        """
        Returns the states of each meter
        """
        meter_states = {}
        for ind, meter in enumerate(self._meters):
            state = meter.get_classy_state()
            meter_states[ind] = {}
            meter_states[ind]["state"] = state
        return meter_states

    def set_classy_state(self, state):
        """
        Set the state of each meter
        """
        assert len(state) == len(self._meters), "Incorrect state dict for meters"
        for ind, meter in enumerate(self._meters):
            meter.set_classy_state(state[ind]["state"])

    def __repr__(self):
        value = self.value
        # convert top_k list into csv format for easy copy pasting
        for k in self._topk_values:
            top_k_str = f"top_{k}"
            hr_format = ["%.1f" % (100 * x) for x in value[top_k_str]]
            value[top_k_str] = ",".join(hr_format)

        repr_dict = {"name": self.name, "num_meters": self._num_meters, "value": value}
        return pprint.pformat(repr_dict, indent=2)

    def update(
        self,
        model_output: Union[torch.Tensor, List[torch.Tensor]],
        target: torch.Tensor,
    ):
        """
        Updates the value of the meter for the given model output list and targets.

        Args:
            model_output: list of tensors of shape (B, C) where each value is
                          either logit or class probability.
            target:       tensor of shape (B).

        NOTE: For binary classification, C=2.
        """
        if isinstance(model_output, torch.Tensor):
            model_output = [model_output]
        assert isinstance(model_output, list)
        assert len(model_output) == self._num_meters
        for (meter, output) in zip(self._meters, model_output):
            meter.update(output, target)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



vissl/meters/precision_at_k_list_meter.py [73:167]:
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    @property
    def value(self):
        """
        Value of the meter globally synced. For each output, all the top-k values are
        returned. If there are several meters attached to the same layer
        name, a list of top-k values will be returned for that layer name meter.
        """
        val_dict = {}
        for ind, meter in enumerate(self._meters):
            meter_val = meter.value
            sample_count = meter._total_sample_count
            val_dict[ind] = {}
            val_dict[ind]["val"] = meter_val
            val_dict[ind]["sample_count"] = sample_count

        # also create dict w.r.t top-k
        output_dict = {}
        for k in self._topk_values:
            top_k_str = f"top_{k}"
            output_dict[top_k_str] = {}
            for ind in range(len(self._meters)):
                meter_name = (
                    self._meter_names[ind] if (len(self._meter_names) > 0) else ind
                )
                val = 100.0 * round(float(val_dict[ind]["val"][top_k_str]), 6)
                # we could have several meters with the same name. We append the result
                # to the dict.
                if meter_name not in output_dict[top_k_str]:
                    output_dict[top_k_str][meter_name] = [val]
                else:
                    output_dict[top_k_str][meter_name].append(val)
        for topk in output_dict:
            for k in output_dict[topk]:
                if len(output_dict[topk][k]) == 1:
                    output_dict[topk][k] = output_dict[topk][k][0]
        return output_dict

    def sync_state(self):
        """
        Globally syncing the state of each meter across all the trainers.
        """
        for _, meter in enumerate(self._meters):
            meter.sync_state()

    def get_classy_state(self):
        """
        Returns the states of each meter
        """
        meter_states = {}
        for ind, meter in enumerate(self._meters):
            state = meter.get_classy_state()
            meter_states[ind] = {}
            meter_states[ind]["state"] = state
        return meter_states

    def set_classy_state(self, state):
        """
        Set the state of each meter
        """
        assert len(state) == len(self._meters), "Incorrect state dict for meters"
        for ind, meter in enumerate(self._meters):
            meter.set_classy_state(state[ind]["state"])

    def __repr__(self):
        value = self.value
        # convert top_k list into csv format for easy copy pasting
        for k in self._topk_values:
            top_k_str = f"top_{k}"
            hr_format = ["%.1f" % (100 * x) for x in value[top_k_str]]
            value[top_k_str] = ",".join(hr_format)

        repr_dict = {"name": self.name, "num_meters": self._num_meters, "value": value}
        return pprint.pformat(repr_dict, indent=2)

    def update(
        self,
        model_output: Union[torch.Tensor, List[torch.Tensor]],
        target: torch.Tensor,
    ):
        """
        Updates the value of the meter for the given model output list and targets.

        Args:
            model_output: list of tensors of shape (B, C) where each value is
                          either logit or class probability.
            target:       tensor of shape (B).

        NOTE: For binary classification, C=2.
        """
        if isinstance(model_output, torch.Tensor):
            model_output = [model_output]
        assert isinstance(model_output, list)
        assert len(model_output) == self._num_meters
        for (meter, output) in zip(self._meters, model_output):
            meter.update(output, target)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -



