Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
Y
YeaZ-GUI
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
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
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
chadhat
YeaZ-GUI
Commits
42e3f8dc
Commit
42e3f8dc
authored
3 years ago
by
lpbsscientist
Browse files
Options
Downloads
Patches
Plain Diff
SJR: allow running NN from command line.
parent
4e54fbb0
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
GUI_main.py
+6
-4
6 additions, 4 deletions
GUI_main.py
Launch_NN_command_line.py
+101
-0
101 additions, 0 deletions
Launch_NN_command_line.py
unet/neural_network.py
+11
-12
11 additions, 12 deletions
unet/neural_network.py
with
118 additions
and
16 deletions
GUI_main.py
+
6
−
4
View file @
42e3f8dc
...
...
@@ -748,22 +748,24 @@ class App(QMainWindow):
print
(
'
--------- Finished segmenting.
'
)
def
LaunchPrediction
(
self
,
im
,
is_pc
):
@staticmethod
def
LaunchPrediction
(
im
,
is_pc
,
pretrained_weights
=
None
):
"""
It launches the neural neutwork on the current image and creates
an hdf file with the prediction for the time T and corresponding FOV.
"""
im
=
skimage
.
exposure
.
equalize_adapthist
(
im
)
im
=
im
*
1.0
;
pred
=
nn
.
prediction
(
im
,
is_pc
)
pred
=
nn
.
prediction
(
im
,
is_pc
,
pretrained_weights
)
return
pred
def
ThresholdPred
(
self
,
thvalue
,
pred
):
@staticmethod
def
ThresholdPred
(
thvalue
,
pred
):
"""
Thresholds prediction with value
"""
if
thvalue
==
None
:
thresholdedmask
=
nn
.
threshold
(
pred
)
else
:
thresholdedmask
=
nn
.
threshold
(
pred
,
thvalue
)
thresholdedmask
=
nn
.
threshold
(
pred
,
thvalue
)
return
thresholdedmask
...
...
This diff is collapsed.
Click to expand it.
Launch_NN_command_line.py
0 → 100644
+
101
−
0
View file @
42e3f8dc
# Run:
#
# python Launch_NN_command_line.py -i DIRECTORY/IMAGE_FILE -m OUTPUT_MASK_FILE --path_to_weights PATH_TO_HDF5_FILE --fov N --range_of_frames n1 n2 --min_seed_dist 5 --threshold 0.5
#
# or:
#
# python Launch_NN_command_line.py -i DIRECTORY/IMAGE_FILE -m OUTPUT_MASK_FILE --image_type pc_OR_bf --fov N --range_of_frames n1 n2 --min_seed_dist 5 --threshold 0.5
import
sys
sys
.
path
.
append
(
"
./unet
"
)
sys
.
path
.
append
(
"
./disk
"
)
#Import all the other python files
#this file handles the interaction with the disk, so loading/saving images
#and masks and it also runs the neural network.
from
GUI_main
import
App
from
segment
import
segment
import
Reader
as
nd
import
argparse
import
skimage
import
neural_network
as
nn
def
LaunchInstanceSegmentation
(
reader
,
image_type
,
fov_indices
=
[
0
],
time_value1
=
0
,
time_value2
=
0
,
thr_val
=
None
,
min_seed_dist
=
5
,
path_to_weights
=
None
):
"""
"""
# cannot have both path_to_weights and image_type supplied
if
(
image_type
is
not
None
)
and
(
path_to_weights
is
not
None
):
print
(
"
image_type and path_to_weights cannot be both supplied.
"
)
return
# check if correct imaging value
if
(
image_type
not
in
[
'
bf
'
,
'
pc
'
])
and
(
path_to_weights
is
None
):
print
(
"
Wrong imaging type value (
'
{}
'
)!
"
.
format
(
image_type
),
"
imaging type must be either
'
bf
'
or
'
pc
'"
)
return
is_pc
=
image_type
==
'
pc
'
# check range_of_frames constraint
if
time_value1
>
time_value2
:
print
(
"
Error
"
,
'
Invalid Time Constraints
'
)
return
# displays that the neural network is running
print
(
'
Running the neural network...
'
)
for
fov_ind
in
fov_indices
:
#iterates over the time indices in the range
for
t
in
range
(
time_value1
,
time_value2
+
1
):
print
(
'
--------- Segmenting field of view:
'
,
fov_ind
,
'
Time point:
'
,
t
)
#calls the neural network for time t and selected fov
im
=
reader
.
LoadOneImage
(
t
,
fov_ind
)
try
:
pred
=
App
.
LaunchPrediction
(
im
,
is_pc
,
pretrained_weights
=
path_to_weights
)
except
ValueError
:
print
(
'
Error!
'
,
'
The neural network weight files could not
'
'
be found.
\n
Make sure to download them from
'
'
the link in the readme and put them into
'
'
the folder unet, or specify a path to a custom weights file with -w argument.
'
)
return
thresh
=
App
.
ThresholdPred
(
thr_val
,
pred
)
seg
=
segment
(
thresh
,
pred
,
min_seed_dist
)
reader
.
SaveMask
(
t
,
fov_ind
,
seg
)
print
(
'
--------- Finished segmenting.
'
)
# apply tracker if wanted and if not at first time
temp_mask
=
reader
.
CellCorrespondence
(
t
,
fov_ind
)
reader
.
SaveMask
(
t
,
fov_ind
,
temp_mask
)
def
main
(
args
):
if
'
.h5
'
in
args
.
mask_path
:
args
.
mask_path
=
args
.
mask_path
.
replace
(
'
.h5
'
,
''
)
reader
=
nd
.
Reader
(
""
,
args
.
mask_path
,
args
.
image_path
)
LaunchInstanceSegmentation
(
reader
,
args
.
image_type
,
args
.
fov
,
args
.
range_of_frames
[
0
],
args
.
range_of_frames
[
1
],
args
.
threshold
,
args
.
min_seed_dist
,
args
.
path_to_weights
)
if
__name__
==
'
__main__
'
:
parser
=
argparse
.
ArgumentParser
(
description
=
''
,
formatter_class
=
argparse
.
ArgumentDefaultsHelpFormatter
)
parser
.
add_argument
(
'
-i
'
,
'
--image_path
'
,
type
=
str
,
help
=
"
Specify the path to a single image or to a folder of images
"
,
required
=
True
)
parser
.
add_argument
(
'
-m
'
,
'
--mask_path
'
,
type
=
str
,
help
=
"
Specify where to save predicted masks
"
,
required
=
True
)
parser
.
add_argument
(
'
--image_type
'
,
type
=
str
,
help
=
"
Specify the imaging type, possible
'
bf
'
and
'
pc
'
. Supersedes path_to_weights.
"
)
parser
.
add_argument
(
'
--path_to_weights
'
,
default
=
None
,
type
=
str
,
help
=
"
Specify weights path.
"
)
parser
.
add_argument
(
'
--fov
'
,
default
=
[
0
],
nargs
=
'
+
'
,
type
=
int
,
help
=
"
Specify field of view index.
"
)
parser
.
add_argument
(
'
--range_of_frames
'
,
nargs
=
2
,
default
=
[
0
,
0
],
type
=
int
,
help
=
"
Specify start and end in range of frames.
"
)
parser
.
add_argument
(
'
--threshold
'
,
default
=
None
,
type
=
float
,
help
=
"
Specify threshold value.
"
)
parser
.
add_argument
(
'
--min_seed_dist
'
,
default
=
5
,
type
=
int
,
help
=
"
Specify minimum distance between seeds.
"
)
args
=
parser
.
parse_args
()
main
(
args
)
This diff is collapsed.
Click to expand it.
unet/neural_network.py
+
11
−
12
View file @
42e3f8dc
...
...
@@ -46,7 +46,7 @@ def threshold(im,th = None):
return
bi
def
prediction
(
im
,
is_pc
):
def
prediction
(
im
,
is_pc
,
pretrained_weights
=
None
):
"""
Calculate the prediction of the label corresponding to image im
Param:
...
...
@@ -60,19 +60,18 @@ def prediction(im, is_pc):
col_add
=
16
-
ncol
%
16
padded
=
np
.
pad
(
im
,
((
0
,
row_add
),
(
0
,
col_add
)))
# WHOLE CELL PREDICTION
model
=
unet
(
pretrained_weights
=
None
,
input_size
=
(
None
,
None
,
1
))
if
is_pc
:
path
=
path_weights
+
'
unet_weights_batchsize_25_Nepochs_100_SJR0_10.hdf5
'
else
:
path
=
path_weights
+
'
weights_budding_BF_multilab_0_1.hdf5
'
if
pretrained_weights
is
None
:
if
is_pc
:
pretrained_weights
=
path_weights
+
'
unet_weights_batchsize_25_Nepochs_100_SJR0_10.hdf5
'
else
:
pretrained_weights
=
path_weights
+
'
weights_budding_BF_multilab_0_1.hdf5
'
if
not
os
.
path
.
exists
(
p
ath
):
if
not
os
.
path
.
exists
(
p
retrained_weights
):
raise
ValueError
(
'
Path does not exist
'
)
model
.
load_weights
(
path
)
# WHOLE CELL PREDICTION
model
=
unet
(
pretrained_weights
=
pretrained_weights
,
input_size
=
(
None
,
None
,
1
))
results
=
model
.
predict
(
padded
[
np
.
newaxis
,:,:,
np
.
newaxis
],
batch_size
=
1
)
...
...
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