action='store_true', help="Flag to indicate testing (default=False)") parser.add_argument( "--equal-distribution", dest="is_equal_distribution", action='store_true', help="Flag to indicate active learning equal distribution (default=False)") args = parser.parse_args() out_dir = args.out_dir_path ############################################################## ## Make directory and initialize some required variables # U.mkdir_p(out_dir) U.mkdir_p(out_dir + '/data') U.mkdir_p(out_dir + '/preds') U.mkdir_p(out_dir + '/models') U.mkdir_p(out_dir + '/models/best_weights') U.set_logger(out_dir) U.print_args(args) # import sys # sys.stdout = open(out_dir + '/stdout.txt', 'w') # sys.stderr = open(out_dir + '/stderr.txt', 'w') if args.is_test and args.test_path == None: logger.error("Please enter the path to the file for testing!") exit()
from core import reader as dataset_reader from core.evaluator import Evaluator from core import utils as U import core.utils as U import core.helper as helper import pickle as pk import torch from torch.autograd import Variable import numpy as np from datetime import datetime import logging logger = logging.getLogger(__name__) global_base_path = "web_flask/web_movie_review/" U.mkdir_p(global_base_path) U.set_logger(global_base_path) class ModelEvaluate: def __init__(self): self.base_path = global_base_path self.vocab_path = "best_model/vocab_v50000.pkl" self.model_path = "best_model/best_model_weights.h5" ####################################################################################### ## Prepare data # logger.info("Loading vocab...") with open(self.vocab_path, 'rb') as vocab_file: [self.vocab] = pk.load(vocab_file)
help="Batch size for evaluation (default=32)") parser.add_argument("--seed", dest="seed", type=int, metavar='<int>', default=1337, help="Random seed (default=1337)") parser.add_argument("--shuffle-seed", dest="shuffle_seed", type=int, metavar='<int>', default=1337, help="Random shuffle seed (default=1337)") args = parser.parse_args() U.mkdir_p(args.out_dir_path) U.mkdir_p(args.out_dir_path + '/preds') U.mkdir_p(args.out_dir_path + '/models') U.set_logger(args.out_dir_path) U.print_args(args) itemid, title, image_path, y_multi, class_titles, class_max_number, num_classes = R.read_csv_train( args.train_csv_path) final_itemid, final_title, final_image_path = R.read_csv_final_test( args.final_test_csv_path) vocab, x_title = R.create_vocab(title) final_x_title = R.convert_word_to_idx_using_vocab(vocab, final_title) ############################################################## ## Process data and train model according to class category
def eval_retrieval(evalargs): # Set up evaluation: save_dir = evalargs.save_dir mkdir_p(evalargs.save_dir) model_configs = get_model_config(evalargs.ModelPath) # Set up graph: pred_config = PredictConfig( model=DH3D(model_configs), session_init=get_model_loader(evalargs.ModelPath), input_names=['pointclouds'], output_names=['globaldesc'], # ['globaldesc'], output_weights ) predictor = OfflinePredictor(pred_config) # Data: df, totalbatch = get_eval_global_testdata(model_configs, evalargs.data_path, evalargs.ref_gt_file) # Predict: pcdnum = 0 for [pcds, names] in df: # pcds is a list, batchsize x numpts x 3 batch = pcds.shape[0] if totalbatch > batch: numpts = pcds.shape[1] pcddim = pcds.shape[2] padzeros = np.zeros([totalbatch - batch, numpts, pcddim], dtype=np.float32) pcds = np.vstack([pcds, padzeros]) results = predictor(pcds) global_feats = results[0] for i in range(batch): pcdnum += 1 globaldesc = global_feats[i] name = names[i] savename = os.path.join(evalargs.save_dir, name) basedir = os.path.dirname(savename) mkdir_p(basedir) globaldesc.tofile(savename) print('predicted {} poitnclouds \n'.format(pcdnum)) # Evaluation recall: if evalargs.eval_recall: evaluator = GlobalDesc_eval( result_savedir='./', desc_dir=save_dir, database_file=os.path.join(evalargs.data_path, evalargs.ref_gt_file), query_file=os.path.join(evalargs.data_path, evalargs.qry_gt_file), max_num_nn=25) evaluator.evaluate() print("evaluation finished!\n") if evalargs.delete_tmp: # delete all the descriptors descdirs = [os.path.join(save_dir, f) for f in os.listdir(save_dir)] descdirs = [d for d in descdirs if os.path.isdir(d)] for d in descdirs: shutil.rmtree(d)
"Flag not to use class weight (i.e., not using weighted loss function based on class distribution)" ) parser.add_argument( "--shuffle-path", dest="shuffle_path", type=str, metavar='<str>', help= "The path to the shuffle file containing the permutation of index for training (*.txt) (numpy array)" ) args = parser.parse_args() out_dir = args.out_dir_path U.mkdir_p(out_dir + '/data') U.mkdir_p(out_dir + '/preds') U.mkdir_p(out_dir + '/models') U.set_logger(out_dir) U.print_args(args) import sys sys.stdout = open(out_dir + '/stdout.txt', 'w') sys.stderr = open(out_dir + '/stderr.txt', 'w') #################################################################################### ## Argument Validation # # Assert to check either train,dev,test path or processed data path is specified # Either of them must be set
"Prediction Score: %.2f %% (Positive Review)" % (percentScore)) else: logger.info(U.BColors.BRED + "Prediction Score: %.2f %% (Negative Review)" % (percentScore)) ############################################################################################# ## Start Attention Visualization # t_attention_start = time() logger.info("Processing attention visualization...") # Create output folder output_foldername = 'test_single_output/' U.mkdir_p(output_foldername) # make duplicate to match the test_x format score_list = [percentScore, percentScore] # Get the attention weights from the model attention_weights = model.att_weights.data.numpy() assert (test_x.shape == attention_weights.shape) # Do attention visualization and export to pdf helper.do_attention_visualization(attention_weights, test_x, vocab, filename_list, score_list, output_foldername=output_foldername)