Commit 5342bb82 authored by Jiri Borovec's avatar Jiri Borovec

preproc real data

parent 897e2d05
......@@ -227,7 +227,7 @@ def export_visualization_image(img, i, out_dir, prefix='debug', name='',
plt.ylabel(labels[1])
n_fig = 'APDL_{}_{}_iter_{:04d}.png'.format(prefix, name, i)
p_fig = os.path.join(out_dir, n_fig)
logger.debug('.. export Vusialization as "{}"'.format(p_fig))
logger.debug('.. export Vusialization as "{}...{}"'.format(p_fig[:19], p_fig[-19:]))
fig.savefig(p_fig, bbox_inches='tight', pad_inches=0.05)
plt.close()
......@@ -285,7 +285,7 @@ def alpe_initialisation(imgs, init_atlas, init_weights, out_dir, out_prefix):
return atlas, w_bins
def alpe_update_weights(imgs, atlas):
def alpe_update_weights(imgs, atlas, ovp_m=False):
""" single iteration of the block coordinate descent algo
:param imgs: [np.array<w, h>]
......@@ -294,10 +294,12 @@ def alpe_update_weights(imgs, atlas):
"""
# update w_bins
logger.debug('... perform pattern Weights')
# w_bins = [ptn_weight.weights_image_atlas_overlap_major(img, atlas)
# for img in imgs]
w_bins = [ptn_weight.weights_image_atlas_overlap_partial(img, atlas)
for img in imgs]
if ovp_m:
w_bins = [ptn_weight.weights_image_atlas_overlap_major(img, atlas)
for img in imgs]
else:
w_bins = [ptn_weight.weights_image_atlas_overlap_partial(img, atlas)
for img in imgs]
# add once for patterns that are not used at all
# w_bins = ptn_weight.fill_empty_patterns(np.array(w_bins))
return np.array(w_bins)
......@@ -349,8 +351,8 @@ def alpe_update_atlas(imgs, atlas, w_bins, lb_max, gc_coef, gc_reinit, ptn_split
def alpe_pipe_atlas_learning_ptn_weights(imgs, init_atlas=None, init_weights=None,
gc_coef=0.0, thr_step_diff=0.0, max_iter=99, gc_reinit=True,
ptn_split=True, out_prefix='debug', out_dir=''):
""" the experiments_all pipeline for block coordinate descent algo with graphcut
ptn_split=True, w_ovp_m=False, out_prefix='debug', out_dir=''):
""" the experiments_synthetic pipeline for block coordinate descent algo with graphcut
:param imgs: [np.array<w, h>]
:param init_atlas: np.array<w, h>
......@@ -377,7 +379,7 @@ def alpe_pipe_atlas_learning_ptn_weights(imgs, init_atlas=None, init_weights=Non
logger.info('ERROR: the atlas does not contain '
'any label... {}'.format(np.unique(atlas)))
w_bins = alpe_update_weights(imgs, atlas)
w_bins = alpe_update_weights(imgs, atlas, w_ovp_m)
# plt.subplot(221), plt.imshow(atlas, interpolation='nearest')
# plt.subplot(222), plt.imshow(w_bins, aspect='auto')
atlas, w_bins = alpe_repaire_atlas_weights(imgs, atlas, w_bins, lb_max)
......
This diff is collapsed.
......@@ -11,23 +11,24 @@ from skimage import io, draw, transform, filters
import logging
logger = logging.getLogger(__name__)
sys.path.append(os.path.abspath(os.path.join('..','..'))) # Add path to root
import src.ownUtils.toolDataIO as tl_data
import src.own_utils.tool_data_io as tl_data
jirka = False
if jirka:
DEFAULT_PATH_DATA = '/home/jirka/TEMP/'
DEFAULT_PATH_DATA = '/jirka/jirka/TEMP/'
else:
# DEFAULT_PATH_DATA = '/datagrid/Medical/microscopy/drosophila_segmOvary/'
DEFAULT_PATH_DATA = '/datagrid/temporary/Medical/'
DEFAULT_DIR_APD = 'atomicPatternDictionary'
DEFAULT_DIR_APD = 'atomicPatternDictionary_v2'
DEFAULT_PATH_APD = os.path.join(DEFAULT_PATH_DATA, DEFAULT_DIR_APD)
# DEFAULT_IM_SIZE = (512, 512)
# DEFAULT_IM_SIZE = (256, 256)
DEFAULT_IM_SIZE = (64, 64)
DEFAULT_NB_PTNS = 6
DEFAULT_IM_SIZE = (128, 128)
# DEFAULT_IM_SIZE = (64, 64)
DEFAULT_NB_PTNS = 25
# DEFAULT_NB_SPLS = 1500
DEFAULT_NB_SPLS = 800
DEFAULT_NB_SPLS = 1200
DEFAULT_DIR_DICT = 'dictionary'
DEFAULT_IM_POSIX = '.png'
DEFAULT_IM_PATTERN = 'pattern_{:03d}'
......@@ -261,7 +262,7 @@ def export_image(outDir, im, im_name, n_template=DEFAULT_IM_SEGM):
if not type(im_name)==str:
im_name = n_template.format(im_name)
p_img = os.path.join(outDir, im_name + DEFAULT_IM_POSIX)
logger.debug('... saving pattern image "{}"'.format(p_img))
logger.debug(' .. saving image "{}...{}"'.format(p_img[:25], p_img[-25:]))
io.imsave(p_img, im/float(np.max(im))*1.)
# Image.fromarray(im).save(pImg)
return p_img
......@@ -373,17 +374,16 @@ def dataset_load_images(name=DEFAULT_DATASET, path_base=DEFAULT_PATH_APD,
:return:
"""
p_dir = os.path.join(path_base, name)
logger.info('loading folder "{}" -> {}'.format(p_dir,
os.path.exists(p_dir)))
logger.info('loading folder ({}) <- "{}"'.format(os.path.exists(p_dir), p_dir))
p_imgs = glob.glob(os.path.join(p_dir, im_ptn + '*' + im_posix))
logger.debug('number samples in dataset "{}" is: {}'.format(name,
len(p_imgs)))
logger.debug('number spls {} in dataset "{}"'.format(len(p_imgs), name))
p_imgs = sorted(p_imgs)[:nb_spls]
imgs = []
for i, pIm in enumerate(p_imgs):
im = io.imread(pIm)
imgs.append(im / np.max(im))
return imgs
imgs.append(im / float(np.max(im)))
im_names = [os.path.splitext(os.path.basename(p))[0] for p in p_imgs]
return imgs, im_names
def dataset_load_weights(path_base=DEFAULT_PATH_APD, n_file=DEFAULT_NAME_WEIGHTS):
......@@ -413,7 +413,7 @@ def dataset_create_atlas(name='dictionary', path_base=DEFAULT_PATH_APD,
:param im_ptn: str
:return: np.array<w, h>
"""
imgs = dataset_load_images(name, path_base, im_ptn)
imgs, _ = dataset_load_images(name, path_base, im_ptn)
assert len(imgs) > 0
atlas = np.zeros_like(imgs[0])
for i, im in enumerate(imgs):
......@@ -421,6 +421,23 @@ def dataset_create_atlas(name='dictionary', path_base=DEFAULT_PATH_APD,
return np.array(atlas, dtype=np.uint8)
def dataset_export_images(p_out, imgs, names=None):
""" export complete dataset
:param p_out: str
:param imgs: [np.array<w, h>]
:param names: [str] or None (use indexes)
:return:
"""
create_clean_folder(p_out)
logger.debug('export {} images into "{}"'.format(len(imgs), p_out))
if names is None:
names = range(len(imgs))
for i, im in enumerate(imgs):
export_image(p_out, im, names[i])
return
def dataset_convert_nifti(path_in, path_out, posix=DEFAULT_IM_POSIX):
""" having a datset of png images conver them into nifti images
......@@ -437,7 +454,7 @@ def dataset_convert_nifti(path_in, path_out, posix=DEFAULT_IM_POSIX):
name = os.path.splitext(os.path.basename(p_im))[0]
p_out = os.path.join(path_out, name)
logger.debug('... converting "{}" -> "{}"'.format(p_im, p_out))
tl_data.convertImage_img2niftiGray(p_im, p_out)
tl_data.convert_img_2_nifti_gray(p_im, p_out)
return None
......@@ -455,6 +472,8 @@ def generate_all(path_out=DEFAULT_PATH_APD):
:param path_out: str, path to the output directory
:return:
"""
if not os.path.exists(path_out):
os.mkdir(path_out)
# im_dict = dictionary_generate_rnd_pattern()
im_dict = dictionary_generate_atlas()
......
"""
run experiments with Atomic Learning Pattern Encoding
Example run:
>> nohup python experiments_alpe.py > ~/Medical-temp/experiments_APD-v1/nohup.log &
"""
# to suppres all visu, has to be on the beginning
# import matplotlib
# matplotlib.use('Agg')
import os
import numpy as np
import generate_dataset as gen_data
import multiprocessing as mproc
from skimage import filters
from skimage import morphology
import logging
logger = logging.getLogger(__name__)
jirka = False
if jirka:
DEFAULT_PATH_DATA = '/jirka/jirka/TEMP/APD_real_data'
else:
DEFAULT_PATH_DATA = '/datagrid/Medical/microscopy/drosophila/real_segmentations'
REAL_DATASET_NAME = '108_genes_expression'
# BINARY_DATASET_POSIX = '_binary'
DEFAULT_NB_THREADS = int(mproc.cpu_count() * 0.8)
DEFAULT_PARAMS = {
'computer': os.uname(),
'in_path': DEFAULT_PATH_DATA,
'dataset': REAL_DATASET_NAME,
'out_path': DEFAULT_PATH_DATA,
}
IMAGE_BINARY_THRESHOLD = 0.5
def extend_images(imgs):
""" the method requres images of equal dims
so if there is different image extend them by zeros
:param imgs: [np.array<w, h>]
:return: [np.array<w, h>]
"""
im_sizes = [im.shape for im in imgs]
if len(set(im_sizes)) == 1:
return imgs
w_max = max([w for w, h in im_sizes])
h_max = max([h for w, h in im_sizes])
m_size = (w_max, h_max)
for i, im in enumerate(imgs):
if im.shape == m_size:
continue
logger.debug('.. extend img {} dims {} -> {}'.format(i, im.shape, m_size))
im_new = np.zeros(m_size)
im_new[:im.sahpe[0], :im.sahpe[1]] = im
imgs[i] = im_new
return imgs
def find_borders(im_sum):
""" find from sided the rows and col where the image set is zero
:param im_sum: np.array<w, h>
:return:
"""
border = {}
for i in range(len(im_sum.shape)):
idx = np.nonzero(im_sum.sum(axis=i))[0]
border[i] = min(idx) - 1, max(idx) + 1
return border
def crop_images(imgs):
""" try to cut out image rows and colums that are useless
:param imgs: [np.array<w, h>]
:return: [np.array<w, h>]
"""
im_sum = np.array(imgs).sum(axis=0)
border = find_borders(im_sum)
logger.debug('image crop limits: {}'.format(border))
imgs_crop = [None] * len(imgs)
logger.debug('crop all {} images'.format(len(imgs)))
for i, im in enumerate(imgs):
# the sum dim is perpendicular to cut dim
imgs_crop[i] = im[border[1][0]:border[1][1], border[0][0]:border[0][1]]
logger.debug('image crop finished')
return imgs_crop
def threshold_image_fix(img):
img_th = img > IMAGE_BINARY_THRESHOLD
return img_th
def threshold_image_otsu(img):
th = filters.threshold_otsu(img[img > 0])
logger.debug('threshold with val: {}'.format(th))
img_th = img > th
return img_th
def threshold_image_adapt(img):
img_th = filters.threshold_adaptive(img, 80)
selem = morphology.disk(5)
img_th = morphology.opening(img_th, selem)
return img_th
def threshold_images(imgs, fn_th):
""" threshold images by specific level
:param imgs: [np.array<w, h>]
:param th: float
:return: [np.array<w, h>]
"""
logger.debug('binary images...')
mproc_pool = mproc.Pool(DEFAULT_NB_THREADS)
imgs_th = mproc_pool.map(fn_th, imgs)
mproc_pool.close()
mproc_pool.join()
return imgs_th
def binarise_main(params=DEFAULT_PARAMS):
""" run all experiments
:return:
"""
imgs, names = gen_data.dataset_load_images(params['dataset'], params['in_path'])
logger.debug('loaded {} images of size {}'.format(len(imgs), imgs[0].shape))
imgs = extend_images(imgs)
imgs = crop_images(imgs)
imgs_th = threshold_images(imgs, threshold_image_fix)
p_out = os.path.join(params['in_path'], params['dataset']+'_binary-fix')
gen_data.dataset_export_images(p_out, imgs_th, names)
imgs_th = threshold_images(imgs, threshold_image_otsu)
p_out = os.path.join(params['in_path'], params['dataset']+'_binary-otsu')
gen_data.dataset_export_images(p_out, imgs_th, names)
imgs_th = threshold_images(imgs, threshold_image_adapt)
p_out = os.path.join(params['in_path'], params['dataset']+'_binary-adapt')
gen_data.dataset_export_images(p_out, imgs_th, names)
return
if __name__ == "__main__":
logging.basicConfig(level=logging.DEBUG)
logger.info('running...')
binarise_main()
logger.info('DONE')
\ 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