Commit 4cab48b9 by Jiri Borovec

update & fix gene dataset

parent d4f03b3f
......@@ -29,6 +29,7 @@ IMAGE_SIZE_2D = (128, 128)
IMAGE_SIZE_3D = (16, 128, 128)
NB_ATM_PATTERNS = 9
NB_SAMPLES = 50
RND_PATTERN_OCCLUSION = 0.25
IMAGE_POSIX = '.png'
IMAGE_PATTERN = 'pattern_{:03d}'
SEGM_PATTERN = 'sample_{:05d}'
......@@ -36,6 +37,7 @@ BLOCK_NB_LOAD_IMAGES = 50
DIR_NAME_DICTIONARY = 'dictionary'
CSV_NAME_WEIGHTS = 'combination.csv'
NAME_DATASET = 'datasetBinary_raw'
COLUMN_NAME = 'ptn_{:02d}'
def create_elastic_deform_2d(im_size, coef=0.5, grid_size=(20, 20)):
......@@ -232,7 +234,7 @@ def dictionary_generate_rnd_pattern(path_out, dir_name=DIR_NAME_DICTIONARY,
:param im_size: (int<w>, int<h>)
:return: [np.array<w, h>] list of independent patters in the dictionary
"""
logger.info('generate Dictionary composed from %i patterns and img. size %s',
logger.info('generate Dict. composed from %i patterns and img. size %s',
nb_ptns, repr(im_size))
out_dir = os.path.join(path_out, dir_name)
create_clean_folder(out_dir)
......@@ -245,11 +247,38 @@ def dictionary_generate_rnd_pattern(path_out, dir_name=DIR_NAME_DICTIONARY,
return list_imgs
def generate_rand_patterns_occlusion(idx, im_ptns, out_dir,
ptn_ration=RND_PATTERN_OCCLUSION):
"""
:param idx:
:param im_ptns:
:param out_dir:
:param ptn_ration:
:return:
"""
np.random.seed()
bool_combine = np.random.random(len(im_ptns)) < ptn_ration
# if there is non above threshold select one random
if not any(bool_combine):
bool_combine[np.random.randint(0, len(bool_combine))] = True
logger.debug('combination vector is %s', repr(bool_combine.tolist()))
im = sum(np.asarray(im_ptns)[bool_combine])
# convert sum to union such as all above 0 set as 1
im[im > 0.] = 1
im_name = SEGM_PATTERN.format(idx)
export_image(out_dir, im, idx)
ptn_weights = [int(x) for x in bool_combine]
return idx, im, im_name, ptn_weights
def dataset_binary_combine_patterns(im_ptns, out_dir, nb_samples=NB_SAMPLES,
ptn_ration=0.25):
ptn_ration=RND_PATTERN_OCCLUSION,
nb_jobs=NB_THREADS):
""" generate a Binary dataset composed from N samples and given ration
of pattern occlusion
:param nb_jobs: int
:param im_ptns: [np.array<w, h>] list of ind. patters in the dictionary
:param out_dir: str, path to the results directory
:param nb_samples: int, number of samples in dataset
......@@ -257,30 +286,27 @@ def dataset_binary_combine_patterns(im_ptns, out_dir, nb_samples=NB_SAMPLES,
an input observation / image
:return: [np.array<w, h>], df<nb_imgs, nb_lbs>
"""
logger.info('generate a Binary dataset composed from %i samples '
'and ration pattern occlusion %s', nb_samples, repr(ptn_ration))
logger.info('generate a Binary dataset composed from %i samples '
'and ration pattern occlusion %f', nb_samples, ptn_ration)
create_clean_folder(out_dir)
df_weights = pd.DataFrame()
im_spls = []
im_spls = [None] * nb_samples
mproc_pool = mproc.Pool(nb_jobs)
logger.debug('running in %i threads...', nb_jobs)
tqdm_bar = tqdm.tqdm(total=nb_samples)
for i in range(nb_samples):
# random vector for threshold
combine = np.random.random(len(im_ptns)) < ptn_ration
# if there is non above threshold select one random
if not any(combine):
combine[np.random.randint(0,len(combine))] = True
logger.debug('combination vector is %s', repr(combine.tolist()))
df_weights = df_weights.append({
'name': SEGM_PATTERN.format(i),
'combination': ';'.join(str(int(x)) for x in combine)},
ignore_index=True)
im = sum(np.asarray(im_ptns)[combine])
# convert sum to union such as all above 0 set as 1
im[im > 0.] = 1
im_spls.append(im)
export_image(out_dir, im, i)
for idx, im, im_name, ptn_weights in mproc_pool.imap_unordered(
partial(generate_rand_patterns_occlusion, im_ptns=im_ptns,
out_dir=out_dir, ptn_ration=ptn_ration,), range(nb_samples)):
im_spls[idx] = im
df_weights = df_weights.append(pd.Series([im_name] + ptn_weights),
ignore_index=True)
tqdm_bar.update(1)
df_weights.set_index('name')
mproc_pool.close()
mproc_pool.join()
df_weights.columns = ['name'] + [COLUMN_NAME.format(i + 1)
for i in range(len(df_weights.columns) - 1)]
df_weights = df_weights.set_index('name')
logger.debug(df_weights.head())
return im_spls, df_weights
......@@ -349,8 +375,8 @@ def dataset_apply_image_function(imgs, out_dir, func, coef=0.5, nb_jobs=NB_THREA
:param coef: float, a param describing the how much it is deformed (0 = None)
:return: [np.array<w, h>]
"""
logger.info('generate a Binary dataset composed from %i samples '
'and deformation coef. %f', len(imgs), coef)
logger.info('apply costume funstion "%s" on %i samples with coef. %f',
func.__name__, len(imgs), coef)
create_clean_folder(out_dir)
imgs_new = [None] * len(imgs)
......@@ -376,7 +402,7 @@ def image_transform_binary2prob(im, coef=0.1):
:param coef: float, influence hoe strict the boundary between F-B is
:return: np.array<w, h> float image
"""
logger.info('... transform binary image to probability')
logger.debug('... transform binary image to probability')
im_dist = ndimage.distance_transform_edt(im)
im_dist -= ndimage.distance_transform_edt(1-im)
im_prob = 1. / (1. + np.exp(-coef * im_dist))
......@@ -394,7 +420,7 @@ def add_image_prob_noise(im, ration=0.1):
:param ration: float (0, 1) means 0 = no noise
:return: np.array<w, h> float image
"""
logger.info('... add smooth noise to a probability image')
logger.debug('... add smooth noise to a probability image')
rnd = 2 * (np.random.random(im.shape) - 0.5)
rnd[abs(rnd) > ration] = 0
im_noise = np.abs(im - rnd)
......
......@@ -67,9 +67,9 @@ def generate_all(path_out=DEFAULT_PATH_APD, csv_name=NAME_WEIGHTS,
im_dict = tl_dataset.dictionary_generate_atlas(path_out, im_size=atlas_size,
nb_ptns=nb_patterns)
im_comb, df_comb = tl_dataset.dataset_binary_combine_patterns(im_dict,
im_comb, df_weights = tl_dataset.dataset_binary_combine_patterns(im_dict,
path_dir('datasetBinary_raw'), nb_samples)
df_comb.to_csv(os.path.join(path_dir('datasetBinary_raw'), csv_name))
df_weights.to_csv(os.path.join(path_out, csv_name))
ds_apply = partial(tl_dataset.dataset_apply_image_function, nb_jobs=NB_THREADS)
......
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