Example #1
0
    modelName = 'FerretAndMouseROIs_Relu_BatchNorm_Dropout{}_{}'.format(
        options['blocks'], options['epochs'])  #_Relu_5x100_BatchNorm
    filePath_model = r'{}\Code\Dave\{}_model_{}.h5'.format(
        baseFolder, modelType, modelName)
    filePath_history = r'{}\Code\Dave\{}_history_{}.hdf5'.format(
        baseFolder, modelType, modelName)
    filePath_params = r'{}\Code\Dave\{}_params_{}.txt'.format(
        baseFolder, modelType, modelName)
    if reuseModel:
        model.loadModel(filePath_model)
        model.loadModelHistory(filePath_history)
        model.loadModelParameters(filePath_params)
    if trainModel:
        model.trainModel(train_images, train_labels, test_images, test_labels,
                         options)
        model.saveModel(filePath_model)
        model.saveModelHistory(filePath_history)
        model.saveModelParameters(filePath_params)

    # Show training/test accuracy and loss for model
    try:
        modelHistory = model.modelHistory
        metrics = modelHistory.keys()
        for metric_name in metrics:
            metric = modelHistory[metric_name]
            fig, ax = plt.subplots()
            ax.plot(metric.T)
            if (metric_name.startswith('accuracy')):
                ax.set_ylim([0.5, 1])
            elif (metric_name.startswith('loss')):
                ax.set_ylim([0, 1.25 * np.percentile(metric.flatten(), 95)])
    options.update({'augment_flipHoriz':
                    True})  # Boolean flag for horizontal flip
    options.update({'augment_flipVert':
                    True})  # Boolean flag for vertical flip
    options.update(
        {'augment_fillMode': 'reflect'}
    )  # Points outside image boundary are filled with: {"constant", "nearest", "reflect" or "wrap"}
    options.update(
        {'augment_fillVal': 0}
    )  # If augment_fillMode=='constant', points outside image boundary are filled with fillVal
    options.update({'augment_zca_whitening': False})  # Apply ZCA whitening
    model = UNet()
    if trainModel:
        accuracy = model.trainModel(train_images, train_labels, test_images,
                                    test_labels, options)
        model.saveModel(r'D:\Code\ROI Segmentation\Code\Dave\Unet_model2.h5')

        # Show accuracy model
        fig, ax = plt.subplots()
        ax.plot(accuracy)
        ax.set_ylim([0.5, 1])
        ax.set_ylabel('Classification accuracy')
        ax.set_xlabel('Training epoch')
        ax.legend(['Training data', 'Test data'])
        x0, x1 = ax.get_xlim()
        y0, y1 = ax.get_ylim()
        ax.set_aspect(abs(x1 - x0) / abs(y1 - y0))
    else:
        model.loadModel(r'D:\Code\ROI Segmentation\Code\Dave\Unet_model2.h5')

    # Get validation experience (ferret image and ROIs)