Commit 0a742391 authored by Oleg Dzhimiev's avatar Oleg Dzhimiev

new scripts

parent 5591b717
#!/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)
This diff is collapsed.
#!/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)
#!/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)
#!/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
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment