forked from facebookresearch/nougat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
predict.py
211 lines (202 loc) · 7.26 KB
/
predict.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
"""
Copyright (c) Meta Platforms, Inc. and affiliates.
This source code is licensed under the MIT license found in the
LICENSE file in the root directory of this source tree.
"""
import sys
from pathlib import Path
import logging
import re
import argparse
import re
import os
from functools import partial
import torch
from torch.utils.data import ConcatDataset
from tqdm import tqdm
from nougat import NougatModel
from nougat.utils.dataset import LazyDataset
from nougat.utils.device import move_to_device, default_batch_size
from nougat.utils.checkpoint import get_checkpoint
from nougat.postprocessing import markdown_compatible
import pypdf
logging.basicConfig(level=logging.INFO)
def get_args():
parser = argparse.ArgumentParser()
parser.add_argument(
"--batchsize",
"-b",
type=int,
default=default_batch_size(),
help="Batch size to use.",
)
parser.add_argument(
"--checkpoint",
"-c",
type=Path,
default=None,
help="Path to checkpoint directory.",
)
parser.add_argument(
"--model",
"-m",
type=str,
default="0.1.0-small",
help=f"Model tag to use.",
)
parser.add_argument("--out", "-o", type=Path, help="Output directory.")
parser.add_argument(
"--recompute",
action="store_true",
help="Recompute already computed PDF, discarding previous predictions.",
)
parser.add_argument(
"--full-precision",
action="store_true",
help="Use float32 instead of bfloat16. Can speed up CPU conversion for some setups.",
)
parser.add_argument(
"--no-markdown",
dest="markdown",
action="store_false",
help="Do not add postprocessing step for markdown compatibility.",
)
parser.add_argument(
"--markdown",
action="store_true",
help="Add postprocessing step for markdown compatibility (default).",
)
parser.add_argument(
"--no-skipping",
dest="skipping",
action="store_false",
help="Don't apply failure detection heuristic.",
)
parser.add_argument(
"--pages",
"-p",
type=str,
help="Provide page numbers like '1-4,7' for pages 1 through 4 and page 7. Only works for single PDF input.",
)
parser.add_argument("pdf", nargs="+", type=Path, help="PDF(s) to process.")
args = parser.parse_args()
if args.checkpoint is None or not args.checkpoint.exists():
args.checkpoint = get_checkpoint(args.checkpoint, model_tag=args.model)
if args.out is None:
logging.warning("No output directory. Output will be printed to console.")
else:
if not args.out.exists():
logging.info("Output directory does not exist. Creating output directory.")
args.out.mkdir(parents=True)
if not args.out.is_dir():
logging.error("Output has to be directory.")
sys.exit(1)
if len(args.pdf) == 1 and not args.pdf[0].suffix == ".pdf":
# input is a list of pdfs
try:
pdfs_path = args.pdf[0]
if pdfs_path.is_dir():
args.pdf = list(pdfs_path.rglob("*.pdf"))
else:
args.pdf = [
Path(l) for l in open(pdfs_path).read().split("\n") if len(l) > 0
]
logging.info(f"Found {len(args.pdf)} files.")
except:
pass
if args.pages and len(args.pdf) == 1:
pages = []
for p in args.pages.split(","):
if "-" in p:
start, end = p.split("-")
pages.extend(range(int(start) - 1, int(end)))
else:
pages.append(int(p) - 1)
args.pages = pages
else:
args.pages = None
return args
def main():
args = get_args()
model = NougatModel.from_pretrained(args.checkpoint)
model = move_to_device(model, bf16=not args.full_precision, cuda=args.batchsize > 0)
if args.batchsize <= 0:
# set batch size to 1. Need to check if there are benefits for CPU conversion for >1
args.batchsize = 1
model.eval()
datasets = []
for pdf in args.pdf:
if not pdf.exists():
continue
if args.out:
out_path = args.out / pdf.with_suffix(".mmd").name
if out_path.exists() and not args.recompute:
logging.info(
f"Skipping {pdf.name}, already computed. Run with --recompute to convert again."
)
continue
try:
dataset = LazyDataset(
pdf,
partial(model.encoder.prepare_input, random_padding=False),
args.pages,
)
except pypdf.errors.PdfStreamError:
logging.info(f"Could not load file {str(pdf)}.")
continue
datasets.append(dataset)
if len(datasets) == 0:
return
dataloader = torch.utils.data.DataLoader(
ConcatDataset(datasets),
batch_size=args.batchsize,
shuffle=False,
collate_fn=LazyDataset.ignore_none_collate,
)
predictions = []
file_index = 0
page_num = 0
for i, (sample, is_last_page) in enumerate(tqdm(dataloader)):
model_output = model.inference(
image_tensors=sample, early_stopping=args.skipping
)
# check if model output is faulty
for j, output in enumerate(model_output["predictions"]):
if page_num == 0:
logging.info(
"Processing file %s with %i pages"
% (datasets[file_index].name, datasets[file_index].size)
)
page_num += 1
if output.strip() == "[MISSING_PAGE_POST]":
# uncaught repetitions -- most likely empty page
predictions.append(f"\n\n[MISSING_PAGE_EMPTY:{page_num}]\n\n")
elif args.skipping and model_output["repeats"][j] is not None:
if model_output["repeats"][j] > 0:
# If we end up here, it means the output is most likely not complete and was truncated.
logging.warning(f"Skipping page {page_num} due to repetitions.")
predictions.append(f"\n\n[MISSING_PAGE_FAIL:{page_num}]\n\n")
else:
# If we end up here, it means the document page is too different from the training domain.
# This can happen e.g. for cover pages.
predictions.append(
f"\n\n[MISSING_PAGE_EMPTY:{i*args.batchsize+j+1}]\n\n"
)
else:
if args.markdown:
output = markdown_compatible(output)
predictions.append(output)
if is_last_page[j]:
out = "".join(predictions).strip()
out = re.sub(r"\n{3,}", "\n\n", out).strip()
if args.out:
out_path = args.out / Path(is_last_page[j]).with_suffix(".mmd").name
out_path.parent.mkdir(parents=True, exist_ok=True)
out_path.write_text(out, encoding="utf-8")
else:
print(out, "\n\n")
predictions = []
page_num = 0
file_index += 1
if __name__ == "__main__":
main()