-
Notifications
You must be signed in to change notification settings - Fork 27
/
Copy pathdemo.py
194 lines (168 loc) · 6.57 KB
/
demo.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
import sys
import string
import argparse
import torch
import torch.backends.cudnn as cudnn
import torch.utils.data
import torch.nn.functional as F
from utils import CTCLabelConverter, AttnLabelConverter
from dataset import RawDataset, AlignCollate
from model import Model
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
def demo(opt):
"""model configuration"""
if "CTC" in opt.Prediction:
converter = CTCLabelConverter(opt.character)
else:
converter = AttnLabelConverter(opt.character)
opt.sos_token_index = converter.dict["[SOS]"]
opt.eos_token_index = converter.dict["[EOS]"]
opt.num_class = len(converter.character)
model = Model(opt)
print(
"model input parameters",
opt.imgH,
opt.imgW,
opt.num_fiducial,
opt.input_channel,
opt.output_channel,
opt.hidden_size,
opt.num_class,
opt.batch_max_length,
opt.Transformation,
opt.FeatureExtraction,
opt.SequenceModeling,
opt.Prediction,
)
model = torch.nn.DataParallel(model).to(device)
# load model
print("loading pretrained model from %s" % opt.saved_model)
model.load_state_dict(torch.load(opt.saved_model, map_location=device))
# prepare data. two demo images from https://github.com/bgshih/crnn#run-demo
AlignCollate_demo = AlignCollate(opt, mode="test")
demo_data = RawDataset(root=opt.image_folder, opt=opt) # use RawDataset
demo_loader = torch.utils.data.DataLoader(
demo_data,
batch_size=opt.batch_size,
shuffle=False,
num_workers=int(opt.workers),
collate_fn=AlignCollate_demo,
pin_memory=True,
)
# predict
model.eval()
with torch.no_grad():
log = open(f"./log_demo_result.txt", "w")
for image_tensors, image_path_list in demo_loader:
batch_size = image_tensors.size(0)
image = image_tensors.to(device)
if "CTC" in opt.Prediction:
preds = model(image)
else:
# For max length prediction
text_for_pred = (
torch.LongTensor(batch_size)
.fill_(converter.dict["[SOS]"])
.to(device)
)
preds = model(image, text_for_pred, is_train=False)
# Select max probabilty (greedy decoding) then decode index to character
preds_size = torch.IntTensor([preds.size(1)] * batch_size).to(device)
_, preds_index = preds.max(2)
preds_str = converter.decode(preds_index, preds_size)
dashed_line = "-" * 80
head = f'{"image_path":25s}\t{"predicted_labels":25s}\tconfidence score'
print(f"{dashed_line}\n{head}\n{dashed_line}")
log.write(f"{dashed_line}\n{head}\n{dashed_line}\n")
preds_prob = F.softmax(preds, dim=2)
preds_max_prob, _ = preds_prob.max(dim=2)
for img_name, pred, pred_max_prob in zip(
image_path_list, preds_str, preds_max_prob
):
if "Attn" in opt.Prediction:
pred_EOS = pred.find("[EOS]")
pred = pred[:pred_EOS] # prune after "end of sentence" token ([s])
pred_max_prob = pred_max_prob[:pred_EOS]
# calculate confidence score (= multiply of pred_max_prob)
confidence_score = pred_max_prob.cumprod(dim=0)[-1]
print(f"{img_name:25s}\t{pred:25s}\t{confidence_score:0.4f}")
log.write(f"{img_name:25s}\t{pred:25s}\t{confidence_score:0.4f}\n")
log.close()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument(
"--image_folder",
default="demo_image/",
help="path to image_folder which contains text images",
)
parser.add_argument(
"--workers", type=int, help="number of data loading workers", default=4
)
parser.add_argument("--batch_size", type=int, default=192, help="input batch size")
parser.add_argument(
"--saved_model", required=True, help="path to saved_model to evaluation"
)
""" Data processing """
parser.add_argument(
"--batch_max_length", type=int, default=25, help="maximum-label-length"
)
parser.add_argument(
"--imgH", type=int, default=32, help="the height of the input image"
)
parser.add_argument(
"--imgW", type=int, default=100, help="the width of the input image"
)
parser.add_argument(
"--character",
type=str,
default="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ!\"#$%&'()*+,-./:;<=>?@[\\]^_`{|}~",
help="character label",
)
parser.add_argument("--Aug", type=str, default="None", help="placeholder")
parser.add_argument("--semi", type=str, default="None", help="placeholder")
""" Model Architecture """
parser.add_argument("--model_name", type=str, required=True, help="CRNN|TRBA")
parser.add_argument(
"--num_fiducial",
type=int,
default=20,
help="number of fiducial points of TPS-STN",
)
parser.add_argument(
"--input_channel",
type=int,
default=3,
help="the number of input channel of Feature extractor",
)
parser.add_argument(
"--output_channel",
type=int,
default=512,
help="the number of output channel of Feature extractor",
)
parser.add_argument(
"--hidden_size", type=int, default=256, help="the size of the LSTM hidden state"
)
opt = parser.parse_args()
if opt.model_name == "CRNN":
opt.Transformation = "None"
opt.FeatureExtraction = "VGG"
opt.SequenceModeling = "BiLSTM"
opt.Prediction = "CTC"
elif opt.model_name == "TRBA":
opt.Transformation = "TPS"
opt.FeatureExtraction = "ResNet"
opt.SequenceModeling = "BiLSTM"
opt.Prediction = "Attn"
cudnn.benchmark = True # It fasten training.
cudnn.deterministic = True
opt.num_gpu = torch.cuda.device_count()
if opt.num_gpu > 1:
print(
"We recommend to use 1 GPU, check your GPU number, you would miss CUDA_VISIBLE_DEVICES=0 or typo"
)
print("To use multi-gpu setting, remove or comment out these lines")
sys.exit()
if sys.platform == "win32":
opt.workers = 0
demo(opt)