File size: 14,358 Bytes
d61b9c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
"""
Copyright (c) 2019-present NAVER Corp.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
"""
import torch
import torch.nn as nn
import torch.nn.functional as F

from modules_srn.transformation import TPS_SpatialTransformerNetwork
from modules_srn.feature_extraction import VGG_FeatureExtractor, RCNN_FeatureExtractor, ResNet_FeatureExtractor
from modules_srn.sequence_modeling import BidirectionalLSTM
from modules_srn.prediction import Attention
from modules_srn.resnet_aster import ResNet_ASTER

from modules_srn.bert import Bert_Ocr
from modules_srn.bert import Config

from modules_srn.SRN_modules import Transforme_Encoder, SRN_Decoder, Torch_transformer_encoder
from modules_srn.resnet_fpn import ResNet_FPN
import settings
import sys

# singleChar - if -1 then STRScore outputs all char, however if
# 0 - N, then it will output the single character confidence of the index 0 to N
class STRScore(nn.Module):
    def __init__(self, opt, converter, device, gtStr="", enableSingleCharAttrAve=False, model=None):
        super(STRScore, self).__init__()
        self.enableSingleCharAttrAve = enableSingleCharAttrAve
        self.singleChar = -1
        self.opt = opt
        self.converter = converter
        self.device = device
        self.gtStr = gtStr
        self.model = model # Pass here if you want to use it
        self.blank = torch.tensor([-1], dtype=torch.float).to(self.device)
        self.separator = torch.tensor([-2], dtype=torch.float).to(self.device)

    # singleChar - if >=0, then the output of STRScore will only be a single character
    # instead of a whole. The char index will be equal to the parameter "singleChar".
    def setSingleCharOutput(self, singleChar):
        self.singleChar = singleChar

    def forward(self, preds):
        preds = preds[2] # Access second index
        bs = preds.shape[0]
        # text_for_loss, length_for_loss = self.converter.encode(labels, batch_max_length=self.opt.batch_max_length)
        text_for_loss_length = self.opt.batch_max_length + 1

        # _, preds_index = preds.topk(1, dim=-1, largest=True, sorted=True)
        # preds_index = preds_index.view(-1, self.converter.batch_max_length)
        # print("preds shape: ", preds.shape)
        # print("preds_index: ", preds_index)
        # preds_str = self.converter.decode(preds_index, length_for_pred)
        if settings.MODEL == 'vitstr':
            preds_str = self.converter.decode(preds_index[:, 1:], length_for_pred)
        elif settings.MODEL == 'srn':
            _, preds_index = preds.max(2)
            length_for_pred = torch.IntTensor([self.opt.batch_max_length] * bs).to(self.device)
            preds_str = self.converter.decode(preds_index, length_for_pred)
            # sys.exit()
        elif settings.MODEL == 'parseq':
            preds_str, confidence = self.model.tokenizer.decode(preds)

        # Confidence score
        # ARGMAX calculation
        sum = torch.FloatTensor([0]*bs).to(self.device)
        if self.opt.confidence_mode == 0:
            preds_prob = F.softmax(preds, dim=2)
            # preds_prob shape:  torch.Size([1, 25, 96])
            preds_max_prob, preds_max_idx = preds_prob.max(dim=2)
            # preds_max_prob shape:  torch.Size([1, 25])
            confidence_score_list = []
            count = 0
            for one_hot_preds, pred, pred_max_prob in zip(preds_prob, preds_str, preds_max_prob):
                if settings.MODEL == 'vitstr' or settings.MODEL == 'srn':
                    if self.enableSingleCharAttrAve:
                        one_hot = one_hot_preds[self.singleChar, :]
                        # pred = pred[self.singleChar]
                        pred_max_prob = pred_max_prob[self.singleChar]
                    else:
                        pred_EOS = pred.find('[s]')
                        pred = pred[:pred_EOS]
                        pred_max_prob = pred_max_prob[:pred_EOS]

                    # if pred_max_prob.shape[0] == 0: continue
                    if self.enableSingleCharAttrAve:
                        sum = one_hot
                        # sum shape:  torch.Size([96])
                        sum = sum.unsqueeze(0)
                    else:
                        if self.opt.scorer == "cumprod":
                            confidence_score = pred_max_prob.cumprod(dim=0)[-1] ### Maximum is 1
                            sum[count] += confidence_score
                        elif self.opt.scorer == "mean":
                            confidence_score = torch.mean(pred_max_prob) ### Maximum is 1
                            sum[count] += confidence_score
                        sum = sum.unsqueeze(1)

                elif settings.MODEL == 'parseq':
                    if self.enableSingleCharAttrAve:
                        one_hot = one_hot_preds[self.singleChar, :]
                        # pred = pred[self.singleChar]
                        pred_max_prob = pred_max_prob[self.singleChar]
                    else:
                        pred_EOS = len(pred) # Predition string already has no EOS, fully intact
                        pred_max_prob = pred_max_prob[:pred_EOS]

                    # if pred_max_prob.shape[0] == 0: continue
                    if self.enableSingleCharAttrAve:
                        sum = one_hot
                        # sum shape:  torch.Size([96])
                        sum = sum.unsqueeze(0)
                    else:
                        if self.opt.scorer == "cumprod":
                            confidence_score = pred_max_prob.cumprod(dim=0)[-1] ### Maximum is 1
                            sum[count] += confidence_score
                        elif self.opt.scorer == "mean":
                            confidence_score = torch.mean(pred_max_prob) ### Maximum is 1
                            sum[count] += confidence_score
                        sum = sum.unsqueeze(1)
                count += 1
            # return sum.detach().cpu().numpy()
            # print("sumshape: ", sum.shape)
        elif self.opt.confidence_mode == 1:
            preds_prob = F.softmax(preds, dim=2)
            ### Predicted indices
            preds_max_prob = torch.argmax(preds_prob, 2)
            # print("preds_max_prob shape: ", preds_max_prob.shape)
            ### Ground truth indices
            gtIndices, _ = self.converter.encode([self.gtStr for i in range(0,preds_prob.shape[0])], batch_max_length=self.opt.batch_max_length-1)
            # print("gtIndices shape: ", gtIndices.shape)
            ### Acquire levenstein distance
            m = torch.tensor([preds_prob.shape[1] for i in range(0, gtIndices.shape[0])], dtype=torch.float).to(self.device)
            n = torch.tensor([preds_prob.shape[1] for i in range(0, gtIndices.shape[0])], dtype=torch.float).to(self.device)
            # print("m: ", m)
            # print("preds_max_prob dtype: ", preds_max_prob.dtype)
            # print("gtIndices dtype: ", gtIndices.dtype)
            preds_max_prob = preds_max_prob.type(torch.float)
            gtIndices = gtIndices.type(torch.float)
            r = levenshtein_distance(preds_max_prob.to(self.device), gtIndices.to(self.device), n, m, torch.cat([self.blank, self.separator]), torch.empty([], dtype=torch.float).to(self.device))
            # print("r shape: ", r.shape)
            # confidence_score_list = []
            # count = 0
            # for pred, pred_max_prob in zip(preds_str, preds_max_prob):
            #     if 'Attn' in self.opt.Prediction:
            #         pred_EOS = pred.find('[s]')
            #         pred = pred[:pred_EOS]
            #         pred_max_prob = pred_max_prob[:pred_EOS] ### Use score of all letters
            #         # pred_max_prob = pred_max_prob[0:1] ### Use score of first letter only
            #         if pred_max_prob.shape[0] == 0: continue
            #         confidence_score = pred_max_prob.cumprod(dim=0)[-1]
            #         sum[count] += confidence_score
            #     count += 1
            # return sum.detach().cpu().numpy()
            # print("sumshape: ", sum.shape)
            # sum = sum.unsqueeze(1)
            rSoft = F.softmax(r[:,2].type(torch.float))
            # rSoft = rSoft.contiguous()
            rNorm = rSoft.max()-rSoft
            sum = rNorm.unsqueeze(1)
        return sum

class Model(nn.Module):

    def __init__(self, opt):
        super(Model, self).__init__()
        self.opt = opt
        self.stages = {'Trans': opt.Transformation, 'Feat': opt.FeatureExtraction,
                       'Seq': opt.SequenceModeling, 'Pred': opt.Prediction}

        """ Transformation """
        if opt.Transformation == 'TPS':
            self.Transformation = TPS_SpatialTransformerNetwork(
                F=opt.num_fiducial, I_size=(opt.imgH, opt.imgW), I_r_size=(opt.imgH, opt.imgW), I_channel_num=opt.input_channel)
        else:
            print('No Transformation module specified')

        """ FeatureExtraction """
        if opt.FeatureExtraction == 'VGG':
            self.FeatureExtraction = VGG_FeatureExtractor(opt.input_channel, opt.output_channel)
        elif opt.FeatureExtraction == 'RCNN':
            self.FeatureExtraction = RCNN_FeatureExtractor(opt.input_channel, opt.output_channel)
        elif opt.FeatureExtraction == 'ResNet':
            self.FeatureExtraction = ResNet_FeatureExtractor(opt.input_channel, opt.output_channel)
            self.AdaptiveAvgPool = nn.AdaptiveAvgPool2d((None, 1))  # Transform final (imgH/16-1) -> 1
        elif opt.FeatureExtraction == 'AsterRes':
            self.FeatureExtraction = ResNet_ASTER(opt.input_channel, opt.output_channel)
        elif opt.FeatureExtraction == 'ResnetFpn':
            self.FeatureExtraction = ResNet_FPN()
        else:
            raise Exception('No FeatureExtraction module specified')
        self.FeatureExtraction_output = opt.output_channel  # int(imgH/16-1) * 512


        """ Sequence modeling"""
        if opt.SequenceModeling == 'BiLSTM':
            self.SequenceModeling = nn.Sequential(
                BidirectionalLSTM(self.FeatureExtraction_output, opt.hidden_size, opt.hidden_size),
                BidirectionalLSTM(opt.hidden_size, opt.hidden_size, opt.hidden_size))
            self.SequenceModeling_output = opt.hidden_size
        elif opt.SequenceModeling == 'Bert':
            cfg = Config()
            cfg.dim = opt.output_channel; cfg.dim_c = opt.output_channel              # 降维减少计算量
            cfg.p_dim = opt.position_dim                        # 一张图片cnn编码之后的特征序列长度
            cfg.max_vocab_size = opt.batch_max_length + 1                # 一张图片中最多的文字个数, +1 for EOS
            cfg.len_alphabet = opt.alphabet_size                # 文字的类别个数
            self.SequenceModeling = Bert_Ocr(cfg)
        elif opt.SequenceModeling == 'SRN':
            self.SequenceModeling = Transforme_Encoder(n_layers=2, n_position=opt.position_dim)
            # self.SequenceModeling = Torch_transformer_encoder(n_layers=2, n_position=opt.position_dim)
            self.SequenceModeling_output = 512
        else:
            print('No SequenceModeling module specified')
            self.SequenceModeling_output = self.FeatureExtraction_output

        """ Prediction """
        if opt.Prediction == 'CTC':
            self.Prediction = nn.Linear(self.SequenceModeling_output, opt.num_class)
        elif opt.Prediction == 'Attn':
            self.Prediction = Attention(self.SequenceModeling_output, opt.hidden_size, opt.num_class)
        elif opt.Prediction == 'Bert_pred':
            pass
        elif opt.Prediction == 'SRN':
            self.Prediction = SRN_Decoder(n_position=opt.position_dim, N_max_character=opt.batch_max_character + 1, n_class=opt.alphabet_size)
        else:
            raise Exception('Prediction is neither CTC or Attn')

    def forward(self, input, text=None, is_train=True):
        """ Transformation stage """
        if not self.stages['Trans'] == "None":
            input = self.Transformation(input)


        """ Feature extraction stage """
        visual_feature = self.FeatureExtraction(input)
        # if self.stages['Feat'] == 'AsterRes' or self.stages['Feat'] == 'ResnetFpn':
        if self.stages['Feat'] == 'AsterRes' or self.stages['Feat'] == 'ResnetFpn':
            b, c, h, w = visual_feature.shape
            visual_feature = visual_feature.permute(0, 1, 3, 2)
            visual_feature = visual_feature.contiguous().view(b, c, -1)
            visual_feature = visual_feature.permute(0, 2, 1)    # batch, seq, feature
        else:
            visual_feature = self.AdaptiveAvgPool(visual_feature.permute(0, 3, 1, 2))  # [b, c, h, w] -> [b, w, c, h]
            visual_feature = visual_feature.squeeze(3)


        """ Sequence modeling stage """
        if self.stages['Seq'] == 'BiLSTM':
            contextual_feature = self.SequenceModeling(visual_feature)
        elif self.stages['Seq'] == 'Bert':
            pad_mask = text
            contextual_feature = self.SequenceModeling(visual_feature, pad_mask)
        elif self.stages['Seq'] == 'SRN':
            contextual_feature = self.SequenceModeling(visual_feature, src_mask=None)[0]
        else:
            contextual_feature = visual_feature  # for convenience. this is NOT contextually modeled by BiLSTM


        """ Prediction stage """
        if self.stages['Pred'] == 'CTC':
            prediction = self.Prediction(contextual_feature.contiguous())
        elif self.stages['Pred'] == 'Bert_pred':
            prediction = contextual_feature
        elif self.stages['Pred'] == 'SRN':
            prediction = self.Prediction(contextual_feature)
        else:
            prediction = self.Prediction(contextual_feature.contiguous(), text, is_train, batch_max_length=self.opt.batch_max_length)

        return prediction