Esempio n. 1
0
        def get_similarity_values(q1_csc, q2_csc):
            cosine_sim = []
            manhattan_dis = []
            eucledian_dis = []
            jaccard_dis = []
            minkowsk_dis = []

            for i, j in zip(q1_csc, q2_csc):
                sim = cs(i, j)
                cosine_sim.append(sim[0][0])
                sim = md(i, j)
                manhattan_dis.append(sim[0][0])
                sim = ed(i, j)
                eucledian_dis.append(sim[0][0])
                i_ = i.toarray()
                j_ = j.toarray()
                try:
                    sim = jsc(i_, j_)
                    jaccard_dis.append(sim)
                except:
                    jaccard_dis.append(0)

                sim = minkowski_dis.pairwise(i_, j_)
                minkowsk_dis.append(sim[0][0])
            return cosine_sim, manhattan_dis, eucledian_dis, jaccard_dis, minkowsk_dis
Esempio n. 2
0
def get_score(output, target):
    intersection = np.logical_and(target, output)
    union = np.logical_or(target, output)
    iou_score = np.sum(intersection)/ np.sum(union)
    y_true = target.reshape(-1)
    y_pred = output.reshape(-1)
    jacc_sim = jsc(y_true, y_pred, average=None)
    mean_jacc_sim = np.mean(jacc_sim)
    return iou_score, mean_jacc_sim
Esempio n. 3
0
def train_epoch(epoch, model, device, data_loader, test_loader, optimizer,
                best_jaccard, best_loss):
    model.train()
    pid = os.getpid()
    for batch_idx, (data, target) in enumerate(data_loader):
        optimizer.zero_grad()
        output = model(data.to(device))

        target = target.to(device)
        loss = loss_seg_fn(
            output.reshape(-1, ).to(device),
            target.reshape(-1, ).to(device))

        target_cont = target[:, 0].data.cpu().numpy().reshape(-1)
        out_probs_cont = output[:, 0].data.cpu().numpy().reshape(-1)

        sampled_cont = (np.random.rand(len(out_probs_cont)) <
                        out_probs_cont).astype(int)

        jaccard = jsc(target_cont, sampled_cont)

        loss.backward()
        optimizer.step()
        accs.append(loss.item())
        conts.append(jaccard)
        if batch_idx % 32 == 0:
            val_jaccard = validate(test_loader, model, device)
            print(
                '{}\tTrain Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}\tJac: {:.6f}\tVal Jac: {:.6f}\tBest Val Jac: {:.6f}'
                .format(pid, epoch, batch_idx * len(data),
                        len(data_loader.dataset),
                        100. * batch_idx / len(data_loader),
                        np.mean(np.array(accs[-100:])),
                        np.mean(np.array(conts[-100:])), val_jaccard,
                        best_jaccard))
            if val_jaccard > best_jaccard:
                best_jaccard = val_jaccard
                print('Saving model -- epoch no. ', epoch)
                torch.save(
                    {
                        'epoch': epoch,
                        'jaccard': val_jaccard,
                        'model_state_dict': model.state_dict()
                    }, './weights/nao.pt')
            model.train()

            if np.mean(np.array(accs[-100:])) < best_loss - 0.02:
                print('Saving overfit model -- epoch no. ', epoch)
                best_loss = np.mean(np.array(accs[-100:]))
                torch.save(
                    {
                        'epoch': epoch,
                        'loss': best_loss,
                        'model_state_dict': model.state_dict()
                    }, './weights/nao_overfit.pt')

    return best_jaccard, best_loss
Esempio n. 4
0
 def compute_iou(self, pred, target):
     """
     Compute the IOU
     """
     print("shape of the original prediction", np.shape(pred.cpu().data.numpy()))
     lbl = pred.cpu().data.numpy()[:,0,:,:].reshape(-1) > 0
     tgt = target.cpu().data.numpy()[:,0,:,:].reshape(-1)
     print("shape of lbl in compute iou", np.shape(lbl))
     print("shape of tgt in compute iou", np.shape(tgt))
     return jsc(tgt, lbl)
Esempio n. 5
0
def validation_IOU(prediction_batch, actual_batch):
    '''
    NOT IN USE - mean_IOU is used
    '''
    numpy_output = prediction_batch.cpu().numpy()
    prediction = np.argmax(numpy_output, axis=1)
    labels = actual_batch.cpu().numpy()
    jsc_labels = labels.reshape(-1)
    jsc_outputs = prediction.reshape(-1)

    return jsc(jsc_outputs, jsc_labels)
    def compute_iou(self, pred, target):
        """
        Compute the IOU
        """
        if isinstance(pred, torch.Tensor):
            lbl = pred.cpu().data.numpy().reshape(-1) > 0
        else:  # this is a numpy array
            lbl = pred.reshape(-1) > 0

        if isinstance(target, torch.Tensor):
            tgt = target.cpu().data.numpy().reshape(-1)
        else:  # This is a numpy array
            tgt = target.reshape(-1)
        #print("shape of lbl in compute iou", np.shape(lbl))
        #print("shape of tgt in compute iou", np.shape(tgt))
        return jsc(tgt, lbl)
Esempio n. 7
0
def calcul_iou_for_focal(net, dataset, gpu=False):
    tot = 0
    for i, (img, true_mask) in enumerate(dataset):
        img = Variable(img)
        if gpu:
            img = img.cuda()

        pred_mask = net(img).cpu()
        pred_mask = pred_mask.data.numpy()
        true_mask = true_mask.numpy()
        pred_mask = np.argmax(pred_mask, axis=1)
        pred_mask = pred_mask.reshape(-1)
        true_mask = true_mask.reshape(-1)
        tot += jsc(pred_mask, true_mask)

    return tot / i
Esempio n. 8
0
def compute_jaccard(gt, pred):
  """Function to compute Jaccard coefficient between two binary volumes
  
  Parameters
  ----------
  gt : numpy array
      Ground truth volume. Must have dtype=np.uint8
  pred : numpy array
      Predicted segmentation. Must have dtype=np.uint8
  
  Returns
  -------
  float
      Value of the Jaccard index
  """
  jaccard = jsc(pred.flatten(), gt.flatten(), average = 'binary')
  return jaccard
Esempio n. 9
0
def validate(test_loader, model, device, gamma=0.2):
    model.eval()
    losses = []
    jaccards = []
    with torch.no_grad():
        for batch_idx, (data, target) in enumerate(test_loader):
            output = model(data.to(device))
            target = target.to(device)
            loss = loss_seg_fn(output[:, 0].reshape(-1, ).to(device),
                               target[:, 0].reshape(-1, ).to(device))
            losses.append(loss.item())
            target_cont = target[:, 0].data.cpu().numpy().reshape(-1)
            out_probs_cont = output[:, 0].data.cpu().numpy().reshape(-1)
            sampled_cont = (np.random.rand(len(out_probs_cont)) <
                            out_probs_cont).astype(int)
            jaccard = jsc(target_cont, sampled_cont)
            jaccards.append(jaccard)
    return np.mean(np.array(jaccards))
Esempio n. 10
0
def test():
    jsc_list = []

    data_loader = pascalVOCLoader
    data_path = "/home/temp_siplab/Datasets/VOCdevkit/VOC2012/"

    loader = data_loader(data_path,
                         is_transform=True,
                         split="train",
                         img_size=(256, 256),
                         ohe=False)

    n_classes = loader.n_classes
    testloader = data.DataLoader(loader,
                                 batch_size=batch_size,
                                 num_workers=4,
                                 shuffle=True)

    os.system("mkdir -p checkpoints")

    for i, (images, labels) in enumerate(testloader):

        if use_gpu:
            images = Variable(images.cuda())
            labels = Variable(labels.cuda())
        else:
            images = Variable(images)
            labels = Variable(labels)

        segmentor.zero_grad()

        output = segmentor(images)
        lbl = labels.cpu().numpy().reshape(-1)
        target = torch.max(output, dim=1)[1].cpu().detach().numpy().reshape(-1)

        jsc_list.append(jsc(target, lbl))
    print(np.mean(jsc_list))
            else:
                raise ValueError("Unknown loss")

            train_loss += loss.item()

            # backward loss and next step
            loss.backward()
            optimizer.step()

            # compute the accuracy
            # compute the accuracy
            lbl = y.cpu().numpy().reshape(-1)
            pred_labels = pred.detach().cpu().numpy().astype('float32')
            pred_labels = np.argmax(pred_labels,
                                    axis=1).reshape(-1).astype(np.uint8)
            batch_jacc = jsc(pred_labels, lbl, average='binary')
            print("Loss: ", loss.item())
            print("Training - Batch JSC: ", batch_jacc)
            print("Num 1s: ", len(pred_labels[pred_labels > 0]))
            jaccs_train.append(batch_jacc)

        # -----------------------------
        # validation samples
        # -----------------------------

        # set the model into train mode
        lesion_model.eval(
        )  #Put in evaluation mode. Eg so that things like dropout don't occur during evaluation
        for b_v, (data, target) in enumerate(validation_dataloader):

            print("Validation. Mini-batch ", b_v + 1, "/",
Esempio n. 12
0
jaccards = []
kls = []
sims = []
tracking = [0]
contour_matching = []

with torch.no_grad():
    for batch_idx, (test_images, test_labels) in enumerate(test_loader):
        flow = np.load(flow_val_data[count])[0].reshape(128, 228, 2)
        output = net(Variable(test_images).cuda().to(device))
        out_probs_cont = output.data.cpu().numpy().reshape(-1)
        output_mask = (np.random.rand(len(out_probs_cont)) <
                       out_probs_cont).astype(int)
        gt_mask = test_labels.reshape(-1, ).data.cpu().numpy()

        jaccard = jsc(gt_mask, output_mask)
        jaccards.append(jaccard)

        kl = KL(normalize(out_probs_cont), normalize(gt_mask.reshape(-1, )))
        kls.append(kl)
        sim = histogram_intersection(gt_mask, output_mask)
        sims.append(sim)

        #    if image_val_data[count].split('_')[2] == image_val_data[count-1].split('_')[2]:
        #        coh_err = coherence_error(flow, prev_prediction, output_mask)
        #        tracking.append(coh_err)

        #    contour_pred, contour_gt = find_contours(output_mask, 1), find_contours(gt_mask, 1)
        #print(jaccard, kl, sim)
        prev_prediction = output_mask
        count += 1
Esempio n. 13
0
    print('-------- EPOCH {} -------'.format(epoch))

    # TRAINING
    for i, (inputs, targets) in enumerate(train_data, 0):

        labels = targets.squeeze(1).type(torch.int64)

        if gpu:
            inputs = inputs.cuda()
            labels = labels.cuda()

        logits, y_pred = model(inputs)

        batch_iou = jsc(
            labels.cpu().numpy().reshape(-1),
            torch.argmax(y_pred, dim=1).cpu().numpy().reshape(-1),
            average='macro',
        )

        tr_epoch_iou += batch_iou
        tr_batch_count += 1

        # Zero gradients, perform a backward pass, and update the weights.
        optimizer.zero_grad()
        tr_loss = criterion(logits, labels)
        tr_epoch_loss += tr_loss.item()
        print("----- BATCH: {} TRAIN LOSS: {}".format(i, tr_loss.item()))

        # backprop
        tr_loss.backward()
        optimizer.step()
Esempio n. 14
0
kls = []
sims = []
tracking = [0]
contour_matching = []

for batch_idx, (test_images, test_labels) in enumerate(test_loader):
    gt_mask = test_labels.reshape(-1,).data.cpu().numpy()
    for i in range(len(test_labels)):
        gt_mask = test_labels[i].reshape(-1,).data.cpu().numpy()

        prec = precision_score(gt_mask, center)
        recall = recall_score(gt_mask, center)
        if prec + recall != 0:
            F = 2 * (prec * recall) / (prec + recall)
            Fs.append(F)

        jaccard = jsc(gt_mask, center)
        jaccards.append(jaccard)

        kl = KLD(center, gt_mask)
        kls.append(kl)

        sim = SIM(center, gt_mask)
        sims.append(sim)

print('sim', np.mean(np.array(sims)))
print('F', np.mean(np.array(Fs)))
print('kl', np.mean(np.array(kls)))
print('jacc', np.mean(np.array(jaccards)))
def get_jaccard(pred_bbox, truth_bbox):
    pred_mask = get_mask_from_bbox(pred_bbox)
    truth_mask = get_mask_from_bbox(truth_bbox)
    return jsc(y_true=truth_mask, y_pred=pred_mask, average='micro')
Esempio n. 16
0
def coherence_error(flow, pred_1, pred_2):
    pred_1 = pred_1.reshape(128, 228)
    projected = cv2.remap(pred_1, flow, None, cv2.INTER_NEAREST)
    return jsc(pred_2, projected.reshape(-1, ))