File size: 16,385 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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
"""
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.nn as nn

from modules_trba.transformation import TPS_SpatialTransformerNetwork
from modules_trba.feature_extraction import VGG_FeatureExtractor, RCNN_FeatureExtractor, ResNet_FeatureExtractor
from modules_trba.sequence_modeling import BidirectionalLSTM
from modules_trba.prediction import Attention
import numpy as np
import torch
import torch.nn.functional as F
import random
import copy
# from torch_edit_distance import levenshtein_distance

class STRScore(nn.Module):
    def __init__(self, opt, converter, device, gtStr="", enableSingleCharAttrAve=False):
        super(STRScore, self).__init__()
        self.opt = opt
        self.converter = converter
        self.device = device
        self.gtStr = gtStr
        self.enableSingleCharAttrAve = enableSingleCharAttrAve
        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):
        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
        length_for_pred = torch.IntTensor([self.opt.batch_max_length] * bs).to(self.device)
        if 'CTC' in self.opt.Prediction:
            # Calculate evaluation loss for CTC decoder.
            preds_size = torch.FloatTensor([preds.size(1)] * bs)
            if self.opt.baiduCTC:
                _, preds_index = preds.max(2)
                preds_index = preds_index.view(-1)
            else:
                _, preds_index = preds.max(2)
            # print("preds_index shape: ", preds_index.shape)
            preds_str = self.converter.decode(preds_index.data, preds_size.data)
            # preds_str = self.converter.decode(preds_index, length_for_pred)
            preds = preds.log_softmax(2).permute(1, 0, 2)
        else:
            preds = preds[:, :text_for_loss_length, :]

            # select max probabilty (greedy decoding) then decode index to character
            _, preds_index = preds.max(2)
            # print("preds shape: ", preds.shape)
            # print("preds_index: ", preds_index)
            preds_str = self.converter.decode(preds_index, length_for_pred)
            # print("preds_str: ", preds_str)

        # Confidence score
        # ARGMAX calculation
        sum = torch.FloatTensor([0]*bs).to(self.device)
        if self.enableSingleCharAttrAve:
            sum = torch.zeros((bs, preds.shape[2])).to(self.device)
        if self.opt.confidence_mode == 0:
            preds_prob = F.softmax(preds, dim=2)
            # print("preds_prob shape: ", preds_prob.shape)
            preds_max_prob, _ = preds_prob.max(dim=2)
            # print("preds_max_prob shape: ", preds_max_prob.shape)
            confidence_score_list = []
            count = 0
            for one_hot_preds, pred, pred_max_prob in zip(preds_prob, preds_str, preds_max_prob):
                if 'Attn' in self.opt.Prediction:
                    if self.enableSingleCharAttrAve:
                        one_hot = one_hot_preds[self.singleChar, :]
                        sum[count] = one_hot
                    else:
                        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
                        if self.opt.scorer == "cumprod":
                            confidence_score = pred_max_prob.cumprod(dim=0)[-1] ### Maximum is 1
                        elif self.opt.scorer == "mean":
                            confidence_score = torch.mean(pred_max_prob) ### Maximum is 1
                        sum[count] += confidence_score
                        sum = sum.unsqueeze(1)
                elif 'CTC' in self.opt.Prediction:
                    if self.opt.scorer == "cumprod":
                        confidence_score = pred_max_prob.cumprod(dim=0)[-1] ### Maximum is 1
                    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)
            print("sum shape: ", sum.shape)
        return sum

class SuperPixler(nn.Module):
  def __init__(self, n_super_pixel, imageList, super_pixel_width, super_pixel_height, opt):
    super(SuperPixler, self).__init__()
    self.opt = opt
    self.imageList = imageList
    self.n_super_pixel = n_super_pixel
    # self.image = image
    # self.image = image.transpose(2, 0, 1) # model expects images in BRG, not RGB, so transpose color channels
    # self.mean_color = self.image.mean()
    # self.image = np.expand_dims(self.image, axis=0)
    self.super_pixel_width = super_pixel_width
    self.super_pixel_height = super_pixel_height
  # def setImage(self, image):
  #     self.image = image
  #     self.image_height = image.shape[2]
  #     self.image_width = image.shape[3]
  def sampleImages(self, size):
        newImgList = []
        for i in range(0, size):
            randIdx = random.randint(0, len(self.imageList)-1)
            newImgList.append(copy.deepcopy(self.imageList[randIdx]))
        return np.array(newImgList)
  def forward(self, x):
    """
    In the forward step we accept the super pixel masks and transform them to a batch of images
    """
    # x = self.sampleMasks(image.shape[0])
    image = self.sampleImages(x.shape[0])
    self.image = image
    self.image_height = image.shape[2]
    self.image_width = image.shape[3]
    self.mean_color = self.image.mean()
    # self.mean_color = self.image.mean(axis=(1,2,3))
    # pixeled_image = np.repeat(self.image.copy(), x.shape[0], axis=0)# WARNING:
    pixeled_image = self.image.copy()
    # print("pixeled_image shape: ", pixeled_image.shape)
    # print("x shape: ", x.shape)
    for i, super_pixel in enumerate(x.T):
        images_to_pixelate = [bool(p) for p in super_pixel]
        # print("super_pixel shape: ", super_pixel.shape)
        # print("images_to_pixelate len: ", len(images_to_pixelate))
        # print("i: {}, superPix: {}, images_to_pixelate: {}".format(i, super_pixel, images_to_pixelate))
        x = (i*self.super_pixel_height//self.image_height)*self.super_pixel_width
        y = i*self.super_pixel_height%self.image_height
        ### Reshape image means since it has n-dim size, not a single number. Need to repeat sideways.
        # origShapeToApply = pixeled_image[images_to_pixelate,:,y:y+self.super_pixel_height,x:x+self.super_pixel_width].shape
        # print("origShapeToApply: ", origShapeToApply)
        # mean_color_spec = np.tile(self.mean_color, origShapeToApply[1:]) #
        # mean_color_spec = np.reshape(mean_color_spec, origShapeToApply[::-1]).T ### reshape to reversed
        ### Apply image means
        pixeled_image[images_to_pixelate,:,y:y+self.super_pixel_height,x:x+self.super_pixel_width] = self.mean_color
    return pixeled_image

class CastNumpy(nn.Module):
    def __init__(self, device):
        super(CastNumpy, self).__init__()
        self.device = device

    def forward(self, image):
        """
        In the forward function we accept the inputs and cast them to a pytorch tensor
        """

        image = np.ascontiguousarray(image)
        image = torch.from_numpy(image).to(self.device)
        if image.ndimension() == 3:
          image = image.unsqueeze(0)
        image_half = image.half()
        return image_half.float()

class Model(nn.Module):

    def __init__(self, opt, device, feature_ext_outputs=None):
        super(Model, self).__init__()
        self.opt = opt
        self.device = device
        self.gtText = None
        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)
        else:
            raise Exception('No FeatureExtraction module specified')
        self.FeatureExtraction_output = opt.output_channel  # int(imgH/16-1) * 512
        self.AdaptiveAvgPool = nn.AdaptiveAvgPool2d((None, 1))  # Transform final (imgH/16-1) -> 1

        """ 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
        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)
        else:
            raise Exception('Prediction is neither CTC or Attn')

        ### Set feature map outputter modules

        if opt.output_feat_maps:
            feature_ext_outputs.set_feature_ext(self.FeatureExtraction)
            ### Define hooks
            feature_ext_outputs = feature_ext_outputs
            totalCNNLayers = 0
            idxToOutput = []
            layersList = []

            layerCount = 0
            # print("list(self.FeatureExtraction._modules.items()): ", list(self.FeatureExtraction._modules.items()))
            # print("list(self.FeatureExtraction.ConvNet_modules.items())[0][1]: ", list(self.FeatureExtraction.ConvNet._modules.items())[0][1])
            first_layer = list(self.FeatureExtraction.ConvNet._modules.items())[0][1]
            first_layer.register_backward_hook(feature_ext_outputs.append_first_grads)
            for layer in self.FeatureExtraction.modules():
                if isinstance(layer, nn.Conv2d):
                    layerCount += 1
                    if layerCount >= opt.min_layer_out and layerCount <= opt.max_layer_out:
                        layer.register_forward_hook(feature_ext_outputs.append_layer_out)
                        layer.register_backward_hook(feature_ext_outputs.append_grad_out)
    # def get_feature_ext(self):
    #     return self.FeatureExtraction
    def setGTText(self, text):
        self.gtText = text
    def forward(self, input, text="", is_train=True):
        if self.opt.is_shap:
            text = torch.LongTensor(input.shape[0], self.opt.batch_max_length + 1).fill_(0).to(self.device)
        elif self.gtText is not None:
            text = self.gtText
        else:
            text = torch.LongTensor(input.shape[0], self.opt.batch_max_length + 1).fill_(0).to(self.device)
            # print("text shape: ", text.shape) (1,26)
        tpsOut = input.contiguous()
        """ Transformation stage """
        if not self.stages['Trans'] == "None":
            tpsOut = self.Transformation(tpsOut)
        # print("Transformation feature shape: ", input.shape)

        """ Feature extraction stage """
        visual_feature = self.FeatureExtraction(tpsOut)
        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)
        # print("visual feature shape: ", visual_feature.shape)

        """ Sequence modeling stage """
        if self.stages['Seq'] == 'BiLSTM':
            contextual_feature = self.SequenceModeling(visual_feature)
        else:
            contextual_feature = visual_feature  # for convenience. this is NOT contextually modeled by BiLSTM
        # print("Sequence feature shape: ", contextual_feature.shape)

        """ Prediction stage """
        if self.stages['Pred'] == 'CTC':
            prediction = self.Prediction(contextual_feature.contiguous())
        else:
            prediction = self.Prediction(contextual_feature.contiguous(), text, is_train, batch_max_length=self.opt.batch_max_length)
        # print("prediction feature shape: ", prediction.shape)
        # return prediction, tpsOut
        return prediction