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)
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)
def train_from_samples(self, raw_patches, raw_originals): min_prob = self._settings['min_prob'] 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) permutations = [[lookup[ii] for ii in rows] for rows in II] permutations = np.asarray(permutations) 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) ml = self._num_true_parts counts = np.bincount(comps[:, 0], minlength=ml) ag.info('Training counts:', counts) # Reject some parts ok = counts >= self._settings['min_count'] ag.info('Keeping', ok.sum(), 'out of', ok.size, 'parts') self._num_true_parts = ok.sum() mm.means_ = mm.means_[ok] mm.weights_ = mm.weights_[ok] counts_final = counts[ok] # Store info sh = (self._num_true_parts * P,) + raw_patches.shape[2:] self._parts = mm.means_.reshape(sh) self._parts_vis = self._parts.copy() if self._settings['circular']: sh = raw_patches.shape[2:4] assert sh[0] == sh[1], 'Must use square parts with circular' side = sh[0] off = -(side - 1) / 2 # Remove edges and make circular x, y = np.meshgrid(np.arange(side) + off, np.arange(side) + off) mask = (x ** 2) + (y ** 2) <= (side / 2) ** 2 mask0 = mask[np.newaxis, ..., np.newaxis] # We'll set them to any value (0.1). This could use better handling # if so that the likelihoods aren't ruined. self._parts = mask0 * self._parts + ~mask0 * 0.1 self._parts_vis[:, ~mask, :] = np.nan from vzlog import default as vz from pylab import cm grid = ag.plot.ImageGrid(mask, cmap=cm.jet) grid.save(vz.impath(), scale=10) self._train_info['counts_initial'] = counts self._train_info['counts'] = counts_final # Visualize parts : we iterate only over 'ok' ones self._visparts = np.asarray([ raw_originals[comps[:, 0] == k, comps[comps[:, 0] == k][:, 1]].mean(0) for k in np.where(ok)[0] ]) self._preprocess()
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)