Example #1
0
def eval_net(net, loader, device, n_val):
    """Evaluation without the densecrf with the dice coefficient"""
    print('eval in process')
    net.eval()
    tot = 0
    accuracy = 0
    dice_panck = 0
    dice_nuclei = 0
    dice_lcell = 0
    dice_avr = 0
    for batch in loader:
        imgs = batch['image']
        true_masks = batch['mask']

        imgs = imgs.to(device=device, dtype=torch.float32)
        true_masks = true_masks.to(device=device, dtype=torch.long)
        true_masks = true_masks.squeeze(1)
        # mask_pred, aux = net(imgs) #for deeplabv3 and deeplabv3+
        mask_pred = net(imgs)
        for true_mask, pred in zip(true_masks, mask_pred):

            if net.n_classes > 1:
                tot += F.cross_entropy(pred.unsqueeze(dim=0),
                                       true_mask.unsqueeze(dim=0)).item()
            else:
                tot += dice_coeff(pred, true_mask.squeeze(dim=1)).item()

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

            probs = probs.cpu().detach().numpy()

            true_mask1 = true_mask.cpu().numpy()

            probs = np.argmax(probs, axis=0)
            #probs = nuclei_process(probs)
            accuracy += accuracy_score(probs, true_mask1)
            dice_avr += diceCoeff_avr(probs, true_mask1)
            dice_panck += diceCoeff_panck(probs, true_mask1)
            dice_nuclei += diceCoeff_nuclei(probs, true_mask1)
            dice_lcell += diceCoeff_lcell(probs, true_mask1)
    return tot / n_val, accuracy / n_val, dice_avr / n_val, dice_panck / n_val, dice_nuclei / n_val, dice_lcell / n_val
Example #2
0
    #for ph2
    fname = fname.split('_')[0]
    #print(fname)
    #save the images
    x1 = plt.imsave('./model/pred_threshold/' + fname + '_thrPrediction.png', s)

    y = Image.open(x_sort_testL[i])
    s2 = data_transform32(y)
    s3 = np.array(s2)
   # s2 =threshold_predictions_v(s2)

    #save the Images
    y1 = plt.imsave('./model/label_threshold/' + fname + '_thrLabel.png', s3)

    total = dice_coeff(s, s3)
    acc, sen, spe, dice, jacc = accuracy_score(s, s3)
    names = x_sort_testP[i].split('/')
    logInfoHead = names[-1]+'(dice1): '+str(total)
    logInfoBody = 'accuracy:%.5f, sensitivity:%.5f, specificity:%.5f, dice:%.5f, jaccard:%.5f'%(acc, sen, spe, dice, jacc)
    f.write(logInfoHead+"\n")
    f.write(logInfoBody+"\n")
    print(logInfoHead)
    print(logInfoBody)
    t_acc += acc
    t_sen += sen
    t_spe += spe
    t_dice += dice
    t_jacc += jacc
    if total <= 0.65:
        x_count += 1
        f2.write("name:%s, dice:%.5f"%(names[-1], total)+"\n")
Example #3
0
    dice_lcell = 0

    for filename in os.listdir(in_files):
        count = count + 1
        img = Image.open(in_files + "/" + filename)
        img_array = np.array(img)
        img = img_array
        mask = predict_img(net=net,
                           full_img=img,
                           scale_factor=1.0,
                           use_dense_crf=False,
                           device=device)
        mask = nuclei_process(mask)
        true_mask = Image.open(true_masks + "/" + filename[:-4] + ".bmp")
        true_mask = np.array(true_mask)
        accuracy += accuracy_score(mask, true_mask)
        precision += precision_score(mask, true_mask)
        recall += recall_score(mask, true_mask)
        f1 += f1_score(mask, true_mask)
        iou += IOU(mask, true_mask)
        diceCoeff += diceCoeff_avr(mask, true_mask)
        dice_panck += diceCoeff_panck(mask, true_mask)
        dice_nuclei += diceCoeff_nuclei(mask, true_mask)
        dice_lcell += diceCoeff_lcell(mask, true_mask)
        mask_viz = mask_to_image(mask)
        #imageio.imwrite(output + "/" + filename, mask_viz)
        mask = Image.fromarray((mask).astype(np.uint8))
        imageio.imwrite(output + "/" + filename[:-4] + '.bmp', mask)

    print("num of samples%.0f" % count)
    print("accuracy = %.3f" % (accuracy / count))
Example #4
0
    #    digits.target_names
    #    some_digit = X[666] # 随机取一个样本
    #    some_digmit_image = some_digit.reshape(8, 8)
    #    plt.imshow(some_digmit_image, cmap = matplotlib.cm.binary)
    #    plt.show()
    #    y[666]

    # knn进行分类
    X_train, X_test, y_train, y_test = train_test_split(X,
                                                        y,
                                                        test_size=0.2,
                                                        random_state=666)
    knn_clf = KNeighborsClassifier(n_neighbors=5)
    knn_clf.fit(X_train, y_train)
    y_predict = knn_clf.predict(X_test)
    acc = accuracy_score(y_test, y_predict)
    print(acc)
    # 直接调用检验预测的方法
    acc = knn_clf.score(X_test, y_test)

    # 指定最佳值的分数,初始化为0.0;设置最佳值k,初始值为-1
    best_score = 0.0
    best_k = -1
    for k in range(1, 11):  # 暂且设定到1~11的范围内
        knn_clf = KNeighborsClassifier(n_neighbors=k)
        knn_clf.fit(X_train, y_train)
        score = knn_clf.score(X_test, y_test)
        print(score)
        if score > best_score:
            best_k = k
            best_score = score
    im_tb = Image.open('/home/malav/Desktop/Pytorch_Computer/DATA/test_new_3C_I_ori_same/0131_0009.png')
    im_label = Image.open('/home/malav/Desktop/Pytorch_Computer/DATA/test_new_3C_L_ori_same/0131_0009.png')
    s_tb = data_transform(im_tb)
    s_label = data_transform(im_label)

    pred_tb = model_test(s_tb.unsqueeze(0).to(device)).cpu()
    pred_tb = F.sigmoid(pred_tb)
    pred_tb = pred_tb.detach().numpy()

   #pred_tb = threshold_predictions_v(pred_tb)

    x1 = plt.imsave(
        './model/pred/img_iteration_' + str(n_iter) + '_epoch_'
        + str(i) + '.png', pred_tb[0][0])

    accuracy = accuracy_score(pred_tb[0][0], s_label)

    #######################################################
    #To write in Tensorboard
    #######################################################

    train_loss = train_loss / len(train_idx)
    valid_loss = valid_loss / len(valid_idx)

    if (i+1) % 1 == 0:
        print('Epoch: {}/{} \tTraining Loss: {:.6f} \tValidation Loss: {:.6f}'.format(i + 1, epoch, train_loss,
                                                                                      valid_loss))
        writer1.add_scalar('Train Loss', train_loss, n_iter)
        writer1.add_scalar('Validation Loss', valid_loss, n_iter)
        #writer1.add_image('Pred', pred_tb[0]) #try to get output of shape 3
Example #6
0
 def score(self, X_test, y_test):
     """根据测试数据集,X_test和y_test确定当前模型准确度"""
     y_predict = self.predict(X_test)
     return accuracy_score(y_test, y_predict)