예제 #1
0
def predict_vgg():

    # VGG16 generators

    # Set up generators
    test_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.vgg16.preprocess_input).flow_from_directory(
        test2_path,
        target_size=(image_size, image_size),
        batch_size=test_batch_size,
        shuffle=False)

    my_model = applications.VGG16(include_top=True, weights='imagenet')

    initial_model = Sequential()
    for layer in my_model.layers[:22]:
        initial_model.add(layer)

    initial_model.layers.pop()

    for layer in initial_model.layers:
        layer.trainable = False

    initial_model.add(Dense(1024, activation='relu'))
    initial_model.add(Dropout(0.5))
    initial_model.add(Dense(7, activation="softmax"))

    initial_model.load_weights('modelVGG.h5')

    model = initial_model

    print(model.summary())

    plot_model(model, to_file="VGG16Model.png", show_shapes=True)

    test_batches.reset()

    test_labels = test_batches.classes

    # Make predictions
    predictions = model.predict_generator(test_batches,
                                          steps=test_steps,
                                          verbose=1)

    # Declare a function for plotting the confusion matrix
    def plot_confusion_matrix(cm,
                              classes,
                              normalize=False,
                              title='Confusion matrix',
                              cmap=plt.cm.Blues):
        """
        This function prints and plots the confusion matrix.
        Normalization can be applied by setting `normalize=True`.
        """
        if normalize:
            cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
            print("Normalized confusion matrix")
        else:
            print('Confusion matrix, without normalization')

        print(cm)

        plt.imshow(cm, interpolation='nearest', cmap=cmap)
        plt.title(title)
        plt.colorbar()
        tick_marks = np.arange(len(classes))
        plt.xticks(tick_marks, classes, rotation=45)
        plt.yticks(tick_marks, classes)

        fmt = '.2f' if normalize else 'd'
        thresh = cm.max() / 2.
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            plt.text(j,
                     i,
                     format(cm[i, j], fmt),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

        plt.ylabel('True label')
        plt.xlabel('Predicted label')
        plt.tight_layout()
        plt.savefig('confusion_matrix_VGG16.png')

    cm = confusion_matrix(test_labels, predictions.argmax(axis=1))

    cm_plot_labels = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']

    plot_confusion_matrix(cm, cm_plot_labels)

    recall = np.diag(cm) / np.sum(cm, axis=1)
    precision = np.diag(cm) / np.sum(cm, axis=0)

    print("Mean recall " + str(np.mean(recall).item()))
    print("Mean precision " + str(np.mean(precision).item()))

    print("Mean recall " + str(
        recall_score(
            test_labels, predictions.argmax(axis=1), average='weighted')))
    print("Balanced Accuracy " +
          str(balanced_accuracy_score(test_labels, predictions.argmax(
              axis=1))))
    print("Mean Precision " + str(
        precision_score(
            test_labels, predictions.argmax(axis=1), average='weighted')))
    print("Mean f1 score " + str(
        f1_score(test_labels, predictions.argmax(axis=1), average='weighted')))

    file = open("VGG16_results.txt", "w+")
    file.write("Mean recall " + str(np.mean(recall).item()) + "\n")
    file.write("Mean precision " + str(np.mean(precision).item()) + "\n")
    file.write("Mean recall " + str(
        recall_score(
            test_labels, predictions.argmax(axis=1), average='weighted')) +
               "\n")
    file.write(
        "Balanced Accuracy " +
        str(balanced_accuracy_score(test_labels, predictions.argmax(axis=1))) +
        "\n")
    file.write("Mean Precision " + str(
        precision_score(
            test_labels, predictions.argmax(axis=1), average='weighted')) +
               "\n")
    file.write("Mean f1 score " + str(
        f1_score(test_labels, predictions.argmax(
            axis=1), average='weighted')) + "\n")

    file.close()

    # print("Roc AUC score: " + str(roc_auc_score(test_labels, predictions.argmax(axis=1))))

    predicted_class_indices = np.argmax(predictions, axis=1)

    labels = train_batches.class_indices
    labels = dict((v, k) for k, v in labels.items())
    predictions = [labels[k] for k in predicted_class_indices]

    akiec_predictions = labels[0]

    filenames = test_batches.filenames
    results = pd.DataFrame({
        "Filename": filenames,
        "Vgg_Predictions": predictions
    })
    results.to_csv("test_prediction_vgg16.csv", index=False)
예제 #2
0
def predict_densenet121():

    # DesneNet generators

    test_batches = ImageDataGenerator(
        preprocessing_function= \
            applications.densenet.preprocess_input).flow_from_directory(
        test2_path,
        target_size=(image_size, image_size),
        batch_size=test_batch_size,
        shuffle=False)

    input_tensor = Input(shape=(224, 224, 3))

    #Loading the model
    model = DenseNet121(input_tensor=input_tensor,
                        weights='imagenet',
                        include_top=False)

    # add a global spatial average pooling layer
    x = model.output
    x = GlobalAveragePooling2D()(x)
    x = Dense(1024, activation='relu')(x)
    predictions = Dense(7, activation='softmax')(x)

    # this is the model we will train
    model = Model(inputs=model.input, outputs=predictions)

    model.load_weights('modelDenseNet121.h5')

    test_batches.reset()

    test_labels = test_batches.classes

    # Make predictions
    predictions = model.predict_generator(test_batches,
                                          steps=test_steps,
                                          verbose=1)

    # Declare a function for plotting the confusion matrix
    def plot_confusion_matrix(cm,
                              classes,
                              normalize=False,
                              title='Confusion matrix',
                              cmap=plt.cm.Blues):
        """
        This function prints and plots the confusion matrix.
        Normalization can be applied by setting `normalize=True`.
        """
        if normalize:
            cm = cm.astype('float') / cm.sum(axis=1)[:, np.newaxis]
            print("Normalized confusion matrix")
        else:
            print('Confusion matrix, without normalization')

        print(cm)

        plt.imshow(cm, interpolation='nearest', cmap=cmap)
        plt.title(title)
        plt.colorbar()
        tick_marks = np.arange(len(classes))
        plt.xticks(tick_marks, classes, rotation=45)
        plt.yticks(tick_marks, classes)

        fmt = '.2f' if normalize else 'd'
        thresh = cm.max() / 2.
        for i, j in itertools.product(range(cm.shape[0]), range(cm.shape[1])):
            plt.text(j,
                     i,
                     format(cm[i, j], fmt),
                     horizontalalignment="center",
                     color="white" if cm[i, j] > thresh else "black")

        plt.ylabel('True label')
        plt.xlabel('Predicted label')
        plt.tight_layout()
        plt.savefig('confusion_matrix_DenseNet121.png')

    cm = confusion_matrix(test_labels, predictions.argmax(axis=1))

    cm_plot_labels = ['akiec', 'bcc', 'bkl', 'df', 'mel', 'nv', 'vasc']

    plot_confusion_matrix(cm, cm_plot_labels)

    recall = np.diag(cm) / np.sum(cm, axis=1)
    precision = np.diag(cm) / np.sum(cm, axis=0)

    print("Mean recall " + str(np.mean(recall).item()))
    print("Mean precision " + str(np.mean(precision).item()))

    print("Mean recall " + str(
        recall_score(
            test_labels, predictions.argmax(axis=1), average='weighted')))
    print("Balanced Accuracy " +
          str(balanced_accuracy_score(test_labels, predictions.argmax(
              axis=1))))
    print("Mean Precision " + str(
        precision_score(
            test_labels, predictions.argmax(axis=1), average='weighted')))
    print("Mean f1 score " + str(
        f1_score(test_labels, predictions.argmax(axis=1), average='weighted')))

    file = open("DenseNet121_results_last", "w+")
    file.write("Mean recall " + str(np.mean(recall).item()) + "\n")
    file.write("Mean precision " + str(np.mean(precision).item()) + "\n")
    file.write("Mean recall " + str(
        recall_score(
            test_labels, predictions.argmax(axis=1), average='weighted')) +
               "\n")
    file.write(
        "Balanced Accuracy " +
        str(balanced_accuracy_score(test_labels, predictions.argmax(axis=1))) +
        "\n")
    file.write("Mean Precision " + str(
        precision_score(
            test_labels, predictions.argmax(axis=1), average='weighted')) +
               "\n")
    file.write("Mean f1 score " + str(
        f1_score(test_labels, predictions.argmax(
            axis=1), average='weighted')) + "\n")

    file.close()

    predicted_class_indices = np.argmax(predictions, axis=1)

    labels = train_batches.class_indices
    labels = dict((v, k) for k, v in labels.items())
    predictions = [labels[k] for k in predicted_class_indices]

    filenames = test_batches.filenames
    results = pd.DataFrame({
        "Filename": filenames,
        "DenseNet121_Predictions": predictions
    })
    results.to_csv("test_prediction_densenet121.csv", index=False)