def main():
    args = parse_args()
    sampling_rate = args.sampling_rate
    model_json_path = args.model_json_path
    training_path = args.training_path
    validation_path = args.validation_path

    model = build_model(sampling_rate, model_json_path)
    train_model(model, sampling_rate, training_path, validation_path)
Beispiel #2
0
def visualize_models(input_path, model_jsons):
    # Parse the input data
    data = h5py.File(input_path, 'r')['data']

    input_signal = data['signal']
    target_signal = data['target']
    sampling_rate = data.attrs['sampling_rate']
    ts = np.linspace(0, input_signal.size / sampling_rate, input_signal.size)

    # Reconstruct the models
    models = [build_model(sampling_rate, x) for x in model_jsons]

    models_out = []
    for model in models:
        models_out.append(model(input_signal))

    # Visualize raw outputs
    fig, ax = plt.subplots(1, 1)
    target_signal = normalize(target_signal)
    filtered_target = SimpleSplineFilter(sampling_rate, [], {
        'local_window_length': 60 / 200,
        'ds': 20,
        's': 45
    }).calc_feature(target_signal)
    filtered_target = SimpleLocalNorm(sampling_rate, [], {
        "local_window_length": 40
    }).calc_feature(filtered_target)
    ax.set_title("Model Output")
    for i, model in enumerate(models):
        ax.plot(ts, models_out[i], label=model.name)
    ax.plot(ts, filtered_target, label="Filtered Thermistor")
    ax.set_xlabel("Time in seconds")
    plt.legend()
    plot_max()

    fig, ax = plt.subplots(1, 1)
    bpm = thermistor.instant_bpm(target_signal,
                                 sampling_rate,
                                 interpolate=False)
    ax.plot(bpm[0], bpm[1], '+-', linewidth=0.5, label="Thermistor RR")

    for i, model in enumerate(models):
        predicted = thermistor.instant_bpm(models_out[i],
                                           sampling_rate,
                                           interpolate=False)
        ax.plot(predicted[0],
                predicted[1],
                '+-',
                linewidth=0.5,
                label=model.name)

    plt.legend()
    plt.xlabel("Time in s")
    plt.ylabel("RR in bpm")
    plt.title("Predicted vs Thermistor RR")
    plot_max()
def visualize_stft_extractor(input_path, model_json, thing):
    font = {'family' : 'normal',
            'size'   : 16}

    matplotlib.rc('font', **font)

    # Parse the input data
    data = h5py.File(input_path, 'r')['data']

    input_signal = data['signal']
    target_signal = data['target']
    sampling_rate = data.attrs['sampling_rate']
    ts = np.linspace(0, input_signal.size/sampling_rate, input_signal.size)

    # Reconstruct the model
    model = build_model(sampling_rate, model_json)
    model_out = model(input_signal)

    target_signal = normalize(target_signal)
    filtered_target = SimpleSplineFilter(sampling_rate, [], {'local_window_length':60/200,'ds':20,'s':45}).calc_feature(target_signal)
    filtered_target = SimpleLocalNorm(sampling_rate, [], {"local_window_length":40}).calc_feature(filtered_target)

    centroid = thermistor.stft_centroid_bpm(model_out, sampling_rate)
    tcentroid = thermistor.stft_centroid_bpm(target_signal, sampling_rate)

    fig, (ax, ax1) = plt.subplots(1,2)
    ax.plot(ts, centroid, label="Predicted Centroid RR")
    ax.plot(ts, tcentroid, label="Target Centroid RR")
    ax.set_xlabel("Time in s")
    ax.set_ylabel("RR in bpm")
    # ax.set_title("STFT Centroid of {} and Thermistor on {}".format(model.name, os.path.basename(input_path).split('.')[-2]))
    ax.set_title("{}: STFT Centroid of {} and Thermistor".format(thing, model.name))
    plt.legend()
    # plot_max()

    rmean_sq_error_centroid = np.sqrt(np.mean((tcentroid-centroid)**2))

    model_bpm = thermistor.instant_bpm(model_out)
    t_bpm = thermistor.instant_bpm(target_signal)

    # fig, ax = plt.subplots(1,1)
    ax1.plot(ts, model_bpm, label="Predicted Instant RR")
    ax1.plot(ts, t_bpm, label="Target Instant RR")
    ax1.set_xlabel("Time in s")
    ax1.set_ylabel("RR in bpm")
    # ax1.set_title("{}: Instant RR of {} and Thermistor".format(model.name, os.path.basename(input_path).split('.')[-2]))
    ax1.set_title("{}: Instant RR of {} and Thermistor".format(thing, model.name))
    plt.legend()
    plot_max()

    rmean_sq_error_bpm = np.sqrt(np.mean((t_bpm-model_bpm)**2))
    print("Root Mean Squared Error of Centroid: {}".format(rmean_sq_error_centroid))
    print("Root Mean Squared Error of Instant BPM: {}".format(rmean_sq_error_bpm))
def visualize_model(input_path, model_json):
    # Parse the input data
    data = h5py.File(input_path, 'r')['data']

    input_signal = data['signal']
    target_signal = data['target']
    sampling_rate = data.attrs['sampling_rate']
    ts = np.linspace(0, input_signal.size / sampling_rate, input_signal.size)

    # Reconstruct the model
    model = build_model(sampling_rate, model_json)

    target_signal = normalize(target_signal)
    filtered_target = SimpleSplineFilter(sampling_rate, [], {
        'local_window_length': 60 / 200,
        'ds': 20,
        's': 45
    }).calc_feature(target_signal)
    filtered_target = SimpleLocalNorm(sampling_rate, [], {
        "local_window_length": 40
    }).calc_feature(filtered_target)

    # Loop over all features and plot them against thermistor output and raw input
    print(model.get_names())
    print(model.get_list())
    for (name, feature) in zip(model.get_names(), model.get_list()):
        print(name)
        # visualize raw output
        feature_out = feature.calc_feature(input_signal)

        fig, ax = plt.subplots(1, 1)
        ax.set_title("Feature: {}".format(name))
        ax.plot(ts, feature_out, label=name)
        ax.plot(ts, filtered_target, label="thermistor")
        # ax.plot(ts, normalize(input_signal), label="input")
        ax.set_xlabel("time in seconds")
        plt.legend()
        plt.show()
        # plot_max()
        print(name)
Beispiel #5
0
def visualize_stft_extractor(input_path, model_json):
    # Parse the input data
    data = h5py.File(input_path, 'r')['data']

    input_signal = data['signal']
    target_signal = data['target']
    sampling_rate = data.attrs['sampling_rate']
    ts = np.linspace(0, input_signal.size / sampling_rate, input_signal.size)

    # Reconstruct the model
    model = build_model(sampling_rate, model_json)
    model_out = model(input_signal)

    filtered_target = SimpleSplineFilter(sampling_rate, [], {
        'local_window_length': 60 / 200,
        'ds': 20,
        's': 45
    }).calc_feature(target_signal)
    filtered_target = SimpleLocalNorm(sampling_rate, [], {
        "local_window_length": 40
    }).calc_feature(filtered_target)

    # Visualize STFT
    max_freq = 30 / 60
    downsample = 2

    f, t, Zxx = signal.stft(model_out[::downsample],
                            fs=sampling_rate / downsample,
                            nperseg=8000 // downsample,
                            noverlap=8000 // downsample - 10,
                            boundary=None)
    bf, bt, bZxx = signal.stft(filtered_target[::downsample],
                               fs=sampling_rate / downsample,
                               nperseg=8000 // downsample,
                               noverlap=8000 // downsample - 10,
                               boundary=None)
    max_bin = np.searchsorted(f, max_freq)
    min_bin = 2

    fig, ax = plt.subplots(2, 1)
    ax[0].pcolormesh(bt, bf[min_bin:max_bin] * 60,
                     np.log(1 + np.abs(bZxx)[min_bin:max_bin]))
    ax[1].pcolormesh(t, f[min_bin:max_bin] * 60,
                     np.log(1 + np.abs(Zxx)[min_bin:max_bin]))

    # Argmax in frequency
    bmaxbins = np.argmax(np.abs(bZxx)[min_bin:max_bin], axis=0) + min_bin
    # Histogram correction
    # ax[0].plot(bt, (bf[bmaxbins]+bf[bmaxbins+1])/2*60, 'r-')
    maxbins = np.argmax(np.abs(Zxx)[min_bin:max_bin], axis=0) + min_bin
    # ax[1].plot(t, (f[maxbins]+f[maxbins+1])/2*60, 'r-')

    ax[0].set_title("Thermistor STFT P=3 Centroid")
    ax[1].set_title("Prediction STFT P=3 Centroid")
    ax[0].set_ylabel("RR in bpm")
    ax[1].set_xlabel("Time in s")
    ax[1].set_ylabel("RR in bpm")

    def centroid(Z, f, axis=0, p=1):
        Z = np.power(Z, p)
        a = f
        if (axis == 0):
            centroid = np.dot(np.transpose(Z), a) / np.sum(Z, axis=axis)
        elif (axis == 1):
            centroid = np.dot(Z, a) / np.sum(Z, axis=axis)
        return centroid

    # Centroid in frequency
    bcentroid = centroid(np.log(1 + np.abs(bZxx))[min_bin:max_bin],
                         bf[min_bin:max_bin],
                         axis=0,
                         p=3)
    ax[0].plot(t, bcentroid * 60, 'r-')
    centroid = centroid(np.log(1 + np.abs(Zxx))[min_bin:max_bin],
                        f[min_bin:max_bin],
                        axis=0,
                        p=3)
    ax[1].plot(t, centroid * 60, 'r-')
    """
    """

    plot_max()
Beispiel #6
0
def main():

    # make analysis environment
    limit_gpu_memory()
    args = parse_arguments()
    seed_every_thing(args["seed"])
    write_out_dir = path.normpath(
        path.join(getcwd(), 'reports', args["out_dir"]))
    makedirs(write_out_dir, exist_ok=True)

    print('-' * 140)

    if args["train_mode"] == 'pre-train':

        for source in listdir('dataset/source'):

            # skip source dataset without pickle file
            data_dir_path = path.join('dataset', 'source', source)
            if not path.exists(f'{data_dir_path}/X_train.pkl'): continue

            # make output directory
            write_result_out_dir = path.join(write_out_dir, args["train_mode"],
                                             source)
            makedirs(write_result_out_dir, exist_ok=True)

            # load dataset
            X_train, y_train, X_test, y_test = \
                read_data_from_dataset(data_dir_path)
            period = (len(y_train) + len(y_test)) // 30
            X_train = np.concatenate(
                (X_train, X_test),
                axis=0)  # no need for test data when pre-training
            y_train = np.concatenate(
                (y_train, y_test),
                axis=0)  # no need for test data when pre-training
            X_train, X_valid, y_train, y_valid =  \
                train_test_split(X_train, y_train, test_size=args["valid_ratio"], shuffle=False)
            print(f'\nSource dataset : {source}')
            print(f'\nX_train : {X_train.shape[0]}')
            print(f'\nX_valid : {X_valid.shape[0]}')

            # construct the model
            file_path = path.join(write_result_out_dir, 'best_model.hdf5')
            callbacks = make_callbacks(file_path)
            input_shape = (period, X_train.shape[1])
            model = build_model(input_shape, args["gpu"], write_result_out_dir)

            # train the model
            bsize = len(y_train) // args["nb_batch"]
            RTG = ReccurentTrainingGenerator(X_train,
                                             y_train,
                                             batch_size=bsize,
                                             timesteps=period,
                                             delay=1)
            RVG = ReccurentTrainingGenerator(X_valid,
                                             y_valid,
                                             batch_size=bsize,
                                             timesteps=period,
                                             delay=1)
            H = model.fit_generator(RTG,
                                    validation_data=RVG,
                                    epochs=args["nb_epochs"],
                                    verbose=1,
                                    callbacks=callbacks)
            save_lr_curve(H, write_result_out_dir)

            # clear memory up
            keras.backend.clear_session()
            save_arguments(args, write_result_out_dir)
            print('\n' * 2 + '-' * 140 + '\n' * 2)

    elif args["train_mode"] == 'transfer-learning':

        for target in listdir('dataset/target'):

            # skip target in the absence of pickle file
            if not path.exists(f'dataset/target/{target}/X_train.pkl'):
                continue

            for source in listdir(f'{write_out_dir}/pre-train'):

                # make output directory
                write_result_out_dir = path.join(write_out_dir,
                                                 args["train_mode"], target,
                                                 source)
                pre_model_path = f'{write_out_dir}/pre-train/{source}/best_model.hdf5'
                if not path.exists(pre_model_path): continue
                makedirs(write_result_out_dir, exist_ok=True)

                # load dataset
                data_dir_path = f'dataset/target/{target}'
                X_train, y_train, X_test, y_test = \
                    read_data_from_dataset(data_dir_path)
                period = (len(y_train) + len(y_test)) // 30
                X_train, X_valid, y_train, y_valid = \
                    train_test_split(X_train, y_train, test_size=args["valid_ratio"], shuffle=False)
                print(f'\nTarget dataset : {target}')
                print(f'\nX_train : {X_train.shape[0]}')
                print(f'\nX_valid : {X_valid.shape[0]}')
                print(f'\nX_test : {X_test.shape[0]}')

                # construct the model
                pre_model = load_model(pre_model_path)
                file_path = path.join(write_result_out_dir,
                                      'transferred_best_model.hdf5')
                callbacks = make_callbacks(file_path)
                input_shape = (period, X_train.shape[1])
                model = build_model(input_shape,
                                    args["gpu"],
                                    write_result_out_dir,
                                    pre_model=pre_model,
                                    freeze=args["freeze"])

                # train the model
                bsize = len(y_train) // args["nb_batch"]
                RTG = ReccurentTrainingGenerator(X_train,
                                                 y_train,
                                                 batch_size=bsize,
                                                 timesteps=period,
                                                 delay=1)
                RVG = ReccurentTrainingGenerator(X_valid,
                                                 y_valid,
                                                 batch_size=bsize,
                                                 timesteps=period,
                                                 delay=1)
                H = model.fit_generator(RTG,
                                        validation_data=RVG,
                                        epochs=args["nb_epochs"],
                                        verbose=1,
                                        callbacks=callbacks)
                save_lr_curve(H, write_result_out_dir)

                # prediction
                best_model = load_model(file_path)
                RPG = ReccurentPredictingGenerator(X_test,
                                                   batch_size=1,
                                                   timesteps=period)
                y_test_pred = best_model.predict_generator(RPG)

                # save log for the model
                y_test = y_test[-len(y_test_pred):]
                save_prediction_plot(y_test, y_test_pred, write_result_out_dir)
                save_yy_plot(y_test, y_test_pred, write_result_out_dir)
                mse_score = save_mse(y_test,
                                     y_test_pred,
                                     write_result_out_dir,
                                     model=best_model)
                args["mse"] = mse_score
                save_arguments(args, write_result_out_dir)
                keras.backend.clear_session()
                print('\n' * 2 + '-' * 140 + '\n' * 2)

    elif args["train_mode"] == 'without-transfer-learning':

        for target in listdir('dataset/target'):

            # make output directory
            write_result_out_dir = path.join(write_out_dir, args["train_mode"],
                                             target)
            makedirs(write_result_out_dir, exist_ok=True)

            # load dataset
            data_dir_path = path.join('dataset', 'target', target)
            X_train, y_train, X_test, y_test = \
                read_data_from_dataset(data_dir_path)
            period = (len(y_train) + len(y_test)) // 30
            X_train, X_valid, y_train, y_valid =  \
                train_test_split(X_train, y_train, test_size=args["valid_ratio"], shuffle=False)
            print(f'\nTarget dataset : {target}')
            print(f'\nX_train : {X_train.shape[0]}')
            print(f'\nX_valid : {X_valid.shape[0]}')
            print(f'\nX_test : {X_test.shape[0]}')

            # construct the model
            file_path = path.join(write_result_out_dir, 'best_model.hdf5')
            callbacks = make_callbacks(file_path)
            input_shape = (period, X_train.shape[1])
            model = build_model(input_shape, args["gpu"], write_result_out_dir)

            # train the model
            bsize = len(y_train) // args["nb_batch"]
            RTG = ReccurentTrainingGenerator(X_train,
                                             y_train,
                                             batch_size=bsize,
                                             timesteps=period,
                                             delay=1)
            RVG = ReccurentTrainingGenerator(X_valid,
                                             y_valid,
                                             batch_size=bsize,
                                             timesteps=period,
                                             delay=1)
            H = model.fit_generator(RTG,
                                    validation_data=RVG,
                                    epochs=args["nb_epochs"],
                                    verbose=1,
                                    callbacks=callbacks)
            save_lr_curve(H, write_result_out_dir)

            # prediction
            best_model = load_model(file_path)
            RPG = ReccurentPredictingGenerator(X_test,
                                               batch_size=1,
                                               timesteps=period)
            y_test_pred = best_model.predict_generator(RPG)

            # save log for the model
            y_test = y_test[-len(y_test_pred):]
            save_prediction_plot(y_test, y_test_pred, write_result_out_dir)
            save_yy_plot(y_test, y_test_pred, write_result_out_dir)
            mse_score = save_mse(y_test,
                                 y_test_pred,
                                 write_result_out_dir,
                                 model=best_model)
            args["mse"] = mse_score
            save_arguments(args, write_result_out_dir)

            # clear memory up
            keras.backend.clear_session()
            print('\n' * 2 + '-' * 140 + '\n' * 2)

    elif args["train_mode"] == 'bagging':

        for target in listdir('dataset/target'):

            # make output directory
            write_result_out_dir = path.join(write_out_dir, args["train_mode"],
                                             target)
            makedirs(write_result_out_dir, exist_ok=True)

            # load dataset
            data_dir_path = path.join('dataset', 'target', target)
            X_train, y_train, X_test, y_test = \
                read_data_from_dataset(data_dir_path)
            period = (len(y_train) + len(y_test)) // 30

            # make subsets
            b_star = optimal_block_length(y_train)
            b_star_cb = math.ceil(b_star[0].b_star_cb)
            print(f'optimal block length for circular bootstrap = {b_star_cb}')
            subsets_y_train = circular_block_bootstrap(
                y_train,
                block_length=b_star_cb,
                replications=args["nb_subset"],
                replace=True)
            subsets_X_train = []
            for i in range(X_train.shape[1]):
                np.random.seed(0)
                X_cb = circular_block_bootstrap(X_train[:, i],
                                                block_length=b_star_cb,
                                                replications=args["nb_subset"],
                                                replace=True)
                subsets_X_train.append(X_cb)
            subsets_X_train = np.array(subsets_X_train)
            subsets_X_train = subsets_X_train.transpose(1, 2, 0)

            # train the model for each subset
            model_dir = path.join(write_result_out_dir, 'model')
            makedirs(model_dir, exist_ok=True)
            for i_subset, (i_X_train, i_y_train) in enumerate(
                    zip(subsets_X_train, subsets_y_train)):

                i_X_train, i_X_valid, i_y_train, i_y_valid = \
                    train_test_split(i_X_train, i_y_train, test_size=args["valid_ratio"], shuffle=False)

                # construct the model
                file_path = path.join(model_dir, f'best_model_{i_subset}.hdf5')
                callbacks = make_callbacks(file_path, save_csv=False)
                input_shape = (period, i_X_train.shape[1]
                               )  # x_train.shape[2] is number of variable
                model = build_model(input_shape,
                                    args["gpu"],
                                    write_result_out_dir,
                                    savefig=False)

                # train the model
                bsize = len(i_y_train) // args["nb_batch"]
                RTG = ReccurentTrainingGenerator(i_X_train,
                                                 i_y_train,
                                                 batch_size=bsize,
                                                 timesteps=period,
                                                 delay=1)
                RVG = ReccurentTrainingGenerator(i_X_valid,
                                                 i_y_valid,
                                                 batch_size=bsize,
                                                 timesteps=period,
                                                 delay=1)
                H = model.fit_generator(RTG,
                                        validation_data=RVG,
                                        epochs=args["nb_epochs"],
                                        verbose=1,
                                        callbacks=callbacks)

            keras.backend.clear_session()
            print('\n' * 2 + '-' * 140 + '\n' * 2)

    elif args["train_mode"] == 'noise-injection':

        for target in listdir('dataset/target'):

            # make output directory
            write_result_out_dir = path.join(write_out_dir, args["train_mode"],
                                             target)
            makedirs(write_result_out_dir, exist_ok=True)

            # load dataset
            data_dir_path = path.join('dataset', 'target', target)
            X_train, y_train, X_test, y_test = \
                read_data_from_dataset(data_dir_path)
            period = (len(y_train) + len(y_test)) // 30
            X_train, X_valid, y_train, y_valid =  \
                train_test_split(X_train, y_train, test_size=args["valid_ratio"], shuffle=False)
            print(f'\nTarget dataset : {target}')
            print(f'\nX_train : {X_train.shape}')
            print(f'\nX_valid : {X_valid.shape}')
            print(f'\nX_test : {X_test.shape[0]}')

            # construct the model
            file_path = path.join(write_result_out_dir, 'best_model.hdf5')
            callbacks = make_callbacks(file_path)
            input_shape = (period, X_train.shape[1])
            model = build_model(input_shape,
                                args["gpu"],
                                write_result_out_dir,
                                noise=args["noise_var"])

            # train the model
            bsize = len(y_train) // args["nb_batch"]
            RTG = ReccurentTrainingGenerator(X_train,
                                             y_train,
                                             batch_size=bsize,
                                             timesteps=period,
                                             delay=1)
            RVG = ReccurentTrainingGenerator(X_valid,
                                             y_valid,
                                             batch_size=bsize,
                                             timesteps=period,
                                             delay=1)
            H = model.fit_generator(RTG,
                                    validation_data=RVG,
                                    epochs=args["nb_epochs"],
                                    verbose=1,
                                    callbacks=callbacks)
            save_lr_curve(H, write_result_out_dir)

            # prediction
            best_model = load_model(file_path)
            RPG = ReccurentPredictingGenerator(X_test,
                                               batch_size=1,
                                               timesteps=period)
            y_test_pred = best_model.predict_generator(RPG)

            # save log for the model
            y_test = y_test[-len(y_test_pred):]
            save_prediction_plot(y_test, y_test_pred, write_result_out_dir)
            save_yy_plot(y_test, y_test_pred, write_result_out_dir)
            mse_score = save_mse(y_test,
                                 y_test_pred,
                                 write_result_out_dir,
                                 model=best_model)
            args["mse"] = mse_score
            save_arguments(args, write_result_out_dir)

            # clear memory up
            keras.backend.clear_session()
            print('\n' * 2 + '-' * 140 + '\n' * 2)
Beispiel #7
0
def visualize_model(input_path, model_json):
    # Parse the input data
    data = h5py.File(input_path, 'r')['data']

    input_signal = data['signal']
    target_signal = data['target']
    sampling_rate = data.attrs['sampling_rate']
    ts = np.linspace(0, input_signal.size / sampling_rate, input_signal.size)

    # Reconstruct the model
    model = build_model(sampling_rate, model_json)

    # Visualize raw output
    model_out = model(input_signal)
    fig, ax = plt.subplots(2, 1)
    ax[0].set_title("Model Output")
    ax[0].plot(ts, model_out, label="Raw Input Signal")

    ax[1].set_title("Raw Target Signal")
    ax[1].plot(ts, target_signal, label="Raw Target Signal")

    ax[0].set_xlabel("Time in seconds")
    ax[1].set_xlabel("Time in seconds")
    plot_max()

    fig, ax = plt.subplots(1, 1)
    target_signal = normalize(target_signal)
    filtered_target = SimpleSplineFilter(sampling_rate, [], {
        'local_window_length': 60 / 200,
        'ds': 20,
        's': 45
    }).calc_feature(target_signal)
    filtered_target = SimpleLocalNorm(sampling_rate, [], {
        "local_window_length": 40
    }).calc_feature(filtered_target)
    ax.set_title("Model Output")
    ax.plot(ts, model_out, label="Raw Model Output")
    ax.plot(ts, filtered_target, label="Filtered Thermistor")
    ax.set_xlabel("Time in seconds")
    plt.legend()
    plot_max()

    # Visualize STFT
    fig, ax = plt.subplots(2, 1)
    max_freq = 30 / 60
    downsample = 2
    f, t, Zxx = signal.stft(model_out[::downsample],
                            fs=sampling_rate / downsample,
                            nperseg=12000 // downsample,
                            noverlap=12000 // downsample - 10,
                            boundary=None)
    bf, bt, bZxx = signal.stft(filtered_target[::downsample],
                               fs=sampling_rate / downsample,
                               nperseg=12000 // downsample,
                               noverlap=12000 // downsample - 10,
                               boundary=None)
    max_bin = np.searchsorted(f, max_freq)
    ax[0].pcolormesh(bt, bf[2:max_bin] * 60,
                     np.log(1 + np.abs(bZxx)[2:max_bin]))
    ax[0].set_title("Thermistor")
    ax[1].set_xlabel("Time in s")
    ax[1].set_ylabel("RR in bpm")
    ax[1].pcolormesh(t, f[2:max_bin] * 60, np.log(1 + np.abs(Zxx)[2:max_bin]))
    ax[1].set_title("Prediction")
    # ax2.set_ylim(f[2]*60, f[max_bin]*60) #f[max_bin]*60)
    plot_max()

    fig, ax = plt.subplots(1, 1)
    bpm = thermistor.instant_bpm(target_signal,
                                 sampling_rate,
                                 interpolate=False)
    predicted = thermistor.instant_bpm(model_out,
                                       sampling_rate,
                                       interpolate=False)
    ax.plot(bpm[0], bpm[1], label="Thermistor RR")
    ax.plot(predicted[0], predicted[1], label="Predicted RR")
    plt.legend()
    plt.xlabel("Time in s")
    plt.ylabel("RR in bpm")
    plt.title("Predicted vs Thermistor RR")
    plot_max()

    fig = plt.figure()
    ax = plt.axes(projection='3d')
    ax.contour3D(f, t, Z, 50, cmap='binary')
    ax.set_xlabel('x')
    ax.set_ylabel('y')
    ax.set_zlabel('z')
test_word_sequences = pad_sequences(test_word_sequences, maxlen=max_length, padding='post')
print(train_word_sequences[:1])
print(test_word_sequences[:1])
pred_prob = np.zeros((len(test_word_sequences),58), dtype=np.float32)

start_time = time.time()
print("Loading embedding matrix ...")
embedding_matrix_glove, nb_words = load_glove(word_dict, lemma_dict,glove_path)
embedding_matrix_fasttext, nb_words = load_fasttext(word_dict, lemma_dict,fasttext_path)
embedding_matrix = np.concatenate((embedding_matrix_glove, embedding_matrix_fasttext), axis=1)
print("--- %s seconds ---" % (time.time() - start_time))

# Trainning time ----------------------------------------------------------------------------
start_time = time.time()
print("Start training ...")
model = build_model(embedding_matrix, nb_words, embedding_size)
all_preds = []
model.fit(train_word_sequences, y, batch_size=batch_size, epochs=num_epoch-1, verbose=2)

pred_prob += 0.15*np.squeeze(model.predict(test_word_sequences, batch_size=batch_size, verbose=2))
model.fit(train_word_sequences, y, batch_size=batch_size, epochs=1, verbose=2)
pred_prob += 0.35*np.squeeze(model.predict(test_word_sequences, batch_size=batch_size, verbose=2))

del model, embedding_matrix_fasttext, embedding_matrix
gc.collect()
K.clear_session()
print("--- %s seconds ---" % (time.time() - start_time))

#Training another model ---------------------------------------------------------------------
start_time = time.time()
print("Loading embedding matrix ...")
    embedding_vector = embedding_index.get(word)
    if embedding_vector is not None: embedding_matrix[i] = embedding_vector


from keras.optimizers import Adam, RMSprop
from keras.callbacks import EarlyStopping, ModelCheckpoint, LearningRateScheduler
from keras.layers import GRU, BatchNormalization, Conv1D, MaxPooling1D

file_path = "best_model.hdf5"
check_point = ModelCheckpoint(file_path, monitor = "val_loss", verbose = 1,
                              save_best_only = True, mode = "min")

early_stop = EarlyStopping(monitor = "val_loss", mode = "min", patience = 5)


model = build_model(lr = 1e-3, lr_d = 0, units = 112, dr = 0.2)
pred = model.predict(test, batch_size = 1024, verbose = 1)
oof_pred = model.predict(X_train,batch_size = 1024, verbose = 1)

#Saving oof_pred
np.save('oof_Bi-GRU-LSTM-CNN-Poolings-Fasttext.np',oof_pred)
print('finish saving oof numpy array')
submission = pd.read_csv("../input/ndsc-beginner/data_info_val_sample_submission.csv")

np.save('prediction_array.np',pred)
print('finish saving test numpy array')
y_te = [np.argmax(preds) for preds in pred]
submission['Category'] = y_te
submission.to_csv("submission.csv", index = False)
print("[{}] Completed!".format(time.time() - start_time))