print("Performing rejection sampling for initialization.")
# Rejection sampling for best initialization.
n_inits = 10
for init_id in range(n_inits):
    model = keras.models.Model(inputs=inputs, outputs=outputs)
    model.compile(loss='binary_crossentropy',
                  optimizer=Adam(lr),
                  metrics=["accuracy"])

    training_streamer = localmodule.multiplex_tfr(
        train_data_dir,
        n_input_hops,
        batch_size,
        mode="train",
        aug_kind_str=aug_kind_str,
        tfr_str=tfr_str,
        partial_labels=False,
        structured=False,
        single_output="coarse",
        active_streamers=active_streamers,
        streamer_rate=streamer_rate,
        num_cpus=num_cpus,
        align_perturb=align_perturb)

    history = model.fit_generator(training_streamer,
                                  steps_per_epoch=steps_per_epoch,
                                  epochs=4,
                                  verbose=False,
                                  callbacks=[history_callback],
                                  workers=0,
                                  validation_data=validation_data,
                                  use_multiprocessing=True,
dense2 = keras.layers.Dense(
    1,
    kernel_initializer="normal",
    activation="sigmoid",
    kernel_regularizer=keras.regularizers.l2(0.00002))(dense1)

# Compile model, print model summary.
model = keras.models.Model(inputs=inputs, outputs=dense2)
model.compile(loss="binary_crossentropy",
              optimizer="adam",
              metrics=["accuracy"])
model.summary()

# Build Pescador streamers corresponding to log-mel-spectrograms in augmented
# training and validation sets.
training_streamer = localmodule.multiplex_tfr(aug_kind_str, training_units,
                                              n_input_hops, batch_size)
validation_streamer = localmodule.multiplex_tfr(aug_kind_str, validation_units,
                                                n_input_hops, batch_size)

# Create directory for model, unit, and trial.
model_name = "icassp-convnet"
if not aug_kind_str == "none":
    model_name = "_".join([model_name, "aug-" + aug_kind_str])
model_dir = os.path.join(models_dir, model_name)
os.makedirs(model_dir, exist_ok=True)
unit_dir = os.path.join(model_dir, unit_str)
os.makedirs(unit_dir, exist_ok=True)
trial_dir = os.path.join(unit_dir, trial_str)
os.makedirs(trial_dir, exist_ok=True)

# Define Keras callback for checkpointing model.