def match_anchor_to_bbox(ground_truth, anchors, device, iou_threshold=0.5):
    """Assign ground-truth bounding boxes to anchor boxes similar to them."""
    num_anchors, num_gt_boxes = anchors.shape[0], ground_truth.shape[0]

    # Element `x_ij` in the `i^th` row and `j^th` column is the IoU
    # of the anchor box `anc_i` to the ground-truth bounding box `box_j`

    jaccard = box_iou(anchors, ground_truth)
    # Initialize the tensor to hold assigned ground truth bbox for each anchor
    anchors_bbox_map = np.full((num_anchors, ),
                               fill_value=-1,
                               dtype=np.int32,
                               ctx=device)
    # Assign ground truth bounding box according to the threshold
    max_ious, indices = np.max(jaccard, axis=1), np.argmax(jaccard, axis=1)

    anc_i = np.nonzero(max_ious >= iou_threshold)[0]
    box_j = indices[max_ious >= iou_threshold]
    anchors_bbox_map[anc_i] = box_j

    # Find the largest iou for each bbox
    col_discard = np.full((num_anchors, ), -1)
    row_discard = np.full((num_gt_boxes, ), -1)

    for _ in range(num_gt_boxes):
        max_idx = np.argmax(jaccard)
        box_idx = (max_idx % num_gt_boxes).astype('int32')
        anc_idx = (max_idx / num_gt_boxes).astype('int32')
        anchors_bbox_map[anc_idx] = box_idx
        jaccard[:, box_idx] = col_discard
        jaccard[anc_idx, :] = row_discard

    return anchors_bbox_map
Example #2
0
def predict_snli(net, vocab, premise, hypothesis):
    premise = np.array(vocab[premise], ctx=d2l.try_gpu())
    hypothesis = np.array(vocab[hypothesis], ctx=d2l.try_gpu())
    label = np.argmax(net([premise.reshape((1, -1)),
                           hypothesis.reshape((1, -1))]), axis=1)
    return 'entailment' if label == 0 else 'contradiction' if label == 1 \
            else 'neutral'
Example #3
0
def test_acc(net, test_feats, test_thrpts, print_log=True):
    """Test the model accuracy."""

    if print_log:
        log.info('\nExpected\tThrpt-Pred\tThrpt-Error')

    thrpt_pred_range = (float('inf'), 0)

    # Confusion matrix for each class.
    error = 0
    TP = []
    FN = []
    FP = []

    for start in range(0, len(test_feats), 512):
        thrpt_preds = net(test_feats[start:min(start + 512, len(test_feats))])

        for idx, thrpt_pred_prob in enumerate(thrpt_preds):
            real = test_thrpts[start + idx].tolist()
            thrpt_pred = int(np.argmax(thrpt_pred_prob).tolist())

            while len(TP) <= max(real, thrpt_pred):
                TP.append(0)
                FN.append(0)
                FP.append(0)

            if thrpt_pred == real:
                TP[real] += 1
            else:
                error += 1
                FN[real] += 1
                FP[thrpt_pred] += 1

            thrpt_pred_range = (min(thrpt_pred_range[0], thrpt_pred),
                                max(thrpt_pred_range[1], thrpt_pred))
            if print_log:
                log.info('\t%d\t%d', real, thrpt_pred)

    accuracy = 100.0 * (1.0 - (error / len(test_feats)))
    recalls = [
        '{:.2f}%'.format(100.0 * tp / (tp + fn)) if tp + fn > 0 else 'N/A'
        for tp, fn in zip(TP, FN)
    ]
    precisions = [
        '{:.2f}%'.format(100.0 * tp / (tp + fp)) if tp + fp > 0 else 'N/A'
        for tp, fp in zip(TP, FP)
    ]

    log.info('Thrpt predict range: %d, %d', thrpt_pred_range[0],
             thrpt_pred_range[1])
    log.info('Recalls: %s', ', '.join(recalls[-3:-1]))
    log.info('Precisions: %s', ', '.join(precisions[-3:-1]))

    if print_log:
        log.info('Accuracy: %.2f%%', accuracy)
    return accuracy
def test_argmax():
    A = np.zeros((INT_OVERFLOW, 2))
    A[10][1] = 1
    A.attach_grad()
    with mx.autograd.record():
        B = np.argmax(A)
    print(B)
    assert B == 21
    B.backward()
    assert A.grad.shape == (INT_OVERFLOW, 2)
    assert A.grad[0][0] == 0
Example #5
0
    def decode_step(self,
                    step_input: np.ndarray,
                    states: List,
                    vocab_slice_ids: Optional[np.ndarray] = None):
        logits, states, target_factor_outputs = self._model.decode_step(step_input, states, vocab_slice_ids)
        if not self._skip_softmax:
            logits = npx.log_softmax(logits, axis=-1, temperature=self._softmax_temperature)
        scores = -logits

        target_factors = None  # type: Optional[np.ndarray]
        if target_factor_outputs:
            # target factors are greedily 'decoded'.
            factor_predictions = [npx.cast(np.expand_dims(np.argmax(tfo, axis=1), axis=1), dtype='int32') for tfo in target_factor_outputs]
            target_factors = factor_predictions[0] if len(factor_predictions) == 1 \
                else np.concatenate(factor_predictions, axis=1)
        return scores, states, target_factors
Example #6
0
def IoU(pred, target):
    eps = 1e-7
    '''
        iou or jaccard loss for binary classes
    '''
    intersect = pixel_accuracy(pred, target) * target.sum()
    if pred.shape[1] == 1:
        classes = (pred > 0.5).astype('int32')
    else:
        classes = np.argmax(pred, axis=1)
    union = pred.sum() + target.sum().astype('int32') - intersect.astype(
        'int32')

    if union > 0:
        sum_ious = intersect / (union + eps)
    elif union <= 0:
        sum_ious = 0
    '''
        Autograd wants to minimize a loss, so negate the IOU
    '''
    return 1. - sum_ious
Example #7
0
def pixel_accuracy(output, y):
    '''
        binary class prediction accuracy. 
        output is dim(B, 1, W, H, {D})
        target is dim(B,  W, H, {D})
    '''
    true_pos = np.sum(y)
    if output.shape[1] == 1:
        classes = (output > 0.5).astype('float32')
    if output.shape[1] == 2:
        classes = np.argmax(output, axis=1)
    acc = (classes.astype('bool') * y.astype('bool')).sum()
    # print('Acc:',acc)
    if true_pos > 0:
        pix_acc = acc / y.sum().astype('float32')
    if true_pos == 0 and acc == 0:
        pix_acc = 1.0
    if true_pos == 0 and acc != 0:
        pix_acc = 0.0

    return pix_acc
def multibox_detection(cls_probs,
                       offset_preds,
                       anchors,
                       nms_threshold=0.5,
                       pos_threshold=0.00999999978):
    device, batch_size = cls_probs.ctx, cls_probs.shape[0]
    anchors = np.squeeze(anchors, axis=0)
    num_classes, num_anchors = cls_probs.shape[1], cls_probs.shape[2]
    out = []
    # print(offset_preds)
    for i in range(batch_size):
        cls_prob, offset_pred = cls_probs[i], offset_preds[i].reshape(-1, 4)
        conf, class_id = np.max(cls_prob[1:], 0), np.argmax(cls_prob[1:], 0)
        predicted_bb = offset_inverse(anchors, offset_pred)
        keep = nms(predicted_bb, conf, 0.5)
        print(keep)
        # Find all non_keep indices and set the class_id to background
        all_idx = np.arange(num_anchors, dtype=np.int32, ctx=device)
        combined = np.concatenate((keep, all_idx))
        unique, counts = np.unique(combined, return_counts=True)
        print(unique, " . ", counts)
        non_keep = unique[counts == 1]
        all_id_sorted = np.concatenate((keep, non_keep))
        class_id[non_keep] = -1
        print(class_id)
        class_id = class_id[all_id_sorted].astype('float32')
        print(class_id)
        conf, predicted_bb = conf[all_id_sorted], predicted_bb[all_id_sorted]
        print(conf)
        print(predicted_bb)
        # threshold to be a positive prediction
        below_min_idx = (conf < pos_threshold)
        class_id[below_min_idx] = -1
        conf[below_min_idx] = 1 - conf[below_min_idx]
        pred_info = np.concatenate((np.expand_dims(
            class_id, axis=1), np.expand_dims(conf, axis=1), predicted_bb),
                                   axis=1)
        out.append(pred_info)
    return np.stack(out)
Example #9
0
    def get_corrupted_tokens(self, inputs, original_tokens, masked_positions, logits):
        """
        Sample from the generator to create corrupted input.

        Parameters
        ----------
        F
        inputs
            The masked input
            - layout = 'NT'
                Shape (batch_size, seq_length)
            - layout = 'TN'
                Shape (seq_length, batch_size)
        original_tokens
            The original tokens that appear in the unmasked input sequence
            Shape (batch_size, num_masked_positions).
        masked_positions
            The masked position of the sequence
            Shape (batch_size, num_masked_positions).
        logits
            The logits of each tokens
            Shape (batch_size, num_masked_positions, vocab_size)

        Returns
        -------
        corrupted_tokens
            Shape (batch_size, )
        fake_data
            - layout = 'NT'
                Shape (batch_size, seq_length)
            - layout = 'TN'
                Shape (seq_length, batch_size)
        labels
            - layout = 'NT'
                Shape (batch_size, seq_length)
            - layout = 'TN'
                Shape (seq_length, batch_size)
        """

        if self._disallow_correct:
            # TODO(sxjscience), Revise the implementation
            disallow = npx.one_hot(masked_positions, depth=self.vocab_size, dtype=self._dtype)
            logits = logits - 1000.0 * disallow
        # gumbel_softmax() samples from the logits with a noise of Gumbel distribution
        prob = gumbel_softmax(
            F,
            logits,
            temperature=self._temperature,
            eps=self._gumbel_eps,
            use_np_gumbel=False)
        corrupted_tokens = np.argmax(prob, axis=-1).astype(np.int32)

        if self.disc_backbone.layout == 'TN':
            inputs = inputs.T
        original_data = update_vectors_by_position(F,
            inputs, original_tokens, masked_positions)
        fake_data = update_vectors_by_position(F,
            inputs, corrupted_tokens, masked_positions)
        updates_mask = add_vectors_by_position(np.zeros_like(inputs),
                np.ones_like(masked_positions), masked_positions)
        # Dealing with multiple zeros in masked_positions which
        # results in a non-zero value in the first index [CLS]
        updates_mask = np.minimum(updates_mask, 1)
        labels = updates_mask * np.not_equal(fake_data, original_data)
        if self.disc_backbone.layout == 'TN':
            return corrupted_tokens, fake_data.T, labels.T
        else:
            return corrupted_tokens, fake_data, labels
Example #10
0
        train_acc_sum = sum(
            d2l.accuracy(py.asnumpy(), y.asnumpy()) for py, y in zip(pys, ys))
        l, acc = train_loss_sum, train_acc_sum
        metric.add(l, acc, ys_in.shape[0], ys_in.size)
        timer.stop()
        if (i + 1) % (num_batches // 5) == 0:
            animator.add(
                epoch + i / num_batches,
                (metric[0] / metric[2], metric[1] / metric[3], None, None))

    # val_acc = d2l.evaluate_accuracy_gpus(net, val_iter, split_f)
    metric_val = d2l.Accumulator(2)  # num_corrected_examples, num_examples
    for i, (Xs_in, ys_in) in enumerate(DataLoader_Single_test):
        Xs = gluon.utils.split_and_load(Xs_in.astype("float32"), ctx)
        ys = gluon.utils.split_and_load(ys_in.astype("float32"), ctx)
        pys = [net(X) for X in Xs]
        ls = [loss(py, y) for py, y in zip(pys, ys)]
        val_loss_sum = sum([float(l.sum().asnumpy()[0]) for l in ls])

        OA_val = np.sum(
            np.argmax(pys[0].asnumpy(), axis=1) == ys[0].asnumpy()).astype(
                "float32") / np.prod(ys[0].shape)
        metric_val.add(OA_val, len(ys))

    val_acc = OA_val
    animator.add(epoch + 1,
                 (None, None, val_loss_sum / ys_in.shape[0], val_acc))
print('loss %.3f, train acc %.3f, val acc %.3f' %
      (metric[0] / metric[2], metric[1] / metric[3], val_acc))
print('%.1f examples/sec on %s' %
      (metric[2] * num_epochs / timer.sum(), d2l.try_all_gpus()))
def predict_sentiment(net, vocab, sentence):
    sentence = np.array(vocab[sentence.split()], ctx=d2l.try_gpu())
    label = np.argmax(net(sentence.reshape(1, -1)), axis=1)
    return 'positive' if label == 1 else 'negative'