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
        if(i.find("rotat") == -1 and i.find("flip") == -1 and i.find("trans") == -1):
            class_count_real += label
        class_count_aug += label
    return class_count_real.astype("float32")/(class_count_real[0]+class_count_real[1]), class_count_aug.astype("float32")/(class_count_aug[0]+class_count_aug[1])
#
#
#
model = build_cnn(int(sys.argv[1]))
model.summary()
print(model.optimizer.get_config())
print("Epochs: ",EPOCHS)
print("Batch-size: ", BATCH_SIZE)
#
#
#
train_imgs = LoadBreakhisList(TRAIN_FILE)
random.shuffle(train_imgs)
val_imgs = LoadBreakhisList(VAL_FILE)
random.shuffle(val_imgs)
#
main_batch_size = BATCH_SIZE
nr_batches_val = len(val_imgs)/main_batch_size
nr_batches = len(train_imgs)/main_batch_size
#
#
#
#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):