def __init__(self): model_path = model_root / "saved_models/BAN_MEVF/model_epoch19.pth" data_RAD = model_root / "data_RAD" dictionary = dataset_RAD.Dictionary.load_from_file(data_RAD / "dictionary.pkl") args = Bunch( RAD_dir=str(data_RAD), autoencoder=True, maml=True, feat_dim=64, op="c", num_hid=1024, rnn="LSTM", gamma=2, ae_model_path="pretrained_ae.pth", maml_model_path="pretrained_maml.weights", activation="relu", dropout=0.5, eps_cnn=1e-5, momentum_cnn=0.05, map_location=device, ) self.eval_dset = dataset_RAD.VQAFeatureDataset("test", args, dictionary) self.model = base_model.build_BAN(self.eval_dset, args) model_data = torch.load(model_path, device) self.model.train(False) self.model.load_state_dict(model_data.get("model_state", model_data))
def evaluate(model, args): val_set = dataset_RAD.VQAFeatureDataset('val', args, dictionary, question_len=args.question_len) val_loader = DataLoader(val_set, batch_size, shuffle=True, num_workers=0) # model.load_state_dict(torch.load(args.output + 'SAN.pth')) device = args.device score = 0 upper_bound = 0 num_data = 0 with torch.no_grad(): for i, (v, q, a, _, _) in enumerate(val_loader): if args.mima: v[0] = v[0].to(device) v[1] = v[1].to(device) else: v = v.to(device) q = q.to(device) a = a.to(device).float() pred = model(v, q) batch_score = compute_score_with_logits(pred, a.data).sum() score += batch_score upper_bound += (a.max(1)[0]).sum() num_data += pred.size(0) score = score / len(val_loader.dataset) upper_bound = upper_bound / len(val_loader.dataset) return score, upper_bound
return result, question_types_result # Test phase if __name__ == '__main__': args = parse_args() print(args) torch.backends.cudnn.benchmark = True args.device = torch.device("cuda:" + str(args.gpu) if args.gpu >= 0 else "cpu") # Check if evaluating on TDIUC dataset or VQA dataset if args.use_RAD: dictionary = dataset_RAD.Dictionary.load_from_file( os.path.join(args.RAD_dir, 'dictionary.pkl')) eval_dset = dataset_RAD.VQAFeatureDataset(args.split, args, dictionary) batch_size = args.batch_size constructor = 'build_%s' % args.model model = getattr(base_model, constructor)(eval_dset, args) print(model) eval_loader = DataLoader(eval_dset, batch_size, shuffle=False, num_workers=0, pin_memory=True, collate_fn=utils.trim_collate) def save_questiontype_results(outfile_path, quesntion_types_result): for i in quesntion_types_result:
utils.create_dir(args.output) logger = utils.Logger(os.path.join(args.output, "log.txt")) logger.write(args.__repr__()) # Set GPU device device = torch.device("cuda:" + str(args.gpu) if args.gpu >= 0 else "cpu") args.device = device # Fixed ramdom seed torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) torch.backends.cudnn.benchmark = True torch.backends.cudnn.deterministic = True # Load dictionary and RAD training dataset if args.use_RAD: dictionary = dataset_RAD.Dictionary.load_from_file( os.path.join(args.RAD_dir, "dictionary.pkl")) train_dset = dataset_RAD.VQAFeatureDataset( "train", args, dictionary, question_len=args.question_len) batch_size = args.batch_size # Create VQA model constructor = "build_%s" % args.model model = getattr(base_model, constructor)(train_dset, args) optim = None epoch = 0 # load snapshot if args.input is not None: print("loading %s" % args.input) model_data = torch.load(args.input) model.load_state_dict(model_data.get("model_state", model_data)) model.to(device) optim = torch.optim.Adamax( filter(lambda p: p.requires_grad, model.parameters())) optim.load_state_dict(model_data.get("optimizer_state", model_data))
# args.MFB_K = 3 args.mima = False utils.create_dir(args.output) logger = utils.Logger(os.path.join(args.output, 'log.txt')) logger.write(args.__repr__()) os.environ['CUDA_VISIBLE_DEVICES'] = '0' device = torch.device("cuda:" + str(args.gpu) if args.gpu >= 0 else "cpu") args.device = device # Fixed ramdom seed torch.manual_seed(args.seed) torch.cuda.manual_seed(args.seed) torch.backends.cudnn.benchmark = True torch.backends.cudnn.deterministic = True dictionary = dataset_RAD.Dictionary.load_from_file(os.path.join(args.RAD_dir, 'dictionary.pkl')) train_set = dataset_RAD.VQAFeatureDataset('train', args, dictionary, question_len=args.question_len) batch_size = args.batch_size model = Net(args, len(train_set.label2ans)) model.to(device) # total_params = sum(p.numel() for p in model.parameters()) # print(f'{total_params:,} total parameters.') # total_trainable_params = sum( # p.numel() for p in model.parameters() if p.requires_grad) # print(f'{total_trainable_params:,} training parameters.') train_loader = DataLoader(train_set, batch_size, shuffle=True, num_workers=0) criterion = torch.nn.BCEWithLogitsLoss(reduction='sum') optimizer = torch.optim.Adam(filter(lambda p: p.requires_grad, model.parameters()), 0.001, [0.9, 0.999])