Exemplo n.º 1
0
def compute_overlap(box_arr, anchors_arr, threshold):
    iou = jaccard_overlap(box_arr, anchors_arr)
    overlap = iou > threshold

    good_idxs = np.nonzero(overlap)[0]
    best_idx = np.argmax(iou)
    best = None
    good = []

    if iou[best_idx] > threshold:
        best = Score(best_idx, iou[best_idx])

    for idx in good_idxs:
        good.append(Score(idx, iou[idx]))

    return Overlap(best, good)
Exemplo n.º 2
0
def test(epoch):
    print(">>>Test: ")
    global best_iou
    model.eval()
    preds = []
    gts = []
    with torch.no_grad():
        for i, (img, mask) in val_loader:
            img = img.to(device)
            output = model(img)
            output = F.interpolate(output,
                                   size=(h, w),
                                   mode='bilinear',
                                   align_corners=True)
            probs = F.softmax(output, dim=1)
            _, pred = torch.max(probs, dim=1)
            pred = pred.cpu().data[0].numpy()

            label = mask.cpu().data[0].numpy()
            pred = np.asarray(pred, dtype=np.int)
            label = np.asarray(label, dtype=np.int)
            gts.append(label)
            preds.append(preds)

    whole_brain_preds = np.dstack(preds)
    whole_brain_gts = np.dstack(gts)
    running_metrics = Score(9)
    running_metrics.update(whole_brain_gts, whole_brain_preds)
    scores, class_iou = running_metrics.get_scores()
    mIoU = np.nanmean(class_iou[1::])
    mean_dice = (mIoU * 2) / (mIoU + 1)

    print("mean Iou", mIoU, "mean dice", mean_dice)
    if mIoU > best_iou:
        best_iou = mIoU
        state = {
            "epoch": epoch + 1,
            "model_state": model.state_dict(),
            "optimizer_state": optimizer.state_dict(),
            "scheduler_state": scheduler.state_dict(),
            "best_iou": best_iou,
        }
        save_path = osp.join(osp.split(resume_path)[0], "best_model.pkl")
        print("saving......")
        torch.save(state, save_path)
    return mIoU, mean_dice
Exemplo n.º 3
0
    def eval(self, dataset):
        tp, fp, tn, fn = 0, 0, 0, 0
        with torch.no_grad():
            for data in tqdm(dataset):
                # flush to Bert
                fname, example = data

                try:
                    fvs, input_ids, label_ids, gold_labels = self.transforms(
                        example, dataset.label_list)
                except RuntimeError:
                    print(f"{fname} cannot put in memory!")
                    continue

                # extract Element/Main tokens
                ents, ent_golds, _ = self.extract_tokens(
                    fvs.squeeze(0), gold_labels)

                for i, ent in enumerate(ents):
                    # convert to torch.tensor
                    inputs = torch.empty(
                        [len(ent),
                         self.bert_config.hidden_size]).to(self.device)
                    for j, token in enumerate(ent):
                        inputs[j, :] = token
                    target = ent_golds[i]
                    inputs = torch.mean(inputs, dim=0, keepdim=True)

                    # classification
                    outputs = self.mlp(inputs)
                    if target == 1:
                        if outputs < 0.5:
                            fn += 1
                        else:
                            tp += 1
                    else:
                        if outputs < 0.5:
                            tn += 1
                        else:
                            fp += 1

        return Score(tp, fp, tn, fn).calc_score()
Exemplo n.º 4
0
    def eval(self):
        self.load_model()

        correct_save_dir = self.save_dir / "correct"
        if not correct_save_dir.exists():
            correct_save_dir.mkdir(parents=True)
        incorrect_save_dir = self.save_dir / "incorrect"
        if not incorrect_save_dir.exists():
            incorrect_save_dir.mkdir(parents=True)

        tp, fp, tn, fn = 0, 0, 0, 0
        with torch.no_grad():
            for gold_data, target_data in tqdm(
                    zip(self.gold_dataset, self.target_dataset)):
                # flush to Bert
                gold_fname, gold_example = gold_data
                target_fname, target_example = target_data
                if not gold_fname == target_fname:
                    import pdb
                    pdb.set_trace()
                assert gold_fname == target_fname

                _, _, _, _, gold_labels = self.transforms(
                    gold_example, self.gold_dataset.label_list, is_gold=True)
                fvs, input_ids, label_ids, label_map, pred_labels = self.transforms(
                    target_example,
                    self.target_dataset.label_list,
                    is_gold=False)

                # extract Element/Main tokens
                is_correct = True
                _, ent_gold_labels, golds_mask = Trainer.extract_tokens(
                    fvs.squeeze(0), gold_labels)
                golds = {}
                if len(ent_gold_labels) >= 1:
                    i = 0
                    while True:
                        try:
                            ent_start = golds_mask.index(i)
                        except ValueError:
                            break
                        for n, j in enumerate(golds_mask[ent_start:]):
                            if j != i:
                                ent_end = (ent_start + n - 1)
                                break
                        golds[(ent_start, ent_end)] = ent_gold_labels[i]
                        i += 1

                ents, ent_pred_labels, preds_mask = Trainer.extract_tokens(
                    fvs.squeeze(0), pred_labels)

                preds = {}
                if len(ent_pred_labels) >= 1:
                    i = 0
                    while True:
                        try:
                            ent_start = preds_mask.index(i)
                        except ValueError:
                            break
                        for n, j in enumerate(preds_mask[ent_start:]):
                            if j != i:
                                ent_end = (ent_start + n - 1)
                                break
                        preds[(ent_start, ent_end)] = ent_pred_labels[i]
                        i += 1
                for gold_span, gold_label in golds.items():
                    if gold_span not in preds.keys():
                        if gold_label == 1:
                            fn += 1
                            is_correct = False

                ents_pred = [0] * len(ents)
                for i, pred in enumerate(preds):
                    # convert to torch.tensor
                    inputs = torch.empty(
                        [len(ents[i]),
                         self.bert_config.hidden_size]).to(self.device)
                    for j, token in enumerate(ents[i]):
                        inputs[j, :] = token

                    inputs = torch.mean(inputs, dim=0, keepdim=True)
                    outputs = self.mlp(inputs)

                    if pred in golds.keys():
                        target = golds[pred]
                        if target == 1:
                            if outputs < self.clf_th:
                                fn += 1
                                is_correct = False
                            else:
                                tp += 1
                        else:
                            if outputs < self.clf_th:
                                tn += 1
                            else:
                                fp += 1
                                is_correct = False
                    else:
                        if outputs < self.clf_th:
                            pass
                        else:
                            fp += 1
                            is_correct = False

                    outputs_ = outputs.to('cpu').detach().numpy().copy()
                    if np.all(outputs_ > self.clf_th):
                        ents_pred[i] = 1

                if is_correct:
                    save_dir = correct_save_dir
                else:
                    save_dir = incorrect_save_dir
                save_path = save_dir / (target_fname + ".conll")
                lines = []
                elem_cnt = -1
                for i in range(len(target_example.text)):
                    text = target_example.text[i]
                    label = target_example.label[i]
                    start = target_example.start[i]
                    end = target_example.end[i]
                    if label == "B-Element":
                        elem_cnt += 1
                        if ents_pred[elem_cnt] == 1:
                            lines.append(f"B-Main\t{start}\t{end}\t{text}")
                        elif ents_pred[elem_cnt] == 0:
                            lines.append(f"{label}\t{start}\t{end}\t{text}")
                    elif label == "I-Element":
                        if ents_pred[elem_cnt] == 1:
                            lines.append(f"I-Main\t{start}\t{end}\t{text}")
                        elif ents_pred[elem_cnt] == 0:
                            lines.append(f"{label}\t{start}\t{end}\t{text}")
                    else:
                        lines.append(f"{label}\t{start}\t{end}\t{text}")

                with save_path.open("w") as f:
                    f.write("\n".join(lines))

        return Score(tp, fp, tn, fn).calc_score()
Exemplo n.º 5
0
from utils import GreedyMotifSearch, RandomizedMotifSearch, GibbsSampler, Consensus, Score


if __name__ == '__main__':
    DosR = []
    with open('data//DosR.txt') as file:
        DosR = file.read().splitlines()
    print(DosR)
    motifs = GreedyMotifSearch(DosR, 15, WithPseudocounts=False)
    consensus = Consensus(motifs)
    print(consensus)
    print(Score(motifs), consensus)
    motifs = RandomizedMotifSearch(DosR, 15)
    consensus = Consensus(motifs)
    print(consensus)
    print(Score(motifs), consensus)
    motifs = GibbsSampler(DosR, 15, 10, 100)
    consensus = Consensus(motifs)
    print(motifs)
    print(consensus)
    print(Score(motifs), consensus)