def optimize_cnn(hype_space): """Build a convolutional neural network and train it.""" if not is_gpu_available(): tf.logging.warning('GPUs are not available') tf.logging.debug("Hyperspace: ", hype_space) tf.logging.debug("\n") try: model, model_name, result, _ = build_and_train(hype_space) tf.logging.info("Training ended with success:") tf.logging.info("Model name: ", model_name) # Save training results to disks with unique filenames # TODO do we need this? this save to json on disc not to mongo. Not sure if we want always save to disc save_json_result(model_name, result) K.clear_session() del model tf.logging.info('before return result') return result except Exception as err: try: K.clear_session() except: pass err_str = str(err) tf.logging.error(err_str) traceback_str = str(traceback.format_exc()) tf.logging.error(traceback_str) return { 'status': STATUS_FAIL, 'err': err_str, 'traceback': traceback_str }
def optimize_cnn(hype_space): """Build a convolutional neural network and train it.""" if not is_gpu_available(): tf.logging.warning('GPUs are not available') tf.logging.debug("Hyperspace: ", hype_space) tf.logging.debug("\n") try: model, model_name, result, _ = build_and_train( hype_space, log_for_tensorboard=True) tf.logging.info("Training ended with success:") tf.logging.info("Model name: %s", model_name) # Save training results to disks with unique filenames save_json_result(model_name, result) export_model(model_name) K.clear_session() del model tf.logging.info('before return result') return result except Exception as err: err_str = str(err) tf.logging.error(err_str) traceback_str = str(traceback.format_exc()) tf.logging.error(traceback_str) return { 'status': STATUS_FAIL, 'err': err_str, 'traceback': traceback_str }
TARGET_DATA_DIR = "data/" # Labels File LABEL_DIR = "data/labels/" labels_file = "data/labels.csv" label_values_file = "data/label_values.json" # Train and Validation Images TRAIN_IMAGES_FOLDER = "data/train/" VALID_IMAGES_FOLDER = "data/valid/" TEST_IMAGES_FOLDER = "data/test/" if __name__ == "__main__": target_dims = get_attribute_dims(label_values_file) use_gpu = is_gpu_available() pretrained_conv_model, _, _ = get_pretrained_model( "vgg16", pop_last_pool_layer=True, use_gpu=use_gpu) train_valid_test_split("data/images/", "data/") attribute_models = create_attributes_model( AttributeFCN, 512, pretrained_conv_model, target_dims, # dict(list(target_dims.items())[:3]), "weights/vgg16-fcn-266-2/", labels_file, TRAIN_IMAGES_FOLDER, VALID_IMAGES_FOLDER, num_epochs=50,
) tf.logging.info("Best: %s", best) return best if __name__ == "__main__": """Plot the model and run the optimisation forever (and saves results).""" tf.logging.set_verbosity(tf.logging.DEBUG) # Print ENV Variables tf.logging.debug('=' * 20 + ' Environment Variables ' + '=' * 20) for k, v in os.environ.items(): tf.logging.debug('{}: {}'.format(k, v)) if not is_gpu_available(): tf.logging.warning('GPUs are not available') tf.logging.info("Plotting a demo model that would represent " "a quite normal model (or a bit more huge), " "and then the best model...") plot_base_model() tf.logging.info("Now, we train many models, one after the other. " "Note that hyperopt has support for cloud " "distributed training using MongoDB.") tf.logging.info("\nYour results will be saved in the folder named 'results/'. " "You can sort that alphabetically and take the greatest one. " "As you run the optimization, results are consinuously saved into a "
def main(): parser = argparse.ArgumentParser() # Required Parameters parser.add_argument( "--data_dir", default=None, type=str, required=True, help= "The input data dir. Should contain the .tsv files (or other data files) for the task." ) parser.add_argument("--model_type", default=None, type=str, help="bert OR lstm", required=True) parser.add_argument( "--output_dir", default=None, type=str, required=True, help= "The output_result directory where the model predictions will be written." ) parser.add_argument("--output_mode", default="regression", type=str, help="classification or regression", required=True) parser.add_argument("--domain", default="celtrion", type=str, help="celtrion", required=True) parser.add_argument("--target", default="close", type=str, help="close, open, volume", required=True) # Other Parameters parser.add_argument("--use_gpu", help="use gpu=True or False", default=True) parser.add_argument("--learning_rate", default=5e-5, type=float, help="The initial learning rate for Adam.") parser.add_argument("--num_train_epochs", default=3.0, type=float, help="Total number of training epochs to perform.") parser.add_argument("--per_gpu_train_batch_size", default=8, type=int, help="Batch size per GPU/CPU for classifier.") parser.add_argument("--per_gpu_eval_batch_size", default=8, type=int, help="Batch size per GPU/CPU for classifier.") parser.add_argument("--do_train", action='store_true', help="Whether to run training.") parser.add_argument("--do_eval", action='store_true', help="Whether to run eval.") parser.add_argument("--window_size", default=50, type=int, help="window size for lstm") args = parser.parse_args() # use GPU ? if not is_gpu_available(): args.use_gpu = False # Model model_type = args.model_type output_mode = args.output_mode # data data_root = args.data_dir # output output_root = args.output_dir prepare_dir(output_root) fns = { 'input': { 'train': os.path.join(data_root, 'train.csv'), 'test': os.path.join(data_root, 'test.csv') }, 'output': { # 'csv' : os.path.join() # 필요시에 ~~ }, 'model': os.path.join(output_root, 'model.out') } # Train if args.do_train: hps = HParams( # domain ------------------------------------------- domain=args.domain, target=args.target, # gpu setting ---------------------------------------- use_gpu=args.use_gpu, # train settings ---------------------------------------- learning_rate=args.learning_rate, num_train_epochs=args.num_train_epochs, per_gpu_train_batch_size=args.per_gpu_train_batch_size, window_size=args.window_size, # model settings ---------------------------------------- model_type=model_type, output_mode=output_mode) hps.show() print("*********** Start Training ***********") run_train(fns['input']['train'], fns['model'], hps) if args.do_eval: print("*********** Start Evaluating ***********") batch_size = args.per_gpu_eval_batch_size run_eval(fns['input']['test'], fns['model'], batch_size)