Ejemplo n.º 1
0
 def summarize_smooth(self, noise=0.1):
     ''' Find best estimate for diffeomorphism 
         looking at each singularly. '''
     maximum_likelihood_index = np.zeros(self.shape, dtype='int32')
     variance = np.zeros(self.shape, dtype='float32')
     epsilon = None
     for c in coords_iterate(self.shape):
         k = self.flattening.cell2index[c]
         sim = self.neighbor_similarity_flat[k]
         if epsilon is None:
             epsilon = np.random.randn(*sim.shape)
         sim_min = sim.min()
         sim_max = sim.max()
         if sim_max == sim_min:
             best_index = 0
             variance[c] = 0
         else:
             std = noise * (sim_max - sim_min)
             best = np.argmin(sim + epsilon * std)
             best_index = self.neighbor_indices_flat[k][best]
             variance[c] = sim[best]
         maximum_likelihood_index[c] = best_index
     d = self.flattening.flat2coords(maximum_likelihood_index)
     variance = variance - variance.min()
     vmax = variance.max()
     if vmax > 0:
         variance *= (1 / vmax)
     return Diffeomorphism2D(d, variance)
def consistency_based_uncertainty(action, normalize_distance):
    '''
    Update the uncertainties for the action by the improved uncertainty 
    classification based on comparing the diffeomorphism with its inverse. 
    '''
    # print('Using length score function %s' % length_score)
    d1 = action.diffeo.d
    d2 = action.diffeo_inv.d
    v1, v2 = consistency_based_uncertainty2(d1, d2, normalize_distance)
    D1 = Diffeomorphism2D(d1, v1)
    D2 = Diffeomorphism2D(d2, v2)
    a = DiffeoAction(label=action.label,
                     diffeo=D1,
                     diffeo_inv=D2,
                     original_cmd=action.original_cmd)
    return a
Ejemplo n.º 3
0
def DDSFromSymbolic(resolution, symdiffeosystem):  # @UnusedVariable
    """ 
        Creates a DiffeoSystem from synthetic diffeomorphisms. 
    """
    diffeo2s_config = get_diffeo2s_config()
    diffeo2dds_config = get_diffeo2dds_config()

    _, symdds = diffeo2dds_config.symdds.instance_smarter(symdiffeosystem)

    logger.info('Creating symbolic diffeomorphism (resolution = %d)' %
                resolution)

    diffeoactions = []
    for _, action in enumerate(symdds.actions):

        id_diffeo, diffeo = parse_diffeo_spec(diffeo2s_config,
                                              action['diffeo'])
        label = action.get('label', id_diffeo)

        original_cmd = np.array(action['original_cmd'])

        logger.info('Getting symbolic diffeomorphism %r' % id_diffeo)

        shape = (resolution, resolution)
        viewport = SquareDomain([[-1, +1], [-1, +1]])
        manifold = diffeo.get_topology()
        D, Dinfo = diffeo_from_function_viewport(diffeo, manifold, viewport,
                                                 shape)
        D2d = Diffeomorphism2D(D, Dinfo)

        diffeo_inv = diffeo.get_inverse()
        D_inv, Dinfo_inv = \
            diffeo_from_function_viewport(diffeo_inv, manifold, viewport, shape)
        D2d_inv = Diffeomorphism2D(D_inv, Dinfo_inv)

        action = DiffeoAction(label=label,
                              diffeo=D2d,
                              diffeo_inv=D2d_inv,
                              original_cmd=original_cmd)
        diffeoactions.append(action)

    dds = DiffeoSystem('unnamed', actions=diffeoactions)
    return dds
Ejemplo n.º 4
0
 def summarize_averaged(self, n=10, noise=0.1):
     d = []
     for _ in range(n):
         diff = self.summarize_smooth(noise)
         d.append(diff.d)
         # print('.')
     ds = np.array(d, 'float')
     avg = ds.mean(axis=0)
     # var  = diff.variance
     var = ds[:, :, :, 0].var(axis=0) + ds[:, :, :, 1].var(axis=0)
     # print var.shape
     assert avg.shape == diff.d.shape
     return Diffeomorphism2D(avg, var)
Ejemplo n.º 5
0
    def get_value(self):
        ''' 
            Find maximum likelihood estimate for diffeomorphism looking 
            at each pixel singularly. 
            
            Returns a Diffeomorphism2D.
        '''
        if not self.initialized():
            msg = 'Cannot summarize() because not initialized yet.'
            raise Diffeo2dEstimatorInterface.NotReady(msg)
        certainty = np.zeros(self.shape, dtype='float32')
        certainty.fill(np.nan)

        dd = diffeo_identity(self.shape)
        dd[:] = -1
        for i in range(self.nsensels):

            if self.inference_method == DiffeomorphismEstimatorFaster.Order:
                eord_score = self.neig_eord_score[i, :]
                best = np.argmin(eord_score)

            if self.inference_method == DiffeomorphismEstimatorFaster.Similarity:
                esim_score = self.neig_esim_score[i, :]
                best = np.argmin(esim_score)

            jc = self.flat_structure.neighbor_cell(i, best)
            ic = self.flat_structure.flattening.index2cell[i]

            if self.inference_method == DiffeomorphismEstimatorFaster.Order:
                certain = -np.min(eord_score) / np.mean(eord_score)

            if self.inference_method == DiffeomorphismEstimatorFaster.Similarity:
                first = np.sort(esim_score)[:10]
                certain = -(first[0] - np.mean(first[1:]))
                # certain = -np.min(esim_score) / np.mean(esim_score)


#            certain = np.min(esim_score) / self.num_samples
#            certain = -np.mean(esim_score) / np.min(esim_score)

            dd[ic[0], ic[1], 0] = jc[0]
            dd[ic[0], ic[1], 1] = jc[1]
            certainty[ic[0], ic[1]] = certain

        certainty = certainty - certainty.min()
        vmax = certainty.max()
        if vmax > 0:
            certainty *= (1.0 / vmax)

        return Diffeomorphism2D(dd, certainty)
Ejemplo n.º 6
0
 def commutator(a1, a2):
     # TODO: use uncertainty
     d1 = a1.d
     d1_inv = a1.d_inv
     d2 = a2.d
     d2_inv = a2.d_inv
     C = diffeo_compose
     z = C(C(C(d1, d2), d1_inv), d2_inv)
     diffeo = Diffeomorphism2D(z)
     label = "[%s,%s]" % (a1.label, a2.label)
     primitive = False
     invertible = True
     original_cmd = None
     return Action(diffeo, label, primitive, invertible, original_cmd)
Ejemplo n.º 7
0
    def summarize_continuous(self, quivername):
        center = np.zeros(list(self.shape) + [2], dtype='float32')
        spread = np.zeros(self.shape, dtype='float32')
        maxerror = np.zeros(self.shape, dtype='float32')
        minerror = np.zeros(self.shape, dtype='float32')

        # from PIL import Image  # @UnresolvedImport
        # sim_image = Image.new('L', np.flipud(self.shape) * np.flipud(self.lengths))
        # zer_image = Image.new('L', np.flipud(self.shape) * np.flipud(self.lengths))
        for c in coords_iterate(self.shape):
            # find index in flat array
            k = self.flattening.cell2index[c]
            # Look at the average similarities of the neihgbors
            sim = self.neighbor_similarity_flat[k]

            sim_square = sim.reshape(
                self.lengths).astype('float32') / self.num_samples
            from diffeo2d_learn.library.simple.display import sim_square_modify
            sim_square, minerror[c], maxerror[c] = sim_square_modify(
                sim_square,
                np.min(self.neighbor_similarity_flat) / self.num_samples,
                np.max(self.neighbor_similarity_flat) / self.num_samples)
            # avg_square = (np.max(sim_square) + np.min(sim_square)) / 2
            sim_zeroed = np.zeros(sim_square.shape)
            sim_zeroed[sim_square > 0.85] = sim_square[sim_square > 0.85]
            from diffeo2d_learn.library.simple.display import get_cm
            center[c], spread[c] = get_cm(sim_square)

            # p0 = tuple(np.flipud(np.array(c) * self.lengths))
            # sim_image.paste(Image.fromarray((sim_square * 255).astype('uint8')), p0 + tuple(p0 + self.lengths))
            # zer_image.paste(Image.fromarray((sim_zeroed * 255).astype('uint8')), p0 + tuple(p0 + self.lengths))

        # sim_image.save(quivername + 'simimage.png')
        # zer_image.save(quivername + 'simzeroed.png')

        from diffeo2d_learn.library.simple.display import display_disp_quiver
        display_disp_quiver(center, quivername)
        from diffeo2d_learn.library.simple.display import display_continuous_stats
        display_continuous_stats(center, spread, minerror, maxerror,
                                 quivername)

        dcont = displacement_to_coord(center)
        diff = dcont.astype('int')
        diff = get_valid_diffeomorphism(diff)
        diffeo2d = Diffeomorphism2D(diff)

        return diffeo2d
Ejemplo n.º 8
0
 def make_hard(dd):
     assert isinstance(dd, Diffeomorphism2D)
     if use_isomorphism_heuristics:
         stats = diffeo_stats(dd.d)
         per = np.percentile(stats.norm, norm_percentile)
         limit = per * factor
         # / 3.0
         # print('norm mean/mean: %g %g' % (np.mean(stats.norm), np.median(stats.norm)))            
         # for i in range(0, 100, 5):
         #    print(' %3d%% = %g' % (i, np.percentile(stats.norm, i)))
         # limit = np.percentile(stats.norm, info_percentile)
         # if limit <= 1:
         #    print('limit was %g' % limit)
         #    limit = 4
         variance = (stats.norm > limit).astype('float')
         logger.info('---hard choices---')
         logger.info('  per: %g pixels * %g =' % (per, factor))
         logger.info('limit: %g pixels' % limit)
         logger.info('  vis: %.1f%% ' % (100 * np.mean(variance)))
     else:
         variance = (dd.variance > info_threshold).astype('float')
     return Diffeomorphism2D(dd.d, variance)
Ejemplo n.º 9
0
    def get_value(self):
        ''' 
            Find maximum likelihood estimate for diffeomorphism looking 
            at each pixel singularly. 
            
            Returns a Diffeomorphism2D.
        '''

        if not self.initialized():
            msg = 'No data seen yet'
            raise Diffeo2dEstimatorInterface.NotReady(msg)

        maximum_likelihood_index = np.zeros(self.shape, dtype='int32')
        variance = np.zeros(self.shape, dtype='float32')
        E2 = np.zeros(self.shape, dtype='float32')
        E3 = np.zeros(self.shape, dtype='float32')
        E4 = np.zeros(self.shape, dtype='float32')
        num_problems = 0

        i = 0
        # for each coordinate
        for c in coords_iterate(self.shape):
            # find index in flat array
            k = self.flattening.cell2index[c]
            # Look at the average similarities of the neihgbors
            sim = self.neighbor_similarity_flat[k]
            sim_min = sim.min()
            sim_max = sim.max()
            if sim_max == sim_min:
                # if all the neighbors have the same similarity
                best_index = 0
                variance[c] = 0  # minimum information
                maximum_likelihood_index[c] = best_index
            else:
                best = np.argmin(sim)
                best_index = self.neighbor_indices_flat[k][best]
                # uncertainty ~= similarity of the best pixel
                variance[c] = sim[best]
                maximum_likelihood_index[c] = best_index

            E2[c] = self.neighbor_similarity_best[k] / self.num_samples
            # Best match error
            E3[c] = np.min(
                self.neighbor_num_bestmatch_flat[k]) / self.num_samples

            E4[c] = np.min(self.neighbor_argsort_flat[k]) / self.num_samples

            i += 1

        d = self.flattening.flat2coords(maximum_likelihood_index)

        if num_problems > 0:
            logger.info('Warning, %d were not informative.' % num_problems)
            pass

        # normalization for this variance measure
        vmin = variance.min()
        variance = variance - vmin
        vmax = variance.max()
        if vmax > 0:
            variance *= (1 / vmax)

        # return maximum likelihood plus uncertainty measure
        return Diffeomorphism2D(d, variance)  # , E2, E3, E4)