Beispiel #1
0
    def forward(self, y_logits: torch.Tensor, y_target: torch.Tensor) -> torch.Tensor:
        """ logsoftmax+nll
        :param y_logits: NCHW logits
        :param y_target: NCHW one-hot encoded label
        :return: cross_entropy loss
        """

        return F.cross_entropy(y_logits, y_target.argmax(1), reduction=self.reduction, weight=self.weight)
Beispiel #2
0
    def _obtain_labels_(
        self, score_arc: torch.Tensor, score_rel: torch.Tensor
    ) -> Tuple[List[List[int]], List[List[str]]]:

        arc_prediction: torch.Tensor = score_arc.argmax(-1)
        relation_prediction: torch.Tensor = score_rel.argmax(-1)
        relation_prediction = relation_prediction.gather(-1, arc_prediction.unsqueeze(-1)).squeeze(-1)

        decoded_arc_prediction = [
            [arc + 1 if token_index != arc else 0 for token_index, arc in enumerate(batch)] for batch in arc_prediction
        ]
        decoded_relation_prediction = [
            [self.relations_dictionary.get_item_for_index(rel_tag_idx) for rel_tag_idx in batch]
            for batch in relation_prediction
        ]

        return decoded_arc_prediction, decoded_relation_prediction
Beispiel #3
0
 def log_prob(self, scores: torch.Tensor,
              action: torch.Tensor) -> torch.Tensor:
     greedy_indices = self._get_greedy_indices(scores)
     # pyre-fixme[16]: `Tensor` has no attribute `argmax`.
     match = greedy_indices == action.argmax(-1)
     lp = torch.zeros(scores.shape[0]).float()
     lp[match] = -float("inf")
     return lp
Beispiel #4
0
def compute_guessed(logits: Tensor,
                    targets: Tensor,
                    to_device='cpu') -> Tensor:
    assert logits.ndim == 2
    assert targets.ndim == 1
    assert len(logits) == len(targets)

    return (logits.argmax(dim=1) == targets).float().detach().to(to_device)
 def log_prob(self, scores: torch.Tensor,
              action: torch.Tensor) -> torch.Tensor:
     max_index = self.sample_action(scores).argmax(-1)
     opt = max_index == action.argmax(-1)
     n = len(scores)
     lp = torch.ones(n) * self.epsilon / n
     lp[opt] = 1 - self.epsilon + self.epsilon / n
     return lp
Beispiel #6
0
def accuracy_rate(predictions: torch.Tensor, labels: torch.Tensor) -> float:
    """Return the accuracy rate based on dense predictions and sparse labels."""
    assert len(predictions) == len(labels), "Predictions and labels must have the same length."
    assert len(labels.shape) == 1, "Labels must be a column vector."

    return (  # type: ignore
        float((predictions.argmax(1) == labels.to(torch.long)).sum()) / predictions.shape[0]
    )
Beispiel #7
0
 def loss(self, cls_logits: torch.Tensor, gt_labels: torch.Tensor):
     losses = dict()
     cls_logits = cls_logits.view(-1, self.class_num)
     batch_size = cls_logits.size(0)
     cls_preds = cls_logits.argmax(dim=1)
     losses['loss_cls'] = F.cross_entropy(cls_logits, gt_labels.view(-1))
     losses['accuracy'] = (cls_preds.eq(gt_labels.view(-1))).float().sum() / batch_size
     return losses
Beispiel #8
0
def probs2class(probs: Tensor) -> Tensor:
    b, _, *img_shape = probs.shape
    assert simplex(probs)

    res = probs.argmax(dim=1)
    assert res.shape == (b, *img_shape)

    return res
Beispiel #9
0
 def forward(self, pred: T, y: T):
     if self.multiclass:
         assert len(pred.shape) == 2
         pred = pred.argmax(dim=1)
     else:
         assert len(pred.shape) == 1
         pred = pred > 0
     return self.metric(pred.long(), y.long())
Beispiel #10
0
def error_rate(predictions: torch.Tensor, labels: torch.Tensor) -> float:
    """Return the error rate based on dense predictions and dense labels."""
    check.equal_lengths(predictions, labels)
    check.len_eq(labels.shape, 1, "Labels must be a column vector")

    return (  # type: ignore
        1.0 - float((predictions.argmax(1) == labels.to(torch.long)).sum()) /
        predictions.shape[0])
Beispiel #11
0
 def forward(self, x: torch.Tensor) -> torch.Tensor:
     indexes = x.argmax(dim=self.index_dim)
     indexes = indexes.type(torch.int64).view(-1, 1)
     n_dims = self.n_dims  # if self.n_dims is not None else int(torch.max(indexes)) + 1
     one_hots = torch.zeros(indexes.size()[0],
                            n_dims).scatter_(1, indexes, 1)
     one_hots = one_hots.view(*indexes.shape, -1)
     return one_hots
Beispiel #12
0
  def compute(self, prediction: Tensor, target: Tensor):
    prediction = prediction.argmax(1)

    correct = (prediction == target).prod(axis=1)
    total = correct.shape[0]
    correct = correct.sum()

    return correct, total
Beispiel #13
0
 def log_prob(self, scores: torch.Tensor, action: torch.Tensor) -> torch.Tensor:
     max_index = self.sample_action(scores).argmax(-1)
     # pyre-fixme[16]: `Tensor` has no attribute `argmax`.
     opt = max_index == action.argmax(-1)
     n = len(scores)
     lp = torch.ones(n) * self.epsilon / n
     lp[opt] = 1 - self.epsilon + self.epsilon / n
     return lp
Beispiel #14
0
def probs2class(probs: Tensor) -> Tensor:
    b, _, w, h = probs.shape  # type: Tuple[int, int, int, int]
    assert simplex(probs)

    res = probs.argmax(dim=1)
    assert res.shape == (b, w, h)

    return res
Beispiel #15
0
    def losses(self, pred_scores: torch.Tensor, pred_deltas: torch.Tensor,
               proposals: List[Instances]) -> Dict[torch.Tensor, torch.Tensor]:

        gt_boxes = Boxes.cat([p.gt_boxes for p in proposals])
        gt_classes = torch.cat([p.gt_classes for p in proposals], dim=0)
        proposal_boxes = Boxes.cat([p.proposal_boxes for p in proposals])
        num_instances = gt_classes.numel()

        valid_idxs = gt_classes >= 0
        foreground_idxs = torch.nonzero(
            (gt_classes >= 0) & (gt_classes != self.num_classes),
            as_tuple=True)[0]

        pred_classes = pred_scores.argmax(dim=1)
        fg_gt_classes = gt_classes[foreground_idxs]
        fg_pred_classes = pred_classes[foreground_idxs]

        num_foreground = foreground_idxs.numel()
        num_false_negative = (
            fg_pred_classes == self.num_classes).nonzero().numel()
        num_accurate = (pred_classes == gt_classes).nonzero().numel()
        fg_num_accurate = (fg_pred_classes == fg_gt_classes).nonzero().numel()

        storage = get_event_storage()
        if num_instances > 0:
            storage.put_scalar("roi_head/cls_accuracy",
                               num_accurate / num_instances)
            if num_foreground > 0:
                storage.put_scalar("roi_head/fg_cls_accuracy",
                                   fg_num_accurate / num_foreground)
                storage.put_scalar("roi_head/false_negative",
                                   num_false_negative / num_foreground)

        if len(proposals) == 0:
            loss_cls = 0.0 * pred_scores.sum()
            loss_loc = 0.0 * pred_deltas.sum()
        else:
            loss_cls = F.cross_entropy(pred_scores,
                                       gt_classes,
                                       reduction="mean")
            gt_proposal_deltas = self.box2box_transform.get_deltas(
                proposal_boxes.tensor, gt_boxes.tensor)

            box_dim = gt_boxes.tensor.size(1)
            fg_gt_classes = gt_classes[foreground_idxs]
            gt_class_cols = box_dim * fg_gt_classes[:, None] + torch.arange(
                box_dim, device=pred_deltas.device)

            loss_loc = smooth_l1_loss(
                pred_deltas[foreground_idxs[:, None], gt_class_cols],
                gt_proposal_deltas[foreground_idxs],
                self.smooth_l1_beta,
                reduction="sum",
            ) / gt_classes.numel()

        loss_cls *= self.loss_weight.get('loss_cls', 1.0)
        loss_loc *= self.loss_weight.get('loss_loc', 1.0)
        return {"loss_cls": loss_cls, "loss_loc": loss_loc}
Beispiel #16
0
def Test_is_equal_label(label_OneHot, output: t.Tensor, backdoor_idx):
    """根据output 检查label是否已经发生了改变"""
    label_backdoor_OneHot = np.where(label_OneHot[backdoor_idx])[0][0]
    target_label = output.argmax().item()
    # print("后门label为{},目标label为{}".format(label_backdoor_OneHot, target_label))
    if label_backdoor_OneHot == target_label:
        return True
    else:
        return False
Beispiel #17
0
 def __call__(self, output: torch.Tensor, target: torch.Tensor):
     output = output.view(-1, output.shape[-1])
     target = target.view(-1)
     pred = output.argmax(dim=-1)
     mask = target == self.ignore_index
     pred.masked_fill_(mask, self.ignore_index)
     n_masked = mask.sum().item()
     self.data.correct += pred.eq(target).sum().item() - n_masked
     self.data.samples += len(target) - n_masked
Beispiel #18
0
def score_per_class(outputs: torch.Tensor, target, n_classes) -> torch.Tensor:
    pred = outputs.argmax(dim=2).t()
    correct = pred.eq(target.view(1, -1).expand_as(pred)).t()
    scores = [torch.zeros(outputs.size()[1], device=correct.device)] * n_classes
    for corr, expected in zip(correct, target):
        # if all values are true we point no one
        if not corr.all().data.item():
            scores[expected] = scores[expected] + corr.float()
    return torch.stack(scores, dim=1, out=None)
Beispiel #19
0
def score_per_model(outputs: torch.Tensor, target) -> torch.Tensor:
    pred = outputs.argmax(dim=2).t()
    correct = pred.eq(target.view(1, -1).expand_as(pred)).t()
    scores = [torch.zeros(outputs.size()[1], device=correct.device)]
    for row in correct:
        # if all values are true we point no one
        if not row.all().data.item():
            scores.append(row.float())
    return torch.vstack(scores, out=None).sum(dim=0)
Beispiel #20
0
 def forward(self, input: torch.Tensor, target: torch.Tensor):
     """
     :param input: [B, T, V]
     :param target: [B, T]
     :return:
     """
     input = input.softmax(-1)
     categorical_input = input.argmax(-1)
     return super().forward(categorical_input, target)
Beispiel #21
0
  def compute(self, prediction: Tensor, target: Tensor):
    prediction = prediction.argmax(1)
    pred_seq_len = (prediction != self._pad).sum(axis=1)
    target_seq_len = (target != self._pad).sum(axis=1)
    matches = torch.eq(pred_seq_len, target_seq_len)
    correct = matches.int().sum()
    total = matches.shape[0]

    return correct, total
Beispiel #22
0
 def trim_attention_matrices(
         attention_matrices: List[torch.Tensor],
         stop_tokens: torch.Tensor,
 ) -> List[List[torch.Tensor]]:
     stop_indexes = stop_tokens.argmax(dim=1)
     result = []
     for i, stop_index in enumerate(stop_indexes):
         result.append([matrix[i, :stop_index, :] for matrix in attention_matrices])
     return result
Beispiel #23
0
    def get_accuracy(outputs: torch.Tensor, labels: torch.Tensor) -> float:
        """
        Method to calculate accuracy of predicted outputs vs ground truth labels.

        :param outputs: torch.Tensor, predicted outputs classes
        :param labels: torch.Tensor, ground truth labels classes
        :return: float, accuracy of the predicted outputs vs the ground truth labels
        """
        return (outputs.argmax(dim=1) == labels).sum().item() / labels.shape[0]
Beispiel #24
0
 def loss(self, y_hat: torch.Tensor, y: torch.Tensor) -> torch.Tensor:
     try:
         loss = self.loss_fn(y_hat, y.argmax(dim=1))
     except BaseException as e:
         print(f'_SoftmaxHead: y_hat: {y_hat}, y: {y}')
         raise e
     if self.loss_reducer_fn is not None:
         loss = self.loss_reducer_fn(loss)
     return loss
Beispiel #25
0
def draw_segmentation_masks(
    image: torch.Tensor,
    masks: torch.Tensor,
    alpha: float = 0.2,
    colors: Optional[List[Union[str, Tuple[int, int, int]]]] = None,
) -> torch.Tensor:
    """
    Draws segmentation masks on given RGB image.
    The values of the input image should be uint8 between 0 and 255.

    Args:
        image (Tensor): Tensor of shape (3 x H x W) and dtype uint8.
        masks (Tensor): Tensor of shape (num_masks, H, W). Each containing probability of predicted class.
        alpha (float): Float number between 0 and 1 denoting factor of transpaerency of masks.
        colors (List[Union[str, Tuple[int, int, int]]]): List containing the colors of masks. The colors can
            be represented as `str` or `Tuple[int, int, int]`.
    """

    if not isinstance(image, torch.Tensor):
        raise TypeError(f"Tensor expected, got {type(image)}")
    elif image.dtype != torch.uint8:
        raise ValueError(f"Tensor uint8 expected, got {image.dtype}")
    elif image.dim() != 3:
        raise ValueError("Pass individual images, not batches")
    elif image.size()[0] != 3:
        raise ValueError(
            "Pass an RGB image. Other Image formats are not supported")

    num_masks = masks.size()[0]
    masks = masks.argmax(0)

    if colors is None:
        palette = torch.tensor([2**25 - 1, 2**15 - 1, 2**21 - 1])
        colors_t = torch.as_tensor([i for i in range(num_masks)
                                    ])[:, None] * palette
        color_arr = (colors_t % 255).numpy().astype("uint8")
    else:
        color_list = []
        for color in colors:
            if isinstance(color, str):
                # This will automatically raise Error if rgb cannot be parsed.
                fill_color = ImageColor.getrgb(color)
                color_list.append(fill_color)
            elif isinstance(color, tuple):
                color_list.append(color)

        color_arr = np.array(color_list).astype("uint8")

    _, h, w = image.size()
    img_to_draw = Image.fromarray(masks.byte().cpu().numpy()).resize((w, h))
    img_to_draw.putpalette(color_arr)

    img_to_draw = torch.from_numpy(np.array(img_to_draw.convert('RGB')))
    img_to_draw = img_to_draw.permute((2, 0, 1))

    return (image.float() * alpha + img_to_draw.float() *
            (1.0 - alpha)).to(dtype=torch.uint8)
Beispiel #26
0
def accuracy(y_true: torch.Tensor, y_pred: torch.Tensor) -> float:
    """Calculates categorical accuracy.

    # Arguments:
        y_true: Ground truth categories. Must have shape [batch_size,]
        y_pred: Prediction probabilities or logits of shape [batch_size, num_categories]
    """
    return torch.eq(y_pred.argmax(dim=-1),
                    y_true).sum().item() / y_pred.shape[0]
Beispiel #27
0
def decode_actions(
    node_logits: torch.Tensor,
    parent_label_logits: torch.Tensor,
    new_label_logits: torch.Tensor,
    label_vocab: List[Label],
) -> List[Any]:
    """
    Decode the most likely actions from action logits for the attach-juxtapose transition system
    """
    batch_size = node_logits.size(0)

    node_idx = node_logits.argmax(dim=-1)
    parent_label_idx = parent_label_logits.argmax(dim=-1)
    new_label_idx = new_label_logits.argmax(dim=-1)

    if node_logits.dim() == 2:
        # decode actions for only the current time step
        return [
            Action(
                "attach"
                if label_vocab[new_label] == DUMMY_LABEL else "juxtapose",
                target_node,
                label_vocab[parent_label],
                label_vocab[new_label],
            ) for target_node, parent_label, new_label in zip(
                node_idx.tolist(), parent_label_idx.tolist(),
                new_label_idx.tolist())
        ]
    else:
        # decode actions for all time steps
        max_len = node_idx.size(1)
        return [
            [
                Action(
                    "attach" if label_vocab[new_label_idx[i, j].item()] ==
                    DUMMY_LABEL  # type: ignore
                    else "juxtapose",
                    target_node=node_idx[i, j].item(),  # type: ignore
                    parent_label=label_vocab[parent_label_idx[
                        i, j]],  # type: ignore
                    new_label=label_vocab[new_label_idx[i, j]],  # type: ignore
                ) for j in range(max_len)
            ] for i in range(batch_size)
        ]
Beispiel #28
0
def _decode_deprels(
    logits_deprel: torch.Tensor, trees: torch.Tensor, root: int = 0
) -> torch.Tensor:
    steps = torch.arange(trees.size(1))
    logits_deprel = [logits_deprel[i, steps, heads] for i, heads in enumerate(trees)]
    logits_deprel = torch.stack(logits_deprel, dim=0)
    logits_deprel[:, :, root] = -float("inf")
    deprels = logits_deprel.argmax(dim=2).detach().cpu()
    deprels.masked_fill_(trees == 0, root)
    return deprels
Beispiel #29
0
def _extremity_game_target_function(
        context: torch.Tensor,
        function_selectors: torch.Tensor) -> torch.Tensor:
    func_idxs = function_selectors.argmax(dim=1)
    func_min_or_max = func_idxs % 2
    param_idxs = func_idxs // 2

    min_obj_per_param = context.argmin(dim=1)
    max_obj_per_param = context.argmax(dim=1)

    targets = []
    for batch in range(context.shape[0]):
        if func_min_or_max[batch] == 0:
            targets.append(
                context[batch, min_obj_per_param[batch][param_idxs[batch]]])
        else:
            targets.append(
                context[batch, max_obj_per_param[batch][param_idxs[batch]]])
    return torch.stack(targets)
Beispiel #30
0
def get_accuracy_with_logits(y_true: torch.Tensor,
                             y_pred: torch.Tensor) -> float:
    """
    Calculate accuracy with logits
    :param y_true: torch.Tensor of ints containing true labels. shape = (N,)
    :param y_pred: torch.Tensor of predicted logits. shape = (N, N_classes)
    :return acc: float which equals to accuracy score
    """

    return (y_true == y_pred.argmax(dim=1)).float().mean().item()