def test_libs():
    img = np.array(Image.open(path['org']), dtype=np.uint8).reshape(
        (1, 512, 512, 3))
    mask = np.array(Image.open(path['mask']).convert('L'),
                    dtype=np.float32) / 255.0
    # mask = cv2.GaussianBlur(mask, (5, 5), 2.0)
    mask_ = np.zeros_like(mask)
    mask_[mask > 0.5] = 1.0
    mask = mask.reshape((1, 512, 512, 1))
    arg_mask = 1.0 - mask
    probs = np.concatenate([arg_mask, mask], axis=3)

    import utils
    crf = utils.dense_crf(probs, img)

    plt.figure()
    plt.subplot(311)
    plt.title('image')
    plt.imshow(img[0, :, :, :], cmap='gray')
    plt.subplot(312)
    plt.title('mask')
    plt.imshow(mask_, cmap='gray')
    plt.subplot(313)
    plt.title('crf')
    plt.imshow(crf[0, :, :, 1], cmap='gray')
    plt.show()
Beispiel #2
0
def predict_img(net,
                full_img):
    global hou
    net.eval()
    full_img = full_img.unsqueeze(0)
    start = time.time()
    pred_mask = net(full_img)
    end = time.time()
    # print(start-end)
    hou = hou + end - start
    pred_mask = pred_mask > 0.5
    pred_mask = pred_mask.float()
    # pred_mask_crop = F.sigmoid(pred_mask_crop)
    mask_pred_show = pred_mask.detach().cpu().numpy()
    mask_pred_show = mask_pred_show.squeeze().astype(np.float32)
    # plt.figure(0)
    # plt.imshow(mask_pred_show)
    # plt.figure(1)
    # img_show = img_crop.squeeze().cpu()
    # plt.imshow(img_show[0])
    # plt.show()

    pred_mask = pred_mask.detach().squeeze()
    pred_mask = pred_mask.unsqueeze(0).cpu()
    # print(pred_mask_crop.shape)

    full_mask = pred_mask.squeeze().cpu()
    full_mask = np.array(full_mask * 255, dtype=np.uint8)
    crf_img = full_img.squeeze().cpu()
    crf_img = TF.to_pil_image(crf_img)
    full_mask = dense_crf(np.array(crf_img, dtype=np.uint8), full_mask)
    full_mask = full_mask.astype(np.float32)
    full_mask = torch.from_numpy(full_mask)
    full_mask = full_mask.unsqueeze(0)
    return full_mask
Beispiel #3
0
def predict(model, dataloader, out_dir, device, sample_index_list):
    '''
    输出预测结果
    :param model:
    :param dataset:
    :param out_dir:
    :param device:
    :return: None
    '''

    if not os.path.exists(out_dir):
        os.mkdir(out_dir)

    # eval mode
    model.eval()
    with torch.no_grad():
        for batch, item in tqdm(enumerate(dataloader)):
            data, _ = item

            data = data.to(device)
            out = model(data)

            # score1 = model(data)
            #
            # score2 = model(torch.flip(data, [0, 3]))
            # #         score2 = score2.cpu().numpy()
            # score2 = torch.flip(score2, [3, 0])
            #
            # score3 = model(torch.flip(data, [0, 2]))
            # #         score3 = score3.cpu().numpy()
            # score3 = torch.flip(score3, [2, 0])
            #
            # out = (score1 + score2 + score3) / 3.0

            if dataloader.batch_size == 1:
                mean = [0.485, 0.456, 0.406]  # dataLoader中设置的mean参数
                std = [0.229, 0.224, 0.225]  # dataLoader中设置的std参数

                data = data.squeeze().cpu().numpy()
                out = F.softmax(out, dim=1)
                out = out.squeeze().cpu().numpy()

                for i in range(len(mean)):  # 反标准化
                    data[i] = data[i] * std[i] + mean[i]
                data = np.array(data * 255).astype(np.uint8).transpose(
                    (1, 2, 0))  # 反ToTensor(),从[0,1]转为[0,255]

                pred = dense_crf(data, out)
                sample_index = sample_index_list[batch * dataloader.batch_size]
                out_name = out_dir + f'/{sample_index}.png'
                cv2.imwrite(out_name, pred + 1)  # 提交的结果需要1~10

            else:
                pred = torch.argmax(out, dim=1).cpu().numpy()
                for i in range(len(pred)):
                    sample_index = sample_index_list[batch *
                                                     dataloader.batch_size + i]
                    out_name = out_dir + f'/{sample_index}.png'
                    cv2.imwrite(out_name, pred[i] + 1)  # 提交的结果需要1~10
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):

    img_height = full_img.size[1]
    img_width = full_img.size[0]

    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)

    left_square, right_square = split_img_into_squares(img)

    left_square = hwc_to_chw(left_square)
    right_square = hwc_to_chw(right_square)

    X_left = torch.from_numpy(left_square).unsqueeze(0)
    X_right = torch.from_numpy(right_square).unsqueeze(0)

    if use_gpu:
        X_left = X_left.cuda()
        X_right = X_right.cuda()

    with torch.no_grad():
        output_left = net(X_left)
        output_right = net(X_right)

        left_probs = F.sigmoid(output_left).squeeze(0)
        right_probs = F.sigmoid(output_right).squeeze(0)
        '''
        tf = transforms.Compose(
                [
                    transforms.ToPILImage(),
                    transforms.Resize(img_height),
                    transforms.ToTensor()
                ]
            )
        '''
        tf = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Scale(img_height),
            transforms.ToTensor()
        ])

        left_probs = tf(left_probs.cpu())
        right_probs = tf(right_probs.cpu())

        left_mask_np = left_probs.squeeze().cpu().numpy()
        right_mask_np = right_probs.squeeze().cpu().numpy()

    full_mask = merge_masks(left_mask_np, right_mask_np, img_width)

    if use_dense_crf:
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    return full_mask > out_threshold
Beispiel #5
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()
        y_pred = y_pred.view(384, 384)
        y = y.view(384, 384)
        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()
    return tot / i
Beispiel #6
0
def predict_img(net,
                full_img,
                device,
                scale_factor=1,
                out_threshold=0.5,
                use_dense_crf=False):
    net.eval()
    img_height = full_img.size[1]

    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)
    img = hwc_to_chw(img)

    X = torch.from_numpy(img).unsqueeze(0)

    X = X.to(device=device)

    with torch.no_grad():
        output = net(X)

        if net.n_classes > 1:
            probs = F.softmax(output, dim=1)
        else:
            probs = torch.sigmoid(output)

        probs = probs.squeeze(0)

        tf = transforms.Compose(
            [
                transforms.ToPILImage(),
                transforms.Resize(img_height),
                transforms.ToTensor()
            ]
        )

        probs = tf(probs.cpu())

        full_mask = probs.squeeze().cpu().numpy()

    if use_dense_crf:
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    return full_mask > out_threshold
Beispiel #7
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):
    net.eval()


    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)
    img = hwc_to_chw(img)
    X = torch.from_numpy(img).unsqueeze(0)

    if use_gpu:
        X = X.cuda()

    with torch.no_grad():
        output = net(X)
        probs = output.squeeze(0)

        tf = transforms.Compose(
            [
                transforms.ToPILImage(),
                transforms.ToTensor()
            ]
        )

        probs = tf(probs.cpu())
        print("Probs - ", probs.shape)
        mask = probs.squeeze().cpu().numpy()
        print("Mask - ", mask.shape)
        plt.imshow(mask)
        plt.show()

    if use_dense_crf:
        mask = dense_crf(np.array(full_img).astype(np.uint8), mask)

    return mask > out_threshold
Beispiel #8
0
def predict_img_batch(net,
                      full_img,
                      scale_factor=0.5,
                      out_threshold=0.5,
                      use_dense_crf=True,
                      use_gpu=True):
    """return fullmask with size (C, H, W)"""
    net.eval()
    img_height = full_img.size[1]
    img_width = full_img.size[0]
    print('imgheight', img_height)
    print('imgwidth', img_width)
    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)

    if len(img.shape) == 2:
        img = img[..., np.newaxis]

    print('img.shape', img.shape)
    left_square, right_square = split_img_into_squares(img)

    left_square = hwc_to_chw(left_square)
    right_square = hwc_to_chw(right_square)

    X_left = torch.from_numpy(left_square).unsqueeze(0)
    X_right = torch.from_numpy(right_square).unsqueeze(0)

    if use_gpu:
        X_left = X_left.cuda()
        X_right = X_right.cuda()

    with torch.no_grad():
        output_left = net(X_left)
        output_right = net(X_right)

        print('output_left.shape', output_left.shape)
        print('output_right.shape', output_right.shape)
        left_probs = output_left.squeeze(0)
        right_probs = output_right.squeeze(0)
        #
        # if not scale_factor==1:
        #     tf = transforms.Compose(
        #         [
        #             transforms.ToPILImage(),
        #             transforms.Resize(img_height),
        #             transforms.ToTensor()
        #         ]
        #     )
        #
        #     left_probs = tf(left_probs.cpu())
        #     right_probs = tf(right_probs.cpu())
        # print('left_probs', left_probs.shape)
        # print('right_probs', right_probs.shape)
        left_mask_np = left_probs.squeeze().cpu().numpy()
        right_mask_np = right_probs.squeeze().cpu().numpy()
        left_mask_np = np.transpose(left_mask_np, axes=[1, 2, 0])
        right_mask_np = np.transpose(right_mask_np, axes=[1, 2, 0])
        if not scale_factor == 1:
            right_mask_np = resize_np(right_mask_np, 1 / scale_factor)
            left_mask_np = resize_np(left_mask_np, 1 / scale_factor)
        print('left_mask_np', left_mask_np.shape)
        print('right_mask_np', right_mask_np.shape)
    left_mask_np = np.transpose(left_mask_np, axes=[2, 0, 1])
    right_mask_np = np.transpose(right_mask_np, axes=[2, 0, 1])
    full_mask = merge_masks(left_mask_np, right_mask_np, img_width)

    # if use_dense_crf:
    if 0:
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    return full_mask
Beispiel #9
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=False,
                use_gpu=True):
    img_height = full_img.size[1]
    img = full_img.resize((128, 128))
    img = np.array(img, dtype=np.float32)
    imgs_switched = hwc_to_chw(img)
    imgs_normalized = normalize(imgs_switched)
    imgs_normalized = torch.from_numpy(imgs_normalized).unsqueeze(0)
    if use_gpu:
        imgs_normalized = imgs_normalized.cuda()
    with torch.no_grad():
        output = net(imgs_normalized)
        prob = torch.sigmoid(output).squeeze(0)

        tf = transforms.Compose([
            transforms.ToPILImage(),
            transforms.Resize(img_height),
            transforms.ToTensor()
        ])

        prob = tf(prob.cpu())
        full_mask = prob.squeeze().cpu().numpy()
    '''#full_img = full_img.resize((128,128))
    img_height = full_img.size[1]
    img_width = full_img.size[0]
    
    img = resize_and_crop(full_img, scale=scale_factor)
    img = normalize(img)

    left_square, right_square = split_img_into_squares(img)

    left_square = hwc_to_chw(left_square)
    right_square = hwc_to_chw(right_square)

    X_left = torch.from_numpy(left_square).unsqueeze(0)
    X_right = torch.from_numpy(right_square).unsqueeze(0)
    
    if use_gpu:
        X_left = X_left.cuda()
        X_right = X_right.cuda()

    with torch.no_grad():
        output_left = net(X_left)
        output_right = net(X_right)

        left_probs = torch.sigmoid(output_left).squeeze(0)
        right_probs = torch.sigmoid(output_right).squeeze(0)

        tf = transforms.Compose(
            [
                transforms.ToPILImage(),
                transforms.Resize(img_height),
                transforms.ToTensor()
            ]
        )
        
        left_probs = tf(left_probs.cpu())
        right_probs = tf(right_probs.cpu())

        left_mask_np = left_probs.squeeze().cpu().numpy()
        right_mask_np = right_probs.squeeze().cpu().numpy()
        #print(left_mask_np.shape, right_mask_np.shape)
    full_mask = merge_masks(left_mask_np, right_mask_np, img_width)'''
    ipdb.set_trace()
    if use_dense_crf:
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)

    return full_mask > out_threshold
Beispiel #10
0
def VGG16(img_input,
          dropout=False,
          with_CPFE=False,
          with_CA=False,
          with_SA=False,
          droup_rate=0.3):
    # Block 1
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv1')(img_input)
    x = Conv2D(64, (3, 3),
               activation='relu',
               padding='same',
               name='block1_conv2')(x)
    C1 = x
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block1_pool')(x)

    if dropout:
        x = Dropout(droup_rate)(x)
    # Block 2
    x = Conv2D(128, (3, 3),
               activation='relu',
               padding='same',
               name='block2_conv1')(x)
    x = Conv2D(128, (3, 3),
               activation='relu',
               padding='same',
               name='block2_conv2')(x)
    C2 = x
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block2_pool')(x)

    if dropout:
        x = Dropout(droup_rate)(x)
    # Block 3
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='block3_conv1')(x)
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='block3_conv2')(x)
    x = Conv2D(256, (3, 3),
               activation='relu',
               padding='same',
               name='block3_conv3')(x)
    C3 = x
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block3_pool')(x)

    if dropout:
        x = Dropout(droup_rate)(x)
    # Block 4
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv1')(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv2')(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block4_conv3')(x)
    C4 = x
    x = MaxPooling2D((2, 2), strides=(2, 2), name='block4_pool')(x)

    if dropout:
        x = Dropout(droup_rate)(x)
    # Block 5
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv1')(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv2')(x)
    x = Conv2D(512, (3, 3),
               activation='relu',
               padding='same',
               name='block5_conv3')(x)
    if dropout:
        x = Dropout(droup_rate)(x)
    C5 = x
    C1 = Conv2D(64, (3, 3), padding='same', name='C1_conv')(C1)
    C1 = BN(C1, 'C1_BN')
    C2 = Conv2D(64, (3, 3), padding='same', name='C2_conv')(C2)
    C2 = BN(C2, 'C2_BN')
    if with_CPFE:
        C3_cfe = CFE(C3, 32, 'C3_cfe')
        C4_cfe = CFE(C4, 32, 'C4_cfe')
        C5_cfe = CFE(C5, 32, 'C5_cfe')
        C5_cfe = BilinearUpsampling(upsampling=(4, 4),
                                    name='C5_cfe_up4')(C5_cfe)
        C4_cfe = BilinearUpsampling(upsampling=(2, 2),
                                    name='C4_cfe_up2')(C4_cfe)
        C345 = Concatenate(name='C345_aspp_concat',
                           axis=-1)([C3_cfe, C4_cfe, C5_cfe])
        if with_CA:
            C345 = ChannelWiseAttention(
                C345, name='C345_ChannelWiseAttention_withcpfe')
    C345 = Conv2D(64, (1, 1), padding='same', name='C345_conv')(C345)
    C345 = BN(C345, 'C345')
    C345 = BilinearUpsampling(upsampling=(4, 4), name='C345_up4')(C345)

    if with_SA:
        SA = SpatialAttention(C345, 'spatial_attention')
        C2 = BilinearUpsampling(upsampling=(2, 2), name='C2_up2')(C2)
        C12 = Concatenate(name='C12_concat', axis=-1)([C1, C2])
        C12 = Conv2D(64, (3, 3), padding='same', name='C12_conv')(C12)
        C12 = BN(C12, 'C12')
        C12 = Multiply(name='C12_atten_mutiply')([SA, C12])
    fea = Concatenate(name='fuse_concat', axis=-1)([C12, C345])
    sa = Conv2D(1, (3, 3), padding='same', name='sa')(fea)

    if False:
        sa = tf.sigmoid(sa) * 255
        sa = dense_crf(np.expand_dims(np.expand_dims(sa, 0), 3),
                       np.expand_dims(img_input, 0))
        sa = sa[0, :, :, 0]
        sa = sa * (sa > 50)  # filter not-so-saliency regions
        threshold_gray = 2
        connectivity = 8
        sa = sa.astype(np.uint8)
        _, sa_bi = cv2.threshold(sa, threshold_gray, 255, 0)
        output = cv2.connectedComponentsWithStats(sa_bi, connectivity,
                                                  cv2.CV_32S)
        stats = output[2]
        # a postprocess for commidity object only
        area_img = img_org.shape[0] * img_org.shape[1]
        threshold_area = 100  # set the region to non-saliency, that area is so small
        for rgns in range(1, stats.shape[0]):
            if area_img / stats[rgns, 4] <= threshold_area:
                continue
            x1, y1 = stats[rgns, 0], stats[rgns, 1]
            x2, y2 = x1 + stats[rgns, 2], y1 + stats[rgns, 3]
            sa[y1:y2, x1:x2] = 0
        _, sa = cv2.threshold(sa, threshold_gray, 255, 0)

    model = Model(inputs=img_input, outputs=sa, name="BaseModel")
    return model
Beispiel #11
0
def evaluate_test(model, test_ds, num_test_examples, cspace, epochs, save_model_path=None, type_train='',write_images=True, it=0):
    if (save_model_path != None):
        model = models.load_model(
            save_model_path,
            custom_objects={
                'bce_dice_loss': bce_dice_loss,
                'dice_loss': dice_loss
            })
    # Let's visualize some of the outputs
    mjccard = 0
    score = 0
    v_jaccard = np.zeros(num_test_examples)
    v_sensitivity = np.zeros(num_test_examples)
    v_specificity = np.zeros(num_test_examples)
    v_accuracy = np.zeros(num_test_examples)
    v_dice = np.zeros(num_test_examples)

    crf_jaccard = np.zeros(num_test_examples)
    crf_sensitivity = np.zeros(num_test_examples)
    crf_specificity = np.zeros(num_test_examples)
    crf_accuracy = np.zeros(num_test_examples)
    crf_dice = np.zeros(num_test_examples)

    data_aug_iter = test_ds.make_one_shot_iterator()
    next_element = data_aug_iter.get_next()
    if(not os.path.exists('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/predict/')):
            os.makedirs('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/predict/')
    for j in range(num_test_examples):
        # Running next element in our graph will produce a batch of images
        batch_of_imgs, label = tf.keras.backend.get_session().run(next_element)
        img = batch_of_imgs[0]

        predicted_label = model.predict(batch_of_imgs)[0]
        mpimg.imsave('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/predict/' + str(j) + '.png', predicted_label[:,:,0])
        mask_pred = (predicted_label[:, :, 0] > 0.55).astype(int)
        label = label.astype(int)

        v_jaccard[j] = fjaccard(label[0, :, :, 0], mask_pred)
        v_sensitivity[j] = utils.sensitivity(label[0,:,:,0], mask_pred)
        v_specificity[j] = utils.specificity(label[0,:,:,0], mask_pred)
        v_accuracy[j] = utils.accuracy(label[0,:,:,0], mask_pred)
        v_dice[j] = utils.dice_coeff(label[0,:,:,0], mask_pred)
        score += v_jaccard[j] if v_jaccard[j] >= 0.65 else 0
        print(score)
        mjccard += v_jaccard[j]

        img_rgb = img[:, :, :3]

        if(cspace == 'HSV'):
            img_rgb = tf.keras.backend.get_session().run(tf.image.hsv_to_rgb(img_rgb))
        elif(cspace == 'LAB'):
            img_rgb = tf.keras.backend.get_session().run(Conv_img.lab_to_rgb(img_rgb))

        crf_mask = utils.dense_crf(np.array(img_rgb*255).astype(np.uint8), np.array(predicted_label[:, :, 0]).astype(np.float32))

        crf_jaccard[j] = fjaccard(label[0, :, :, 0], crf_mask)
        crf_sensitivity[j] = utils.sensitivity(label[0,:,:,0], crf_mask)
        crf_specificity[j] = utils.specificity(label[0,:,:,0], crf_mask)
        crf_accuracy[j] = utils.accuracy(label[0,:,:,0], crf_mask)
        crf_dice[j] = utils.dice_coeff(label[0,:,:,0], crf_mask)

        if(write_images):
            fig = plt.figure(figsize=(25, 25))

            plt.subplot(1, 4, 1)
            plt.imshow(img[:, :, :3])
            plt.title("Input image")
            
            plt.subplot(1, 4, 2)
            plt.imshow(label[0, :, :, 0])
            plt.title("Actual Mask")
            
            plt.subplot(1, 4, 3)
            plt.imshow(predicted_label[:, :, 0] > 0.55)
            plt.title("Predicted Mask\n" +
                        "Jaccard = " + str(v_jaccard[j]) +
                        '\nSensitivity = ' + str(v_sensitivity[j]) +
                        '\nSpecificity = ' + str(v_specificity[j]) +
                        '\nAccuracy = ' + str(v_accuracy[j]) +
                        '\nDice = ' + str(v_dice[j]))
            
            plt.subplot(1, 4, 4)
            plt.imshow(crf_mask)
            plt.title("CRF Mask\n" +
                        "Jaccard = " + str(crf_jaccard[j]) +
                        '\nSensitivity = ' + str(crf_sensitivity[j]) +
                        '\nSpecificity = ' + str(crf_specificity[j]) +
                        '\nAccuracy = ' + str(crf_accuracy[j]) +
                        '\nDice = ' + str(crf_dice[j]))
            
            fig.savefig(
                'pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/' + str(j) + '.png',
                bbox_inches='tight')
            plt.close(fig)
            mpimg.imsave('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/predict/' + str(j) + '.png', predicted_label[:,:,0])
            plt.close()

    mjccard /= num_test_examples
    score /= num_test_examples
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/jaccard', v_jaccard)
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/sensitivity', v_sensitivity)
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/specificity', v_specificity)
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/accuracy', v_accuracy)
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/dice', v_dice)
    with open('pos_results/' + type_train + cspace + '/' + str(epochs)  + '/' + str(it) + '/score','w') as f:
        f.write('Score = ' + str(score) +
        '\nSensitivity = ' + str(np.mean(v_sensitivity)) +
        '\nSpecificity = ' + str(np.mean(v_specificity)) +
        '\nAccuracy = ' + str(np.mean(v_accuracy)) +
        '\nDice = ' + str(np.mean(v_dice)) +
        '\nJaccars = ' + str(np.mean(v_jaccard)))

    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/crf_jaccard', crf_jaccard)
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/crf_sensitivity', crf_sensitivity)
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/crf_crf_specificity', crf_specificity)
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/crf_accuracy', crf_accuracy)
    np.savetxt('pos_results/' + type_train + cspace + '/' + str(epochs) + '/' + str(it) + '/crf_dice', crf_dice)
    with open('pos_results/' + type_train + cspace + '/' + str(epochs)  + '/' + str(it) + '/crf_score','w') as f:
        f.write('Sensitivity = ' + str(np.mean(crf_sensitivity)) +
        '\nSpecificity = ' + str(np.mean(crf_specificity)) +
        '\nAccuracy = ' + str(np.mean(crf_accuracy)) +
        '\nDice = ' + str(np.mean(crf_dice)) +
        '\nJaccars = ' + str(np.mean(crf_jaccard)))

    print('Jccard = ' + str(mjccard))
    print('Score = ' + str(score))
    return mjccard, score
Beispiel #12
0
def predict_img(net,
                full_img,
                scale_factor=0.5,
                out_threshold=0.5,
                use_dense_crf=True,
                use_gpu=False):

    net.eval()
    img_height = full_img.size[1]
    img_width = full_img.size[0]

    # img = resize_and_crop(full_img, scale=scale_factor)
    # img = normalize(img)
    img = np.array(full_img, dtype=np.float32)
    img = normalize(img)

    # left_square, right_square = split_img_into_squares(img)
    #
    # left_square = hwc_to_chw(left_square)
    # right_square = hwc_to_chw(right_square)
    square = hwc_to_chw(img)
    #
    # X_left = torch.from_numpy(left_square).unsqueeze(0)
    # X_right = torch.from_numpy(right_square).unsqueeze(0)
    img = torch.from_numpy(square).unsqueeze(0)
    
    if use_gpu:
        # X_left = X_left.cuda()
        # X_right = X_right.cuda()
        img = img.cuda()

    with torch.no_grad():
        # output_left = net(X_left)
        # output_right = net(X_right)
        output = net(img)

        # left_probs = output_left.squeeze(0)
        # right_probs = output_right.squeeze(0)
        # probs = output # .squeeze(0)
        # arr_probs = np.array(probs.cpu())
        # arr_probs = np.transpose

        # tf = transforms.Compose(
        #     [
        #         # transforms.ToPILImage(),
        #         # transforms.Resize(img_height),
        #         transforms.ToTensor()
        #     ]
        # )
        
        # left_probs = tf(left_probs.cpu())
        # right_probs = tf(right_probs.cpu())
        # probs = tf(arr_probs)

        # left_mask_np = left_probs.squeeze().cpu().numpy()
        # right_mask_np = right_probs.squeeze().cpu().numpy()
        mask_np = output.squeeze().cpu().numpy()

    # full_mask = merge_masks(left_mask_np, right_mask_np, img_width)

    if use_dense_crf:
        # full_mask = dense_crf(np.array(full_img).astype(np.uint8), full_mask)
        full_mask = dense_crf(np.array(full_img).astype(np.uint8), mask_np)

    # return full_mask > out_threshold
    return mask_np > out_threshold