Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
S
SimpleHTR
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
GitLab community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Fabian Mersch
SimpleHTR
Commits
b2ac69ac
Commit
b2ac69ac
authored
6 years ago
by
Harald Scheidl
Browse files
Options
Downloads
Patches
Plain Diff
analyze pixel relevance using norm. histogram for p(x_i) distribution
parent
a78fbdf1
No related branches found
No related tags found
No related merge requests found
Changes
3
Show whitespace changes
Inline
Side-by-side
Showing
3 changed files
data/pixelRelevance.npy
+0
-0
0 additions, 0 deletions
data/pixelRelevance.npy
data/translationInvarianceTexts.pickle
+0
-0
0 additions, 0 deletions
data/translationInvarianceTexts.pickle
src/analyze.py
+44
-21
44 additions, 21 deletions
src/analyze.py
with
44 additions
and
21 deletions
data/pixelRelevance.npy
+
0
−
0
View file @
b2ac69ac
No preview for this file type
This diff is collapsed.
Click to expand it.
data/translationInvarianceTexts.pickle
0 → 100644
+
0
−
0
View file @
b2ac69ac
File added
This diff is collapsed.
Click to expand it.
src/analyze.py
+
44
−
21
View file @
b2ac69ac
...
...
@@ -3,6 +3,7 @@ from __future__ import print_function
import
sys
import
math
import
pickle
import
copy
import
numpy
as
np
import
cv2
...
...
@@ -12,12 +13,16 @@ from Model import Model, DecoderType
from
SamplePreprocessor
import
preprocess
class
FilePaths
:
# constants like filepaths
class
Constants
:
"
filenames and paths to data
"
fnCharList
=
'
../model/charList.txt
'
fnAnalyze
=
'
../data/analyze.png
'
fnPixelRelevance
=
'
../data/pixelRelevance.npy
'
fnTranslationInvariance
=
'
../data/translationInvariance.npy
'
fnTranslationInvarianceTexts
=
'
../data/translationInvarianceTexts.pickle
'
gtText
=
'
are
'
distribution
=
'
histogram
'
# 'histogram' or 'uniform'
def
odds
(
val
):
...
...
@@ -32,19 +37,28 @@ def analyzePixelRelevance():
"
simplified implementation of paper: Zintgraf et al - Visualizing Deep Neural Network Decisions: Prediction Difference Analysis
"
# setup model
model
=
Model
(
open
(
FilePath
s
.
fnCharList
).
read
(),
DecoderType
.
BestPath
,
mustRestore
=
True
)
model
=
Model
(
open
(
Constant
s
.
fnCharList
).
read
(),
DecoderType
.
BestPath
,
mustRestore
=
True
)
# read image and specify ground-truth text
img
=
cv2
.
imread
(
FilePath
s
.
fnAnalyze
,
cv2
.
IMREAD_GRAYSCALE
)
img
=
cv2
.
imread
(
Constant
s
.
fnAnalyze
,
cv2
.
IMREAD_GRAYSCALE
)
(
w
,
h
)
=
img
.
shape
assert
Model
.
imgSize
[
1
]
==
w
gt
=
'
are
'
# compute probability of gt text in original image
batch
=
Batch
([
g
t
],
[
preprocess
(
img
,
Model
.
imgSize
)])
batch
=
Batch
([
Constants
.
gtTex
t
],
[
preprocess
(
img
,
Model
.
imgSize
)])
(
_
,
probs
)
=
model
.
inferBatch
(
batch
,
calcProbability
=
True
,
probabilityOfGT
=
True
)
origProb
=
probs
[
0
]
grayValues
=
[
0
,
63
,
127
,
191
,
255
]
if
Constants
.
distribution
==
'
histogram
'
:
bins
=
[
0
,
31
,
95
,
159
,
223
,
255
]
(
hist
,
_
)
=
np
.
histogram
(
img
,
bins
=
bins
)
pixelProb
=
hist
/
sum
(
hist
)
elif
Constants
.
distribution
==
'
uniform
'
:
pixelProb
=
[
1.0
/
len
(
grayValues
)
for
_
in
grayValues
]
else
:
raise
Exception
(
'
unknown value for Constants.distribution
'
)
# iterate over all pixels in image
pixelRelevance
=
np
.
zeros
(
img
.
shape
,
np
.
float32
)
for
x
in
range
(
w
):
...
...
@@ -52,37 +66,35 @@ def analyzePixelRelevance():
# try a subset of possible grayvalues of pixel (x,y)
imgsMarginalized
=
[]
for
g
in
[
0
,
63
,
127
,
191
,
255
]
:
for
g
in
grayValues
:
imgChanged
=
copy
.
deepcopy
(
img
)
imgChanged
[
x
,
y
]
=
g
imgsMarginalized
.
append
(
preprocess
(
imgChanged
,
Model
.
imgSize
))
# put them all into one batch
batch
=
Batch
([
g
t
]
*
len
(
imgsMarginalized
),
imgsMarginalized
)
batch
=
Batch
([
Constants
.
gtTex
t
]
*
len
(
imgsMarginalized
),
imgsMarginalized
)
# compute probabilities
(
_
,
probs
)
=
model
.
inferBatch
(
batch
,
calcProbability
=
True
,
probabilityOfGT
=
True
)
# marginalize over pixel value (assume uniform distribution)
margProb
=
sum
(
probs
)
/
len
(
probs
)
margProb
=
sum
(
[
probs
[
i
]
*
pixelProb
[
i
]
for
i
in
range
(
len
(
grayValues
))]
)
pixelRelevance
[
x
,
y
]
=
weightOfEvidence
(
origProb
,
margProb
)
print
(
x
,
y
,
pixelRelevance
[
x
,
y
],
origProb
,
margProb
)
np
.
save
(
FilePaths
.
fnPixelRelevance
,
pixelRelevance
)
np
.
save
(
Constants
.
fnPixelRelevance
,
pixelRelevance
)
def
analyzeTranslationInvariance
():
# setup model
model
=
Model
(
open
(
FilePath
s
.
fnCharList
).
read
(),
DecoderType
.
BestPath
,
mustRestore
=
True
)
model
=
Model
(
open
(
Constant
s
.
fnCharList
).
read
(),
DecoderType
.
BestPath
,
mustRestore
=
True
)
# read image and specify ground-truth text
img
=
cv2
.
imread
(
FilePath
s
.
fnAnalyze
,
cv2
.
IMREAD_GRAYSCALE
)
img
=
cv2
.
imread
(
Constant
s
.
fnAnalyze
,
cv2
.
IMREAD_GRAYSCALE
)
(
w
,
h
)
=
img
.
shape
assert
Model
.
imgSize
[
1
]
==
w
gt
=
'
are
'
imgList
=
[]
for
dy
in
range
(
Model
.
imgSize
[
0
]
-
h
+
1
):
...
...
@@ -91,32 +103,43 @@ def analyzeTranslationInvariance():
imgList
.
append
(
preprocess
(
targetImg
,
Model
.
imgSize
))
# put images and gt texts into batch
batch
=
Batch
([
g
t
]
*
len
(
imgList
),
imgList
)
batch
=
Batch
([
Constants
.
gtTex
t
]
*
len
(
imgList
),
imgList
)
# compute probabilities
(
_
,
probs
)
=
model
.
inferBatch
(
batch
,
calcProbability
=
True
,
probabilityOfGT
=
True
)
np
.
save
(
FilePaths
.
fnTranslationInvariance
,
probs
)
(
texts
,
probs
)
=
model
.
inferBatch
(
batch
,
calcProbability
=
True
,
probabilityOfGT
=
True
)
# save results to file
f
=
open
(
Constants
.
fnTranslationInvarianceTexts
,
'
wb
'
)
pickle
.
dump
(
texts
,
f
)
f
.
close
()
np
.
save
(
Constants
.
fnTranslationInvariance
,
probs
)
def
showResults
():
# 1. pixel relevance
pixelRelevance
=
np
.
load
(
FilePath
s
.
fnPixelRelevance
)
pixelRelevance
=
np
.
load
(
Constant
s
.
fnPixelRelevance
)
plt
.
figure
(
'
Pixel relevance
'
)
plt
.
imshow
(
pixelRelevance
,
cmap
=
plt
.
cm
.
jet
,
vmin
=-
0.5
,
vmax
=
0.5
)
plt
.
colorbar
()
img
=
cv2
.
imread
(
FilePath
s
.
fnAnalyze
,
cv2
.
IMREAD_GRAYSCALE
)
img
=
cv2
.
imread
(
Constant
s
.
fnAnalyze
,
cv2
.
IMREAD_GRAYSCALE
)
plt
.
imshow
(
img
,
cmap
=
plt
.
cm
.
gray
,
alpha
=
.
4
)
# 2. translation invariance
probs
=
np
.
load
(
FilePaths
.
fnTranslationInvariance
)
probs
=
np
.
load
(
Constants
.
fnTranslationInvariance
)
f
=
open
(
Constants
.
fnTranslationInvarianceTexts
,
'
rb
'
)
texts
=
pickle
.
load
(
f
)
texts
=
[
'
%d:
'
%
i
+
texts
[
i
]
for
i
in
range
(
len
(
texts
))]
f
.
close
()
plt
.
figure
(
'
Translation invariance
'
)
plt
.
plot
(
probs
,
'
o-
'
)
plt
.
xlabel
(
'
horizontal translation
'
)
plt
.
ylabel
(
'
text probability
'
)
plt
.
xticks
(
np
.
arange
(
len
(
texts
)),
texts
,
rotation
=
'
vertical
'
)
plt
.
xlabel
(
'
horizontal translation and best path
'
)
plt
.
ylabel
(
'
text probability of
"
%s
"'
%
Constants
.
gtText
)
# show both plots
plt
.
show
()
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment