Beispiel #1
0
    def fit(self, x_train, y_train, x_test, y_test, y_true):

        y_pred = np.zeros(shape=y_test.shape)

        l = 0

        for dataset in datasets_names:

            if dataset == self.dataset_name:
                continue

            curr_dir = self.transfer_directory + dataset + '/' + self.dataset_name + '/'

            predictions_file_name = curr_dir + 'y_pred.npy'

            if check_if_file_exits(predictions_file_name):
                # then load only the predictions from the file
                curr_y_pred = np.load(predictions_file_name)
            else:
                # predict from models saved
                model = keras.models.load_model(curr_dir + 'best_model.hdf5')
                curr_y_pred = model.predict(x_test)
                keras.backend.clear_session()
                np.save(predictions_file_name, curr_y_pred)

            y_pred = y_pred + curr_y_pred

            l += 1

            keras.backend.clear_session()

        y_pred = y_pred / l

        # save predictions
        np.save(self.output_directory + 'y_pred.npy', y_pred)

        # convert the predicted from binary to integer
        y_pred = np.argmax(y_pred, axis=1)

        df_metrics = calculate_metrics(y_true, y_pred, 0.0)

        df_metrics.to_csv(self.output_directory + 'df_metrics.csv',
                          index=False)

        print(self.dataset_name, df_metrics['accuracy'][0])

        gc.collect()
Beispiel #2
0
def convert_to_float(s):
    s = s.split('_')[0]
    return float(s)


subprocess.call(['./receptive_field_remove_non_completed.sh'],
                stdout=subprocess.DEVNULL,
                stderr=subprocess.DEVNULL)

if len(sys.argv) == 1:
    matplotlib.use('agg')
    out_df = root_output_directory_df + 'df_res_sub_0.csv'

    idx_out = 1
    while check_if_file_exits(out_df):
        out_df = root_output_directory_df + 'df_res_sub_' + str(
            idx_out) + '.csv'
        idx_out += 1

    columns = [
        'pattern_len', 'pattern_pos', 'ts_len', 'ts_n', 'nb_classes',
        'filters', 'kernel_size', 'depth', 'use_residual', 'use_bottleneck',
        'accuracy'
    ]

    df_results = pd.DataFrame(index=[], columns=columns)

    df_results.to_csv(out_df)

    curr_idx = 0
Beispiel #3
0
    def fit(self, x_train, y_train, x_test, y_test, y_true):
        # no training since models are pre-trained
        start_time = time.time()

        y_pred = np.zeros(shape=y_test.shape)

        ll = 0

        # loop through all classifiers
        for model_name in self.classifiers:
            # loop through different initialization of classifiers
            for itr in self.iterations_to_take:
                if itr == 0:
                    itr_str = ''
                else:
                    itr_str = '_itr_' + str(itr)

                curr_archive_name = self.archive_name + itr_str

                curr_dir = self.models_dir.replace('classifier',
                                                   model_name).replace(
                                                       self.archive_name,
                                                       curr_archive_name)

                model = self.create_classifier(model_name,
                                               None,
                                               None,
                                               curr_dir,
                                               build=False)

                predictions_file_name = curr_dir + 'y_pred.npy'
                # check if predictions already made
                if check_if_file_exits(predictions_file_name):
                    # then load only the predictions from the file
                    curr_y_pred = np.load(predictions_file_name)
                else:
                    # then compute the predictions
                    curr_y_pred = model.predict(x_test,
                                                y_true,
                                                return_df_metrics=False)

                    keras.backend.clear_session()

                    np.save(predictions_file_name, curr_y_pred)

                y_pred = y_pred + curr_y_pred

                ll += 1

        # average predictions
        y_pred = y_pred / ll

        # save predictions
        np.save(self.output_directory + 'y_pred.npy', y_pred)

        # convert the predicted from binary to integer
        y_pred = np.argmax(y_pred, axis=1)

        duration = time.time() - start_time

        df_metrics = calculate_metrics(y_true, y_pred, duration)

        df_metrics.to_csv(self.output_directory + 'df_metrics.csv',
                          index=False)

        gc.collect()