File size: 12,155 Bytes
5f5c8d7
7978529
dffa77d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b4759d0
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dffa77d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7978529
6ccedcd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7978529
 
 
31cf632
 
 
 
 
 
 
 
 
 
 
918d78a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2fbee6c
16c1073
77ae6a6
16c1073
 
 
 
7978529
 
 
 
 
 
77ae6a6
7978529
 
77ae6a6
 
 
 
7978529
 
dffa77d
 
 
6ccedcd
dffa77d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c23c07f
dffa77d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a2001aa
 
dffa77d
 
 
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
import streamlit as st
from PIL import Image
import settings
import captum
import numpy as np
import torch
import torch.nn.functional as F
import torch.backends.cudnn as cudnn
from utils import get_args
from utils import CTCLabelConverter, AttnLabelConverter, Averager, TokenLabelConverter
import string
import time
import sys
from dataset import hierarchical_dataset, AlignCollate
import validators
from model import Model, STRScore
from PIL import Image
from lime.wrappers.scikit_image import SegmentationAlgorithm
from captum._utils.models.linear_model import SkLearnLinearModel, SkLearnRidge
import random
import os
from skimage.color import gray2rgb
import pickle
from train_shap_corr import getPredAndConf
import re
from captum_test import acquire_average_auc, saveAttrData
import copy
from skimage.color import gray2rgb
from matplotlib import pyplot as plt
from torchvision import transforms

from captum.attr import (
    GradientShap,
    DeepLift,
    DeepLiftShap,
    IntegratedGradients,
    LayerConductance,
    NeuronConductance,
    NoiseTunnel,
    Saliency,
    InputXGradient,
    GuidedBackprop,
    Deconvolution,
    GuidedGradCam,
    FeatureAblation,
    ShapleyValueSampling,
    Lime,
    KernelShap
)

from captum.metrics import (
    infidelity,
    sensitivity_max
)

from captum.attr._utils.visualization import visualize_image_attr

device = torch.device('cpu')
opt = get_args(is_train=False)

if opt.sensitive:
    opt.character = string.printable[:-6]  # same with ASTER setting (use 94 char).

cudnn.benchmark = True
cudnn.deterministic = True
# opt.num_gpu = torch.cuda.device_count()

# combineBestDataXAI(opt)
# acquire_average_auc(opt)
# acquireSingleCharAttrAve(opt)
modelName = "parseq"
opt.modelName = modelName
# opt.eval_data = "datasets/data_lmdb_release/evaluation"

if modelName=="vitstr":
    opt.benchmark_all_eval = True
    opt.Transformation = "None"
    opt.FeatureExtraction = "None"
    opt.SequenceModeling = "None"
    opt.Prediction = "None"
    opt.Transformer = True
    opt.sensitive = True
    opt.imgH = 224
    opt.imgW = 224
    opt.data_filtering_off = True
    opt.TransformerModel= "vitstr_base_patch16_224"
    opt.saved_model = "pretrained/vitstr_base_patch16_224_aug.pth"
    opt.batch_size = 1
    opt.workers = 0
    opt.scorer = "mean"
    opt.blackbg = True
elif modelName=="parseq":
    opt.benchmark_all_eval = True
    opt.Transformation = "None"
    opt.FeatureExtraction = "None"
    opt.SequenceModeling = "None"
    opt.Prediction = "None"
    opt.Transformer = True
    opt.sensitive = True
    opt.imgH = 32
    opt.imgW = 128
    opt.data_filtering_off = True
    opt.batch_size = 1
    opt.workers = 0
    opt.scorer = "mean"
    opt.blackbg = True

segmentation_fn = SegmentationAlgorithm('quickshift', kernel_size=4,
                                        max_dist=200, ratio=0.2,
                                        random_seed=random.randint(0, 1000))

if modelName=="vitstr":
    if opt.Transformer:
        converter = TokenLabelConverter(opt)
    elif 'CTC' in opt.Prediction:
        converter = CTCLabelConverter(opt.character)
    else:
        converter = AttnLabelConverter(opt.character)
    opt.num_class = len(converter.character)
    if opt.rgb:
        opt.input_channel = 3
    model_obj = Model(opt)

    model = torch.nn.DataParallel(model_obj).to(device)
    modelCopy = copy.deepcopy(model)

    scoring_singlechar = STRScore(opt=opt, converter=converter, device=device, enableSingleCharAttrAve=True)
    super_pixel_model_singlechar = torch.nn.Sequential(
        # super_pixler,
        # numpy2torch_converter,
        modelCopy,
        scoring_singlechar
    ).to(device)
    modelCopy.eval()
    scoring_singlechar.eval()
    super_pixel_model_singlechar.eval()

    # Single Char Attribution Averaging
    # enableSingleCharAttrAve - set to True
    scoring = STRScore(opt=opt, converter=converter, device=device)
    super_pixel_model = torch.nn.Sequential(
        # super_pixler,
        # numpy2torch_converter,
        model,
        scoring
    ).to(device)
    model.eval()
    scoring.eval()
    super_pixel_model.eval()

elif modelName=="parseq":
    model = torch.hub.load('baudm/parseq', 'parseq', pretrained=True)
    # checkpoint = torch.hub.load_state_dict_from_url('https://github.com/baudm/parseq/releases/download/v1.0.0/parseq-bb5792a6.pt', map_location="cpu")
    # # state_dict = {key.replace("module.", ""): value for key, value in checkpoint["state_dict"].items()}
    # model.load_state_dict(checkpoint)
    model = model.to(device)
    model_obj = model
    converter = TokenLabelConverter(opt)
    modelCopy = copy.deepcopy(model)

    scoring_singlechar = STRScore(opt=opt, converter=converter, device=device, enableSingleCharAttrAve=True, model=modelCopy)
    super_pixel_model_singlechar = torch.nn.Sequential(
        # super_pixler,
        # numpy2torch_converter,
        modelCopy,
        scoring_singlechar
    ).to(device)
    modelCopy.eval()
    scoring_singlechar.eval()
    super_pixel_model_singlechar.eval()

    # Single Char Attribution Averaging
    # enableSingleCharAttrAve - set to True
    scoring = STRScore(opt=opt, converter=converter, device=device, model=model)
    super_pixel_model = torch.nn.Sequential(
        # super_pixler,
        # numpy2torch_converter,
        model,
        scoring
    ).to(device)
    model.eval()
    scoring.eval()
    super_pixel_model.eval()

if opt.blackbg:
    shapImgLs = np.zeros(shape=(1, 1, 224, 224)).astype(np.float32)
    trainList = np.array(shapImgLs)
    background = torch.from_numpy(trainList).to(device)

# x = st.slider('Select a value')
# st.write(x, 'squared is', x * x)

### Returns the mean for each segmentation having shape as the same as the input
### This function can only one attribution image at a time
def averageSegmentsOut(attr, segments):
    averagedInput = torch.clone(attr)
    sortedDict = {}
    for x in np.unique(segments):
        segmentMean = torch.mean(attr[segments == x][:])
        sortedDict[x] = float(segmentMean.detach().cpu().numpy())
        averagedInput[segments == x] = segmentMean
    return averagedInput, sortedDict

### Acquire pixelwise attributions and replace them with ranked numbers averaged
### across segmentation with the largest contribution having the largest number
### and the smallest set to 1, which is the minimum number.
### attr - original attribution
### segm - image segmentations
def rankedAttributionsBySegm(attr, segm):
    aveSegmentations, sortedDict = averageSegmentsOut(attr[0,0], segm)
    totalSegm = len(sortedDict.keys()) # total segmentations
    sortedKeys = [k for k, v in sorted(sortedDict.items(), key=lambda item: item[1])]
    sortedKeys = sortedKeys[::-1] ### A list that should contain largest to smallest score
    currentRank = totalSegm
    rankedSegmImg = torch.clone(attr)
    for totalSegToHide in range(0, len(sortedKeys)):
        currentSegmentToHide = sortedKeys[totalSegToHide]
        rankedSegmImg[0,0][segm == currentSegmentToHide] = currentRank
        currentRank -= 1
    return rankedSegmImg

labels = st.text_input('Drag one of the images from the right towards the box below (or you can choose your own image). '
'You need to put the text of the image in the textbox below first (e.g. GAS) before dragging the image.')

image = Image.open('demo_image/demo_gas.jpg') #Brand logo image (optional)
image2 = Image.open('demo_image/demo_shakeshack.jpg') #Brand logo image (optional)
image3 = Image.open('demo_image/demo_ronaldo.jpg') #Brand logo image (optional)
image4 = Image.open('demo_image/demo_car.jpg') #Brand logo image (optional)
#Create two columns with different width
col1, col2 = st.columns( [0.8, 0.2])
with col1:               # To display the header text using css style
    st.markdown(""" <style> .font {
    font-size:35px ; font-family: 'Cooper Black'; color: #FF9633;}
    </style> """, unsafe_allow_html=True)
    st.markdown('<p class="font">STRExp (Explaining PARSeq STR Model)...</p>', unsafe_allow_html=True)
with col2:               # To display brand logo
    st.image(image,  width=150)
    st.image(image2,  width=150)
    st.image(image3,  width=150)
    st.image(image4,  width=150)

uploaded_file = st.file_uploader("Choose a file", type=["png", "jpg"])
if uploaded_file is not None:
    # To read file as bytes:
    bytes_data = uploaded_file.getvalue()
    pilImg = Image.open(uploaded_file)
    pilImg = pilImg.resize((opt.imgW, opt.imgH))

    orig_img_tensors = transforms.ToTensor()(pilImg).unsqueeze(0)
    img1 = orig_img_tensors.to(device)
    # image_tensors = ((torch.clone(orig_img_tensors) + 1.0) / 2.0) * 255.0
    image_tensors = torch.mean(orig_img_tensors, dim=1).unsqueeze(0).unsqueeze(0)
    imgDataDict = {}
    img_numpy = image_tensors.cpu().detach().numpy()[0] ### Need to set batch size to 1 only
    if img_numpy.shape[0] == 1:
        img_numpy = gray2rgb(img_numpy[0])
    # print("img_numpy shape: ", img_numpy.shape) # (1, 32, 128, 3)
    segmOutput = segmentation_fn(img_numpy[0])

    results_dict = {}
    aveAttr = []
    aveAttr_charContrib = []

    # labels: RONALDO
    segmDataNP = segmOutput
    img1.requires_grad = True
    bgImg = torch.zeros(img1.shape).to(device)

    # preds = model(img1, seqlen=converter.batch_max_length)
    input = img1
    origImgNP = torch.clone(orig_img_tensors).detach().cpu().numpy()[0][0] # (1, 1, 224, 224)
    origImgNP = gray2rgb(origImgNP)
    charOffset = 0
    img1 = transforms.Normalize(0.5, 0.5)(img1) # Between -1 to 1
    labels = labels.lower()
    target = converter.encode([labels])

    ### Local explanations only
    collectedAttributions = []
    for charIdx in range(0, len(labels)):
        scoring_singlechar.setSingleCharOutput(charIdx + charOffset)
        gtClassNum = target[0][charIdx + charOffset]

        gs = GradientShap(super_pixel_model_singlechar)
        baseline_dist = torch.zeros((1, 3, opt.imgH, opt.imgW))
        baseline_dist = baseline_dist.to(device)
        attributions = gs.attribute(input, baselines=baseline_dist, target=0)
        collectedAttributions.append(attributions)
    aveAttributions = torch.mean(torch.cat(collectedAttributions,dim=0), dim=0).unsqueeze(0)
    # if not torch.isnan(aveAttributions).any():
    #     rankedAttr = rankedAttributionsBySegm(aveAttributions, segmDataNP)
    #     rankedAttr = rankedAttr.detach().cpu().numpy()[0][0]
    #     rankedAttr = gray2rgb(rankedAttr)
    #     mplotfig, _ = visualize_image_attr(rankedAttr, origImgNP, method='blended_heat_map', cmap='RdYlGn')
    #     mplotfig.savefig(outputDir + '{}_shapley_l.png'.format(nameNoExt))
    #     mplotfig.clear()
    #     plt.close(mplotfig)

    ### Local Sampling
    gs = GradientShap(super_pixel_model)
    baseline_dist = torch.zeros((1, 3, opt.imgH, opt.imgW))
    baseline_dist = baseline_dist.to(device)
    attributions = gs.attribute(input, baselines=baseline_dist, target=0)
    # if not torch.isnan(attributions).any():
    #     collectedAttributions.append(attributions)
    #     rankedAttr = rankedAttributionsBySegm(attributions, segmDataNP)
    #     rankedAttr = rankedAttr.detach().cpu().numpy()[0][0]
    #     rankedAttr = gray2rgb(rankedAttr)
    #     mplotfig, _ = visualize_image_attr(rankedAttr, origImgNP, method='blended_heat_map', cmap='RdYlGn')
    #     mplotfig.savefig(outputDir + '{}_shapley.png'.format(nameNoExt))
    #     mplotfig.clear()
    #     plt.close(mplotfig)

    ### Global + Local context
    aveAttributions = torch.mean(torch.cat(collectedAttributions,dim=0), dim=0).unsqueeze(0)
    if not torch.isnan(aveAttributions).any():
        rankedAttr = rankedAttributionsBySegm(aveAttributions, segmDataNP)
        rankedAttr = rankedAttr.detach().cpu().numpy()[0][0]
        rankedAttr = gray2rgb(rankedAttr)
        mplotfig, _ = visualize_image_attr(rankedAttr, origImgNP, method='blended_heat_map', cmap='RdYlGn')
        # fig = mplotfig.figure(figsize=(8,8))
        st.pyplot(mplotfig)
        # mplotfig.savefig(outputDir + '{}_shapley_gl.png'.format(nameNoExt))
        # mplotfig.clear()
        # plt.close(mplotfig)