def extract(self, X_F): X = X_F[0] F = X_F[1] #X = X.astype(np.float16) #if X.ndim == 3: #from pnet.cyfuncs import index_map_pooling as poolf #else: #print(np.any(np.isnan(X))) #print(np.all(np.isfinite(X))) print ("inside extrating") print(X.shape) print(X.dtype) support_mask = self._settings.get('support_mask') relu = self._settings.get("relu", True) if support_mask is not None: from pnet.cyfuncs import activation_map_pooling as poolf #feature_map = poolf(X, support_mask.astype(np.uint8), F, self._shape, self._strides) else: from pnet.cyfuncs import activation_map_pooling as poolf feature_map_list = [] batch_number = 500 batch_size = X.shape[0] // batch_number for i in range(batch_number): #print i feature_map_list.append(poolf(X[i * batch_size : min((i+1) * batch_size, X.shape[0])].astype(np.float32), F, self._shape, self._strides, relu).astype(np.float16)) feature_map = np.vstack(feature_map_list) feature_map.astype(np.float16) self._extract_info['concentration'] = np.apply_over_axes(np.mean, feature_map, [0, 1, 2])[0,0,0] ag.info("finish pooling") #print(feature_map.shape) print(np.any(np.isnan(feature_map))) print(np.all(np.isfinite(feature_map))) return feature_map
def extract(self, X_F): X = X_F[0] F = X_F[1] #if X.ndim == 3: #from pnet.cyfuncs import index_map_pooling as poolf #else: support_mask = self._settings.get('support_mask') if support_mask is not None: from pnet.cyfuncs import index_map_pooling_multi_support as poolf feature_map = poolf(X, support_mask.astype(np.uint8), F, self._shape, self._strides) else: from pnet.cyfuncs import index_map_pooling_multi as poolf feature_map = poolf(X, F, self._shape, self._strides) self._extract_info['concentration'] = np.apply_over_axes(np.mean, feature_map, [0, 1, 2])[0,0,0] return feature_map
def _extract(self, phi, data): Z, F = phi(data)[:2] from pnet.cyfuncs import index_map_pooling_multi as poolf X = poolf(Z, F, self._pooling_settings['shape'], self._pooling_settings['strides']) XX = X[:, np.newaxis, np.newaxis] theta = self._models[np.newaxis] llh = XX * np.log(theta) + (1 - XX) * np.log(1 - theta) bb = np.apply_over_axes(np.sum, llh, [-3, -2, -1])[..., 0, 0, 0] if self._settings.get('return_latent_rotation'): yhat = np.vstack([bb.max(-1).argmax(1), bb.max(1).argmax(-1)]).T else: yhat = np.argmax(bb.max(-1), axis=1) return yhat
def extract(self,X_all, Y = None, test_accuracy = False): ag.info("randomPartition SVM start extracting") outer_frame = self._settings.get('outer_frame', 0) dim = (X_all.shape[1],X_all.shape[2]) if outer_frame != 0: XX = np.zeros((X_all.shape[0], X_all.shape[1] + 2 * outer_frame, X_all.shape[2] + 2 * outer_frame, X_all.shape[3]), dtype=np.float16) XX[:, outer_frame:X_all.shape[1] + outer_frame, outer_frame:X_all.shape[2] + outer_frame,:] = X_all X_all = XX numcl = 2 print("Before blowing up the memory") roundNumber = 100 numEachRound = X_all.shape[0] // roundNumber feature_map_list = [] for round in range(roundNumber): print(round) X = np.array(X_all[numEachRound * round : numEachRound * (round + 1)], dtype = np.float16) X_num = X.shape[0] if(numcl == 2): feature_map = np.zeros((X.shape[0],) + dim + (self._num_parts,),dtype=np.float16) else: feature_map = np.zeros((X.shape[0],) + dim + (numcl, self._num_parts,),dtype=np.float16) import itertools argList = list(itertools.product(range(dim[0]),range(dim[1]))) p = Pool(4) args = ((x, y, self._coef,self._intercept, X[:,x:x+self._part_shape[0], y:y+self._part_shape[1],:].reshape(X_num,-1).astype(np.float16)) for(x,y) in argList ) count = 0 for x, y, score in p.imap(calcScore, args): feature_map[:,x, y, :] = score count+=1 p.terminate() # Start to do pooling relu = self._settings.get("relu", True) from pnet.cyfuncs import activation_map_pooling as poolf feature_map_list.append(poolf(feature_map.astype(np.float32), self._num_parts, self._shape, self._strides, relu).astype(np.float16)) result_map = np.vstack(feature_map_list) result_map.astype(np.float16) print(np.any(np.isnan(result_map))) print(np.all(np.isfinite(result_map))) return result_map
def extract(self, Z_): Z = Z_[0] F = Z_[1] from pnet.cyfuncs import index_map_pooling_multi as poolf X = poolf(Z, F, self._pooling_settings['shape'], self._pooling_settings['strides']) theta = self._models[np.newaxis] Yhat = np.zeros(X.shape[0]) #print('Z', Z.shape) #print('mm', mm.shape) if 1: blockSize = 1000 for i in range(0, X.shape[0],blockSize): blockend = min(X.shape[0], i + blockSize) X_part = X[i:blockend] XX = X_part[:,np.newaxis,np.newaxis] llh = XX * np.log(theta) + (1 - XX) * np.log(1 - theta) bb = np.apply_over_axes(np.sum, llh, [-3, -2, -1])[...,0,0,0] Yhat[i:blockend] = np.argmax(bb.max(-1), axis=1) return Yhat
def _extract(self, phi, data): X_F = phi(data) output_dtype = self._settings.get('output_dtype') if isinstance(X_F, tuple): X = X_F[0] F = X_F[1] #if X.ndim == 3: #from pnet.cyfuncs import index_map_pooling as poolf #else: shape = self.calc_shape(X.shape[1:3]) strides = self.calc_strides(X.shape[1:3]) if self._operation == 'max': from pnet.cyfuncs import index_map_pooling_multi as poolf feature_map = poolf(X, F, shape, strides) elif self._operation == 'sum': from pnet.cyfuncs import index_map_sum_pooling_multi as poolf feature_map = poolf(X, F, shape, strides) else: raise ValueError('Unknown pooling operation: {}'.format( self._operation)) c = ag.apply_once(np.mean, feature_map, [0, 1, 2], keepdims=False) self._extract_info['concentration'] = c if output_dtype is not None: return feature_map.astype(output_dtype) else: return feature_map else: X = X_F if self._operation == 'max': op = np.max elif self._operation == 'sum': op = np.sum elif self._operation == 'avg': op = np.mean else: raise ValueError('Unknown pooling operation: {}'.format( self._operation)) if self._final_shape is not None: fs = self._final_shape x_steps = np.arange(fs[0]+1) * X.shape[1] / fs[0] x_bounds = np.round(x_steps).astype(np.int_) y_steps = np.arange(fs[1]+1) * X.shape[2] / fs[1] y_bounds = np.round(y_steps).astype(np.int_) xs = enumerate(zip(x_bounds[:-1], x_bounds[1:])) ys = enumerate(zip(y_bounds[:-1], y_bounds[1:])) N = X.shape[0] F = X.shape[-1] feature_map = np.zeros((N,) + fs + (F,), dtype=output_dtype) for (i, (x0, x1)), (j, (y0, y1)) in itr.product(xs, ys): patch = X[:, x0:x1, y0:y1] feat = ag.apply_once(op, patch, [1, 2], keepdims=False) feature_map[:, i, j] = feat return feature_map else: raise NotImplementedError('Not yet')
for m in range(col//2): for n in range(row//2): for c in range(channel): result[i,m,n,c] = np.max(X[i,2*m:2*m + 2, 2*n:2*n + 2,c]) if(relu): result[i,m,n,c] = max(0,result[i,m,n,c]) return result X = np.random.rand(10000,20,20,100).astype(np.float32) F = 100 shape = (2,2) strides = (2,2) result_cython = poolf(X, F, shape, strides, False) diffArray = result_cython - python_poolf(X, F, shape, strides, False) diffArray = diffArray.reshape(diffArray.shape[0], -1) print(np.mean(np.mean(diffArray, axis = 0), axis = 0)) X = np.random.rand(10000,20,20,100).astype(np.float32) result_cython = poolf(X, F, shape, strides, True) diffArray = result_cython - python_poolf(X, F, shape, strides, True) diffArray = diffArray.reshape(diffArray.shape[0], -1) print(np.mean(np.mean(diffArray, axis = 0), axis = 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)