def main(cube_dir, hyperband_iterations, max_epochs): device_name = tf.test.gpu_device_name() if device_name != '/device:GPU:0': raise SystemError('GPU device not found') logging.info('Found GPU at: {}'.format(device_name)) x, y, val_x, val_y = deep_emulator.data(cube_dir, rescale=True) tuner = Hyperband( build_model, objective='val_mean_absolute_error', hyperband_iterations=hyperband_iterations, max_epochs=max_epochs, directory='results/hyperband', project_name='agnfinder_5layer_dropout' ) early_stopping = keras.callbacks.EarlyStopping(restore_best_weights=True) tuner.search( x, y, callbacks=[early_stopping], validation_data=(val_x, val_y), batch_size=1024 ) tuner.results_summary() models = tuner.get_best_models(num_models=5) for n, model in enumerate(models): logging.info(f'Model {n}') logging.info(model.summary())
def TuneNetwork(): #Function to tune hyperparameters for the neural network tuner = Hyperband(CreateNNmodel, max_epochs=50, objective='val_accuracy', executions_per_trial=7, directory=os.path.normpath('C:/')) tuner.search(x=TrainingInput, y=TrainingOutput, validation_data=(TestingInput, TestingOutput), epochs=50, batch_size=Features) best_model = tuner.get_best_models(num_models=1)[0] loss, accuracy = best_model.evaluate(TestingInput, TestingOutput) print(accuracy)
def hypermodel_exec(x_train, x_test, y_train, y_test): SEED = 17 MAX_TRIALS = 40 EXECUTION_PER_TRIAL = 3 HYPERBAND_MAX_EPOCHS = 20 NUM_CLASSES = 1 # One sigmoid neuron for binary classification INPUT_SHAPE = (32, 32, 3 ) # Depends on embedding type and bp lenght of dataset N_EPOCH_SEARCH = 40 np.random.seed(SEED) hypermodel = HotCNNHyperModel(input_shape=INPUT_SHAPE, num_classes=NUM_CLASSES) # tuner = RandomSearch( # hypermodel, # objective='val_loss', # seed=SEED, # max_trials=MAX_TRIALS, # executions_per_trial=EXECUTION_PER_TRIAL, # directory='random_search', # project_name='hot_cnn_promoter_01' # ) tuner = Hyperband(hypermodel, max_epochs=HYPERBAND_MAX_EPOCHS, objective='val_accuracy', seed=SEED, executions_per_trial=EXECUTION_PER_TRIAL, directory='hyperband', project_name='hot_cnn_promoter_01') tuner.search_space_summary() tuner.search(x_train, y_train, epochs=N_EPOCH_SEARCH, validation_split=0.1) # Show a summary of the search tuner.results_summary() # Retrieve the best model. best_model = tuner.get_best_models(num_models=1)[0] # Evaluate the best model. loss, accuracy = best_model.evaluate(x_test, y_test)
def __train_neural_networks(self, estimator): if estimator.get_metric() == "mse": tuner = Hyperband(estimator, max_epochs=20, objective="val_mse", executions_per_trial=1, directory="regression_nn" + str(random.randint(0, 1000))) else: tuner = Hyperband(estimator, max_epochs=20, objective="val_accuracy", executions_per_trial=1, directory="classification_nn" + str(random.randint(0, 1000))) tuner.search(self.X_train, self.y_train, epochs=1, validation_split=0.1, verbose=0) return tuner.get_best_models(num_models=1)[0]
def tuneHyperband(X, y, max_trials=3): """ Perform Hyperband Tuning to search for the best model and Hyperparameters Arguments: X: Input dataset y: Label or output dataset max_trials: Trials required to perform tuning """ hypermodel = HyperResNet(input_shape=(128, 128, 3), num_classes=10) tuner = Hyperband( hypermodel, max_epochs=max_trials, objective='val_accuracy', seed=42, ) X_train, X_test, y_train, y_test = train_test_split( X, y, test_size=0.2, random_state=49) # searches the tuner space defined by hyperparameters (hp) and returns the # best model tuner.search(X_train, y_train, epochs=5, validation_data=(X_test, y_test)) hyp = tuner.get_best_hyperparameters(num_trials=1)[0] #hyp = tuner.oracle.get_best_trials(num_trials=1)[0].hyperparameters.values #best_hps = np.stack(hyp).astype(None) history = tuner_hist(X, y, tuner, hyp) """ Return: models[0] : best model obtained after tuning best_hps : best Hyperprameters obtained after tuning, stored as array history : history of the data executed from the given model """ return tuner.get_best_models(1)[0], hyp, history
def main(): metadata = load_metadata() # train_metadata2, test_metadata2 = load_metadata2() ''' Fraction of positive samples wanted in both test and validation set, the same as it is in the test set of the Kaggle Chest X-Ray Images (Pneumonia) dataset''' train_metadata, val_metadata, test_metadata = split_dataset(metadata, augmentation + 1) # train_metadata = train_metadata.head(8) # val_metadata = val_metadata.head(256) if augmentation > 0: pos_train_metadata = train_metadata[train_metadata['Pneumonia'] == 1] neg_train_metadata = train_metadata[train_metadata['Pneumonia'] == 0] pos_train_metadata_augmented = augment(metadata=pos_train_metadata, rate=augmentation, batch_size=16, seed=seed) train_metadata = pd.concat([pos_train_metadata, pos_train_metadata_augmented, neg_train_metadata], ignore_index=True) # train_metadata = pd.concat(([train_metadata, train_metadata2]), ignore_index=True) train_metadata = shuffle_DataFrame(train_metadata, seed) """val_metadata2, test_metadata2 = split_dataset2(test_metadata2) val_metadata = pd.concat(([val_metadata, val_metadata2]), ignore_index=True) test_metadata = pd.concat(([test_metadata, test_metadata2]), ignore_index=True)""" print('Train:') print(Counter(train_metadata['Pneumonia'])) print('Validation:') print(Counter(val_metadata['Pneumonia'])) print('Test:') print(Counter(test_metadata['Pneumonia'])) train_ds = make_pipeline(file_paths=train_metadata['File Path'].to_numpy(), y=train_metadata['Pneumonia'].to_numpy(), shuffle=True, batch_size=batch_size, seed=seed) val_ds = make_pipeline(file_paths=val_metadata['File Path'].to_numpy(), y=val_metadata['Pneumonia'].to_numpy(), shuffle=False, batch_size=val_batch_size) samples_ds = make_pipeline(file_paths=train_metadata['File Path'].to_numpy(), y=train_metadata['Pneumonia'].to_numpy(), shuffle=True, batch_size=16, seed=seed) show_samples(samples_ds) del samples_ds # Not needed anymore """str_time = datetime.datetime.now().strftime("%m%d-%H%M%S") log_dir = f'{logs_root}/{str_time}' logs_cb = tf.keras.callbacks.TensorBoard(log_dir=log_dir, histogram_freq=1, profile_batch=0) history = model.fit(x=train_ds, validation_data=val_ds, epochs=300, shuffle=False, callbacks=[logs_cb])""" # TODO what if I use val_loss instead? tuner = Hyperband(make_model_DenseNet121, objective=kt.Objective("val_auc", direction="max"), # Careful to keep the direction updated max_epochs=epochs_base, hyperband_iterations=2, directory='computations', project_name='base-1dataset-densenet121-auc-auc') early_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor='val_auc', patience=patience) tuner.search(x=train_ds, validation_data=val_ds, epochs=epochs_base, shuffle=False, callbacks=[early_stopping_cb]) model_maker = ModelMaker(tuner) tuner_ft = Hyperband(model_maker.make_model_DenseNet121, objective=kt.Objective("val_auc", direction="max"), # Careful to keep the direction updated max_epochs=epochs_ft, hyperband_iterations=2, directory='computations', project_name='fine-1dataset-densenet121-auc-auc') early_stopping_cb = tf.keras.callbacks.EarlyStopping(monitor='val_auc', patience=patience) tuner_ft.search(x=train_ds, validation_data=val_ds, epochs=epochs_ft, shuffle=False, callbacks=[early_stopping_cb]) best_ft_model = tuner_ft.get_best_models()[0] test_ds = make_pipeline(file_paths=test_metadata['File Path'].to_numpy(), y=test_metadata['Pneumonia'].to_numpy(), shuffle=False, batch_size=val_batch_size) test_results = best_ft_model.evaluate(x=test_ds, return_dict=True, verbose=1) print('\nHyper-parameters for the fine-tuned model:') print(tuner_ft.get_best_hyperparameters()[0].values) print('\nTest results on the fine-tuned model:') print(test_results) hps = tuner_ft.get_best_hyperparameters()[0].values dev_metadata = pd.concat([train_metadata, val_metadata], ignore_index=True) dev_metadata = shuffle_DataFrame(dev_metadata, seed=seed) dev_metadata.reset_index(inplace=True, drop=True) fold_size = int(np.ceil(len(dev_metadata) / n_folds)) train_datasets, val_datasets = [], [] for fold in range(n_folds): fold_val_metadata = dev_metadata.iloc[fold * fold_size:fold * fold_size + fold_size, :] val_datasets.append(make_pipeline(file_paths=fold_val_metadata['File Path'].to_numpy(), y=fold_val_metadata['Pneumonia'].to_numpy(), batch_size=val_batch_size, shuffle=False)) fold_train_metadata1 = dev_metadata.iloc[:fold * fold_size, :] fold_train_metadata2 = dev_metadata.iloc[fold * fold_size + fold_size:, :] fold_train_metadata = pd.concat([fold_train_metadata1, fold_train_metadata2], ignore_index=False) train_datasets.append(make_pipeline(file_paths=fold_train_metadata['File Path'].to_numpy(), y=fold_train_metadata['Pneumonia'].to_numpy(), batch_size=batch_size, shuffle=True, seed=seed)) histories, means = k_fold_resumable_fit(model_maker=model_maker.make_model_DenseNet121, hp=hps, comp_dir='computations', project='xval', train_datasets=train_datasets, val_datasets=val_datasets, log_dir='computations/xval/logs', epochs=epochs_ft) print(histories) print(means) pass
hyperband_iterations=2, directory='HyperBandTrials', project_name='PressureOpti_hl') # Defining the Early Stopping Function early_stopping_callback = EarlyStopping(monitor='val_mape', patience=500, min_delta=1e-4, restore_best_weights=True, mode='auto', verbose=True) tuner.search_space_summary() print(model.summary()) tuner.search(X_train, y_train, epochs=2000, validation_data=(X_test, y_test), callbacks=[early_stopping_callback]) models = tuner.get_best_models(num_models=2) models[0].save( '/mnt/IMP/Work/Thesis/NeuralNetwork/DeepLearning-RarefiedFlows/SavedModels/Pressure/HyperBand_hl/Best_Model_1' ) models[1].save( '/mnt/IMP/Work/Thesis/NeuralNetwork/DeepLearning-RarefiedFlows/SavedModels/Pressure/HyperBand_hl/Best_Model_2' ) tuner.results_summary()
max_epochs=MAX_EPOCHS, factor=FACTOR, directory='my_dir', project_name='val_' + obj + '_' + time.ctime()) tuner.search(X_res, pd.get_dummies(y_res).values, epochs=MAX_EPOCHS, batch_size=2048, verbose=0, callbacks=[early_stops(obj)], validation_data=(X_val, pd.get_dummies(y_val).values)) end = time.time() print('Tuning time is %.2f' % (end - start)) print(tuner.oracle.get_best_trials(num_trials=1)[0].hyperparameters.values) bp = pd.Series( tuner.oracle.get_best_trials(num_trials=1)[0].hyperparameters.values, name=obj) #bp = bp.append(pd.Series(end-start,index=['Tuning_time'])) bps = pd.concat((bps, bp), axis=1) models = tuner.get_best_models(num_models=FACTOR) for i in range(FACTOR): Y_pred = models[i].predict(X_test) rsts = rsts.append( get_accs(y_test.values, Y_pred, end - start, 'MLP-' + obj + '-' + str(i + 1))) rsts.to_csv('VCA_Tune2.csv') print(bps) print(rsts.iloc[:, 5:]) bps.to_csv('VCA_Tune2_bps.csv')
def run_model_fitting(PROJECT_NAME): """ """ SENTENCE_DECODER = pd.read_pickle( f'results\\objects\\{PROJECT_NAME}\\sentence_decoder.pkl') TARGET_DECODER = pd.read_pickle( f'results\\objects\\{PROJECT_NAME}\\target_decoder.pkl') TUNING_FRACTION = 0.1 BTCH_LIST = os.listdir(f'data\\preprocessed\\{PROJECT_NAME}\\inputs') BTCH = [i for i in range(len(BTCH_LIST))] BTCH = np.random.choice(BTCH, int(len(BTCH) * TUNING_FRACTION), replace=False) TR_BTCH = BTCH[:int(len(BTCH) * 0.8)] TS_BTCH = BTCH[int(len(BTCH) * 0.8):] tr_generator = DataGenerator(list_batches=TR_BTCH, project_name=PROJECT_NAME, shuffle=True, multi_target=True) ts_generator = DataGenerator(list_batches=TS_BTCH, project_name=PROJECT_NAME, shuffle=True, multi_target=True) model = LanguageModel(max_vocab=len(SENTENCE_DECODER) + 1, multi_target=True, max_target_1=len(SENTENCE_DECODER) + 1, max_target_2=len(TARGET_DECODER)) ES = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5, verbose=1, mode='auto', restore_best_weights=True) tuner_obj = Hyperband(hypermodel=model, max_epochs=30, hyperband_iterations=1, objective='val_loss', directory='o', project_name=f'{PROJECT_NAME}') tuner_obj.search(tr_generator, epochs=30, callbacks=[ES], verbose=2, validation_data=ts_generator) BTCH = [i for i in range(len(BTCH_LIST))] BTCH = np.random.choice(BTCH, int(len(BTCH)), replace=False) TR_BTCH = BTCH[:int(len(BTCH) * 0.8)] TS_BTCH = BTCH[int(len(BTCH) * 0.8):] tr_generator = DataGenerator(list_batches=TR_BTCH, project_name=PROJECT_NAME, shuffle=True, multi_target=True) ts_generator = DataGenerator(list_batches=TS_BTCH, project_name=PROJECT_NAME, shuffle=True, multi_target=True) model = tuner_obj.get_best_models(1)[0] ES = EarlyStopping(monitor='val_loss', min_delta=0.0001, patience=5, verbose=1, mode='auto', restore_best_weights=True) model.fit(tr_generator, epochs=50, verbose=2, callbacks=[ES], validation_data=ts_generator) model.save(f'results\\models\\{PROJECT_NAME}')
X,Y = next(imagenet_gen) trainX, testX, trainY, testY = train_test_split(X, Y, test_size=0.25) tuner = Hyperband( tuner_model, objective='val_categorical_accuracy', max_epochs=40, project_name='tuning_categorical' ) tuner.search_space_summary() tuner.search(trainX, trainY,epochs=25,validation_data=(testX, testY),verbose=0) tuner.results_summary() best_model = tuner.get_best_models(num_models=1)[0] # Evaluate the best model. loss, accuracy = best_model.evaluate(testX, testY) tuner.get_best_hyperparameters()[0].values """Pretty good results : 0.898 vs 0.7551 categorical accuracy ## Testing different applications (ResNet, Inception) """ from keras.applications import VGG16 from keras import Model from keras.layers import AveragePooling2D, Input, Flatten, Dense, Dropout from keras.optimizers import Adam
metrics=[iou, iou_thresholded, 'mse'], distribution_strategy=tf.distribute.MirroredStrategy(), seed=42, hyperband_iterations=3) #print total search space tuner_hb.search_space_summary() #search through the total search space tuner_hb.search(train_generator, epochs=500, verbose=1, validation_data=(img_test, mask_test)) #save best model and hyperparameters best_model = tuner_hb.get_best_models(1)[0] best_hyperparameters = tuner_hb.get_best_hyperparameters(1)[0] print(tuner_hb.get_best_hyperparameters(1)) # model_json = best_model.to_json() with open("hp_bce_all_basicUNet_model.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 best_model.save_weights("hp_bce_all_tune_basicUNet_tuner_model.h5") with open('best_LGG_basicUNet_Param.txt', 'w') as f: print(best_hyperparameters, file=f) print("Saved model to disk")
INPUT_SHAPE = images[0].shape NUM_CLASSES = 1 HYPERBAND_MAX_EPOCHS = 22 SEED = 2 hypermodel = HyperUNet(input_shape=INPUT_SHAPE, classes=NUM_CLASSES) tuner = Hyperband(hypermodel, max_epochs=HYPERBAND_MAX_EPOCHS, objective='val_accuracy', seed=SEED, executions_per_trial=22) tuner.search(train_generator, epochs=20, validation_data=val_generator) best_model = tuner.get_best_models(1)[0] best_hyperparameters = tuner.get_best_hyperparameters(1)[0] # model_json = best_model.to_json() with open("LGG_basicUNet_model.json", "w") as json_file: json_file.write(model_json) # serialize weights to HDF5 best_model.save_weights("LGG_basicUNet_tuner_model.h5") with open('best_LGG_basicUNet_Param.txt', 'w') as f: print(best_hyperparameters, file=f) print("Saved model to disk")