Ejemplo n.º 1
0
    def test_shape_modeling(self, dir_annot=PATH_ANNOT):
        """    """
        list_paths = sorted(glob.glob(os.path.join(dir_annot, '*.png')))
        logging.info('nb images: %i SAMPLES: %s', len(list_paths),
                     repr([os.path.basename(p) for p in list_paths[:5]]))
        list_segms = []
        for path_seg in list_paths:
            seg, _ = tl_data.load_image_2d(path_seg)
            list_segms.append(seg)

        list_rays, _ = seg_rg.compute_object_shapes(list_segms,
                                                    ray_step=25,
                                                    smooth_coef=1,
                                                    interp_order='spline')
        logging.info('nb eggs: %i nb rays: %i', len(list_rays),
                     len(list_rays[0]))

        model, list_mean_cdf = seg_rg.transform_rays_model_sets_mean_cdf_mixture(
            list_rays, 2)

        np.savez(PATH_PKL_MODEL,
                 data={
                     'name': 'set_cdfs',
                     'cdfs': list_mean_cdf,
                     'mix_model': model
                 })
        # with open(PATH_PKL_MODEL, 'w') as fp:
        #     pickle.dump({'name': 'set_cdfs',
        #                  'cdfs': list_mean_cdf,
        #                  'mix_model': model}, fp)
        self.assertTrue(os.path.exists(PATH_PKL_MODEL))

        max_len = max(
            [np.asarray(l_cdf).shape[1] for _, l_cdf in list_mean_cdf])

        fig, axarr = plt.subplots(nrows=len(list_mean_cdf),
                                  ncols=2,
                                  figsize=(12, 3.5 * len(list_mean_cdf)))
        for i, (_, list_cdf) in enumerate(list_mean_cdf):
            cdist = np.zeros((len(list_cdf), max_len))
            cdist[:, :len(list_cdf[0])] = np.array(list_cdf)
            axarr[i, 0].set_title('Inverse cumulative distribution')
            axarr[i, 0].imshow(cdist, aspect='auto')
            axarr[i, 0].set_xlim([0, max_len])
            axarr[i, 0].set_ylabel('Ray steps')
            axarr[i, 0].set_xlabel('Distance [px]')
            axarr[i, 1].set_title('Reconstructions')
            axarr[i, 1].imshow(compute_prior_map(cdist, step=10))

        fig.savefig(os.path.join(PATH_OUTPUT, 'RG2Sp_shape-modeling.pdf'),
                    bbox_inches='tight',
                    pad_inches=0)
def main(path_annot, path_out, nb_comp=5):
    list_paths = sorted(glob.glob(path_annot))
    print('nb images:', len(list_paths), 'SAMPLES:',
          [os.path.basename(p) for p in list_paths[:5]])
    list_segms = []
    for path_seg in list_paths:
        seg = tl_data.io_imread(path_seg)
        list_segms.append(seg)

    list_rays, _ = tl_rg.compute_object_shapes(list_segms,
                                               ray_step=RAY_STEP,
                                               interp_order='spline',
                                               smooth_coef=1)
    logging.info('nb eggs: %i, nb rays: %i', len(list_rays), len(list_rays[0]))

    x_axis = np.linspace(0, 360, len(list_rays[0]), endpoint=False)
    df = pd.DataFrame(np.array(list_rays), columns=x_axis.astype(int))
    path_csv = os.path.join(path_out, NAME_CSV_RAY_ALL)
    logging.info('exporting all Rays: %s', path_csv)
    df.to_csv(path_csv)

    # SINGLE MODEL
    model, list_cdf = tl_rg.transform_rays_model_cdf_mixture(list_rays, 1)
    cdf = np.array(np.array(list_cdf))

    # path_model = os.path.join(path_out, NAME_NPZ_MODEL_SINGLE)
    # logging.info('exporting model: %s', path_model)
    # np.savez(path_model, name='cdf', cdfs=cdf, mix_model=model)
    path_model = os.path.join(path_out, NAME_PKL_MODEL_SINGLE)
    logging.info('exporting model: %s', path_model)
    with open(path_model, 'wb') as fp:
        pickle.dump({'name': 'cdf', 'cdfs': cdf, 'mix_model': model}, fp)

    # MIXTURE MODEL
    model, list_mean_cdf = tl_rg.transform_rays_model_sets_mean_cdf_mixture(
        list_rays, nb_comp)

    # path_model = os.path.join(path_out, NAME_NPZ_MODEL_MIXTURE)
    # logging.info('exporting model: %s', path_model)
    # np.savez(path_model, name='set_cdfs', cdfs=list_mean_cdf,
    #                     mix_model=model)
    path_model = os.path.join(path_out, NAME_PKL_MODEL_MIXTURE)
    logging.info('exporting model: %s', path_model)
    with open(path_model, 'wb') as fp:
        pickle.dump(
            {
                'name': 'set_cdfs',
                'cdfs': list_mean_cdf,
                'mix_model': model
            }, fp)

    logging.info('Done')