def entrance(mode, config, estimator=None): if config is not None: Config.load_config(config) if Config.estimator_name == 'esim': main_fasttext(mode, config, estimator) else: main(mode, config, estimator)
def main(args=NullArgs()): LogHelper.setup() logger = LogHelper.get_logger( os.path.splitext(os.path.basename(__file__))[0]) args.mode = Mode.PREDICT if args.config is not None: Config.load_config(args.config) if args.out_file is not None: Config.relative_path_submission = args.out_file if args.in_file is not None: Config.relative_path_test_file = args.in_file if args.database is not None: Config.relative_path_db = args.database print("relative_path_db " + Config.relative_path_db) print("raw_test_set " + Config.raw_test_set()) if os.path.exists(Config.test_doc_file): os.remove(Config.test_doc_file) if os.path.exists(Config.test_set_file): os.remove(Config.test_set_file) if args.mode in {Mode.PIPELINE, Mode.PREDICT, Mode.PREDICT_ALL_DATASETS}: logger.info( "=========================== Sub-task 1. Document Retrieval ==========================================" ) document_retrieval(logger, args.mode) if args.mode in { Mode.PIPELINE_NO_DOC_RETR, Mode.PIPELINE, Mode.PREDICT, Mode.PREDICT_NO_DOC_RETR, Mode.PREDICT_ALL_DATASETS, Mode.PREDICT_NO_DOC_RETR_ALL_DATASETS }: logger.info( "=========================== Sub-task 2. Sentence Retrieval ==========================================" ) sentence_retrieval_ensemble(logger, args.mode) logger.info( "=========================== Sub-task 3. Claim Validation ============================================" ) rte(logger, args, args.mode)
if __name__ == '__main__': parser = argparse.ArgumentParser() parser.add_argument('--mode', help='\'train\' or \'test\'', required=True) parser.add_argument('--config', help='/path/to/config/file, in JSON format') args = parser.parse_args() print(args) print(type(args)) LogHelper.setup() logger = LogHelper.get_logger( os.path.splitext(os.path.basename(__file__))[0] + "_" + args.mode) logger.info("parameters:\n" + str(vars(args))) if args.config is not None: Config.load_config(args.config) logger.info("scorer type: " + Config.estimator_name) logger.info("random seed: " + str(Config.seed)) logger.info("ESIM arguments: " + str(Config.esim_hyper_param)) # loading FastText takes long time, so better pickle the loaded FastText model # if os.path.splitext(Config.fasttext_path)[1] == '.p': # with open(Config.fasttext_path, "rb") as ft_file: # fasttext_model = pickle.load(ft_file) # else: # fasttext_model = Config.fasttext_path if hasattr(Config, 'is_snopes'): is_snopes = Config.is_snopes else: is_snopes = False logger.debug("is_snopes: " + str(is_snopes))
def main(mode: RTERunPhase, config=None, estimator=None): LogHelper.setup() logger = LogHelper.get_logger( os.path.splitext(os.path.basename(__file__))[0] + "_" + str(mode)) if config is not None and isinstance(config, str): logger.info("model: " + str(mode) + ", config: " + str(config)) Config.load_config(config) logger.info("scorer type: " + Config.estimator_name) logger.info("random seed: " + str(Config.seed)) logger.info("ESIM arguments: " + str(Config.esim_hyper_param)) # loading FastText takes long time, so better pickle the loaded FastText model if os.path.splitext(Config.fasttext_path)[1] == '.p': with open(Config.fasttext_path, "rb") as ft_file: fasttext_model = pickle.load(ft_file) else: fasttext_model = Config.fasttext_path if mode == RTERunPhase.train: # # training mode training_set, fasttext_model, vocab, embeddings = embed_data_set_with_glove_and_fasttext_claim_only( Config.training_set_file, fasttext_model, glove_path=Config.glove_path, threshold_h_sent_size=Config.max_sentence_size) h_sent_sizes = training_set['data']['h_sent_sizes'] h_sizes = np.ones(len(h_sent_sizes), np.int32) training_set['data']['h_sent_sizes'] = np.expand_dims(h_sent_sizes, 1) training_set['data']['h_sizes'] = h_sizes training_set['data']['h_np'] = np.expand_dims( training_set['data']['h_np'], 1) training_set['data']['h_ft_np'] = np.expand_dims( training_set['data']['h_ft_np'], 1) valid_set, _, _, _ = embed_data_set_with_glove_and_fasttext_claim_only( Config.dev_set_file, fasttext_model, vocab_dict=vocab, glove_embeddings=embeddings, threshold_h_sent_size=Config.max_sentence_size) del fasttext_model h_sent_sizes = valid_set['data']['h_sent_sizes'] h_sizes = np.ones(len(h_sent_sizes), np.int32) valid_set['data']['h_sent_sizes'] = np.expand_dims(h_sent_sizes, 1) valid_set['data']['h_sizes'] = h_sizes valid_set['data']['h_np'] = np.expand_dims(valid_set['data']['h_np'], 1) valid_set['data']['h_ft_np'] = np.expand_dims( valid_set['data']['h_ft_np'], 1) X_dict = { 'X_train': training_set['data'], 'X_valid': valid_set['data'], 'y_valid': valid_set['label'], 'embedding': embeddings } if estimator is None: estimator = get_estimator(Config.estimator_name, Config.ckpt_folder) if 'CUDA_VISIBLE_DEVICES' not in os.environ or not str( os.environ['CUDA_VISIBLE_DEVICES']).strip(): os.environ['CUDA_VISIBLE_DEVICES'] = str( GPUtil.getFirstAvailable(maxLoad=1.0, maxMemory=1.0 - Config.max_gpu_memory)[0]) estimator.fit(X_dict, training_set['label']) save_model(estimator, Config.model_folder, Config.pickle_name, logger) elif mode == 'test': # testing mode restore_param_required = estimator is None if estimator is None: estimator = load_model(Config.model_folder, Config.pickle_name) if estimator is None: estimator = get_estimator(Config.estimator_name, Config.ckpt_folder) vocab, embeddings = load_whole_glove(Config.glove_path) vocab = vocab_map(vocab) test_set, _, _, _ = embed_data_set_with_glove_and_fasttext_claim_only( Config.test_set_file, fasttext_model, vocab_dict=vocab, glove_embeddings=embeddings, threshold_h_sent_size=Config.max_sentence_size) del fasttext_model h_sent_sizes = test_set['data']['h_sent_sizes'] h_sizes = np.ones(len(h_sent_sizes), np.int32) test_set['data']['h_sent_sizes'] = np.expand_dims(h_sent_sizes, 1) test_set['data']['h_sizes'] = h_sizes test_set['data']['h_np'] = np.expand_dims(test_set['data']['h_np'], 1) test_set['data']['h_ft_np'] = np.expand_dims( test_set['data']['h_ft_np'], 1) x_dict = {'X_test': test_set['data'], 'embedding': embeddings} if 'CUDA_VISIBLE_DEVICES' not in os.environ or not str( os.environ['CUDA_VISIBLE_DEVICES']).strip(): os.environ['CUDA_VISIBLE_DEVICES'] = str( GPUtil.getFirstAvailable(maxLoad=1.0, maxMemory=1.0 - Config.max_gpu_memory)[0]) predictions = estimator.predict(x_dict, restore_param_required) generate_submission(predictions, test_set['id'], Config.test_set_file, Config.submission_file) if 'label' in test_set: print_metrics(test_set['label'], predictions, logger) return estimator