Exemplo n.º 1
0
def print_prediction(model, img_list, main_batch_size, run_name, output_prediction=False):
        #
    scores = model.evaluate_generator(GeneratorImgs(img_list, batch_size=main_batch_size, width=WIDTH, height=HEIGHT),
                        steps=len(img_list)/main_batch_size)
        #
    print('Test loss: {:.4f}'.format(scores[0]))
    print('Test accuracy: {:.4f}'.format(scores[1]))
    #
    preds_proba = list()
    preds = list()
    conf_by_tumor = np.zeros((8,2))
    labels = list()
    class_count = np.array([0,0])
    imgname = list()
    predictions = list()
    #
    if(output_prediction):
        fpred = open("predictions/{}".format(run_name), "w")
    #
    for x, y, z in ReadImgs(img_list, width=WIDTH, height=HEIGHT):
        predictions.append(model.predict(np.array([x])).squeeze())
        labels.append(y.argmax())
        imgname.append(z)
    #
    predictions = np.array(predictions)
    class_count_real, class_count_aug = classes_count(LoadBreakhisList(TRAIN_FILE))
    predictions /= class_count_aug
    predictions *= class_count_real
    print("P_train: ",class_count_aug)
    print("P_real: ",class_count_real)
    #
    for i in range(len(predictions)):   
        if(output_prediction):
            fpred.write("{};{};".format(imgname[i].split("/")[-1], labels[i]))
        class_count[labels[i]] += 1
        preds.append(predictions[i].argmax())
        preds_proba.append(predictions[i][labels[i]])
        conf_by_tumor[TumorToLabel8(imgname[i])][np.argmax(predictions[i])] += 1
        if(output_prediction):
            for j in predictions[i]:
                fpred.write("{:.4f};".format(j))
            fpred.write("\n")
    #
    if(output_prediction):
        fpred.close()
    #
    fpr, tpr, _ = roc_curve(labels, preds_proba, pos_label=0)
    roc_auc = auc(fpr, tpr)
    #
    print("Test AUC 0: {:.4f}".format(roc_auc))
    #
    print(classification_report(labels, preds, target_names=["malign", "benign"]))
    #
    #fpr, tpr, _ = roc_curve(labels, preds_proba, pos_label=1)
    #roc_auc = auc(fpr, tpr)
    #
    #print("Test AUC 1: {:.4f}".format(roc_auc))
    a = confusion_matrix(labels, preds)
    print("Confusion matrix:\n",a)
    print("Total elements per class:", class_count)
    if(a[0][0]+a[1][0] != 0 and a[0][1]+a[1][1] != 0):
                print("Accuracy per class: {:.3f} {:.3f} {:.3f}\n".format(float(a[0][0])/(a[0][0]+a[0][1]), float(a[1][1])/(a[1][0]+a[1][1]), (float(a[0][0])/(a[0][0]+a[0][1])+float(a[1][1])/(a[1][0]+a[1][1]))/2))
    else:
                print("Zero predictions in one class.")
    print("Confusion by tumor type: \n", conf_by_tumor)
    print("Accuracy by patient: ", accuracy_by_patient(img_list, labels, preds))
    print("Malign  Benign")
    print("M_LC\nM_MC\nM_PC\nM_DC\nB_TA\nB_A\nB_PT\nB_F")
Exemplo n.º 2
0
#
#
#
#for i,j in  GeneratorImgs(train_imgs, batch_size=main_batch_size):
#    for t in range(len(i)):
#        print(j)
#
#for i,j in  GeneratorImgs(val_imgs, batch_size=main_batch_size):
#    for t in range(len(i)):
#        print(j)
#
#exit(0)
#    
#
#
model.fit_generator(GeneratorImgs(train_imgs, batch_size=main_batch_size, height=HEIGHT, width=WIDTH), \
        validation_steps=nr_batches_val, \
        validation_data=GeneratorImgs(val_imgs, batch_size=main_batch_size, height=HEIGHT, width=WIDTH), \
        steps_per_epoch=nr_batches, epochs=EPOCHS, verbose=2, max_queue_size=1, \
        workers=1, use_multiprocessing=False, \
        callbacks=set_callbacks("{}".format(sys.argv[2])))
#
del val_imgs
#
predictions = list()
labels = list()
imgname = list()
test_imgs = LoadBreakhisList(TEST_FILE)
fpred = open("predictions/"+sys.argv[2], "w")
for x, y, z in ReadImgs(test_imgs, width=WIDTH, height=HEIGHT):
    preds = model.predict(np.array([x])).squeeze()