def mvm_form_prod_reorder(self,X,xfactors): xdata=[[],[],[]] ## row index, column index , value (m,n)=X.shape X2=np.dot(X,X.T) if m==400: print(m,n) ctensor=tensor_decomp.cls_tensor_decomp() ctensor.load(X2,xfactors) X2=ctensor.reorder_in_x(X2) (m2,n2)=X2.shape xmean=np.mean(X2) xstd=np.std(X2) lbound=xmean-5*xstd ubound=xmean+5*xstd k=0 icount=0 for i in range(m2): for j in range(n2): v=X2[i,j] if v>=lbound and v<=ubound: xdata[0].append(i) xdata[1].append(j) xdata[2].append(v) k+=1 icount+=1 xdata=mvm_prepare.sort_table(xdata,ifloat=1) return(xdata,m2,n2)
def invert(self,xmatre): ctensor=tensor_decomp.cls_tensor_decomp() xmatout=ctensor.invert_reorder_in_x(xmatre,self.xfactors) return(xmatout)
def load_mmr(self, cData, lviews): ldata_labels = self.read_raw_txt(self.sbasedir, self.labelfile) if self.loadFurther == 1: ldata_labels.append([self.evaldir, 1]) mdata = len(ldata_labels) ## !!!!!!!!!!!!!!!!!!!!!!!!!! ## fixed number of categories od sides ncategory = 4 ## ncategory=3 category_recode = [0, 1, 2, 3] ## #################################### X_out = np.zeros((mdata, ncategory)) for i in range(mdata): ## X_out[i,int(ldata_labels[i][1])-1]=1 icategory = int(ldata_labels[i][1]) - 1 if icategory == 0: X_out[i, category_recode[icategory]] = 1 else: if ncategory == 3: X_out[i, category_recode[icategory]] = 1 else: X_out[i, category_recode[icategory]] = 1 ## number of views: joint force, cart force, cart torque nview = len(self.linputmask) ## find common minimum length of trajectories xlength = np.zeros(mdata) for i in range(mdata): X = np.loadtxt(ldata_labels[i][0], skiprows=1) xlength[i] = X.shape[0] ixin_type = 0 ## =0 simple vectorized =1 angles ixin_decompose = 0 ## =0 no, =1 tensor decomposition icumulant = 0 ## =0 no cumulants, =1 cumulants are the feature components nmin = xlength.min() ## !!!!!!!!!!!!!!!!!!!!!! if nmin > 240: if ixin_decompose == 1: nmin = 240 ## 8*6*5 tfactors = (30, 8, 1, 1) nfactor = len(tfactors) nlayer = 1 ncumulant = 5 if ncumulant > tfactors[0]: ncumulant = tfactors[0] if icumulant == 0: nitem = tfactors[0] else: nitem = ncumulant if ixin_type == 0: nboot = 1 if ixin_decompose == 0: X_in = [] for i in range(nview): nslice = self.linputmask[i][1] - self.linputmask[i][0] X_in.append(np.zeros((mdata, nboot * nmin * nslice))) elif ixin_decompose == 1: X_in = [] for i in range(nview): nslice = self.linputmask[i][1] - self.linputmask[i][0] X_in.append(np.zeros((mdata, nlayer * nitem * nslice))) for i in range(mdata): ## if i==18: ## print(i) X = np.loadtxt(ldata_labels[i][0], skiprows=1) X = X[:nmin, :] if ixin_decompose == 0: for j in range(nview): i0 = self.linputmask[j][0] ik = self.linputmask[j][1] ## fX=X[:,i0:ik].ravel() ## fX=np.sort(fX) ## X_in[j][i]=fX X_in[j][i] = X[:, i0:ik].ravel() elif ixin_decompose == 1: for j in range(nview): i0 = self.linputmask[j][0] ik = self.linputmask[j][1] xmatrix = X[:, i0:ik] ## xamtrix=spsignal.savgol_filter(xmatrix,7,5,deriv=0,axis=0, \ ## mode='interp') ## xfactors assume 240 rows xfactors=np.array([[tfactors[0],ik-i0],[tfactors[1],1], \ [tfactors[2],1],[tfactors[3],1]]) ctensor = tensor_decomp.cls_tensor_decomp() ctensor.load(xmatrix, xfactors) niter = 10 ctensor.decompose2(niter) nslice = self.linputmask[j][1] - self.linputmask[j][0] fX = np.zeros(nlayer * nitem * nslice) ipos = 0 for ilayer in range(nlayer): for ifactor in range(1): zfact = ctensor.lcomponents[ilayer][ifactor] (mzfact, nzfact) = zfact.shape for icol in range(nzfact): if np.mean(zfact[:, icol]) < 0: zfact[:, icol] = -zfact[:, icol] if icumulant == 1: for icol in range(nzfact): xcumulants = cumulants(zfact[:, icol], ncumulant, icentral=1) fX[ipos:ipos + nitem] = xcumulants ipos += nitem else: ## (u,s,v)=np.linalg.svd(zfact) ## zfact=np.dot(u[:,:nzfact],np.diag(s[:nzfact])) for icol in range(nzfact): fX[ipos:ipos + mzfact] = zfact[:, icol] ipos += mzfact ## fX=np.sort(fX) X_in[j][i] = fX elif ixin_type == 1: X_in = [np.zeros((mdata, nmin - 1)) for i in range(nview)] for i in range(mdata): X = np.loadtxt(self.sbasedir + ldata_labels[i][0], skiprows=1) X = X[:nmin, :] for j in range(nview): i0 = self.linputmask[j][0] ik = self.linputmask[j][1] X_in[j][i] = angle_moment(X[:, i0:ik]) ## subspace output kernel cData.YKernel = mmr_kernel_explicit.cls_feature(ifeature=0) cData.YKernel.load_data(X_out, ifeature=0) cData.YKernel.ifeature = 0 cData.YKernel.title = 'output' ## setting output parameters cparams = cls_initial_params() cData.YKernel.kernel_params.set(cparams.get_yparams('kernel', 0)) ## cData.YKernel.prekernel_params.set(cparams.get_yinparams('kernel',0)) cData.YKernel.crossval.set(cparams.get_yparams('cross', 0)) cData.YKernel.norm.set(cparams.get_yparams('norm', 0)) idata = 0 for iview in range(nview): if iview in lviews: cData.XKernel[idata] = mmr_kernel_explicit.cls_feature( ifeature=0) cData.XKernel[idata].load_data(X_in[iview], ifeature=0) cData.XKernel[idata].title = 'input_' + str(iview) ## setting input parameters cData.XKernel[idata].kernel_params.set(cparams. \ get_xparams('kernel',idata)) ## cData.XKernel[idata].prekernel_params.set(cparams. \ ## get_xinparams('kernel',idata)) cData.XKernel[idata].crossval.set( cparams.get_xparams('cross', idata)) cData.XKernel[idata].norm.set( cparams.get_xparams('norm', idata)) idata += 1 cData.ninputview = idata ## set active views cData.mdata = cData.YKernel.dataraw.shape[0] cData.nfold = 2 cData.nrepeat = 2 cData.kmode = 1 ## =0 additive (feature concatenation) ## =1 multiplicative (fetaure tensor product) return
def load(self,ifile): if ifile==0: sdir='/home/szedmak/data/image_text/labeled_images/' sfile='/cambridge/Image007.png' xmatpil=pilimg.open(sdir+sfile) xmatrix=np.array(xmatpil.resize((768,512))) ## 504,756 -> 512,768 self.xfactors=np.array([[32,48,1],[16,16,1],[1,1,3]]) elif ifile==1: sdir='/home/szedmak/src/python/algebra/tensor_decomp/' sfile='handwritten.png' xmatpil=pilimg.open(sdir+sfile) xmatrix=np.array(xmatpil.resize((1024,1024))) ## 504,756 -> 512,768 self.xfactors=np.array([[32,32],[32,32]]) elif ifile==2: sdir='/home/szedmak/src/python/algebra/tensor_decomp/' sfile='tilling.png' xmatpil=pilimg.open(sdir+sfile) xmatrix=np.array(xmatpil.resize((576,512))) ## 504,756 -> 512,768 ## xmatrix=np.mean(xmatrix,axis=2) self.xfactors=np.array([[32,36,1],[16,16,1],[1,1,3]]) ## tilling elif ifile==3: sdir='/home/szedmak/src/python/algebra/tensor_decomp/' sfile='poligon.png' xmatpil=pilimg.open(sdir+sfile) xmatrix=np.array(xmatpil.resize((512,512))) ## 504,756 -> 512,768 self.xfactors=np.array([[32,32,1],[16,16,1],[1,1,3]]) ## poligon elif ifile==4: sdir='/home/szedmak/src/python/algebra/tensor_decomp/' sfile='2008toyroom.png' xmatpil=pilimg.open(sdir+sfile) xmatrix=np.array(xmatpil.resize((768,512))) ## 504,756 -> 512,768 ## xmatrix=np.mean(xmatrix,axis=2) self.xfactors=np.array([[32,48,1],[16,16,1],[1,1,3]]) elif ifile==5: sdir='/home/szedmak/src/python/algebra/tensor_decomp/' sfile='PLayroom_How_to_Organize_Kids_Room_2013.png' xmatpil=pilimg.open(sdir+sfile) xmatrix=np.array(xmatpil.resize((768,512))) ## 504,756 -> 512,768 self.xfactors=np.array([[32,48,1],[16,16,1],[1,1,3]]) print(sdir,sfile) print(self.xfactors) self.baseline(xmatrix) ctensor=tensor_decomp.cls_tensor_decomp() ctensor.load(xmatrix,self.xfactors) X=ctensor.reorder_in_x(xmatrix) ## X=X.T tshape=X.shape mi=tshape[0] mj=tshape[1] ## if len(tshape)>2: ## mk=tshape[2] ## X=X/self.image_scale xdata=[[],[],[]] ## row index, column index , value for i in range(mi): for j in range(mj): xdata[0].append(i) xdata[1].append(int(np.mean(X[i,j]))) xdata[2].append(X[i,j]/self.image_scale) xdata=mvm_prepare.sort_table(xdata,ifloat=1) m1=mi ## m2=mj m2=256 return(xdata,m1,m2)
def load_mmr(self, cData, lviews): ldata_labels = self.read_raw_txt(self.sbasedir,self.labelfile) if self.loadFurther==1: ldata_labels.append([self.evaldir, 1]) mdata=len(ldata_labels) ## !!!!!!!!!!!!!!!!!!!!!!!!!! ## fixed number of categories od sides ncategory=4 ## ncategory=3 category_recode=[0,1,2,3] ## #################################### X_out=np.zeros((mdata,ncategory)) for i in range(mdata): ## X_out[i,int(ldata_labels[i][1])-1]=1 icategory=int(ldata_labels[i][1])-1 if icategory==0: X_out[i,category_recode[icategory]]=1 else: if ncategory==3: X_out[i,category_recode[icategory]]=1 else: X_out[i,category_recode[icategory]]=1 ## number of views: joint force, cart force, cart torque nview=len(self.linputmask) ## find common minimum length of trajectories xlength=np.zeros(mdata) for i in range(mdata): X=np.loadtxt(ldata_labels[i][0],skiprows=1) xlength[i]=X.shape[0] ixin_type=0 ## =0 simple vectorized =1 angles ixin_decompose=0 ## =0 no, =1 tensor decomposition icumulant=0 ## =0 no cumulants, =1 cumulants are the feature components nmin=xlength.min() ## !!!!!!!!!!!!!!!!!!!!!! if nmin>240: if ixin_decompose==1: nmin=240 ## 8*6*5 tfactors=(30,8,1,1) nfactor=len(tfactors) nlayer=1 ncumulant=5 if ncumulant>tfactors[0]: ncumulant=tfactors[0] if icumulant==0: nitem=tfactors[0] else: nitem=ncumulant if ixin_type==0: nboot=1 if ixin_decompose==0: X_in=[] for i in range(nview): nslice=self.linputmask[i][1]-self.linputmask[i][0] X_in.append(np.zeros((mdata,nboot*nmin*nslice))) elif ixin_decompose==1: X_in=[] for i in range(nview): nslice=self.linputmask[i][1]-self.linputmask[i][0] X_in.append(np.zeros((mdata,nlayer*nitem*nslice))) for i in range(mdata): ## if i==18: ## print(i) X=np.loadtxt(ldata_labels[i][0],skiprows=1) X=X[:nmin,:] if ixin_decompose==0: for j in range(nview): i0=self.linputmask[j][0] ik=self.linputmask[j][1] ## fX=X[:,i0:ik].ravel() ## fX=np.sort(fX) ## X_in[j][i]=fX X_in[j][i]=X[:,i0:ik].ravel() elif ixin_decompose==1: for j in range(nview): i0=self.linputmask[j][0] ik=self.linputmask[j][1] xmatrix=X[:,i0:ik] ## xamtrix=spsignal.savgol_filter(xmatrix,7,5,deriv=0,axis=0, \ ## mode='interp') ## xfactors assume 240 rows xfactors=np.array([[tfactors[0],ik-i0],[tfactors[1],1], \ [tfactors[2],1],[tfactors[3],1]]) ctensor=tensor_decomp.cls_tensor_decomp() ctensor.load(xmatrix,xfactors) niter=10 ctensor.decompose2(niter) nslice=self.linputmask[j][1]-self.linputmask[j][0] fX=np.zeros(nlayer*nitem*nslice) ipos=0 for ilayer in range(nlayer): for ifactor in range(1): zfact=ctensor.lcomponents[ilayer][ifactor] (mzfact,nzfact)=zfact.shape for icol in range(nzfact): if np.mean(zfact[:,icol])<0: zfact[:,icol]=-zfact[:,icol] if icumulant==1: for icol in range(nzfact): xcumulants=cumulants(zfact[:,icol],ncumulant,icentral=1) fX[ipos:ipos+nitem]=xcumulants ipos+=nitem else: ## (u,s,v)=np.linalg.svd(zfact) ## zfact=np.dot(u[:,:nzfact],np.diag(s[:nzfact])) for icol in range(nzfact): fX[ipos:ipos+mzfact]=zfact[:,icol] ipos+=mzfact ## fX=np.sort(fX) X_in[j][i]=fX elif ixin_type==1: X_in=[ np.zeros((mdata,nmin-1)) for i in range(nview)] for i in range(mdata): X=np.loadtxt(self.sbasedir+ldata_labels[i][0],skiprows=1) X=X[:nmin,:] for j in range(nview): i0=self.linputmask[j][0] ik=self.linputmask[j][1] X_in[j][i]=angle_moment(X[:,i0:ik]) ## subspace output kernel cData.YKernel=mmr_kernel_explicit.cls_feature(ifeature=0) cData.YKernel.load_data(X_out,ifeature=0) cData.YKernel.ifeature=0 cData.YKernel.title='output' ## setting output parameters cparams=cls_initial_params() cData.YKernel.kernel_params.set(cparams.get_yparams('kernel',0)) ## cData.YKernel.prekernel_params.set(cparams.get_yinparams('kernel',0)) cData.YKernel.crossval.set(cparams.get_yparams('cross',0)) cData.YKernel.norm.set(cparams.get_yparams('norm',0)) idata=0 for iview in range(nview): if iview in lviews: cData.XKernel[idata]=mmr_kernel_explicit.cls_feature(ifeature=0) cData.XKernel[idata].load_data(X_in[iview],ifeature=0) cData.XKernel[idata].title='input_'+str(iview) ## setting input parameters cData.XKernel[idata].kernel_params.set(cparams. \ get_xparams('kernel',idata)) ## cData.XKernel[idata].prekernel_params.set(cparams. \ ## get_xinparams('kernel',idata)) cData.XKernel[idata].crossval.set(cparams.get_xparams('cross',idata)) cData.XKernel[idata].norm.set(cparams.get_xparams('norm',idata)) idata+=1 cData.ninputview=idata ## set active views cData.mdata=cData.YKernel.dataraw.shape[0] cData.nfold=2 cData.nrepeat=2 cData.kmode=1 ## =0 additive (feature concatenation) ## =1 multiplicative (fetaure tensor product) return