Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
P
python3-imagej-tiff
Project
Project
Details
Activity
Releases
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
Elphel
python3-imagej-tiff
Commits
0a742391
Commit
0a742391
authored
Jun 30, 2018
by
Oleg Dzhimiev
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
new scripts
parent
5591b717
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
755 additions
and
78 deletions
+755
-78
imagej_tiffwriter.py
imagej_tiffwriter.py
+100
-0
test_nn_feed.py
test_nn_feed.py
+201
-78
test_nn_infer.py
test_nn_infer.py
+225
-0
test_plot_disp_and_confidence.py
test_plot_disp_and_confidence.py
+162
-0
test_tf_ops.py
test_tf_ops.py
+67
-0
No files found.
imagej_tiffwriter.py
0 → 100644
View file @
0a742391
#!/usr/bin/env python3
'''
/**
* @file imagej_tiff_saver.py
* @brief save tiffs for imagej (1.52d+) - with stacks and hyperstacks
* @par <b>License</b>:
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
'''
__copyright__
=
"Copyright 2018, Elphel, Inc."
__license__
=
"GPL-3.0+"
__email__
=
"oleg@elphel.com"
from
PIL
import
Image
,
TiffImagePlugin
import
numpy
as
np
import
math
def
__get_IJ_IFD
(
t
,
z
,
c
):
ifd
=
TiffImagePlugin
.
ImageFileDirectory_v2
()
#is_hyperstack = 'true' if len(shape)>1 else 'false'
#if (len(shape)>0):
return
ifd
def
save
(
path
,
images
,
force_stack
=
False
,
force_hyperstack
=
False
):
# Got images, analyze shape:
# - possible formats (c == depth):
# -- (t,z,h,w,c)
# -- (t,h,w,c), t or z does not matter
# -- (h,w,c)
# -- (h,w)
# save single layer image
if
len
(
images
.
shape
)
==
2
:
# (h,w) -> (h,w,c=1)
images
=
images
[:,:,
np
.
newaxis
]
elif
len
(
images
.
shape
)
>
2
:
# do nothing
pass
else
:
# (w,) -> (h=1,w,c=1)
images
=
images
[
np
.
newaxis
,:,
np
.
newaxis
]
#imlist = np.ravel(Image.fromarray(images))
t
,
z
,
h
,
w
,
c
=
images
.
shape
c_axis
=
len
(
images
.
shape
)
-
1
channels
=
np
.
squeeze
(
np
.
split
(
images
,
c
,
axis
=
c_axis
))
split_channels
=
np
.
concatenate
(
channels
,
axis
=-
3
)
images_flat
=
np
.
reshape
(
split_channels
,(
-
1
,
h
,
w
))
imlist
=
[]
for
i
in
range
(
images_flat
.
shape
[
0
]):
imlist
.
append
(
Image
.
fromarray
(
images_flat
[
i
]))
imlist
[
0
]
.
save
(
path
,
save_all
=
True
,
append_images
=
imlist
[
1
:],
tiffinfo
=
__get_IJ_IFD
(
t
,
z
,
c
))
# Testing
if
__name__
==
"__main__"
:
def
hamming_window
(
x
,
N
):
y
=
0.54
-
0.46
*
math
.
cos
(
2
*
math
.
pi
*
x
/
(
N
-
1
))
return
y
hw
=
hamming_window
NT
=
5
NC
=
2
NZ
=
3
NX
=
512
NY
=
512
images
=
np
.
empty
((
NT
,
NZ
,
NY
,
NX
,
NC
))
import
time
print
(
str
(
time
.
time
())
+
": Generating test images"
)
for
t
in
range
(
NT
):
for
z
in
range
(
NZ
):
for
c
in
range
(
NC
):
images
[
t
,
z
,:,:,
c
]
=
np
.
array
([[(
255
-
t
*
25
)
*
hw
(
i
,
512
)
*
hw
(
j
,
512
)
for
i
in
range
(
NX
)]
for
j
in
range
(
NY
)],
np
.
float32
)
print
(
str
(
time
.
time
())
+
": Test images generated"
)
print
(
"Images shape: "
+
str
(
images
.
shape
))
v
=
save
(
"result_2.tiff"
,
images
)
test_nn_feed.py
View file @
0a742391
...
@@ -8,8 +8,6 @@ __email__ = "oleg@elphel.com"
...
@@ -8,8 +8,6 @@ __email__ = "oleg@elphel.com"
Open all tiffs in a folder, combine a single tiff from randomly selected
Open all tiffs in a folder, combine a single tiff from randomly selected
tiles from originals
tiles from originals
'''
'''
import
tensorflow
as
tf
#import tensorflow.contrib.slim as slim
from
PIL
import
Image
from
PIL
import
Image
...
@@ -25,8 +23,6 @@ import itertools
...
@@ -25,8 +23,6 @@ import itertools
import
time
import
time
sys
.
exit
()
#http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
#http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
class
bcolors
:
class
bcolors
:
HEADER
=
'
\033
[95m'
HEADER
=
'
\033
[95m'
...
@@ -54,91 +50,154 @@ try:
...
@@ -54,91 +50,154 @@ try:
except
IndexError
:
except
IndexError
:
src
=
"."
src
=
"."
print
(
"Importing TensorCrawl"
)
print_time
()
print_time
()
tlist
=
glob
.
glob
(
src
+
"/*.tiff"
)
import
tensorflow
as
tf
import
tensorflow.contrib.slim
as
slim
print
(
"Found "
+
str
(
len
(
tlist
))
+
" preprocessed tiff files:"
)
print
(
"TensorCrawl imported"
)
print
(
"
\n
"
.
join
(
tlist
))
print_time
()
print_time
()
''' WARNING, assuming:
- timestamps and part of names match
- layer order and names are identical
'''
# open the first one to get dimensions and other info
IS_TEST
=
False
tiff
=
ijt
.
imagej_tiff
(
tlist
[
0
])
#del tlist[0]
#
shape as tiles? make a copy or make writeable
#
BEGIN IF IS_TEST
# (242, 324, 9, 9, 5)
if
not
IS_TEST
:
# get labels
tlist
=
glob
.
glob
(
src
+
"/*.tiff"
)
labels
=
tiff
.
labels
.
copy
()
labels
.
remove
(
VALUES_LAYER_NAME
)
print
(
"Image data layers: "
+
str
(
labels
))
print
(
"
\n
"
.
join
(
tlist
))
print
(
"Layers of interest: "
+
str
(
LAYERS_OF_INTEREST
))
print
(
"Found "
+
str
(
len
(
tlist
))
+
" preprocessed tiff files:"
)
print
(
"Values layer: "
+
str
([
VALUES_LAYER_NAME
]))
print_time
()
''' WARNING, assuming:
- timestamps and part of names match
- layer order and names are identical
'''
# create copies
# open the first one to get dimensions and other info
tiles
=
np
.
copy
(
tiff
.
getstack
(
labels
,
shape_as_tiles
=
True
)
)
tiff
=
ijt
.
imagej_tiff
(
tlist
[
0
]
)
values
=
np
.
copy
(
tiff
.
getvalues
(
label
=
VALUES_LAYER_NAME
))
#del tlist[0]
#gt = values[:,:,1:3]
# shape as tiles? make a copy or make writeable
# (242, 324, 9, 9, 5)
print
(
"Mixed tiled input data shape: "
+
str
(
tiles
.
shape
))
# get labels
#print_time()
labels
=
tiff
.
labels
.
copy
()
labels
.
remove
(
VALUES_LAYER_NAME
)
# now generate a layer of indices to get other tiles
print
(
"Image data layers: "
+
str
(
labels
))
indices
=
np
.
random
.
random_integers
(
0
,
len
(
tlist
)
-
1
,
size
=
(
tiles
.
shape
[
0
],
tiles
.
shape
[
1
]))
print
(
"Layers of interest: "
+
str
(
LAYERS_OF_INTEREST
))
print
(
"Values layer: "
+
str
([
VALUES_LAYER_NAME
]))
#print(indices.shape)
# create copies
tiles
=
np
.
copy
(
tiff
.
getstack
(
labels
,
shape_as_tiles
=
True
))
values
=
np
.
copy
(
tiff
.
getvalues
(
label
=
VALUES_LAYER_NAME
))
# counts tiles from a certain tiff
#gt = values[:,:,1:3]
shuffle_counter
=
np
.
zeros
(
len
(
tlist
),
np
.
int32
)
shuffle_counter
[
0
]
=
tiles
.
shape
[
0
]
*
tiles
.
shape
[
1
]
for
i
in
range
(
1
,
len
(
tlist
)):
print
(
"Mixed tiled input data shape: "
+
str
(
tiles
.
shape
))
#print(tlist[i])
#print_time()
tmp_tiff
=
ijt
.
imagej_tiff
(
tlist
[
i
])
tmp_tiles
=
tmp_tiff
.
getstack
(
labels
,
shape_as_tiles
=
True
)
tmp_vals
=
tmp_tiff
.
getvalues
(
label
=
VALUES_LAYER_NAME
)
#tmp_tiles =
#tiles[indices==i] = tmp_tiff[indices==i]
# straight and clear
# now generate a layer of indices to get other tiles
# can do quicker?
indices
=
np
.
random
.
random_integers
(
0
,
len
(
tlist
)
-
1
,
size
=
(
tiles
.
shape
[
0
],
tiles
.
shape
[
1
]))
for
y
,
x
in
itertools
.
product
(
range
(
indices
.
shape
[
0
]),
range
(
indices
.
shape
[
1
])):
if
indices
[
y
,
x
]
==
i
:
tiles
[
y
,
x
]
=
tmp_tiles
[
y
,
x
]
values
[
y
,
x
]
=
tmp_vals
[
y
,
x
]
shuffle_counter
[
i
]
+=
1
# check shuffle counter
#print(indices.shape)
for
i
in
range
(
1
,
len
(
shuffle_counter
)):
shuffle_counter
[
0
]
-=
shuffle_counter
[
i
]
print
(
"Tiff files parts count in the mixed input = "
+
str
(
shuffle_counter
))
# counts tiles from a certain tiff
print_time
()
shuffle_counter
=
np
.
zeros
(
len
(
tlist
),
np
.
int32
)
shuffle_counter
[
0
]
=
tiles
.
shape
[
0
]
*
tiles
.
shape
[
1
]
# test later
for
i
in
range
(
1
,
len
(
tlist
)):
#print(tlist[i])
tmp_tiff
=
ijt
.
imagej_tiff
(
tlist
[
i
])
tmp_tiles
=
tmp_tiff
.
getstack
(
labels
,
shape_as_tiles
=
True
)
tmp_vals
=
tmp_tiff
.
getvalues
(
label
=
VALUES_LAYER_NAME
)
#tmp_tiles =
#tiles[indices==i] = tmp_tiff[indices==i]
# now pack from 9x9 to 1x25
# straight and clear
# tiles and values
# can do quicker?
for
y
,
x
in
itertools
.
product
(
range
(
indices
.
shape
[
0
]),
range
(
indices
.
shape
[
1
])):
if
indices
[
y
,
x
]
==
i
:
tiles
[
y
,
x
]
=
tmp_tiles
[
y
,
x
]
values
[
y
,
x
]
=
tmp_vals
[
y
,
x
]
shuffle_counter
[
i
]
+=
1
# Parse packing table
# check shuffle counter
# packing table name
for
i
in
range
(
1
,
len
(
shuffle_counter
)):
ptab_name
=
"tile_packing_table.xml"
shuffle_counter
[
0
]
-=
shuffle_counter
[
i
]
ptab
=
pile
.
PackingTable
(
ptab_name
,
LAYERS_OF_INTEREST
)
.
lut
# might not need it because going to loop through anyway
print
(
"Tiff files parts count in the mixed input = "
+
str
(
shuffle_counter
))
packed_tiles
=
np
.
array
([[
pile
.
pack_tile
(
tiles
[
i
,
j
],
ptab
)
for
j
in
range
(
tiles
.
shape
[
1
])]
for
i
in
range
(
tiles
.
shape
[
0
])]
)
print_time
(
)
packed_tiles
=
np
.
dstack
((
packed_tiles
,
values
[:,:,
0
]))
# test later
print
(
"Packed (81x4 -> 1x(25*4+1)) tiled input shape: "
+
str
(
packed_tiles
.
shape
))
# now pack from 9x9 to 1x25
print_time
()
# tiles and values
# Parse packing table
# packing table name
ptab_name
=
"tile_packing_table.xml"
ptab
=
pile
.
PackingTable
(
ptab_name
,
LAYERS_OF_INTEREST
)
.
lut
# might not need it because going to loop through anyway
packed_tiles
=
np
.
array
([[
pile
.
pack_tile
(
tiles
[
i
,
j
],
ptab
)
for
j
in
range
(
tiles
.
shape
[
1
])]
for
i
in
range
(
tiles
.
shape
[
0
])])
packed_tiles
=
np
.
dstack
((
packed_tiles
,
values
[:,:,
0
]))
print
(
"Packed (81x4 -> 1x(25*4+1)) tiled input shape: "
+
str
(
packed_tiles
.
shape
))
print
(
"Values shape "
+
str
(
values
.
shape
))
print_time
()
else
:
print
(
"Init test data"
)
ptab_name
=
"tile_packing_table.xml"
pt
=
pile
.
PackingTable
(
ptab_name
,
LAYERS_OF_INTEREST
)
.
lut
# 9x9 2 layers, no neighbors
l
=
np
.
zeros
((
9
,
9
))
for
y
,
x
in
itertools
.
product
(
range
(
l
.
shape
[
0
]),
range
(
l
.
shape
[
1
])):
l
[
y
,
x
]
=
9
*
y
+
x
l_value
=
np
.
array
([
2.54
,
3.54
,
0.5
])
#print(l)
l1
=
l
l2
=
l
*
2
l3
=
l
*
3
l4
=
l
*
4
ls
=
np
.
dstack
((
l1
,
l2
,
l3
,
l4
))
#print(ls.shape)
l_packed_pre
=
pile
.
pack_tile
(
ls
,
pt
)
#print(l_packed_pre.shape)
#print(l_packed_pre)
l_packed
=
np
.
hstack
((
l_packed_pre
,
l_value
[
0
]))
#print(l_packed.shape)
#print(l_packed)
# use l_packed
packed_tiles
=
np
.
empty
([
1
,
1
,
l_packed
.
shape
[
0
]])
values
=
np
.
empty
([
1
,
1
,
2
])
print
(
packed_tiles
.
shape
)
print
(
values
.
shape
)
packed_tiles
[
0
,
0
]
=
l_packed
values
[
0
,
0
]
=
l_value
[
1
:
3
]
print
(
packed_tiles
[
0
,
0
])
print
(
values
[
0
,
0
])
# END IF IS_TEST
#print("CHECKPOINTE")
#for i in range(tiles.shape[0]):
#for i in range(tiles.shape[0]):
# for j in range(tiles.shape[1]):
# for j in range(tiles.shape[1]):
...
@@ -147,6 +206,7 @@ print_time()
...
@@ -147,6 +206,7 @@ print_time()
#print_time()
#print_time()
result_dir
=
'./result/'
result_dir
=
'./result/'
checkpoint_dir
=
'./result/'
save_freq
=
500
save_freq
=
500
def
lrelu
(
x
):
def
lrelu
(
x
):
...
@@ -154,22 +214,37 @@ def lrelu(x):
...
@@ -154,22 +214,37 @@ def lrelu(x):
def
network
(
input
):
def
network
(
input
):
fc1
=
slim
.
fully_connected
(
input
,
42
,
activation_fn
=
lrelu
,
scope
=
'g_fc1'
)
fc1
=
slim
.
fully_connected
(
input
,
101
,
activation_fn
=
lrelu
,
scope
=
'g_fc1'
)
fc2
=
slim
.
fully_connected
(
fc1
,
21
,
activation_fn
=
lrelu
,
scope
=
'g_fc2'
)
fc2
=
slim
.
fully_connected
(
fc1
,
101
,
activation_fn
=
lrelu
,
scope
=
'g_fc2'
)
fc3
=
slim
.
fully_connected
(
fc2
,
1
,
activation_fn
=
lrelu
,
scope
=
'g_fc3'
)
fc3
=
slim
.
fully_connected
(
fc2
,
101
,
activation_fn
=
lrelu
,
scope
=
'g_fc3'
)
fc4
=
slim
.
fully_connected
(
fc3
,
101
,
activation_fn
=
lrelu
,
scope
=
'g_fc4'
)
fc5
=
slim
.
fully_connected
(
fc4
,
2
,
activation_fn
=
lrelu
,
scope
=
'g_fc5'
)
return
fc
3
return
fc
5
sess
=
tf
.
s
ession
()
sess
=
tf
.
S
ession
()
in_tile
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
None
,
101
])
in_tile
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
101
])
gt
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
None
,
2
])
gt
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
2
])
#cf_cutoff = tf.constant(tf.float32,[None,1])
out
=
network
(
in_tile
)
out
=
network
(
in_tile
)
G_loss
=
tf
.
reduce_mean
(
tf
.
abs
(
out
[:,
0
]
-
gt
[:,
0
]))
# min cutoff
cf_cutoff
=
0.173303
cf_w
=
tf
.
pow
(
tf
.
maximum
(
gt
[:,
1
]
-
cf_cutoff
,
0.0
),
1
)
cf_wsum
=
tf
.
reduce_sum
(
cf_w
)
cf_w_norm
=
cf_w
/
cf_wsum
#out_cf = out[:,1]
G_loss
=
tf
.
reduce_mean
(
tf
.
abs
(
out
[:,
0
]
-
cf_w_norm
*
gt
[:,
0
]))
t_vars
=
tf
.
trainable_variables
()
t_vars
=
tf
.
trainable_variables
()
lr
=
tf
.
placeholder
(
tf
.
float32
)
lr
=
tf
.
placeholder
(
tf
.
float32
)
G_opt
=
tf
.
train
.
AdamOptimizer
(
learning_rate
=
lr
)
.
minimize
(
G_loss
,
var_list
=
[
var
for
var
in
t_vars
if
var
.
name
.
startswith
(
'g_'
)])
G_opt
=
tf
.
train
.
AdamOptimizer
(
learning_rate
=
lr
)
.
minimize
(
G_loss
,
var_list
=
[
var
for
var
in
t_vars
if
var
.
name
.
startswith
(
'g_'
)])
saver
=
tf
.
train
.
Saver
()
saver
=
tf
.
train
.
Saver
()
...
@@ -186,22 +261,70 @@ lastepoch = 0
...
@@ -186,22 +261,70 @@ lastepoch = 0
for
folder
in
allfolders
:
for
folder
in
allfolders
:
lastepoch
=
np
.
maximum
(
lastepoch
,
int
(
folder
[
-
4
:]))
lastepoch
=
np
.
maximum
(
lastepoch
,
int
(
folder
[
-
4
:]))
#g_loss = np.zeros((
,1))
g_loss
=
np
.
zeros
((
packed_tiles
.
shape
[
0
]
*
packed_tiles
.
shape
[
1
]
,
1
))
learning_rate
=
1e-4
learning_rate
=
1e-4
for
epoch
in
range
(
lastepoch
,
4001
):
print
(
bcolors
.
HEADER
+
"Last Epoch = "
+
str
(
lastepoch
)
+
bcolors
.
ENDC
)
for
epoch
in
range
(
lastepoch
,
1
):
#for epoch in range(lastepoch,4001):
if
os
.
path
.
isdir
(
"result/
%04
d"
%
epoch
):
if
os
.
path
.
isdir
(
"result/
%04
d"
%
epoch
):
continue
continue
cnt
=
0
cnt
=
0
if
epoch
>
2000
:
if
epoch
>
2000
:
learning_rate
=
1e-5
learning_rate
=
1e-5
for
ind
in
np
.
random
.
permutation
(
tiles
.
shape
[
0
]
*
tiles
.
shape
[
1
]):
for
ind
in
np
.
random
.
permutation
(
packed_tiles
.
shape
[
0
]
*
packed_
tiles
.
shape
[
1
]):
input_patch
=
tiles
[
i
,
j
]
#print("Iteration "+str(cnt))
gt_patch
=
values
[
i
,
j
,
1
:
2
]
st
=
time
.
time
()
cnt
+=
1
i
=
int
(
ind
/
packed_tiles
.
shape
[
1
])
j
=
ind
%
packed_tiles
.
shape
[
1
]
#input_patch = tiles[i,j]
input_patch
=
np
.
empty
((
1
,
packed_tiles
.
shape
[
2
]))
input_patch
[
0
]
=
packed_tiles
[
i
,
j
]
gt_patch
=
np
.
empty
((
1
,
2
))
if
not
IS_TEST
:
gt_patch
[
0
]
=
values
[
i
,
j
,
1
:
3
]
else
:
gt_patch
[
0
]
=
values
[
i
,
j
]
#print(input_patch)
#print(gt_patch)
gt_patch
[
gt_patch
==-
256
]
=
np
.
nan
skip_iteration
=
False
# if nan skip run!
if
np
.
isnan
(
np
.
sum
(
gt_patch
[
0
])):
skip_iteration
=
True
if
np
.
isnan
(
np
.
sum
(
input_patch
[
0
])):
skip_iteration
=
True
if
skip_iteration
:
#print(bcolors.WARNING+"Found NaN, skipping iteration for tile "+str(i)+","+str(j)+bcolors.ENDC)
pass
else
:
_
,
G_current
,
output
=
sess
.
run
([
G_opt
,
G_loss
,
out
],
feed_dict
=
{
in_tile
:
input_patch
,
gt
:
gt_patch
,
lr
:
learning_rate
})
g_loss
[
ind
]
=
G_current
print
(
"
%
d
%
d Loss=
%.3
f CurrentLoss=
%.3
f Time=
%.3
f"
%
(
epoch
,
cnt
,
np
.
mean
(
g_loss
[
np
.
where
(
g_loss
)]),
G_current
,
time
.
time
()
-
st
))
if
epoch
%
save_freq
==
0
:
if
not
os
.
path
.
isdir
(
result_dir
+
'
%04
d'
%
epoch
):
os
.
makedirs
(
result_dir
+
'
%04
d'
%
epoch
)
saver
.
save
(
sess
,
checkpoint_dir
+
'model.ckpt'
)
print_time
()
print
(
bcolors
.
OKGREEN
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
test_nn_infer.py
0 → 100644
View file @
0a742391
#!/usr/bin/env python3
__copyright__
=
"Copyright 2018, Elphel, Inc."
__license__
=
"GPL-3.0+"
__email__
=
"oleg@elphel.com"
'''
Open all tiffs in a folder, combine a single tiff from randomly selected
tiles from originals
'''
from
PIL
import
Image
import
os
import
sys
import
glob
import
imagej_tiff
as
ijt
import
pack_tile
as
pile
import
numpy
as
np
import
itertools
import
time
#http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
class
bcolors
:
HEADER
=
'
\033
[95m'
OKBLUE
=
'
\033
[94m'
OKGREEN
=
'
\033
[92m'
WARNING
=
'
\033
[38;5;214m'
FAIL
=
'
\033
[91m'
ENDC
=
'
\033
[0m'
BOLD
=
'
\033
[1m'
BOLDWHITE
=
'
\033
[1;37m'
UNDERLINE
=
'
\033
[4m'
def
print_time
():
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
# USAGE: python3 test_3.py some-path
VALUES_LAYER_NAME
=
'other'
LAYERS_OF_INTEREST
=
[
'diagm-pair'
,
'diago-pair'
,
'hor-pairs'
,
'vert-pairs'
]
RADIUS
=
1
try
:
src
=
sys
.
argv
[
1
]
except
IndexError
:
src
=
"."
print
(
"Importing TensorCrawl"
)
print_time
()
import
tensorflow
as
tf
import
tensorflow.contrib.slim
as
slim
print
(
"TensorCrawl imported"
)
print_time
()
result_dir
=
'./result/'
checkpoint_dir
=
'./result/'
save_freq
=
500
def
lrelu
(
x
):
return
tf
.
maximum
(
x
*
0.2
,
x
)
def
network
(
input
):
fc1
=
slim
.
fully_connected
(
input
,
101
,
activation_fn
=
lrelu
,
scope
=
'g_fc1'
)
fc2
=
slim
.
fully_connected
(
fc1
,
101
,
activation_fn
=
lrelu
,
scope
=
'g_fc2'
)
fc3
=
slim
.
fully_connected
(
fc2
,
101
,
activation_fn
=
lrelu
,
scope
=
'g_fc3'
)
fc4
=
slim
.
fully_connected
(
fc3
,
101
,
activation_fn
=
lrelu
,
scope
=
'g_fc4'
)
fc5
=
slim
.
fully_connected
(
fc4
,
2
,
activation_fn
=
lrelu
,
scope
=
'g_fc5'
)
return
fc5
sess
=
tf
.
Session
()
in_tile
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
101
])
gt
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
2
])
out
=
network
(
in_tile
)
#G_loss = tf.reduce_mean(tf.abs(out[:,0]-gt[:,0]))
#t_vars=tf.trainable_variables()
#lr=tf.placeholder(tf.float32)
#G_opt=tf.train.AdamOptimizer(learning_rate=lr).minimize(G_loss,var_list=[var for var in t_vars if #var.name.startswith('g_')])
saver
=
tf
.
train
.
Saver
()
sess
.
run
(
tf
.
global_variables_initializer
())
ckpt
=
tf
.
train
.
get_checkpoint_state
(
checkpoint_dir
)
if
ckpt
:
print
(
'loaded '
+
ckpt
.
model_checkpoint_path
)
saver
.
restore
(
sess
,
ckpt
.
model_checkpoint_path
)
# do not need output for now
#if not os.path.isdir(result_dir + 'final/'):
# os.makedirs(result_dir + 'final/')
tlist
=
glob
.
glob
(
src
+
"/*.tiff"
)
print
(
"
\n
"
.
join
(
tlist
))
print
(
"Found "
+
str
(
len
(
tlist
))
+
" preprocessed tiff files:"
)
print_time
()
''' WARNING, assuming:
- timestamps and part of names match
- layer order and names are identical
'''
# Now PROCESS
for
item
in
tlist
:
print
(
bcolors
.
OKGREEN
+
"Processing "
+
item
+
bcolors
.
ENDC
)
# open the first one to get dimensions and other info
tiff
=
ijt
.
imagej_tiff
(
item
)
# shape as tiles? make a copy or make writeable
# (242, 324, 9, 9, 5)
# get labels
labels
=
tiff
.
labels
.
copy
()
labels
.
remove
(
VALUES_LAYER_NAME
)
print
(
"Image data layers: "
+
str
(
labels
))
print
(
"Layers of interest: "
+
str
(
LAYERS_OF_INTEREST
))
print
(
"Values layer: "
+
str
([
VALUES_LAYER_NAME
]))
# create copies
tiles
=
np
.
copy
(
tiff
.
getstack
(
labels
,
shape_as_tiles
=
True
))
# need values? Well, just to compare
values
=
np
.
copy
(
tiff
.
getvalues
(
label
=
VALUES_LAYER_NAME
))
#gt = values[:,:,1:3]
print
(
"Mixed tiled input data shape: "
+
str
(
tiles
.
shape
))
#print_time()
# now pack from 9x9 to 1x25
# tiles and values
# Parse packing table
# packing table name
ptab_name
=
"tile_packing_table.xml"
ptab
=
pile
.
PackingTable
(
ptab_name
,
LAYERS_OF_INTEREST
)
.
lut
# might not need it because going to loop through anyway
packed_tiles
=
np
.
array
([[
pile
.
pack_tile
(
tiles
[
i
,
j
],
ptab
)
for
j
in
range
(
tiles
.
shape
[
1
])]
for
i
in
range
(
tiles
.
shape
[
0
])])
packed_tiles
=
np
.
dstack
((
packed_tiles
,
values
[:,:,
0
]))
# flatten
packed_tiles_flat
=
packed_tiles
.
reshape
(
-
1
,
packed_tiles
.
shape
[
-
1
])
values_flat
=
values
.
reshape
(
-
1
,
values
.
shape
[
-
1
])
print
(
"Packed (81x4 -> 1x(25*4+1)) tiled input shape: "
+
str
(
packed_tiles_flat
.
shape
))
print
(
"Values shape "
+
str
(
values_flat
.
shape
))
print_time
()
# now run prediction
output
=
sess
.
run
(
out
,
feed_dict
=
{
in_tile
:
packed_tiles_flat
})
print
(
"Output shape: "
+
str
(
output
.
shape
))
# so, let's print
for
i
in
range
(
output
.
shape
[
0
]):
p
=
output
[
i
,
0
]
pc
=
output
[
i
,
1
]
fv
=
values_flat
[
i
,
0
]
gt
=
values_flat
[
i
,
1
]
cf
=
values_flat
[
i
,
2
]
vstring
=
"["
+
"{0:.2f}"
.
format
(
fv
)
+
", "
+
"{0:.2f}"
.
format
(
gt
)
+
", "
+
"{0:.2f}"
.
format
(
cf
)
+
"]"
pstring
=
"["
+
"{0:.2f}"
.
format
(
p
)
+
", "
+
"{0:.2f}"
.
format
(
pc
)
+
"]"
if
not
np
.
isnan
(
p
):
outstring
=
"i: "
+
str
(
i
)
+
" Values: "
+
vstring
+
" Prediction: "
+
pstring
if
cf
<
0.5
:
print
(
outstring
)
else
:
print
(
bcolors
.
WARNING
+
outstring
+
bcolors
.
ENDC
)
#else:
# print("i: "+str(i)+" NaNs")
# check histogram:
#import matplotlib.pyplot as plt
#x = np.histogram(values_flat[:,2])
#plt.hist(x, bins=100)
#plt.ylabel('Confidence')
print_time
()
print
(
bcolors
.
OKGREEN
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
test_plot_disp_and_confidence.py
0 → 100644
View file @
0a742391
#!/usr/bin/env python3
__copyright__
=
"Copyright 2018, Elphel, Inc."
__license__
=
"GPL-3.0+"
__email__
=
"oleg@elphel.com"
'''
Open all tiffs in a folder, combine a single tiff from randomly selected
tiles from originals
'''
from
PIL
import
Image
import
os
import
sys
import
glob
import
imagej_tiff
as
ijt
import
pack_tile
as
pile
import
numpy
as
np
import
itertools
import
time
import
matplotlib.pyplot
as
plt
#http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
class
bcolors
:
HEADER
=
'
\033
[95m'
OKBLUE
=
'
\033
[94m'
OKGREEN
=
'
\033
[92m'
WARNING
=
'
\033
[38;5;214m'
FAIL
=
'
\033
[91m'
ENDC
=
'
\033
[0m'
BOLD
=
'
\033
[1m'
BOLDWHITE
=
'
\033
[1;37m'
UNDERLINE
=
'
\033
[4m'
def
print_time
():
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
# USAGE: python3 test_3.py some-path
VALUES_LAYER_NAME
=
'other'
LAYERS_OF_INTEREST
=
[
'diagm-pair'
,
'diago-pair'
,
'hor-pairs'
,
'vert-pairs'
]
RADIUS
=
1
try
:
src
=
sys
.
argv
[
1
]
except
IndexError
:
src
=
"."
tlist
=
glob
.
glob
(
src
+
"/*.tiff"
)
print
(
"
\n
"
.
join
(
tlist
))
print
(
"Found "
+
str
(
len
(
tlist
))
+
" preprocessed tiff files:"
)
print_time
()
''' WARNING, assuming:
- timestamps and part of names match
- layer order and names are identical
'''
# Now PROCESS
for
item
in
tlist
:
print
(
bcolors
.
OKGREEN
+
"Processing "
+
item
+
bcolors
.
ENDC
)
# open the first one to get dimensions and other info
tiff
=
ijt
.
imagej_tiff
(
item
)
# shape as tiles? make a copy or make writeable
# (242, 324, 9, 9, 5)
# get labels
labels
=
tiff
.
labels
.
copy
()
labels
.
remove
(
VALUES_LAYER_NAME
)
print
(
"Image data layers: "
+
str
(
labels
))
print
(
"Layers of interest: "
+
str
(
LAYERS_OF_INTEREST
))
print
(
"Values layer: "
+
str
([
VALUES_LAYER_NAME
]))
# create copies
#tiles = np.copy(tiff.getstack(labels,shape_as_tiles=True))
# need values? Well, just to compare
values
=
np
.
copy
(
tiff
.
getvalues
(
label
=
VALUES_LAYER_NAME
))
#gt = values[:,:,1:3]
print
(
"Mixed tiled input data shape: "
+
str
(
values
.
shape
))
# plot ground truth
values
[
values
==-
256
]
=
np
.
nan
t
=
values
[:,:,
1
]
mytitle
=
"Ground truth"
fig
=
plt
.
figure
()
fig
.
canvas
.
set_window_title
(
mytitle
)
fig
.
suptitle
(
mytitle
)
plt
.
imshow
(
t
)
plt
.
colorbar
()
#plt.show()
t
=
values
[:,:,
2
]
mytitle
=
"Confidence"
fig
=
plt
.
figure
()
fig
.
canvas
.
set_window_title
(
mytitle
)
fig
.
suptitle
(
mytitle
)
plt
.
imshow
(
t
)
plt
.
colorbar
()
#plt.show()
mytitle
=
"Confidence histogram"
fig
=
plt
.
figure
()
fig
.
canvas
.
set_window_title
(
mytitle
)
fig
.
suptitle
(
mytitle
)
values_flat
=
np
.
ravel
(
values
[:,:,
2
])
#values_flat[values_flat==0] = np.nan
# flat and filtered
values_ff
=
values_flat
[
values_flat
!=
0
]
values_ff
=
np
.
round
(
values_ff
,
3
)
plt
.
hist
(
values_ff
,
bins
=
1000
)
plt
.
ylabel
(
'N'
)
plt
.
show
()
print_time
()
print
(
bcolors
.
OKGREEN
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
test_tf_ops.py
0 → 100644
View file @
0a742391
#!/usr/bin/env python3
import
time
import
numpy
as
np
#http://stackoverflow.com/questions/287871/print-in-terminal-with-colors-using-python
class
bcolors
:
HEADER
=
'
\033
[95m'
OKBLUE
=
'
\033
[94m'
OKGREEN
=
'
\033
[92m'
WARNING
=
'
\033
[38;5;214m'
FAIL
=
'
\033
[91m'
ENDC
=
'
\033
[0m'
BOLD
=
'
\033
[1m'
BOLDWHITE
=
'
\033
[1;37m'
UNDERLINE
=
'
\033
[4m'
def
print_time
():
print
(
bcolors
.
BOLDWHITE
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
# MAIN
print
(
"Importing TensorCrawl"
)
print_time
()
import
tensorflow
as
tf
import
tensorflow.contrib.slim
as
slim
print
(
"TensorCrawl imported"
)
print_time
()
sess
=
tf
.
Session
()
# (10,)
a_in
=
[
0
,
1
,
2
,
3
,
4
,
5
,
6
,
7
,
8
,
9
]
# (1,)
b_in
=
[
1.5
]
# (3,2)
c_in
=
[
[
0.1
,
0.2
],
[
0.3
,
0.4
],
[
0.5
,
0.6
]
]
#print(np.array(a_in).shape)
a
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
10
])
b
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
1
])
c
=
tf
.
concat
([
a
,
b
],
axis
=
1
)
#d = tf.constant(-1.0)
e
=
tf
.
maximum
(
a
-
1.0
,
0.0
)
f
=
tf
.
placeholder
(
tf
.
float32
,[
None
,
3
,
2
])
g
=
tf
.
maximum
(
f
[:,:,
1
]
+
0.01
,
0
)
res
=
sess
.
run
(
g
,
feed_dict
=
{
a
:[
a_in
],
b
:[
b_in
],
f
:[
c_in
]})
#res = sess.run(a,feed_dict={a:a_in})
print
(
res
.
shape
)
print
(
res
)
print_time
()
print
(
bcolors
.
OKGREEN
+
"time: "
+
str
(
time
.
time
())
+
bcolors
.
ENDC
)
\ No newline at end of file
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