def experiment_1(encoder): print("...loading&preprocessing dataset") #train_x, train_y = load_data("data/enRep_USE_None.csv", 40000) train_x, train_y, test_x, test_y, _, _ = utils.load_dataset( 'data/train.csv', 'data/trial.csv', 'data/trial.labels', 'data/test.csv') train_x, test_x, _, max_string_length = preprocessing.preprocessing_pipeline( train_x, test_x, _) print("...creating encoder instance") encoder_instance = encoder(train_x) print("...encoding sentences") embeddings = numpy.zeros((train_x.shape[0], encoder_instance.getSize())) for i in range(0, train_x.shape[0], 1000): print('\r......{0}/{1}'.format(i, train_x.shape[0]), end="") embeddings[i:i + 1000] = encoder_instance.use(train_x[i:i + 1000]) for i in embeddings[0:10]: print(i) print("...preparing labels") train_y_l = numpy.zeros((train_y.shape[0], 6)) for i in range(train_y.shape[0]): if train_y[i] == 'anger': train_y_l[i] = numpy.asarray([1, 0, 0, 0, 0, 0]) elif train_y[i] == 'sad': train_y_l[i] = numpy.asarray([0, 1, 0, 0, 0, 0]) elif train_y[i] == 'joy': train_y_l[i] = numpy.asarray([0, 0, 1, 0, 0, 0]) elif train_y[i] == 'disgust': train_y_l[i] = numpy.asarray([0, 0, 0, 1, 0, 0]) elif train_y[i] == 'fear': train_y_l[i] = numpy.asarray([0, 0, 0, 0, 1, 0]) elif train_y[i] == 'surprise': train_y_l[i] = numpy.asarray([0, 0, 0, 0, 0, 1]) model = model_utils.get_SM_model_2(embeddings[0].shape, 6) print("...Preparations complete:") print("......Embeddings shape (data size, embedding size): {}".format( embeddings.shape)) print("......Labels shape (data size, classes): {}".format(train_y.shape)) model.summary() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=[ 'accuracy', evaluation.f1, evaluation.precision, evaluation.recall ]) callbacks = model_utils.get_callbacks( early_stop_monitor=config.early_stop_monitor, early_stop_patience=config.early_stop_patience, early_stop_mode=config.early_stop_mode) model_info = model.fit(embeddings, train_y_l, epochs=200, batch_size=config.batch_size, validation_split=0.1, shuffle=True, verbose=2)
def main(): midi_dir = '../../../data/query_symlinks' model_num = 12 model_dir = '../../../models/keras/01_vanilla_rnn/{}'.format(model_num) window_size = 20 # only creates if doesn't already exist model_utils.create_model_dir(model_dir) files = [os.path.join(midi_dir, path) for path in os.listdir(midi_dir)] train_generator = data_utils.get_data_generator(files[0:1000], form=FORMAT, window_size=20) val_generator = data_utils.get_data_generator(files[1000:1100], form=FORMAT, window_size=20) model, epoch = get_model(window_size=window_size) print(model.summary()) model_utils.save_model(model, model_dir) callbacks = model_utils.get_callbacks(model_dir) print('fitting model...') model.fit_generator(train_generator, steps_per_epoch=10000, epochs=10, validation_data=val_generator, validation_steps=2000, verbose=1, callbacks=callbacks, initial_epoch=epoch) # generate 10 tracks using random seeds X, y = val_generator.next() generated = model_utils.generate(model, X) for i, midi in enumerate(generated): file = os.path.join(model_dir, 'generated', '{}.mid'.format(i + 1)) midi.write(file.format(i + 1)) print('wrote midi file to {}'.format(file))
def experiment_2(): # w/o candidates test_x_s, test_y, test_label = prepare_testdata() print("...initializing model") model = model_utils.get_SM_model_2((4096, ), embedding_layer=None, units=config.units) model.summary() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) callbacks = model_utils.get_callbacks( early_stop_monitor=config.early_stop_monitor, early_stop_patience=config.early_stop_patience, early_stop_mode=config.early_stop_mode) model_info = model.fit_generator( generators.MySMGenerator("data/enRep_InferSent_None_original.csv", batch=32, embedding_size=4096), validation_data=generators.MySMValidationGenerator( "data/enRep_InferSent_None_original.csv", batch=32, embedding_size=4096), epochs=40, callbacks=callbacks, verbose=2) print('...Evaluation') loss, acc = model.evaluate(test_x_s, test_y, verbose=2) print("...Loss = ", loss) print("...Test accuracy = ", acc) probabilities = model.predict(test_x_s) predictions = utils.indices_to_labels(probabilities.argmax(axis=-1), config.index_to_label) evaluation.calculate_prf(test_label.tolist(), predictions)
embedding_layer=embeddings_layer, units=config.units, dtype=tf.string) else: model = model_utils.get_model(shape, embeddings_layer, config.classes, units=config.units, dtype=inp_type) model.summary() model.compile(loss='categorical_crossentropy', optimizer='adam', metrics=['accuracy']) callbacks = model_utils.get_callbacks( early_stop_monitor=config.early_stop_monitor, early_stop_patience=config.early_stop_patience, early_stop_mode=config.early_stop_mode) weights = model_utils.get_sample_weights_prim(train_y, config.class_weight) # model_info = model.fit(train_x_indices, train_y_oh, epochs=config.epochs, batch_size=config.batch_size, validation_split=0.05, callbacks=callbacks, shuffle=True, verbose=config.verbose) model_info = model.fit(train_x_indices, train_y_oh, epochs=config.epochs, batch_size=config.batch_size, validation_data=(trial_x_indices, trial_y_oh), callbacks=callbacks, shuffle=True, verbose=config.verbose, sample_weight=weights)
def main(): midi_dir = '../../../data/query_symlinks' model_num = 3 model_dir = '../../../models/keras/02_event_rnn/{}'.format(model_num) rythm_test = False window_size = 20 # only creates if doesn't already exist model_utils.create_model_dir(model_dir) files = [os.path.join(midi_dir, path) for path in os.listdir(midi_dir)] train_files = files[0:1000] val_files = files[1000:1250] # get the train/val d train_generator = data_utils.get_data_generator(train_files, form=data_utils.F_EVENT_WINDOW_TIMES, window_size=20) val_generator = data_utils.get_data_generator(val_files, form=data_utils.F_EVENT_WINDOW_TIMES, window_size=20) # train_generator = scale_data(train_generator) # val_generator = scale_data(val_generator) X_timing_seed, _ = val_generator.next() # pdb.set_trace() timing_model, epoch = get_timing_model(model_dir, window_size=window_size, model_index=1) note_model, _ = get_note_model(model_dir, window_size=window_size, model_index=0) model_utils.save_model(note_model, model_dir, model_index=0) model_utils.save_model(timing_model, model_dir, model_index=1) callbacks = model_utils.get_callbacks(model_dir, checkpoint_monitor='val_mae', model_index=1) # print('fitting timing model...') # timing_model.fit_generator(train_generator, # steps_per_epoch=data_utils.WINDOWS_PER_FILE * len(train_files), # epochs=30, # validation_data=val_generator, # validation_steps=data_utils.WINDOWS_PER_FILE * len(val_files), # verbose=1, # callbacks=callbacks, # initial_epoch=epoch) train_generator = data_utils.get_data_generator(train_files, form=data_utils.F_EVENT_WINDOW_NOTES, window_size=20) callbacks = model_utils.get_callbacks(model_dir, checkpoint_monitor='val_mae', model_index=0) val_generator = data_utils.get_data_generator(val_files, form=data_utils.F_EVENT_WINDOW_NOTES, window_size=20) # print('fitting note model...') # note_model.fit_generator(train_generator, # steps_per_epoch=data_utils.WINDOWS_PER_FILE * 10, # epochs=10, # validation_data=val_generator, # validation_steps=data_utils.WINDOWS_PER_FILE * 2, # verbose=1, # callbacks=callbacks, # initial_epoch=epoch) # generate 10 tracks using random seeds X_note_seed, _ = val_generator.next() print('generating notes...') generated_notes = model_utils.generate_notes(note_model, X_note_seed) print('generating timings...') # replace start/end note events with values generated from timing models generated_timings = model_utils.generate_timings(timing_model, X_timing_seed) for i, midi in enumerate(generated_notes): for instrument in midi.instruments: wall_time = 0 if rythm_test: instrument.program = 125 for j, note in enumerate(instrument.notes): if rythm_test: note.pitch = 60 offset = generated_timings[i][j][0] duration = generated_timings[i][j][1] note.start = wall_time + offset note.end = wall_time + offset + duration wall_time = wall_time + offset + duration for i, midi in enumerate(generated_notes): file = os.path.join(model_dir, 'generated', '{}.mid'.format(i + 1)) midi.write(file.format(i + 1)) print('wrote midi file to {}'.format(file))
val_ds = DictImageDataGenerator(preprocessing_function=preprocess_func) val_gen = val_ds.flow_from_dict(val_d, target_size=(IMG_SIZE, IMG_SIZE), batch_size=BATCH_SIZE) val_steps = val_gen.samples // BATCH_SIZE print(val_steps) # test for x, y in train_gen: print(x.shape, y.shape) break print(train_gen.class_indices) mob_model = get_xception_model(IMG_SIZE, LABEL_CNT, dense_dim=None) # mob_model.summary() cb_list = get_callbacks( '../data/checkpoints/detect_if_new_whale_bbox_xception.h5', mob_model) adam_opt = keras.optimizers.Adam(lr=0.00001) mob_model.compile(optimizer=adam_opt, loss='categorical_crossentropy', metrics=['acc']) print('compile done') # train mob_model.fit_generator(train_gen, steps_per_epoch=1000, epochs=100, verbose=1, callbacks=cb_list, validation_data=val_gen, validation_steps=val_steps)
def main(): midi_dir = '../../../data/t-sne_selections/1/0/' model_num = 7 # same as 3 model_dir = '../../../models/keras/' \ '03_vanilla_rnn_intervals/{}'.format(model_num) window_size = 20 # only creates if doesn't already exist model_utils.create_model_dir(model_dir) files = [os.path.join(midi_dir, path) for path in os.listdir(midi_dir)] val_split = 0.2 print(int(float(len(files)) * val_split)) train_generator = data_utils.get_data_generator( files[0:int(float(len(files)) * val_split)], form=FORMAT, window_size=20) val_generator = data_utils.get_data_generator( files[int(float(len(files)) * val_split):], form=FORMAT, window_size=20) # intervals = [] # for i in range(1000): # data = train_generator.next() # for window in data[0]: # for interval in window: # if interval not in intervals: # intervals.append(interval) # pdb.set_trace() # for i in range(1000): # data = train_generator.next() # pdb.set_trace() model, epoch = get_model(window_size=window_size) print(model.summary()) model_utils.save_model(model, model_dir) callbacks = model_utils.get_callbacks(model_dir) # print('fitting model...') model.fit_generator(train_generator, steps_per_epoch=1000, epochs=100, validation_data=val_generator, validation_steps=200, verbose=1, callbacks=callbacks, initial_epoch=epoch) # generate 10 tracks using random seeds X, y = val_generator.next() generated = model_utils.generate_notes_from_intervals(model, X, length=200) for i, midi in enumerate(generated): file = os.path.join(model_dir, 'generated', '{}.mid'.format(i + 1)) midi.write(file.format(i + 1)) print('wrote midi file to {}'.format(file))
print(x1.shape, x2.shape, y.shape) print(y) break # In[ ]: all_model, feat_model, compare_model = create_simaese_model( img_shape=IMAGE_SHAPE, mid_feat_dim=256, mob_alpha=0.75) print(all_model.input_shape, all_model.output_shape) all_model.summary() # In[ ]: # get generator and train cb_list = get_callbacks('../data/checkpoints/mob_299_sim.h5', all_model) adam_opt = keras.optimizers.Adam(lr=0.00005) all_model.compile(optimizer=adam_opt, loss='binary_crossentropy', metrics=['acc']) print('compile done') # In[ ]: # train all_model.fit_generator(train_gen, steps_per_epoch=1000, epochs=100, verbose=2, callbacks=cb_list, validation_data=val_gen,
# In[ ]: all_model, feat_model, compare_model = create_simaese_model( img_shape=IMAGE_SHAPE, mid_feat_dim=512, mob_alpha=0.75, head_model_name='mobilenet_hbp', l2_flag=True) print(all_model.input_shape, all_model.output_shape) all_model.summary() # In[ ]: # get generator and train cb_list = get_callbacks('../data/checkpoints/mob_384_sim_hbp.h5', all_model) adam_opt = keras.optimizers.Adam(lr=0.0003) all_model.compile(optimizer=adam_opt, loss='binary_crossentropy', metrics=['acc']) print('compile done') # In[ ]: # train all_model.fit_generator(train_gen, steps_per_epoch=1000, epochs=100, verbose=2, callbacks=cb_list, validation_data=val_gen,