コード例 #1
0
 def _train_patches(self,partsRegion,shape):
     ORI = self._rotation
     def cycles(X):
         return np.asarray([np.concatenate([X[i:],X[:i]]) for i in xrange(len(X))])
     RR = np.arange(ORI)
     POL = 1
     PP = np.arange(POL)
     II = [list(itr.product(PPi, RRi)) for PPi in cycles(PP) for RRi in cycles(RR)]
     lookup = dict(zip(itr.product(PP,RR),itr.count()))
     num_angle = self._rotation
     d = np.prod(shape)
     permutation = np.empty((num_angle, num_angle * d), dtype = np.int_)
     for a in range(num_angle):
         if a == 0:
             permutation[a] = np.arange(num_angle * d)
         else:
             permutation[a] = np.roll(permutation[a-1], -d)
     from pnet.bernoulli import em
     mm_models = []
     for i in range(self._num_lower_parts//self._rotation):
         block = partsRegion[i]
         ret = em(block.reshape((block.shape[0],-1)),self._num_components, 10, permutation = permutation, numpy_rng = 0, verbose = True)
         mu = ret[1].reshape((self._num_components * self._rotation, ) + shape)
         mm_models.append(mu)
     self._models = np.asarray(mm_models)
コード例 #2
0
    def train_from_samples(self, patches,num_parts):
        #from pnet.latent_bernoulli_mm import LatentBernoulliMM
        from pnet.bernoullimm import BernoulliMM
        min_prob = self._settings.get('min_prob', 0.01)
        flatpatches = patches.reshape((patches.shape[0], -1))
        parts = np.ones((num_parts,) + patches.shape[1:])
        if 0:
            mm = BernoulliMM(n_components=num_parts, n_iter=20, tol=1e-15,n_init=2, random_state=0, min_prob=min_prob, verbose=False)
            print(mm.fit(flatpatches))
            print('AIC', mm.aic(flatpatches))
            print('BIC', mm.bic(flatpatches))
            #import pdb; pdb.set_trace()
            parts = mm.means_.reshape((num_parts,)+patches.shape[1:])
            #self._weights = mm.weights_
        else:
            
            from pnet.bernoulli import em
            ret = em(flatpatches, num_parts,20,numpy_rng=self._settings.get('em_seed',0),verbose=True)
            parts = ret[1].reshape((num_parts,) + patches.shape[1:])
            self._weights = np.arange(self._num_parts)

            #self._weights = mm.weights
            
        # Calculate entropy of parts
        Hall = (parts * np.log(parts) + (1 - parts) * np.log(1 - parts))
        H = -np.apply_over_axes(np.mean, Hall, [1, 2, 3])[:,0,0,0]

        # Sort by entropy
        II = np.argsort(H)

        parts[:] = parts[II]
        #self._train_info['entropy'] = H[II]
        return parts
コード例 #3
0
    def _train(self, phi, data, y):
        X_n = phi(data)
        X = X_n[0]
        num_parts = X_n[1]
        num_orientations = X_n[2]
        num_true_parts = num_parts // num_orientations

        self._extra['training_comp'] = []

        K = int(y.max()) + 1

        mm_models = []

        for k in range(K):
            Xk = X[y == k]
            assert Xk.shape[-1] == 1

            from pnet.cyfuncs import index_map_pooling_multi

            # Rotate all the Xk samples

            print('A')
            XB = index_map_pooling_multi(Xk, num_parts, (1, 1), (1, 1))
            print('B')
            XB = XB.reshape(XB.shape[:-1] + (num_true_parts, num_orientations))

            blocks = []

            print('C')
            for ori in range(0, self._n_orientations):
                angle = ori / self._n_orientations * 360
                # Rotate all images, apply rotational spreading, then do
                # pooling

                from pnet.cyfuncs import rotate_index_map_pooling

                sr = self._pooling_settings.get('rotation_spreading_radius', 0)
                yy1 = rotate_index_map_pooling(Xk[..., 0], angle, sr,
                                               num_orientations,
                                               num_parts,
                                               self._pooling_settings['shape'])

                sh = yy1.shape[:3] + (num_orientations, num_true_parts)
                yy = yy1.reshape(sh)

                blocks.append(yy)

            blocks = np.asarray(blocks).transpose((1, 0, 2, 3, 4, 5))
            print('D')

            """
            from pnet.vzlog import default as vz

            import gv

            for i in range(self._n_orientations):
                gv.img.save_image(vz.impath(), blocks[0, i, :, :, 0].sum(-1))

            vz.finalize()
            """

            shape = blocks.shape[2:4] + (np.prod(blocks.shape[4:]),)

            # Flatten
            blocks = blocks.reshape(blocks.shape[:2] + (-1,))

            n_init = self._settings.get('n_init', 1)
            n_iter = self._settings.get('n_iter', 10)
            seed = self._settings.get('seed', 0)

            ORI = self._n_orientations
            POL = 1

            def cycles(X):
                return np.asarray([np.concatenate([X[i:], X[:i]])
                                   for i in range(len(X))])

            RR = np.arange(ORI)
            PP = np.arange(POL)
            II = [list(itr.product(PPi, RRi))
                  for PPi in cycles(PP)
                  for RRi in cycles(RR)]
            lookup = dict(zip(itr.product(PP, RR), itr.count()))
            permutations = [[lookup[ii] for ii in rows] for rows in II]
            permutations = np.asarray(permutations)

            print('E')

            if 1:
                mm = PermutationMM(n_components=self._n_components,
                                   permutations=permutations,
                                   n_iter=n_iter,
                                   n_init=n_init,
                                   random_state=seed,
                                   min_probability=self._min_prob)
                mm.fit(blocks)
                comps = mm.predict(blocks)
                mu_shape = (self._n_components * self._n_orientations,) + shape
                mu = mm.means_.reshape(mu_shape)

            else:
                num_angle = self._n_orientations
                d = np.prod(shape)
                sh = (num_angle, num_angle * d)
                permutation = np.empty(sh, dtype=np.int_)
                for a in range(num_angle):
                    if a == 0:
                        permutation[a] = np.arange(num_angle * d)
                    else:
                        permutation[a] = np.roll(permutation[a-1], -d)

                from pnet.bernoulli import em


                XX = blocks.reshape((blocks.shape[0], -1))
                print('F Digit:', d)
                ret = em(XX, self._n_components, n_iter,
                         permutation=permutation, numpy_rng=seed,
                         verbose=True)
                print('G')

                comps = ret[3]

                self._extra['training_comp'].append(ret[3])

                mu = ret[1].reshape((self._n_components * self._n_orientations,) + shape)

            if 0: # Build visualizations of all rotations

                ims10k = self._data
                label10k = y

                ims10k_d = ims10k[label10k == d]

                rot_ims10k = np.asarray([[rotate(im, -rot, resize=False) for rot in np.arange(n_orientations) * 360 / n_orientations] for im in ims10k_d])

                vispart_blocks = []

                for phase in range(n_orientations):
                    visparts = np.asarray([
                        rot_ims10k[comps[:, 0]==k, comps[comps[:, 0]==k][:, 1]].mean(0) for k in range(n_comp)
                    ])

                    M = 50

                    grid0 = pnet.plot.ImageGrid(n_comp, min(M, np.max(map(len, XX))), ims10k.shape[1:])
                    for k in range(n_comp):
                        for i in range(min(M, len(XX[k]))):
                            grid0.set_image(XX[k][i], k, i, vmin=0, vmax=1, cmap=cm.gray)
                    grid0.save(vz.impath(), scale=3)

                    for k in range(n_comp):
                        grid.set_image(visparts[k], d, k, vmin=0, vmax=1, cmap=cm.gray)




            mm_models.append(mu)
            print('H')


        self._models = np.asarray(mm_models)
コード例 #4
0
    def _train(self, phi, data, y):
        import types
        if isinstance(data, types.GeneratorType):
            Xs = []
            c = 0
            for i, batch in enumerate(data):
                Xi = phi(batch)
                Xs.append(Xi)
                c += Xi.shape[0]
                ag.info('SVM: Has extracted', c)

            X = np.concatenate(Xs, axis=0)
        else:
            X = phi(data)

        K = y.max() + 1
        mm_models = []
        mm_comps = []
        mm_weights = []
        S = self._settings.get('standardize')
        if S:
            self._means = np.zeros((K, self._n_components))
            self._sigmas = np.zeros((K, self._n_components))

        for k in range(K):
            Xk = X[y == k]
            Xk = Xk.reshape((Xk.shape[0], -1))

            if 1:
                mm = BernoulliMM(n_components=self._n_components,
                                 n_iter=10,
                                 n_init=1,
                                 thresh=1e-5,
                                 random_state=self._settings.get('seed', 0),
                                 min_prob=self._min_prob,
                                 blocksize=200,
                                 verbose=True)
                mm.fit(Xk)

                comps = mm.predict(Xk)
                weights = mm.weights_

                mu = mm.means_.reshape((self._n_components,)+X.shape[1:])
            else:
                from pnet.bernoulli import em
                ret = em(Xk, self._n_components, 10,
                         numpy_rng=self._settings.get('seed', 0),
                         verbose=True)

                mu = ret[1].reshape((self._n_components,)+X.shape[1:])
                comps = ret[3]
                weights = np.exp(ret[0])
                print('--weights', weights)
                weights /= weights.sum()
            mm_models.append(mu)
            mm_comps.append(comps)
            mm_weights.append(weights)

            np.set_printoptions(precision=2, suppress=True)
            print('Weights:', weights)
            print('Weights sorted:', np.sort(weights)[::-1])

            # Add standardization
            if S:
                self._means[k] = np.apply_over_axes(np.sum,
                                                    mu * logit(mu),
                                                    [1, 2, 3]).ravel()
                sig = np.apply_over_axes(np.sum,
                                         logit(mu)**2 * mu * (1 - mu),
                                         [1, 2, 3]).ravel()
                self._sigmas[k] = np.sqrt(sig)

        self._extra['comps'] = mm_comps
        self._extra['weights'] = mm_weights
        self._models = np.asarray(mm_models)
コード例 #5
0
    def train_from_samples(self, patches, original_patches):
        # from pnet.latent_bernoulli_mm import LatentBernoulliMM
        from pnet.bernoullimm import BernoulliMM

        print(patches.shape)
        min_prob = self._settings.get("min_prob", 0.01)
        # num_permutation = self._shifting_shape[0] * self._shifting_shape[1]
        # parts = np.ones((self._num_true_parts * num_permutation ,) + patches.shape[2:])
        parts = np.ones((self._num_parts,) + patches[0].shape)
        d = np.prod(patches.shape[1:])
        # print(d,num_permutation)
        if 0:
            # \permutation = np.empty((num_permutation, num_permutation * d),dtype = np.int_)
            for a in range(num_permutation):
                if a == 0:
                    permutation[a] = np.arange(num_permutation * d)
                else:
                    permutation[a] = np.roll(permutation[a - 1], d)
        flatpatches = patches.reshape((patches.shape[0], -1))
        print(flatpatches.shape)
        if 0:
            mm = BernoulliMM(
                n_components=num_parts, n_iter=20, tol=1e-15, n_init=2, random_state=0, min_prob=min_prob, verbose=False
            )
            print(mm.fit(flatpatches))
            print("AIC", mm.aic(flatpatches))
            print("BIC", mm.bic(flatpatches))
            # import pdb; pdb.set_trace()
            parts = mm.means_.reshape((num_parts,) + patches.shape[1:])
            # self._weights = mm.weights_
        elif 0:

            from pnet.bernoulli import em

            print("before EM")

            ret = em(
                flatpatches,
                self._num_true_parts,
                10,
                mu_truncation=min_prob,
                permutation=permutation,
                numpy_rng=self._settings.get("em_seed", 0),
                verbose=True,
            )
            comps = ret[3]
            parts = ret[1].reshape((self._num_true_parts * num_permutation,) + patches.shape[2:])
            self._weights = np.arange(self._num_parts)
        else:
            rng = np.random.RandomState(self._settings.get("em_seed", 0))
            from pnet.latentShiftEM import LatentShiftEM

            # from latentShiftEM import latentShiftEM
            result = LatentShiftEM(
                flatpatches,
                num_mixture_component=self._num_parts,
                parts_shape=(self._part_shape[0], self._part_shape[1], 8),
                region_shape=(self._sample_shape[1], self._sample_shape[1], 8),
                shifting_shape=self._shifting_shape,
                max_num_iteration=25,
                loglike_tolerance=1e-3,
                mu_truncation=(1, 1),
                additional_mu=None,
                permutation=None,
                numpy_rng=rng,
                verbose=True,
            )
            comps = result[3]
            print(comps.shape)
            print(original_patches.shape)
            print(result[1].shape)
            parts = result[1].reshape((self._num_parts, self._part_shape[0], self._part_shape[1], 8))
            self._bkg_probability = result[4]
        self._parts = parts
        print(comps[:50, 0])
        print(comps[:50, 1])
        self._visparts = np.asarray(
            [original_patches[comps[:, 0] == k, comps[comps[:, 0] == k][:, 1]].mean(0) for k in range(self._num_parts)]
        )
        print(self._visparts.shape)
        import amitgroup.plot as gr

        gr.images(self._visparts, zero_to_one=False, show=False, vmin=0, vmax=1, fileName="moduleShiftingParts1.png")
        return parts
コード例 #6
0
ファイル: parts_layer.py プロジェクト: EdwardBetts/partsNet
    def train_from_samples(self, patches,patches_original):
        #from pnet.latent_bernoulli_mm import LatentBernoulliMM
        from pnet.bernoullimm import BernoulliMM
        min_prob = self._settings.get('min_prob', 0.01)
        flatpatches = patches.reshape((patches.shape[0], -1))
        
        if 1:
            mm = BernoulliMM(n_components=self._num_parts, n_iter=20, tol=1e-15,n_init=2, random_state=self._settings.get('em_seed',0), min_prob=min_prob, verbose=False)
            mm.fit(flatpatches)
            print(mm.fit(flatpatches))
            #print('AIC', mm.aic(flatpatches))
            #print('BIC', mm.bic(flatpatches))
            
            if 0:
                # Draw samples
                #size = (20, 20)
                import gv
                import os
                N = 10000
                D = np.prod(self._part_shape)
                size = (20, 20)
                grid = gv.plot.ImageGrid(size[0], size[1], self._part_shape)
                samp = mm.sample(n_samples=np.prod(size)).reshape((-1,) + self._part_shape)
                samples = mm.sample(n_samples=N).reshape((-1,) + self._part_shape)
                print('samples', samples.shape)

                types = np.asarray(list(gv.multirange(*[2]*D))).reshape((-1,) + self._part_shape)
                th = np.clip(types, 0.01, 0.99)

                t = th[:,np.newaxis]
                x = samples[np.newaxis]

                llh0 = x * np.log(t) + (1 - x) * np.log(1 - t)
                counts0 = np.bincount(np.argmax(llh0.sum(-1).sum(-1), 0), minlength=th.shape[0])
                
                x1 = patches[np.newaxis,...,0]
                llh1 = x1 * np.log(t) + (1 - x1) * np.log(1 - t)
                counts1 = np.bincount(np.argmax(llh1.sum(-1).sum(-1), 0), minlength=th.shape[0])

                #import pdb; pdb.set_trace()

                w0 = counts0 / counts0.sum()
                w1 = counts1 / counts1.sum()

                print('w0', w0)
                print('w1', w1) 
                #import pdb; pdb.set_trace()


            #import pdb; pdb.set_trace()
            self._parts = mm.means_.reshape((self._num_parts,)+patches.shape[1:])
            self._weights = mm.weights_
        else:
            #mm = ag.stats.BernoulliMixture(self._num_parts, flatpatches, max_iter=2000)
            #mm.run_EM(1e-6, min_probability=min_prob)
            #self._parts = mm.templates.reshape((self._num_parts,)+patches.shape[1:])
            #self._weights = mm.weights
            from pnet.bernoulli import em
            ret = em(flatpatches, self._num_parts,20,numpy_rng=self._settings.get('em_seed',0),verbose=True)
            self._parts = ret[1].reshape((self._num_parts,) + patches.shape[1:])
            self._weights = np.arange(self._num_parts)
        if 0:
            predictedGroups = mm.predict(flatpatches) 
        
        # Calculate entropy of parts
        Hall = (self._parts * np.log(self._parts) + (1 - self._parts) * np.log(1 - self._parts))
        H = -np.apply_over_axes(np.mean, Hall, [1, 2, 3])[:,0,0,0]
コード例 #7
0
    def train_from_samples(self, raw_patches, raw_originals):
        min_prob = self._settings.get('min_prob', 0.01)
        print(raw_patches.shape)
        print(raw_originals.shape)
        ORI = self._num_orientations
        POL = self._settings.get('polarities', 1)
        P = ORI * POL

        def cycles(X):
            return np.asarray([np.concatenate([X[i:], X[:i]]) for i in range(len(X))])

        RR = np.arange(ORI)
        PP = np.arange(POL)
        II = [list(itr.product(PPi, RRi)) for PPi in cycles(PP) for RRi in cycles(RR)]
        lookup = dict(zip(itr.product(PP, RR), itr.count()))
        n_init = self._settings.get('n_init', 1)
        n_iter = self._settings.get('n_iter', 10)
        seed = self._settings.get('em_seed', 0)


        num_angle = ORI
        d = np.prod(raw_patches.shape[2:])
        permutation = np.empty((num_angle, num_angle * d), dtype=np.int_)
        for a in range(num_angle):
            if a == 0:
                permutation[a] = np.arange(num_angle * d)
            else:
                permutation[a] = np.roll(permutation[a-1], d)
    
        from pnet.bernoulli import em      

        X = raw_patches.reshape((raw_patches.shape[0], -1))
        print(X.shape)
        if 1:
            ret = em(X, self._num_true_parts, n_iter,
                     mu_truncation=min_prob,
                     permutation=permutation, numpy_rng=seed,
                     verbose=True)

            comps = ret[3]
            self._parts = ret[1].reshape((self._num_true_parts * P,) + raw_patches.shape[2:])

            if comps.ndim == 1:
                comps = np.vstack([comps, np.zeros(len(comps), dtype=np.int_)]).T

        else:
            
            permutations = np.asarray([[lookup[ii] for ii in rows] for rows in II])

            from pnet.permutation_mm import PermutationMM
            mm = PermutationMM(n_components=self._num_true_parts, permutations=permutations, n_iter=n_iter, n_init=n_init, random_state=seed, min_probability=min_prob)

            Xflat = raw_patches.reshape(raw_patches.shape[:2] + (-1,))
            mm.fit(Xflat)


            comps = mm.predict(Xflat)

            self._parts = mm.means_.reshape((mm.n_components * P,) + raw_patches.shape[2:])

        if 0:
            # Reject some parts
            pp = self._parts[::self._num_orientations]
            Hall = -(pp * np.log2(pp) + (1 - pp) * np.log2(1 - pp))
            H = np.apply_over_axes(np.mean, Hall, [1, 2, 3]).ravel()

            from scipy.stats import scoreatpercentile

            Hth = scoreatpercentile(H, 50)
            ok = H <= Hth

            blocks = []
            for i in range(self._num_true_parts):
                if ok[i]:
                    blocks.append(self._parts[i*self._num_orientations:(i+1)*self._num_orientations])
            
            self._parts = np.concatenate(blocks)
            self._num_parts = len(self._parts)
            self._num_true_parts = self._num_parts // self._num_orientations 

        if 0:
            from pylab import cm
            grid2 = pnet.plot.ImageGrid(self._num_true_parts, 8, raw_originals.shape[2:])
            for n in range(self._num_true_parts):
                for e in range(8):
                    grid2.set_image(self._parts[n * self._num_orientations,...,e], n, e, vmin=0, vmax=1, cmap=cm.RdBu_r)
            grid2.save(vz.generate_filename(), scale=5)

        self._train_info['counts'] = np.bincount(comps[:,0], minlength=self._num_true_parts)

        print(self._train_info['counts'])

        self._visparts = np.asarray([
            raw_originals[comps[:,0]==k,comps[comps[:,0]==k][:,1]].mean(0) for k in range(self._num_true_parts)             
        ])

        if 0:

            XX = [
                raw_originals[comps[:,0]==k,comps[comps[:,0]==k][:,1]] for k in range(self._num_true_parts)             
            ]

            N = 100


            m = self._train_info['counts'].argmax()
            mcomps = comps[comps[:,0] == m]

            raw_originals_m = raw_originals[comps[:,0] == m]

            if 0:
                grid0 = pnet.plot.ImageGrid(N, self._num_orientations, raw_originals.shape[2:], border_color=(1, 1, 1))
                for i in range(min(N, raw_originals_m.shape[0])): 
                    for j in range(self._num_orientations):
                        grid0.set_image(raw_originals_m[i,(mcomps[i,1]+j)%self._num_orientations], i, j, vmin=0, vmax=1, cmap=cm.gray)

                grid0.save(vz.generate_filename(), scale=3)

                grid0 = pnet.plot.ImageGrid(self._num_true_parts, N, raw_originals.shape[2:], border_color=(1, 1, 1))
                for m in range(self._num_true_parts):
                    for i in range(min(N, XX[m].shape[0])):
                        grid0.set_image(XX[m][i], m, i, vmin=0, vmax=1, cmap=cm.gray)
                        #grid0.set_image(XX[i][j], i, j, vmin=0, vmax=1, cmap=cm.gray)

                grid0.save(vz.generate_filename(), scale=3)

                grid1 = pnet.plot.ImageGrid(1, self._num_true_parts, raw_originals.shape[2:], border_color=(1, 1, 1))
                for m in range(self._num_true_parts):
                    grid1.set_image(self._visparts[m], 0, m, vmin=0, vmax=1, cmap=cm.gray)

                grid1.save(vz.generate_filename(), scale=5)
コード例 #8
0
    def train(self, X_n, Y, OriginalX = None):
        X = X_n[0]
        num_parts = X_n[1]
        if(len(X_n) == 3):
            num_orientations = X_n[2]
        else:
            num_orientations = 1
        num_true_parts = num_parts // num_orientations

        self._extra['training_comp'] = []

        K = Y.max() + 1

        mm_models = []
        print(X.shape)
        for k in xrange(K):
            Xk = X[Y == k]

            assert Xk.shape[-1] == 1


            from pnet.cyfuncs import index_map_pooling_multi, orientation_pooling

            # Rotate all the Xk samples

            print('A')
            XB = index_map_pooling_multi(Xk, num_parts, (1, 1), (1, 1))
            print('B')
            XB = XB.reshape(XB.shape[:-1] + (num_true_parts, num_orientations))

            blocks = [] 

            print('C')
            for ori in xrange(0, self._n_orientations):
                angle = ori / self._n_orientations * 360
                # Rotate all images, apply rotational spreading, then do pooling

                if 0:
                    print(ori, 'R{')
                    rots = np.asarray([rotate_patch_map(XB[i], angle) for i in xrange(XB.shape[0])])
                    print(ori, 'R}')


                    print(ori, 'P{')
                    yy = orientation_pooling(rots, 
                                             self._pooling_settings['shape'],
                                             self._pooling_settings['strides'],
                                             self._pooling_settings.get('rotation_spreading_radius', 0))
                    print(ori, 'P}')

                from pnet.cyfuncs import rotate_index_map_pooling 


                if num_orientations !=1:
                    yy1 = rotate_index_map_pooling(Xk[...,0], angle, self._pooling_settings.get('rotation_spreading_radius', 0),
                                               num_orientations, 
                                               num_parts,
                                               self._pooling_settings['shape'])
                else:
                    from pnet.cyfuncs import index_map_pooling_multi as poolf
                    print(Xk.shape)
                    yy1 = poolf(Xk,num_parts,self._pooling_settings.get('shape'), self._pooling_settings.get('strides')) 
                print(yy1.shape, num_orientations, num_true_parts)
                yy = yy1.reshape(yy1.shape[:3] + (num_orientations, num_true_parts))
                blocks.append(yy)#.reshape(yy.shape[:-2] + (-1,)))

            blocks = np.asarray(blocks).transpose((1, 0, 2, 3, 4, 5))
            print('D')

            if 0:
                from pnet.vzlog import default as vz

                import gv

                for i in xrange(self._n_orientations):
                    gv.img.save_image(vz.generate_filename(), blocks[0,i,:,:,0].sum(-1))

                vz.finalize()

            shape = blocks.shape[2:4] + (np.prod(blocks.shape[4:]),)

            # Flatten
            blocks = blocks.reshape(blocks.shape[:2] + (-1,))

            n_init = self._settings.get('n_init', 1)
            n_iter = self._settings.get('n_iter', 10)
            seed = self._settings.get('em_seed', 0)

            ORI = self._n_orientations
            POL = 1

            P = ORI * POL


            def cycles(X):
                return np.asarray([np.concatenate([X[i:], X[:i]]) for i in xrange(len(X))])

            RR = np.arange(ORI)
            PP = np.arange(POL)
            II = [list(itr.product(PPi, RRi)) for PPi in cycles(PP) for RRi in cycles(RR)]
            lookup = dict(zip(itr.product(PP, RR), itr.count()))
            permutations = np.asarray([[lookup[ii] for ii in rows] for rows in II])

            print('E')

            if 0:
                mm = PermutationMM(n_components=self._n_components, 
                                   permutations=permutations,
                                   n_iter=n_iter, 
                                   n_init=n_init, 
                                   random_state=seed, 
                                   min_probability=self._min_prob)
                mm.fit(blocks)
                mu = mm.means_.reshape((self._n_components,)+shape)

            else:       
                num_angle = self._n_orientations
                d = np.prod(shape)
                permutation = np.empty((num_angle, num_angle * d), dtype=np.int_)
                for a in range(num_angle):
                    if a == 0:
                        permutation[a] = np.arange(num_angle * d)
                    else:
                        permutation[a] = np.roll(permutation[a-1], -d)

                from pnet.bernoulli import em      


                XX = blocks.reshape((blocks.shape[0], -1))
                print('F')
                ret = em(XX, self._n_components, n_iter,
                         permutation=permutation, numpy_rng=seed,
                         verbose=True)
                print('G')

                self._extra['training_comp'].append(ret[3])

                mu = ret[1].reshape((self._n_components * self._n_orientations,) + shape)


            
            mm_models.append(mu)
            print('H')


        self._models = np.asarray(mm_models)
コード例 #9
0
    for i in range(trainingDataNum):
        for j in extractedFeature[i]:
            if j!=-1:
                k[j]+=1
    numSecondLayerParts = 10 

    print(k) 
    secondLayerPermutation = np.empty((num_rot, num_rot * num_rot * numParts), dtype = np.int_)
    for a in range(num_rot):
        if a == 0:
            secondLayerPermutation[a] = np.arange(num_rot * num_rot * numParts)
        else:
            secondLayerPermutation[a] = np.roll(secondLayerPermutation[a - 1], num_rot * num_rot * numParts)

    from pnet.bernoulli import em
    superParts = [em(np.array(partsRegion[i]).reshape(len(partsRegion[i]),-1),numSecondLayerParts,permutation=secondLayerPermutation,verbose=True) for i in range(numParts)]     
    allVisParts = []
    for i in range(numParts):
        print(superParts[i][3].shape)
        comps = np.array(superParts[i][3])
        raw_originals = np.array(originalPartsList[i])
        print(raw_originals.shape, comps.shape)
        visParts = np.asarray([raw_originals[comps[:,0]==k,(comps[comps[:,0]==k][:,1]-1)%num_rot].mean(0) for k in range(numParts)])
        allVisParts.append(visParts)

    gr.images(np.array(originalPartsList[0])[:20,0],show=False,zero_to_one=False, vmin=0,vmax=1,fileName = 'test.png')
    print(superParts[0][3][:20,:]) 


    print(shiftRotationLayer._visparts.shape)
    """