def process_dataset(filename): dataset = TextImageFromTextTableDataset( filename, args.img_dir, img_transform=ImageToTensor()) data_loader = DataLoader(dataset) phocs = [] samples = [] for sample in tqdm(data_loader): phocs.append(process_image(sample['img'])) samples.append(sample['id'][0]) return np.concatenate(phocs), samples
def process_dataset(filename): dataset = TextImageFromTextTableDataset( filename, args.img_dir, img_transform=ImageToTensor() ) data_loader = DataLoader(dataset) phocs = [] samples = [] for sample in tqdm(data_loader): phocs.append(process_image(sample["img"])) samples.append(sample["id"][0]) return torch.stack(phocs).type("torch.DoubleTensor"), samples
ckpt = torch.load(args.checkpoint) if 'model' in ckpt and 'optimizer' in ckpt: model.load_state_dict(ckpt['model']) else: model.load_state_dict(ckpt) # Ensure parameters are in the correct device model.eval() if args.gpu > 0: model = model.cuda(args.gpu - 1) else: model = model.cpu() dataset = TextImageFromTextTableDataset(args.gt_file, args.img_dir, img_transform=ImageToTensor(), txt_transform=TextToTensor(syms)) dataset_loader = ImageDataLoader(dataset=dataset, image_channels=1, num_workers=8) with torch.cuda.device(args.gpu - 1): for batch in dataset_loader: if args.gpu > 0: x = batch['img'].data.cuda(args.gpu - 1) else: x = batch['img'].data.cpu() y = model(torch.autograd.Variable(x)).data if args.add_softmax: y = torch.nn.functional.log_softmax(y, dim=-1) dump_output_matrix(batch['id'], [y], args.output,
add_argument('output', type=argparse.FileType('w'), help='Filepath of the output file') args = args() syms = laia.utils.SymbolsTable(args.syms) phoc_size = sum(args.phoc_levels) * len(syms) model = build_dortmund_model(phoc_size) log.info('Model has {} parameters', sum(param.data.numel() for param in model.parameters())) model.load_state_dict(torch.load(args.model_checkpoint)) model = model.cuda(args.gpu - 1) if args.gpu > 0 else model.cpu() model.eval() queries_dataset = TextImageFromTextTableDataset( args.queries, args.img_dir, img_transform=ImageToTensor()) queries_loader = DataLoader(queries_dataset) def process_image(sample): sample = Variable(sample, requires_grad=False) sample = sample.cuda(args.gpu - 1) if args.gpu > 0 else sample.cpu() phoc = torch.nn.functional.logsigmoid(model(sample)) return phoc.data.cpu().squeeze() # Predict PHOC vectors phocs = [] labels = [] samples = [] for query in tqdm(queries_loader): phocs.append(process_image(query['img'])) labels.append(query['txt'][0])
add_argument('model_checkpoint', help='Filepath of the model checkpoint') add_argument('output', type=argparse.FileType('w'), help='Filepath of the output file') args = args() syms = laia.utils.SymbolsTable(args.syms) phoc_size = sum(args.phoc_levels) * len(syms) model = build_dortmund_model(phoc_size) log.info('Model has {} parameters', sum(param.data.numel() for param in model.parameters())) model.load_state_dict(torch.load(args.model_checkpoint)) model = model.cuda(args.gpu - 1) if args.gpu > 0 else model.cpu() model.eval() dataset = TextImageFromTextTableDataset( args.gt_txt, args.img_dir, img_transform=ImageToTensor()) loader = DataLoader(dataset) def process_image(sample): sample = Variable(sample, requires_grad=False) sample = sample.cuda(args.gpu - 1) if args.gpu > 0 else sample.cpu() phoc = torch.nn.functional.sigmoid(model(sample)) return phoc.data.cpu().numpy() # Predict PHOC vectors for query in tqdm(loader): phoc = process_image(query['img']) print(query['id'][0], file=args.output, end='') for j in range(phoc.shape[1]): print(' %.12g' % phoc[0, j], file=args.output, end='') print('', file=args.output)
sampler=FixedSizeSampler(tr_ds, args.train_samples_per_epoch) if args.train_samples_per_epoch else None) # Set all these separately because they might change between executions trainer.iterations_per_update = args.iterations_per_update trainer.set_data_loader(tr_ds_loader) trainer.set_batch_input_fn( ImageFeeder( device=args.gpu, # keep_padded_tensors=False, parent_feeder=ItemFeeder('img'))) va_ds = TextImageFromTextTableDataset( args.va_txt_table, args.img_dir, img_transform=ImageToTensor(fixed_height=args.fixed_height), txt_transform=TextToTensor(syms)) va_ds_loader = ImageDataLoader(dataset=va_ds, image_channels=1, batch_size=args.batch_size, num_workers=mp.cpu_count()) evaluator = Evaluator( model=model, data_loader=va_ds_loader, batch_input_fn=ImageFeeder(device=args.gpu, parent_feeder=ItemFeeder('img')), batch_target_fn=ItemFeeder('txt'), progress_bar='Valid' if args.show_progress_bar else None) engine_wrapper = HtrEngineWrapper(trainer, evaluator)