Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
I
Im
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
王肇一
Im
Commits
b5ea10ef
Commit
b5ea10ef
authored
Jan 12, 2020
by
王肇一
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
ready for interactive method
parent
e61c02b6
Show whitespace changes
Inline
Side-by-side
Showing
9 changed files
with
136 additions
and
97 deletions
+136
-97
CVBasedMethod.py
CVBasedMethod.py
+3
-6
UnetBasedMethod.py
UnetBasedMethod.py
+35
-0
kmeans.py
cvBasedMethod/kmeans.py
+2
-1
threshold.py
cvBasedMethod/threshold.py
+1
-0
util.py
cvBasedMethod/util.py
+0
-87
requirement.txt
requirement.txt
+2
-0
resCalc.py
resCalc.py
+91
-0
train.py
train.py
+1
-2
eval.py
utils/eval.py
+1
-1
No files found.
CVBasedMethod.py
View file @
b5ea10ef
...
...
@@ -6,7 +6,7 @@ import argparse
from
cvBasedMethod.util
import
*
from
cvBasedMethod.kmeans
import
kmeans
,
kmeans_back
from
cvBasedMethod.threshold
import
threshold
from
predict
import
predict
from
utils.
predict
import
predict
def
method_kmeans
(
imglist
,
core
=
5
):
...
...
@@ -32,7 +32,7 @@ def method_newThreshold(imglist, process = 8):
def
get_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'Identify targets from background by KMeans or Threshold'
,
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
parser
.
add_argument
(
'-m'
,
'--method'
,
metavar
=
'M'
,
type
=
int
,
default
=
3
,
parser
.
add_argument
(
'-m'
,
'--method'
,
metavar
=
'M'
,
type
=
int
,
default
=
0
,
help
=
'0 for KMeans; 1 for Threshold; 2 for newThreshold; 3 for Unet; 4 for further process'
,
dest
=
'method'
)
parser
.
add_argument
(
'-c'
,
'--core'
,
metavar
=
'C'
,
type
=
int
,
default
=
5
,
help
=
'Num of cluster'
,
dest
=
'core'
)
...
...
@@ -60,7 +60,3 @@ if __name__ == '__main__':
method_threshold
(
path
,
args
.
process
)
elif
args
.
method
==
2
:
method_newThreshold
(
path
,
args
.
process
)
\ No newline at end of file
elif
args
.
method
==
3
:
predict
(
path
,
args
.
load
,
args
.
scale
,
args
.
mask_threshold
)
for
exName
in
filter
(
lambda
x
:
x
!=
'.DS_Store'
,
os
.
listdir
(
'data/output'
)):
draw_bar
(
exName
,
os
.
listdir
(
'data/output/'
+
exName
+
'/csv'
))
UnetBasedMethod.py
View file @
b5ea10ef
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from
multiprocessing
import
Pool
from
tqdm
import
tqdm
import
argparse
import
os
from
utils.predict
import
predict
from
resCalc
import
draw_bar
def
get_args
():
parser
=
argparse
.
ArgumentParser
(
description
=
'A simple toolkit designed by Ulden'
,
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
parser
.
add_argument
(
'--step'
,
'-s'
,
type
=
int
,
default
=
1
,
help
=
"step 1: recognize; step 2: barcharts"
)
# Unet para
parser
.
add_argument
(
'--module'
,
'-m'
,
default
=
'data/module/MODEL.pth'
,
metavar
=
'FILE'
,
help
=
"Specify the file in which the model is stored"
)
parser
.
add_argument
(
'--mask-threshold'
,
'-t'
,
type
=
float
,
help
=
"Minimum probability value to consider a mask pixel white"
,
default
=
0.5
)
parser
.
add_argument
(
'--scale'
,
'-S'
,
type
=
float
,
help
=
"Scale factor for the input images"
,
default
=
0.5
)
return
parser
.
parse_args
()
if
__name__
==
'__main__'
:
args
=
get_args
()
path
=
[(
y
,
x
)
for
y
in
filter
(
lambda
x
:
x
!=
'.DS_Store'
,
os
.
listdir
(
'data/imgs'
))
for
x
in
filter
(
lambda
x
:
x
.
endswith
(
'.tif'
)
and
not
x
.
endswith
(
'dc.tif'
)
and
not
x
.
endswith
(
'DC.tif'
)
and
not
x
.
endswith
(
'dc .tif'
),
os
.
listdir
(
'data/imgs/'
+
y
))]
if
args
.
step
==
1
:
predict
(
path
,
args
.
load
,
args
.
scale
,
args
.
mask_threshold
)
for
exName
in
filter
(
lambda
x
:
x
!=
'.DS_Store'
,
os
.
listdir
(
'data/output'
)):
draw_bar
(
exName
,
os
.
listdir
(
'data/output/'
+
exName
+
'/csv'
))
cvBasedMethod/kmeans.py
View file @
b5ea10ef
...
...
@@ -2,7 +2,8 @@
# -*- coding:utf-8 -*-
from
sklearn.cluster
import
KMeans
from
cvBasedMethod.util
import
*
import
matplotlib.pyplot
as
plt
from
resCalc
import
save_img
,
calcRes
def
kmeans
(
pair
,
cluster_num
=
5
,
filter
=
'butter'
):
img_list
=
pre
(
pair
,
'clahe'
,
filter
)
...
...
cvBasedMethod/threshold.py
View file @
b5ea10ef
#!/usr/bin/env python
# -*- coding:utf-8 -*-
from
cvBasedMethod.util
import
*
from
resCalc
import
save_img
,
calcRes
def
threshold
(
pair
,
filter
=
'butter'
):
...
...
cvBasedMethod/util.py
View file @
b5ea10ef
...
...
@@ -2,12 +2,6 @@
# -*- coding:utf-8 -*-
import
numpy
as
np
import
cv2
as
cv
import
pandas
as
pd
import
matplotlib.pyplot
as
plt
import
seaborn
as
sns
import
logging
import
os
import
re
from
cvBasedMethod.filters
import
fft_mask
,
butterworth
def
remove_scratch
(
img
):
...
...
@@ -62,84 +56,4 @@ def pre(pair,even='clahe',filter='butter'):
return
{
'ori'
:
img
,
'even'
:
even
,
'filtered'
:
filtered
,
'denoise'
:
denoise
,
'blr'
:
blr
}
def
save_img
(
img_list
,
dir
,
name
):
num
=
len
(
img_list
)
plt
.
figure
(
figsize
=
(
100
,
20
))
plt
.
suptitle
(
name
)
for
i
,
title
in
zip
(
range
(
num
),
img_list
):
img
=
img_list
[
title
]
plt
.
subplot
(
1
,
num
,
i
+
1
)
plt
.
title
(
title
)
plt
.
imshow
(
img
,
'gray'
)
try
:
os
.
makedirs
(
'data/output/'
+
dir
+
'/graph'
)
except
:
logging
.
info
(
'Existing dir: data/output/'
+
dir
)
plt
.
savefig
(
'data/output/'
+
dir
+
'/graph/'
+
name
[:
-
4
]
+
'.png'
)
plt
.
close
()
def
calcRes
(
img
,
mask
,
dir
=
'default_output'
,
name
=
'output'
):
dic
=
get_subarea_infor
(
img
,
mask
)
df
=
pd
.
DataFrame
(
dic
)
try
:
os
.
makedirs
(
'data/output/'
+
dir
+
'/csv'
)
except
:
logging
.
info
(
'Existing dir: out/'
+
dir
+
'/csv'
)
if
len
(
df
)
!=
0
:
df
.
to_csv
(
'data/output/'
+
dir
+
'/csv/'
+
name
+
'.csv'
,
index
=
False
)
def
draw_bar
(
exName
,
names
):
df
=
pd
.
DataFrame
(
columns
=
(
'class'
,
'perc'
,
'Label'
,
'Area'
,
'Mean'
,
'Std'
,
'BackMean'
,
'BackStd'
))
for
name
in
names
:
tmp
=
pd
.
read_csv
(
'data/output/'
+
exName
+
'/csv/'
+
name
)
match_group
=
re
.
match
(
'.*
\
s([dD]2[oO]|[lL][bB]|.*ug).*
\
s(.+)
\
.csv'
,
name
)
tmp
[
'perc'
]
=
str
.
lower
(
match_group
.
group
(
1
))[:
-
2
]
if
str
.
lower
(
match_group
.
group
(
1
))
.
endswith
(
'ug'
)
else
str
.
lower
(
match_group
.
group
(
1
))
tmp
[
'perc'
]
.
replace
({
'd2o'
:
'0'
},
inplace
=
True
)
tmp
[
'class'
]
=
str
.
lower
(
match_group
.
group
(
2
))
df
=
df
.
append
(
tmp
,
ignore_index
=
True
,
sort
=
True
)
df
=
df
[
df
[
'Area'
]
>
19
]
df
[
'Pure'
]
=
df
[
'Mean'
]
-
df
[
'BackMean'
]
sns
.
set_style
(
"darkgrid"
)
#sns.catplot(x = 'perc',y = 'Mean',hue = 'class',kind='bar',data = df)
sns
.
pairplot
(
df
,
vars
=
[
'Area'
,
'Mean'
,
'perc'
,
'class'
])
plt
.
show
()
def
get_subarea_infor
(
img
,
mask
):
area_num
,
labels
,
stats
,
centroids
=
cv
.
connectedComponentsWithStats
(
mask
)
info
=
[]
for
i
in
filter
(
lambda
x
:
x
!=
0
,
range
(
area_num
)):
group
=
np
.
where
(
labels
==
i
)
img_value
=
img
[
group
]
area_tmp
=
len
(
group
[
0
])
mean_tmp
=
np
.
mean
(
img_value
)
std_tmp
=
np
.
std
(
img_value
)
pos
=
[(
group
[
0
][
i
],
group
[
1
][
i
])
for
i
in
range
(
len
(
group
[
0
]))]
res
=
np
.
zeros
([
200
,
200
],
np
.
uint8
)
for
x
in
range
(
200
):
for
y
in
range
(
200
):
if
(
x
,
y
)
in
pos
:
res
[
x
,
y
]
=
mask
[
x
,
y
]
else
:
res
[
x
,
y
]
=
0
kernel
=
np
.
ones
((
17
,
17
),
np
.
uint8
)
mask_background
=
cv
.
erode
(
255
-
res
,
kernel
)
minimask
=
cv
.
bitwise_xor
(
mask_background
,
255
-
res
)
realminimask
=
cv
.
bitwise_and
(
minimask
,
255
-
mask
)
img_background
=
img
[
np
.
where
(
realminimask
!=
0
)]
mean_value
=
np
.
mean
(
img_background
)
std_value
=
np
.
std
(
img_background
)
info
.
append
({
'Label'
:
i
,
'Area'
:
area_tmp
,
'Mean'
:
mean_tmp
,
'Std'
:
std_tmp
,
'BackMean'
:
mean_value
,
'BackStd'
:
std_value
})
return
info
\ No newline at end of file
requirement.txt
View file @
b5ea10ef
...
...
@@ -4,3 +4,4 @@ tqdm
matplotlib
numpy
opencv-python
seaborn
\ No newline at end of file
resCalc.py
View file @
b5ea10ef
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import
pandas
as
pd
import
numpy
as
np
import
cv2
as
cv
import
matplotlib.pyplot
as
plt
import
seaborn
as
sns
import
logging
import
os
import
re
def
save_img
(
img_list
,
dir
,
name
):
num
=
len
(
img_list
)
plt
.
figure
(
figsize
=
(
100
,
20
))
plt
.
suptitle
(
name
)
for
i
,
title
in
zip
(
range
(
num
),
img_list
):
img
=
img_list
[
title
]
plt
.
subplot
(
1
,
num
,
i
+
1
)
plt
.
title
(
title
)
plt
.
imshow
(
img
,
'gray'
)
try
:
os
.
makedirs
(
'data/output/'
+
dir
+
'/graph'
)
except
:
logging
.
info
(
'Existing dir: data/output/'
+
dir
)
plt
.
savefig
(
'data/output/'
+
dir
+
'/graph/'
+
name
[:
-
4
]
+
'.png'
)
plt
.
close
()
def
calcRes
(
img
,
mask
,
dir
=
'default_output'
,
name
=
'output'
):
dic
=
get_subarea_infor
(
img
,
mask
)
df
=
pd
.
DataFrame
(
dic
)
try
:
os
.
makedirs
(
'data/output/'
+
dir
+
'/csv'
)
except
:
logging
.
info
(
'Existing dir: out/'
+
dir
+
'/csv'
)
if
len
(
df
)
!=
0
:
df
.
to_csv
(
'data/output/'
+
dir
+
'/csv/'
+
name
+
'.csv'
,
index
=
False
)
def
draw_bar
(
exName
,
names
):
df
=
pd
.
DataFrame
(
columns
=
(
'class'
,
'perc'
,
'Label'
,
'Area'
,
'Mean'
,
'Std'
,
'BackMean'
,
'BackStd'
))
for
name
in
names
:
tmp
=
pd
.
read_csv
(
'data/output/'
+
exName
+
'/csv/'
+
name
)
match_group
=
re
.
match
(
'.*
\
s([dD]2[oO]|[lL][bB]|.*ug).*
\
s(.+)
\
.csv'
,
name
)
tmp
[
'perc'
]
=
str
.
lower
(
match_group
.
group
(
1
))[:
-
2
]
if
str
.
lower
(
match_group
.
group
(
1
))
.
endswith
(
'ug'
)
else
str
.
lower
(
match_group
.
group
(
1
))
tmp
[
'perc'
]
.
replace
({
'd2o'
:
'0'
},
inplace
=
True
)
tmp
[
'class'
]
=
str
.
lower
(
match_group
.
group
(
2
))
df
=
df
.
append
(
tmp
,
ignore_index
=
True
,
sort
=
True
)
df
=
df
[
df
[
'Area'
]
>
19
]
df
[
'Pure'
]
=
df
[
'Mean'
]
-
df
[
'BackMean'
]
sns
.
set_style
(
"darkgrid"
)
# sns.catplot(x = 'perc',y = 'Mean',hue = 'class',kind='bar',data = df)
sns
.
pairplot
(
df
,
vars
=
[
'Area'
,
'Mean'
,
'perc'
,
'class'
])
plt
.
show
()
def
get_subarea_infor
(
img
,
mask
):
area_num
,
labels
,
stats
,
centroids
=
cv
.
connectedComponentsWithStats
(
mask
)
info
=
[]
for
i
in
filter
(
lambda
x
:
x
!=
0
,
range
(
area_num
)):
group
=
np
.
where
(
labels
==
i
)
img_value
=
img
[
group
]
area_tmp
=
len
(
group
[
0
])
mean_tmp
=
np
.
mean
(
img_value
)
std_tmp
=
np
.
std
(
img_value
)
pos
=
[(
group
[
0
][
i
],
group
[
1
][
i
])
for
i
in
range
(
len
(
group
[
0
]))]
res
=
np
.
zeros
([
200
,
200
],
np
.
uint8
)
for
x
in
range
(
200
):
for
y
in
range
(
200
):
if
(
x
,
y
)
in
pos
:
res
[
x
,
y
]
=
mask
[
x
,
y
]
else
:
res
[
x
,
y
]
=
0
kernel
=
np
.
ones
((
17
,
17
),
np
.
uint8
)
mask_background
=
cv
.
erode
(
255
-
res
,
kernel
)
minimask
=
cv
.
bitwise_xor
(
mask_background
,
255
-
res
)
realminimask
=
cv
.
bitwise_and
(
minimask
,
255
-
mask
)
img_background
=
img
[
np
.
where
(
realminimask
!=
0
)]
mean_value
=
np
.
mean
(
img_background
)
std_value
=
np
.
std
(
img_background
)
info
.
append
({
'Label'
:
i
,
'Area'
:
area_tmp
,
'Mean'
:
mean_tmp
,
'Std'
:
std_tmp
,
'BackMean'
:
mean_value
,
'BackStd'
:
std_value
})
return
info
\ No newline at end of file
train.py
View file @
b5ea10ef
...
...
@@ -3,13 +3,12 @@ import logging
import
os
import
sys
import
numpy
as
np
import
torch
import
torch.nn
as
nn
from
torch
import
optim
from
tqdm
import
tqdm
from
eval
import
eval_net
from
utils.
eval
import
eval_net
from
unet
import
UNet
from
torch.utils.tensorboard
import
SummaryWriter
...
...
utils/eval.py
View file @
b5ea10ef
...
...
@@ -2,7 +2,7 @@ import torch
import
torch.nn.functional
as
F
from
tqdm
import
tqdm
from
dice_loss
import
dice_coeff
from
utils.
dice_loss
import
dice_coeff
def
eval_net
(
net
,
loader
,
device
,
n_val
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment