def run(self):
        ###### Create the datasets

        data_path = "./datasets/report_ultimate_random/"

        training_data_path = data_path + "train_2.h5"
        testing_data_path = data_path + "test_2.h5"
        ds_training = DatasetBrainParcellation()
        ds_training.read(training_data_path)
        # image = PIL.Image.fromarray(tile_raster_images(X=ds_training.inputs[0:100],
        #                                                img_shape=(29, 29), tile_shape=(10, 10),
        #                                                tile_spacing=(1, 1)))
        # image.save(self.path + "filters_corruption_30.png")

        prop_validation = 0.5  # Percentage of the testing dataset that is used for validation (early stopping)
        ds_testing = DatasetBrainParcellation()
        ds_testing.read(testing_data_path)
        ds_validation, ds_testing = ds_testing.split_datapoints_proportion(prop_validation)
        # Few stats about the targets
        analyse_classes(np.argmax(ds_training.outputs, axis=1))

        # Scale some part of the data
        s = pickle.load(open(self.path + "s.scaler", "rb"))
        s.scale(ds_testing.inputs)

        ###### Create the network

        # Load the networks
        net1 = NetworkUltimateMLPWithoutCentroids()
        net1.init(29, 13, 135)
        net1.load_parameters(open_h5file(self.path + "net_no_centroids.net"))

        net2 = NetworkUltimateMLP()
        net2.init(29, 13, 134, 135)
        net2.load_parameters(open_h5file(self.path + "net.net"))

        ###### Evaluate on testing

        compute_centroids_estimate(ds_testing, net1, net2, s)
    def run(self):
        ###### Create the datasets

        data_path = "./datasets/report_ultimate_random/"

        training_data_path = data_path + "train_2.h5"
        testing_data_path = data_path + "test_2.h5"
        ds_training = DatasetBrainParcellation()
        ds_training.read(training_data_path)
        # image = PIL.Image.fromarray(tile_raster_images(X=ds_training.inputs[0:100],
        #                                                img_shape=(29, 29), tile_shape=(10, 10),
        #                                                tile_spacing=(1, 1)))
        # image.save(self.path + "filters_corruption_30.png")

        prop_validation = 0.5  # Percentage of the testing dataset that is used for validation (early stopping)
        ds_testing = DatasetBrainParcellation()
        ds_testing.read(testing_data_path)
        ds_validation, ds_testing = ds_testing.split_datapoints_proportion(prop_validation)
        # Few stats about the targets
        analyse_classes(np.argmax(ds_training.outputs, axis=1))

        # Scale some part of the data
        s = pickle.load(open(self.path + "s.scaler", "rb"))
        s.scale(ds_testing.inputs)

        ###### Create the network

        # Load the networks
        net1 = NetworkUltimateMLPWithoutCentroids()
        net1.init(29, 13, 135)
        net1.load_parameters(open_h5file(self.path + "net_no_centroids.net"))

        net2 = NetworkUltimateMLP()
        net2.init(29, 13, 134, 135)
        net2.load_parameters(open_h5file(self.path + "net.net"))

        ###### Evaluate on testing

        compute_centroids_estimate(ds_testing, net1, net2, s)
    def run(self):
        ###### Create the datasets

        # Testing
        data_cf_test = load_config("cfg_testing_data_creation.py")
        data_cf_test.general[
            "file_path"] = data_cf_test.data_path + "test_temp.h5"
        data_generator_test = DataGeneratorBrain()
        data_generator_test.init_from_config(data_cf_test)
        ds_test_temp = DatasetBrainParcellation()
        vx, inputs, outputs, file_ids = data_generator_test.generate_parallel(
            data_cf_test.general["n_data"])
        ds_test_temp.populate(inputs, outputs, vx, file_ids)
        ds_test_temp.shuffle_data()

        # Training
        prop_validation = 0.1
        data_cf_train = load_config("cfg_training_data_creation.py")
        data_cf_train.general[
            "file_path"] = data_cf_train.data_path + "train_temp.h5"
        data_generator_train = DataGeneratorBrain()
        data_generator_train.init_from_config(data_cf_train)
        ds_train_temp = DatasetBrainParcellation()
        vx, inputs, outputs, file_ids = data_generator_train.generate_parallel(
            data_cf_train.general["n_data"])
        ds_train_temp.populate(inputs, outputs, vx, file_ids)
        ds_train_temp.shuffle_data()
        [ds_train_temp,
         ds_validation_temp] = ds_train_temp.split_dataset_proportions(
             [1 - prop_validation, prop_validation])

        ## Scale some part of the data
        print "Scaling"
        s = Scaler([slice(-134, None, None)])
        s.compute_parameters(ds_train_temp.inputs)
        s.scale(ds_train_temp.inputs)
        s.scale(ds_validation_temp.inputs)
        s.scale(ds_test_temp.inputs)
        pickle.dump(s, open(self.path + "s.scaler", "wb"))

        ###### Create the network
        net = NetworkUltimateConv()
        net.init(29, 29, 13, 134, 135)

        print net

        ###### Configure the trainer

        # Cost function
        cost_function = CostNegLL()

        # Learning update
        learning_rate = 0.05
        momentum = 0.5
        lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

        # Create monitors and add them to the trainer
        freq = 1
        freq2 = 0.00001
        err_training = MonitorErrorRate(freq, "Train", ds_train_temp)
        err_testing = MonitorErrorRate(freq, "Test", ds_test_temp)
        err_validation = MonitorErrorRate(freq, "Val", ds_validation_temp)
        # dice_training = MonitorDiceCoefficient(freq, "Train", ds_train_temp, 135)
        dice_testing = MonitorDiceCoefficient(freq, "Test", ds_test_temp, 135)
        # dice_validation = MonitorDiceCoefficient(freq, "Val", ds_validation_temp, 135)

        # Create stopping criteria and add them to the trainer
        max_epoch = MaxEpoch(100)
        #early_stopping = EarlyStopping(err_validation, 10, 0.99, 5)

        # Create the network selector
        params_selector = ParamSelectorBestMonitoredValue(err_validation)

        # Create the trainer object
        batch_size = 200
        t = Trainer(net, cost_function, params_selector, [max_epoch],
                    lr_update, ds_train_temp, batch_size,
                    [err_training, err_validation, err_testing, dice_testing])

        ###### Train the network

        n_iter = 1
        evolution = np.zeros((2, n_iter))

        n_validation_data = ds_validation_temp.n_data

        for i in xrange(n_iter):

            # Train
            t.train()
            net.save_parameters(self.path + "net.net")

            # Test
            print "ERRORS::::"
            out_pred = net.predict(ds_test_temp.inputs, 1000)
            errors = np.argmax(out_pred, axis=1) != np.argmax(
                ds_test_temp.outputs, axis=1)
            print np.mean(errors)
            evolution[0, i] = np.mean(errors)

            out_pred = net.predict(ds_validation_temp.inputs, 1000)
            errors = np.argmax(out_pred, axis=1) != np.argmax(
                ds_validation_temp.outputs, axis=1)
            print np.mean(errors)
            evolution[1, i] = np.mean(errors)

            vx_errors = ds_train_temp.vx[errors]
            file_ids_errors = ds_validation_temp.file_ids[errors]
            inputs_errors = ds_validation_temp.inputs[errors]
            outputs_errors = ds_validation_temp.outputs[errors]

            # Update datasets, trainer, monitors
            n_data = int(round(0.9 * data_cf_train.general["n_data"]))
            prop_validation = 0.1
            vx, inputs, outputs, file_ids = data_generator_train.generate_parallel(
                n_data)
            s.scale(inputs)
            split = int(round(n_data * (1 - prop_validation)))
            ds_train_temp.populate(
                np.concatenate([inputs[0:split], inputs_errors], axis=0),
                np.concatenate([outputs[0:split], outputs_errors], axis=0),
                np.concatenate([vx[0:split], vx_errors], axis=0),
                np.concatenate([file_ids[0:split], file_ids_errors], axis=0))
            ds_train_temp.shuffle_data()

            ds_validation_temp.populate(inputs[split:n_data],
                                        outputs[split:n_data],
                                        vx[split:n_data],
                                        file_ids[split:n_data])
            ds_validation_temp.shuffle_data()

            print ds_train_temp.n_data

            t.init()

        print evolution

        ###### Plot the records
        # save_records_plot(self.path, [err_training, err_validation, err_testing], "err", t.n_train_batches, "upper right")
        # save_records_plot(self.path, [dice_testing], "dice", t.n_train_batches, "lower right")

        ###### Save the network

        net.save_parameters(self.path + "net.net")
Example #4
0
    extract_vx = ExtractVoxelAll(1)
    pick_vx = PickVoxel(select_region, extract_vx)
    pick_patch = create_pick_features(cf_data)
    pick_tg = create_pick_target(cf_data)

    # Create the data generator
    data_gen = DataGeneratorBrain()
    data_gen.init_from(file_list, pick_vx, pick_patch, pick_tg)

    # Evaluate the centroids
    net_wo_centroids_path = "./experiments/report_3_patches_balanced_conv/"
    net_wo_centroids = NetworkThreePatchesConv()
    net_wo_centroids.init(29, 135)
    net_wo_centroids.load_parameters(
        open_h5file(net_wo_centroids_path + "net.net"))
    ds_testing = DatasetBrainParcellation()
    ds_testing.read(data_path + "train.h5")
    pred_wo_centroids = np.argmax(net_wo_centroids.predict(
        ds_testing.inputs, 1000),
                                  axis=1)
    region_centroids = RegionCentroids(134)
    region_centroids.update_barycentres(ds_testing.vx, pred_wo_centroids)

    # Generate and evaluate the dataset
    start_time = time.clock()
    dices = np.zeros((n_files, 134))
    errors = np.zeros((n_files, ))

    pred_functions = {}
    for atlas_id in xrange(n_files):
    def run(self):

        data_path = "./datasets/final_exp_n_layers_2000/"
        range_n_layers = np.arange(0, 6, 1)
        error_rates = np.zeros(range_n_layers.shape)
        dice_coeffs = np.zeros(range_n_layers.shape)

        for idx, n_layers in enumerate(range_n_layers):

            print "patch width {}".format(n_layers)

            ### Load the config file
            data_cf_train = load_config("cfg_training_data_creation.py")
            data_cf_test = load_config("cfg_testing_data_creation.py")

            # Create the folder if it does not exist
            if not os.path.exists(data_path):
                os.makedirs(data_path)
            data_cf_train.data_path = data_path
            data_cf_test.data_path = data_path
            data_cf_train.general["file_path"] = data_path + "train.h5"
            data_cf_test.general["file_path"] = data_path + "test.h5"

            ### Generate and write on file the dataset
            generate_and_save(data_cf_train)
            generate_and_save(data_cf_test)

            ###### Create the datasets

            training_data_path = data_path + "train.h5"
            testing_data_path = data_path + "test.h5"
            ds_training = DatasetBrainParcellation()
            ds_training.read(training_data_path)
            # image = PIL.Image.fromarray(tile_raster_images(X=ds_training.inputs[0:100],
            #                                                img_shape=(29, 29), tile_shape=(10, 10),
            #                                                tile_spacing=(1, 1)))
            # image.save(self.path + "filters_corruption_30.png")

            ds_validation = DatasetBrainParcellation()
            ds_validation.read(testing_data_path)
            # Few stats about the targets
            analyse_classes(np.argmax(ds_training.outputs, axis=1))

            # Scale some part of the data
            # s = Scaler([slice(-134, None, None)])
            # s.compute_parameters(ds_training.inputs)
            # s.scale(ds_training.inputs)
            # s.scale(ds_validation.inputs)
            # s.scale(ds_testing.inputs)
            # pickle.dump(s, open(self.path + "s.scaler", "wb"))

            ###### Create the network

            net = MLP()
            net.init([29**2] + [2000]*n_layers +[135])

            print net

            # image = PIL.Image.fromarray(tile_raster_images(X=net.get_layer(0).get_layer_block(0).w.get_value().reshape((10,-1)),
            #                                                img_shape=(5, 5), tile_shape=(3, 3),
            #                                                tile_spacing=(1, 1)))
            # image.save(self.path + "filters_corruption_30.png")

            ###### Configure the trainer

            # Cost function
            cost_function = CostNegLL()

            # Learning update
            learning_rate = 0.05
            momentum = 0.5
            lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

            # Create monitors and add them to the trainer
            err_validation = MonitorErrorRate(1, "Validation", ds_validation)
            dice_validation = MonitorDiceCoefficient(1, "Validation", ds_validation, 135)

            # Create stopping criteria and add them to the trainer
            max_epoch = MaxEpoch(300)
            early_stopping = EarlyStopping(err_validation, 5, 0.99, 5)

            # Create the network selector
            params_selector = ParamSelectorBestMonitoredValue(err_validation)

            # Create the trainer object
            batch_size = 200
            t = Trainer(net, cost_function, params_selector, [max_epoch, early_stopping],
                        lr_update, ds_training, batch_size,
                        [err_validation, dice_validation])


            ###### Train the network

            t.train()

            ###### Plot the records

            error_rates[idx] = err_validation.get_minimum()
            dice_coeffs[idx] = dice_validation.get_maximum()

        print error_rates
        print dice_coeffs

        plt.figure()
        plt.plot(range_n_layers, error_rates, label="Validation error rates")
        plt.plot(range_n_layers, dice_coeffs, label="Validation dice coefficient")

        plt.xlabel('Number of layers')
        plt.savefig(self.path + "res.png")
        tikz_save(self.path + "res.tikz", figureheight = '\\figureheighttik', figurewidth = '\\figurewidthtik')
    def run(self):
        ###### Create the datasets

        # aa = CostNegLLWeighted(np.array([0.9, 0.1]))
        # e = theano.function(inputs=[], outputs=aa.test())
        # print e()

        ## Load the data
        training_data_path = self.data_path + "train.h5"
        ds_training = DatasetBrainParcellation()
        ds_training.read(training_data_path)

        [ds_training, ds_validation] = ds_training.split_dataset_proportions([0.95, 0.05])

        testing_data_path = self.data_path + "test.h5"
        ds_testing = DatasetBrainParcellation()
        ds_testing.read(testing_data_path)

        ## Display data sample
        # image = PIL.Image.fromarray(tile_raster_images(X=ds_training.inputs[0:50],
        #                                                img_shape=(29, 29), tile_shape=(5, 10),
        #                                                tile_spacing=(1, 1)))
        # image.save(self.path + "filters_corruption_30.png")

        ## Few stats about the targets
        classes, proportion_class = analyse_classes(np.argmax(ds_training.outputs, axis=1), "Training data:")
        print classes
        ## Scale some part of the data
        print "Scaling"
        s = Scaler([slice(-134, None, None)])
        s.compute_parameters(ds_training.inputs)
        s.scale(ds_training.inputs)
        s.scale(ds_validation.inputs)
        s.scale(ds_testing.inputs)
        pickle.dump(s, open(self.path + "s.scaler", "wb"))

        ###### Create the network
        net = NetworkUltimateConv()
        net.init(33, 29, 5, 134, 135)

        print net

        ###### Configure the trainer

        # Cost function
        cost_function = CostNegLL(net.ls_params)

        # Learning update
        learning_rate = 0.05
        momentum = 0.5
        lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

        # Create monitors and add them to the trainer
        freq = 1
        freq2 = 0.00001
        # err_training = MonitorErrorRate(freq, "Train", ds_training)
        # err_testing = MonitorErrorRate(freq, "Test", ds_testing)
        err_validation = MonitorErrorRate(freq, "Val", ds_validation)
        # dice_training = MonitorDiceCoefficient(freq, "Train", ds_training, 135)
        dice_testing = MonitorDiceCoefficient(freq, "Test", ds_testing, 135)
        # dice_validation = MonitorDiceCoefficient(freq, "Val", ds_validation, 135)

        # Create stopping criteria and add them to the trainer
        max_epoch = MaxEpoch(300)
        early_stopping = EarlyStopping(err_validation, 10, 0.99, 5)

        # Create the network selector
        params_selector = ParamSelectorBestMonitoredValue(err_validation)

        # Create the trainer object
        batch_size = 200
        t = Trainer(net, cost_function, params_selector, [max_epoch, early_stopping],
                    lr_update, ds_training, batch_size,
                    [err_validation, dice_testing])

        ###### Train the network

        t.train()

        ###### Plot the records

        # pred = np.argmax(t.net.predict(ds_testing.inputs, 10000), axis=1)
        # d = compute_dice(pred, np.argmax(ds_testing.outputs, axis=1), 134)
        # print "Dice test: {}".format(np.mean(d))
        # print "Error rate test: {}".format(error_rate(np.argmax(ds_testing.outputs, axis=1), pred))

        save_records_plot(self.path, [err_validation], "err", t.n_train_batches, "upper right")
        # save_records_plot(self.path, [dice_testing], "dice", t.n_train_batches, "lower right")

        ###### Save the network

        net.save_parameters(self.path + "net.net")
from spynet.utils.utilities import open_h5file

if __name__ == '__main__':
    """
    Evaluate a trained network (without approximating the centroids)
    """

    experiment_path = "./experiments/paper_ultimate_conv/"
    data_path = "./datasets/paper_ultimate_conv/"

    # Load the network
    net = NetworkUltimateConv()
    net.init(33, 29, 5, 134, 135)
    net.load_parameters(open_h5file(experiment_path + "net.net"))
    n_out = net.n_out

    # Load the scaler
    scaler = pickle.load(open(experiment_path + "s.scaler", "rb"))

    testing_data_path = data_path + "test.h5"
    ds_testing = DatasetBrainParcellation()
    ds_testing.read(testing_data_path)
    scaler.scale(ds_testing.inputs)

    out_pred = net.predict(ds_testing.inputs, 1000)
    errors = np.argmax(out_pred, axis=1) != np.argmax(ds_testing.outputs,
                                                      axis=1)
    dice = compute_dice(np.argmax(out_pred, axis=1),
                        np.argmax(ds_testing.outputs, axis=1), 134)
    print np.mean(dice)
    print np.mean(errors)
    def run(self):
        ###### Create the datasets

        # Testing
        data_cf_test = load_config("cfg_testing_data_creation.py")
        data_cf_test.general["file_path"] = data_cf_test.data_path + "test_temp.h5"
        data_generator_test = DataGeneratorBrain()
        data_generator_test.init_from_config(data_cf_test)
        ds_test_temp = DatasetBrainParcellation()
        vx, inputs, outputs, file_ids = data_generator_test.generate_parallel(data_cf_test.general["n_data"])
        ds_test_temp.populate(inputs, outputs, vx, file_ids)
        ds_test_temp.shuffle_data()

        # Training
        prop_validation = 0.1
        data_cf_train = load_config("cfg_training_data_creation.py")
        data_cf_train.general["file_path"] = data_cf_train.data_path + "train_temp.h5"
        data_generator_train = DataGeneratorBrain()
        data_generator_train.init_from_config(data_cf_train)
        ds_train_temp = DatasetBrainParcellation()
        vx, inputs, outputs, file_ids = data_generator_train.generate_parallel(data_cf_train.general["n_data"])
        ds_train_temp.populate(inputs, outputs, vx, file_ids)
        ds_train_temp.shuffle_data()
        [ds_train_temp, ds_validation_temp] = ds_train_temp.split_dataset_proportions(
            [1 - prop_validation, prop_validation]
        )

        ## Scale some part of the data
        print "Scaling"
        s = Scaler([slice(-134, None, None)])
        s.compute_parameters(ds_train_temp.inputs)
        s.scale(ds_train_temp.inputs)
        s.scale(ds_validation_temp.inputs)
        s.scale(ds_test_temp.inputs)
        pickle.dump(s, open(self.path + "s.scaler", "wb"))

        ###### Create the network
        net = NetworkUltimateConv()
        net.init(29, 29, 13, 134, 135)

        print net

        ###### Configure the trainer

        # Cost function
        cost_function = CostNegLL()

        # Learning update
        learning_rate = 0.05
        momentum = 0.5
        lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

        # Create monitors and add them to the trainer
        freq = 1
        freq2 = 0.00001
        err_training = MonitorErrorRate(freq, "Train", ds_train_temp)
        err_testing = MonitorErrorRate(freq, "Test", ds_test_temp)
        err_validation = MonitorErrorRate(freq, "Val", ds_validation_temp)
        # dice_training = MonitorDiceCoefficient(freq, "Train", ds_train_temp, 135)
        dice_testing = MonitorDiceCoefficient(freq, "Test", ds_test_temp, 135)
        # dice_validation = MonitorDiceCoefficient(freq, "Val", ds_validation_temp, 135)

        # Create stopping criteria and add them to the trainer
        max_epoch = MaxEpoch(100)
        # early_stopping = EarlyStopping(err_validation, 10, 0.99, 5)

        # Create the network selector
        params_selector = ParamSelectorBestMonitoredValue(err_validation)

        # Create the trainer object
        batch_size = 200
        t = Trainer(
            net,
            cost_function,
            params_selector,
            [max_epoch],
            lr_update,
            ds_train_temp,
            batch_size,
            [err_training, err_validation, err_testing, dice_testing],
        )

        ###### Train the network

        n_iter = 1
        evolution = np.zeros((2, n_iter))

        n_validation_data = ds_validation_temp.n_data

        for i in xrange(n_iter):

            # Train
            t.train()
            net.save_parameters(self.path + "net.net")

            # Test
            print "ERRORS::::"
            out_pred = net.predict(ds_test_temp.inputs, 1000)
            errors = np.argmax(out_pred, axis=1) != np.argmax(ds_test_temp.outputs, axis=1)
            print np.mean(errors)
            evolution[0, i] = np.mean(errors)

            out_pred = net.predict(ds_validation_temp.inputs, 1000)
            errors = np.argmax(out_pred, axis=1) != np.argmax(ds_validation_temp.outputs, axis=1)
            print np.mean(errors)
            evolution[1, i] = np.mean(errors)

            vx_errors = ds_train_temp.vx[errors]
            file_ids_errors = ds_validation_temp.file_ids[errors]
            inputs_errors = ds_validation_temp.inputs[errors]
            outputs_errors = ds_validation_temp.outputs[errors]

            # Update datasets, trainer, monitors
            n_data = int(round(0.9 * data_cf_train.general["n_data"]))
            prop_validation = 0.1
            vx, inputs, outputs, file_ids = data_generator_train.generate_parallel(n_data)
            s.scale(inputs)
            split = int(round(n_data * (1 - prop_validation)))
            ds_train_temp.populate(
                np.concatenate([inputs[0:split], inputs_errors], axis=0),
                np.concatenate([outputs[0:split], outputs_errors], axis=0),
                np.concatenate([vx[0:split], vx_errors], axis=0),
                np.concatenate([file_ids[0:split], file_ids_errors], axis=0),
            )
            ds_train_temp.shuffle_data()

            ds_validation_temp.populate(
                inputs[split:n_data], outputs[split:n_data], vx[split:n_data], file_ids[split:n_data]
            )
            ds_validation_temp.shuffle_data()

            print ds_train_temp.n_data

            t.init()

        print evolution

        ###### Plot the records
        # save_records_plot(self.path, [err_training, err_validation, err_testing], "err", t.n_train_batches, "upper right")
        # save_records_plot(self.path, [dice_testing], "dice", t.n_train_batches, "lower right")

        ###### Save the network

        net.save_parameters(self.path + "net.net")
    select_region = SelectWholeBrain()
    extract_vx = ExtractVoxelAll(1)
    pick_vx = PickVoxel(select_region, extract_vx)
    pick_patch = create_pick_features(cf_data)
    pick_tg = create_pick_target(cf_data)

    # Create the data generator
    data_gen = DataGeneratorBrain()
    data_gen.init_from(file_list, pick_vx, pick_patch, pick_tg)

    # Evaluate the centroids
    net_wo_centroids_path = "./experiments/report_3_patches_balanced_conv/"
    net_wo_centroids = NetworkThreePatchesConv()
    net_wo_centroids.init(29, 135)
    net_wo_centroids.load_parameters(open_h5file(net_wo_centroids_path + "net.net"))
    ds_testing = DatasetBrainParcellation()
    ds_testing.read(data_path + "train.h5")
    pred_wo_centroids = np.argmax(net_wo_centroids.predict(ds_testing.inputs, 1000), axis=1)
    region_centroids = RegionCentroids(134)
    region_centroids.update_barycentres(ds_testing.vx, pred_wo_centroids)

    # Generate and evaluate the dataset
    start_time = time.clock()
    dices = np.zeros((n_files, 134))
    errors = np.zeros((n_files,))

    pred_functions = {}
    for atlas_id in xrange(n_files):

        print "Atlas: {}".format(atlas_id)
    def run(self):

        data_path = "./datasets/final_exp_size_patch/"
        range_patch_size = np.arange(3, 37, 2)
        error_rates = np.zeros(range_patch_size.shape)
        dice_coeffs = np.zeros(range_patch_size.shape)

        for idx, patch_size in enumerate(range_patch_size):

            print "patch width {}".format(patch_size)

            ### Load the config file
            data_cf_train = load_config("cfg_training_data_creation.py")
            data_cf_test = load_config("cfg_testing_data_creation.py")
            data_cf_train.data_path = data_path
            data_cf_test.data_path = data_path
            data_cf_train.pick_features[0]["patch_width"] = patch_size
            data_cf_test.pick_features[0]["patch_width"] = patch_size

            ### Generate and write on file the dataset
            generate_and_save(data_cf_train)
            generate_and_save(data_cf_test)

            ###### Create the datasets

            training_data_path = data_path + "train.h5"
            testing_data_path = data_path + "test.h5"
            ds_training = DatasetBrainParcellation()
            ds_training.read(training_data_path)
            # image = PIL.Image.fromarray(tile_raster_images(X=ds_training.inputs[0:100],
            #                                                img_shape=(29, 29), tile_shape=(10, 10),
            #                                                tile_spacing=(1, 1)))
            # image.save(self.path + "filters_corruption_30.png")

            ds_validation = DatasetBrainParcellation()
            ds_validation.read(testing_data_path)
            # Few stats about the targets
            analyse_classes(np.argmax(ds_training.outputs, axis=1))

            # Scale some part of the data
            # s = Scaler([slice(-134, None, None)])
            # s.compute_parameters(ds_training.inputs)
            # s.scale(ds_training.inputs)
            # s.scale(ds_validation.inputs)
            # s.scale(ds_testing.inputs)
            # pickle.dump(s, open(self.path + "s.scaler", "wb"))

            ###### Create the network

            net = MLP()
            net.init([
                patch_size**2, patch_size**2, patch_size**2, patch_size**2, 135
            ])

            print net

            # image = PIL.Image.fromarray(tile_raster_images(X=net.get_layer(0).get_layer_block(0).w.get_value().reshape((10,-1)),
            #                                                img_shape=(5, 5), tile_shape=(3, 3),
            #                                                tile_spacing=(1, 1)))
            # image.save(self.path + "filters_corruption_30.png")

            ###### Configure the trainer

            # Cost function
            cost_function = CostNegLL()

            # Learning update
            learning_rate = 0.05
            momentum = 0.5
            lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

            # Create monitors and add them to the trainer
            err_validation = MonitorErrorRate(1, "Validation", ds_validation)
            dice_validation = MonitorDiceCoefficient(1, "Validation",
                                                     ds_validation, 135)

            # Create stopping criteria and add them to the trainer
            max_epoch = MaxEpoch(300)
            early_stopping = EarlyStopping(err_validation, 5, 0.99, 5)

            # Create the network selector
            params_selector = ParamSelectorBestMonitoredValue(err_validation)

            # Create the trainer object
            batch_size = 200
            t = Trainer(net, cost_function, params_selector,
                        [max_epoch, early_stopping], lr_update, ds_training,
                        batch_size, [err_validation, dice_validation])

            ###### Train the network

            t.train()

            ###### Plot the records

            error_rates[idx] = err_validation.get_minimum()
            dice_coeffs[idx] = dice_validation.get_maximum()

        print error_rates
        print dice_coeffs

        plt.figure()
        plt.plot(range_patch_size, error_rates, label="Validation error rates")
        plt.plot(range_patch_size,
                 dice_coeffs,
                 label="Validation dice coefficient")

        plt.xlabel('Patch size')
        plt.savefig(self.path + "res.png")
        tikz_save(self.path + "res.tikz",
                  figureheight='\\figureheighttik',
                  figurewidth='\\figurewidthtik')
Example #11
0
from data_brain_parcellation import DatasetBrainParcellation, DataGeneratorBrain, list_miccai_files, RegionCentroids
from spynet.utils.utilities import open_h5file


if __name__ == '__main__':
    """
    Evaluate a trained network (without approximating the centroids)
    """

    experiment_path = "./experiments/paper_ultimate_conv/"
    data_path = "./datasets/paper_ultimate_conv/"

    # Load the network
    net = NetworkUltimateConv()
    net.init(33, 29, 5, 134, 135)
    net.load_parameters(open_h5file(experiment_path + "net.net"))
    n_out = net.n_out

    # Load the scaler
    scaler = pickle.load(open(experiment_path + "s.scaler", "rb"))

    testing_data_path = data_path + "test.h5"
    ds_testing = DatasetBrainParcellation()
    ds_testing.read(testing_data_path)
    scaler.scale(ds_testing.inputs)

    out_pred = net.predict(ds_testing.inputs, 1000)
    errors = np.argmax(out_pred, axis=1) != np.argmax(ds_testing.outputs, axis=1)
    dice = compute_dice(np.argmax(out_pred, axis=1), np.argmax(ds_testing.outputs, axis=1), 134)
    print np.mean(dice)
    print np.mean(errors)
    # Options for the generation of the dataset
    # The generation/evaluation of the dataset has to be split into batches as a whole brain does not fit into memory
    batch_size = 50000
    select_region = SelectWholeBrain()
    extract_vx = ExtractVoxelAll(1)
    pick_vx = PickVoxel(select_region, extract_vx)
    pick_patch = create_pick_features(cf_data)
    pick_tg = create_pick_target(cf_data)

    # Create the data generator
    data_gen = DataGeneratorBrain()
    data_gen.init_from(file_list, pick_vx, pick_patch, pick_tg)

    # Evaluate the centroids
    data_cf_train = imp.load_source("cf_data", data_path + "cfg_training_data_creation.py")
    ds_train_temp = DatasetBrainParcellation()
    ds_train_temp.populate_from_config(data_cf_train)
    region_centroids = RegionCentroids(134)
    region_centroids.update_barycentres(ds_train_temp.vx, np.argmax(ds_train_temp.outputs, axis=1))

    # Generate and evaluate the dataset
    start_time = time.clock()
    dices = np.zeros((n_files, 134))
    errors = np.zeros((n_files,))

    pred_functions = {}
    for atlas_id in xrange(n_files):

        print "Atlas: {}".format(atlas_id)

        ls_vx = []
    def run(self):
        ###### Create the datasets

        # aa = CostNegLLWeighted(np.array([0.9, 0.1]))
        # e = theano.function(inputs=[], outputs=aa.test())
        # print e()

        ## Load the data
        training_data_path = self.data_path + "train.h5"
        ds_training = DatasetBrainParcellation()
        ds_training.read(training_data_path)

        [ds_training,
         ds_validation] = ds_training.split_dataset_proportions([0.95, 0.05])

        testing_data_path = self.data_path + "test.h5"
        ds_testing = DatasetBrainParcellation()
        ds_testing.read(testing_data_path)

        ## Display data sample
        # image = PIL.Image.fromarray(tile_raster_images(X=ds_training.inputs[0:50],
        #                                                img_shape=(29, 29), tile_shape=(5, 10),
        #                                                tile_spacing=(1, 1)))
        # image.save(self.path + "filters_corruption_30.png")

        ## Few stats about the targets
        classes, proportion_class = analyse_classes(
            np.argmax(ds_training.outputs, axis=1), "Training data:")
        print(classes)
        ## Scale some part of the data
        print("Scaling")
        s = Scaler([slice(-134, None, None)])
        s.compute_parameters(ds_training.inputs)
        s.scale(ds_training.inputs)
        s.scale(ds_validation.inputs)
        s.scale(ds_testing.inputs)
        pickle.dump(s, open(self.path + "s.scaler", "wb"))

        ###### Create the network
        # net = NetworkUltimateConv()
        # net = NetworkWithoutCentroidConv()
        # net.init(29,13,135)
        net = NetworkConvDropout()
        # net = NetworkConvVGGDropout()
        # net = ResNet()
        net.init(29, 29, 13, 134, 135)
        # net = NetworkDeeperConv()
        # net.init(29, 29, 13, 134, 135,3)

        print(net)

        ###### Configure the trainer

        # Cost function
        # cost_function = CostNegLL(net.ls_params)
        cost_function = CostNegLL()

        # Learning update
        learning_rate = 0.05
        momentum = 0.5
        lr_update = LearningUpdateGDMomentum(learning_rate, momentum)

        # Create monitors and add them to the trainer
        freq = 1
        freq2 = 0.00001
        # err_training = MonitorErrorRate(freq, "Train", ds_training)
        err_testing = MonitorErrorRate(freq, "Test", ds_testing)
        err_validation = MonitorErrorRate(freq, "Val", ds_validation)
        # dice_training = MonitorDiceCoefficient(freq, "Train", ds_training, 135)
        dice_testing = MonitorDiceCoefficient(freq, "Test", ds_testing, 135)
        dice_validation = MonitorDiceCoefficient(freq, "Val", ds_validation,
                                                 135)

        # Create stopping criteria and add them to the trainer
        max_epoch = MaxEpoch(300)
        early_stopping = EarlyStopping(err_validation, 10, 0.99, 5)

        # Create the network selector
        params_selector = ParamSelectorBestMonitoredValue(err_validation)

        # Create the trainer object
        # batch_size = 200
        batch_size = 10
        t = Trainer(
            net, cost_function, params_selector, [max_epoch, early_stopping],
            lr_update, ds_training, batch_size,
            [err_testing, err_validation, dice_testing, dice_validation])

        ###### Train the network

        t.train()

        ###### Plot the records

        # pred = np.argmax(t.net.predict(ds_testing.inputs, 10000), axis=1)
        # d = compute_dice(pred, np.argmax(ds_testing.outputs, axis=1), 134)
        # print "Dice test: {}".format(np.mean(d))
        # print "Error rate test: {}".format(error_rate(np.argmax(ds_testing.outputs, axis=1), pred))

        save_records_plot(self.path, [err_validation, err_testing], "err",
                          t.n_train_batches, "upper right")
        save_records_plot(self.path, [dice_validation, dice_testing], "dice",
                          t.n_train_batches, "lower right")

        ###### Save the network

        net.save_parameters(self.path + "net.net")
    def run(self):
        ###### Create the datasets

        # aa = CostNegLLWeighted(np.array([0.9, 0.1]))
        # e = theano.function(inputs=[], outputs=aa.test())
        # print e()

        ## Load the data
        training_data_path = self.data_path + "train.h5"
        ds_training = DatasetBrainParcellation()
        ds_training.read(training_data_path)

        ds_training, ds_validation = ds_training.split_dataset_proportions(
            [0.95, 0.05])

        testing_data_path = self.data_path + "test.h5"
        ds_testing = DatasetBrainParcellation()
        ds_testing.read(testing_data_path)

        ## Display data sample
        # image = PIL.Image.fromarray(tile_raster_images(X=ds_training.inputs[0:50],
        #                                                img_shape=(29, 29), tile_shape=(5, 10),
        #                                                tile_spacing=(1, 1)))
        # image.save(self.path + "filters_corruption_30.png")

        ## Few stats about the targets
        classes, proportion_class = analyse_classes(
            np.argmax(ds_training.outputs, axis=1), "Training data:")
        print(classes)
        ## Scale some part of the data
        print("Scaling")
        s = Scaler([slice(-134, None, None)])
        s.compute_parameters(ds_training.inputs)
        s.scale(ds_training.inputs)
        s.scale(ds_validation.inputs)
        s.scale(ds_testing.inputs)
        pickle.dump(s, open(self.path + "s.scaler", "wb"))

        ###### Create the network
        input_var = T.matrix('inputs')
        target_var = T.matrix('targets')
        # net = ConvNet(135, input_var, target_var, 29, 29, 13, 134)
        # net = ResNet(135, input_var, target_var, 29, 29, 13, 134)
        # net = VGGNet(135, input_var, target_var, 29, 29, 13, 134)
        # net = Conv3DNet_Multidropout(135, input_var, target_var, 29, 29, 13, 134)
        # net = Conv3DNetComp_Lg(135, input_var, target_var, 29, 29, 134)
        # net = GoogLeNet(135, input_var, target_var, 29, 29, 13, 134)
        # net = Conv3DNet_SmCompFilter(135, input_var, target_var, 29, 29, 13, 134)
        # net = Conv3DNet_HeNorm(135, input_var, target_var, 29, 29, 13, 134)
        # net = SmallInception(135, input_var, target_var, 29, 29, 13, 134)
        # net = Conv3DNet_NoCentroid(135, input_var, target_var, 29, 29, 13, 134)
        # net = ConvNet_VerySmall(135, input_var, target_var, 29, 29, 13, 134)
        #try:
        net = Inceptionv4Simple(135, input_var, target_var, 29, 29, 13, 134)
        # except Exception as e:
        # print("Program terminated at: " + datetime.datetime.now())
        #  print(e)

        # print net.net
        learning_rate = 0.045
        # learning_rate = 0.0001

        # Create stopping criteria and add them to the trainer
        max_epoch = 15
        # early_stopping = EarlyStopping(err_validation, 10, 0.99, 5)

        # Create the trainer object
        batch_size = 50
        t = Trainer(net, ds_testing, ds_validation, ds_training, batch_size,
                    learning_rate)

        ###### Train the network

        t.train()

        ###### Plot the records

        # pred = np.argmax(t.net.predict(ds_testing.inputs, 10000), axis=1)
        # d = compute_dice(pred, np.argmax(ds_testing.outputs, axis=1), 134)
        # print "Dice test: {}".format(np.mean(d))
        # print "Error rate test: {}".format(error_rate(np.argmax(ds_testing.outputs, axis=1), pred))

        save_records_plot(self.path, [t.val_errs, t.test_errs],
                          ["validation", "test", "Error"], "upper right")
        save_records_plot(self.path, [t.val_dices, t.test_dices],
                          ["validation", "test", "Dice coefficient"],
                          "lower right")

        ###### Save the network
        np.savez(self.path + 'model.npz', *t.best_net_param)
        print("network model saved")