-
Notifications
You must be signed in to change notification settings - Fork 8
/
inference.py
144 lines (130 loc) · 4.3 KB
/
inference.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
import torch
import os
from train import id_to_string
from checkpoint import load_checkpoint
from torchvision import transforms
from dataset import LoadEvalDataset, collate_eval_batch
from flags import Flags
from utils import get_network
import csv
from torch.utils.data import DataLoader
import argparse
import random
from tqdm import tqdm
def main(parser):
"""Inference code
"""
is_cuda = torch.cuda.is_available()
# load pretrained model checkpoint
checkpoint = load_checkpoint(parser.checkpoint, cuda=is_cuda)
options = Flags(checkpoint["configs"]).get()
torch.manual_seed(options.seed)
random.seed(options.seed)
torch.backends.cudnn.deterministic = True
torch.backends.cudnn.benchmark = False
hardware = "cuda" if is_cuda else "cpu"
device = torch.device(hardware)
print("--------------------------------")
print("Running {} on device {}\n".format(options.network, device))
model_checkpoint = checkpoint["model"]
if model_checkpoint:
print(
"[+] Checkpoint\n",
"Resuming from epoch : {}\n".format(checkpoint["epoch"]),
)
print(options.input_size.height)
# transform to be applied on a sample.
transformed = transforms.Compose(
[
transforms.Resize((options.input_size.height, options.input_size.width)),
transforms.ToTensor(),
]
)
dummy_gt = "\sin " * parser.max_sequence # set maximum inference sequence
# make dataset from test folder
root = os.path.join(os.path.dirname(parser.file_path), "images")
with open(parser.file_path, "r") as fd:
reader = csv.reader(fd, delimiter="\t")
data = list(reader)
test_data = [[os.path.join(root, x[0]), x[0], dummy_gt] for x in data]
test_dataset = LoadEvalDataset(
test_data, checkpoint["token_to_id"], checkpoint["id_to_token"], crop=False, transform=transformed,
rgb=options.data.rgb
)
test_data_loader = DataLoader(
test_dataset,
batch_size=parser.batch_size,
shuffle=False,
num_workers=options.num_workers,
collate_fn=collate_eval_batch,
)
print(
"[+] Data\n",
"The number of test samples : {}\n".format(len(test_dataset)),
)
model = get_network(
options.network,
options,
model_checkpoint,
device,
test_dataset,
)
model.eval()
results = []
for d in tqdm(test_data_loader):
input = d["image"].to(device)
expected = d["truth"]["encoded"].to(device)
output = model(input, expected, False, 0.0)
decoded_values = output.transpose(1, 2)
_, sequence = torch.topk(decoded_values, 1, dim=1)
sequence = sequence.squeeze(1)
sequence_str = id_to_string(sequence, test_data_loader, do_eval=1)
for path, predicted in zip(d["file_path"], sequence_str):
results.append((path, predicted))
# save inference results as csv file
os.makedirs(parser.output_dir, exist_ok=True)
with open(os.path.join(parser.output_dir, "output.csv"), "w") as w:
for path, predicted in results:
w.write(path + "\t" + predicted + "\n")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--checkpoint",
dest="checkpoint",
default="./log/satrn/checkpoints/0015.pth",
type=str,
help="Path of checkpoint file",
)
parser.add_argument(
"--max_sequence",
dest="max_sequence",
default=230,
type=int,
help="maximun sequence when doing inference",
)
parser.add_argument(
"--batch_size",
dest="batch_size",
default=8,
type=int,
help="batch size when doing inference",
)
eval_dir = os.environ.get('SM_CHANNEL_EVAL', '/opt/ml/input/data/')
file_path = os.path.join(eval_dir, 'eval_dataset/input.txt')
parser.add_argument(
"--file_path",
dest="file_path",
default=file_path,
type=str,
help="file path when doing inference",
)
output_dir = os.environ.get('SM_OUTPUT_DATA_DIR', 'submit')
parser.add_argument(
"--output_dir",
dest="output_dir",
default=output_dir,
type=str,
help="output directory",
)
parser = parser.parse_args()
main(parser)