Esempio n. 1
0
def eval_net(net, dataset, gpu=False):
    tot = 0
    for i, b in enumerate(dataset):
        X = b[0]
        y = b[1]

        X = torch.FloatTensor(X).unsqueeze(0)
        y = torch.ByteTensor(y).unsqueeze(0)

        if gpu:
            X = Variable(X, volatile=True).cuda()
            y = Variable(y, volatile=True).cuda()
        else:
            X = Variable(X, volatile=True)
            y = Variable(y, volatile=True)

        y_pred = net(X)

        y_pred = (F.sigmoid(y_pred) > 0.6).float()
        # y_pred = F.sigmoid(y_pred).float()

        dice = dice_coeff(y_pred, y.float()).data[0]
        tot += dice

    print("i=", i)
    return tot / i
def eval_net(net, dataset, gpu=False):
    tot = 0
    for i, b in enumerate(dataset):
        X = b[0]
        y = b[1]

        X = torch.FloatTensor(X).unsqueeze(0)
        y = torch.ByteTensor(y).unsqueeze(0)

        if gpu:
            X = Variable(X, volatile=True).cuda()
            y = Variable(y, volatile=True).cuda()
        else:
            X = Variable(X, volatile=True)
            y = Variable(y, volatile=True)

        y_pred = net(X)

        y_pred = (F.sigmoid(y_pred) > 0.6).float()
        print('X shape: ', X.shape)
        print('y_pred shape: ', y_pred.shape)

        dice = dice_coeff(y_pred, y.float()).data[0]
        tot += dice

        if 0:
            X = X.data.squeeze(0).cpu().numpy()
            X = np.transpose(X, axes=[1, 2, 0])
            y = y.data.squeeze(0).cpu().numpy()
            y_pred = y_pred.data.squeeze(0).squeeze(0).cpu().numpy()
            print(y_pred.shape)

            fig = plt.figure()
            ax1 = fig.add_subplot(1, 4, 1)
            ax1.imshow(X)
            ax2 = fig.add_subplot(1, 4, 2)
            ax2.imshow(y)
            ax3 = fig.add_subplot(1, 4, 3)
            ax3.imshow((y_pred > 0.5))

            Q = dense_crf(((X * 255).round()).astype(np.uint8), y_pred)
            ax4 = fig.add_subplot(1, 4, 4)
            print(Q)
            ax4.imshow(Q > 0.5)
            plt.show()
    print("i=", i)
    return tot / i
def eval_net(net, dataset, gpu=False):
    tot = 0
    mean_iou = 0
    mean_precision = 0
    mean_recall = 0
    for i, b in enumerate(dataset):
        X = b[0]
        y = b[1]

        X = torch.FloatTensor(X).unsqueeze(0)
        y = torch.ByteTensor(y).unsqueeze(0)

        if gpu:
            X = Variable(X, volatile=True).cuda()
            y = Variable(y, volatile=True).cuda()
        else:
            X = Variable(X, volatile=True)
            y = Variable(y, volatile=True)

        y_pred = net(X)

        y_pred = (F.sigmoid(y_pred) > 0.6).float()

        dice = dice_coeff(y_pred, y.float()).data[0]
        tot += dice

        # print('y_pred.data shape: ', y_pred.data.squeeze(0).squeeze(0).cpu().numpy().shape)
        # print('y.data: ', y.data.squeeze(0).cpu().numpy().shape)
        y = y.data.squeeze(0).cpu().numpy()
        y = (y > 0.6).astype(np.float32)
        y_pred = y_pred.data.squeeze(0).squeeze(0).cpu().numpy()
        y_pred = (y_pred > 0.5).astype(np.float32)
        # print('y.shape: ', y.shape)
        # print('y_pred.shape: ', y_pred.shape)

        # print('y binary : ', np.array_equal(y, y.astype(bool)))
        # print('y_pred binary : ', np.array_equal(y_pred, y_pred.astype(bool)))
        iou_val = get_iou(y_pred, y)
        mean_iou += iou_val
        precision_val = get_precision(y_pred, y)
        mean_precision += precision_val
        recall_val = get_recall(y_pred, y)
        mean_recall += recall_val

        if 0:
            X = X.data.squeeze(0).cpu().numpy()
            X = np.transpose(X, axes=[1, 2, 0])
            # y = y.data.squeeze(0).cpu().numpy()
            # y_pred = y_pred.data.squeeze(0).squeeze(0).cpu().numpy()

            fig = plt.figure()
            ax1 = fig.add_subplot(1, 4, 1)
            ax1.imshow(X)
            ax2 = fig.add_subplot(1, 4, 2)
            ax2.imshow(y)
            ax3 = fig.add_subplot(1, 4, 3)
            ax3.imshow(y_pred)

            Q = dense_crf(((X * 255).round()).astype(np.uint8), y_pred)
            ax4 = fig.add_subplot(1, 4, 4)
            print(Q)
            ax4.imshow(Q > 0.5)
            plt.show()
    print("i=", i)
    return tot / i, mean_iou / (i + 1.0), mean_precision / (
        i + 1.0), mean_recall / (i + 1.0)