def derivativeCore(X,G,listofmatrices):#The entries are tensors #Firstterm=T.tensor(np.copy(mxnet_backend.to_numpy(X))) Firstterm=tl.tensor(X) Firstterm=Tensor_matrixproduct(Firstterm,Operations_listmatrices(listofmatrices,"Transpose")) #Secondterm=T.tensor(np.copy(mxnet_backend.to_numpy(G))) Secondterm=tl.tensor(G) Secondterm=Tensor_matrixproduct(Secondterm,Operations_listmatrices(listofmatrices,"Transposetimes")) Res=Firstterm-Secondterm return Res
def derivativeCore(X, G, listofmatrices): #This function computes the derivative of the differentiable part of the objective function with respect to G #All the parameters are of tensor type #Firstterm=T.tensor(np.copy(mxnet_backend.to_numpy(X))) Firstterm = tl.tensor(X) Firstterm = Tensor_matrixproduct( Firstterm, Operations_listmatrices(listofmatrices, "Transpose")) #Secondterm=T.tensor(np.copy(mxnet_backend.to_numpy(G))) Secondterm = tl.tensor(G) Secondterm = Tensor_matrixproduct( Secondterm, Operations_listmatrices(listofmatrices, "Transposetimes")) Res = Firstterm - Secondterm return Res
def ErrorSingle(args): #This function computes the square of the fitting error error = np.power( tl.norm( args[0][args[3]] - Tensor_matrixproduct(args[1][args[3]], args[2]), 2), 2) return error
def ErrorSingleALTO(args): A = tl.tensor(args[0][args[3]]) B = Tensor_matrixproduct( tl.tensor(args[1][args[3]]), Operations_listmatrices(args[2][args[3]], "Tensorize")) res = np.power(tl.norm(A - B, 2), 2) return res
def Error(X,G,listoffactors,setting,pool): #This function computes the fitting error in batch and online setting #All the parameters are of tensor type if(setting=="Single"): error=np.power(tl.norm(X-Tensor_matrixproduct(G,listoffactors),2),2) return error if(setting=="MiniBatch"): Errorlist=ErrorSet(X,G,listoffactors,pool) return np.sum(np.array(Errorlist))
def GenerateTensorsNonnegative(Numberofexamples,randomseed): np.random.seed(randomseed) Xtrain=np.random.rand(Numberofexamples,30,40,50) Coretensorsize=np.array([Numberofexamples,20,20,20]) Greal=np.maximum(np.random.normal(loc=0,scale=1,size=Coretensorsize),0) #The line below changes listoffactorsreal=[np.random.normal(loc=0,scale=1/10,size=(30,20)),np.random.normal(loc=0,scale=1/10,size=(40,20)),np.random.normal(loc=0,scale=1/10,size=(50,20))] listoffactorsreal=Nonnegativepart(listoffactorsreal) for n in range(Numberofexamples): Xtrain[n,:,:,:]=mxnet_backend.to_numpy(Tensor_matrixproduct(tl.tensor(Greal[n,:,:,:]),Operations_listmatrices(listoffactorsreal,"Tensorize"))) #Xtrain=Xtrain/T.norm(T.tensor(Xtrain),2) return Xtrain
def HOSVD(Tensor, Coretensorsize): N = len(Tensor.shape) listofmatrices = [] for n in range(N): U, s, V = np.linalg.svd(mxnet_backend.to_numpy(unfold(Tensor, n)), full_matrices=True) A = U[:, 0:Coretensorsize[n]] listofmatrices.append(A) Coretensor = Tensor_matrixproduct( tl.tensor(Tensor), Operations_listmatrices( Operations_listmatrices(listofmatrices, "Transpose"), "Tensorize")) Coretensor = mxnet_backend.to_numpy(Coretensor) return Coretensor, listofmatrices
def Sparse_code(X, G_init, listoffactors, Nonnegative, step, max_iter, alpha, theta, epsilon): #The parameters are tensors #This function is used to perform the sparse coding step #All the tensor and parameters are of tensor type #G_new=T.tensor(np.copy(mxnet_backend.to_numpy(G_init))) G_new = tl.tensor(G_init) G_old = tl.tensor(np.zeros(G_new.shape)) G_result = tl.tensor(np.zeros(G_new.shape)) Lambda = alpha * theta error = np.power( tl.norm(X - Tensor_matrixproduct(G_new, listoffactors), 2), 2) + Lambda * tl.norm(G_new, 1) previous_error = 0 nb_iter = 0 error_list = [error] while (nb_iter <= max_iter): nb_iter = nb_iter + 1 previous_error = error G_old = G_new G_new = G_old - step * derivativeCore(X, G_old, listoffactors) if (Nonnegative == True): G_new = np.maximum( G_old - step * (derivativeCore(X, G_old, listoffactors) + alpha * theta * np.ones(G_old.shape)), 0) if (Nonnegative == False): G_new = Proximal_operator(G_new, step) error = np.power( tl.norm(X - Tensor_matrixproduct(G_new, listoffactors), 2), 2) + Lambda * tl.norm(G_new, 1) G_result = G_new error_list.append(error) if (np.abs(previous_error - error) / error < epsilon): G_result = G_old error_list = error_list[0:len(error_list) - 1] break return G_result, error_list, nb_iter
def ALTO_single(X, Coretensorsize, K, Pre_existingfactors, sigma): #All the parameters are tensors ListoffactorsU = list(Pre_existingfactors) ListoffactorsV = Augmentlist(ListoffactorsU, K, sigma) Stilde = Tensor_matrixproduct( X, Operations_listmatrices(ListoffactorsV, "Transpose")) #core,factors=tucker(Stilde,Coretensorsize,init='random',random_state=2): this was the initial line core, factors = tucker(Stilde, Coretensorsize, init='random', random_state=1) Listoffactorsresult = [] for i in range(len(factors)): Listoffactorsresult.append( np.dot(mxnet_backend.to_numpy(ListoffactorsV[i]), mxnet_backend.to_numpy(factors[i]))) Listoffactorsresult = Operations_listmatrices(Listoffactorsresult, "Tensorize") return core, Listoffactorsresult
def OnlineTensorlearningallblocks(Similaritymatrix, listresponses, listpredictors, Rank, P, Q, M, K, mu, alpha, Methodname): #listresponses contain the data for two consecutive time samples Choleskysimilaritymatrix = np.copy(Similaritymatrix) Oldparamtensor = np.zeros((P, Q, M)) Coretensorsize = [Rank, Rank, Rank] rmselist = [] for m in range(M): #pdb.set_trace() Oldparamtensor[:, :, m] = np.dot(listresponses[0][:, :, m], np.linalg.pinv(listpredictors[0][:, :, m])) Core, Newloadingmatrices = HOSVD( tl.tensor(Oldparamtensor), Coretensorsize ) #tucker(tl.tensor(Oldparamtensor),Coretensorsize,init='svd',random_state=1) Newparametertensor = Tensor_matrixproduct( tl.tensor(Core), Operations_listmatrices(Newloadingmatrices, "Tensorize")) Oldloadingmatrices = [] for l in range(len(listresponses) - 1): ResponsetensorX = listresponses[l + 1] PredictortensorZ = listpredictors[l + 1] Oldparamtensor = Newparametertensor Oldloadingmatrices = Newloadingmatrices print("The block number is") print(l) Newparametertensor, Newloadingmatrices = OnlineTensorlearningsingleblock( Choleskysimilaritymatrix, mxnet_backend.to_numpy(Oldparamtensor), Oldloadingmatrices, ResponsetensorX, PredictortensorZ, alpha, M, K, Coretensorsize, Methodname) rmselist.append( RMSE(ResponsetensorX, mxnet_backend.to_numpy(Newparametertensor), PredictortensorZ)) return mxnet_backend.to_numpy(Newparametertensor), rmselist
def Reconstruction2d(Originalimage, patchsize, Coretensorsize, alpha, theta, val, pool): [I, J] = np.array(Originalimage.shape, dtype=int) Listrestauration = [] n0 = np.min([I, J]) Imrestored = np.zeros((I, J)) #nbx=int(np.floor(n0/patchsize)) nbpatches = int(np.floor(n0 / patchsize)) Setofpatches = np.zeros((patchsize, patchsize, nbpatches)) mask = Definemasksingleslice2d(Originalimage, val) Setofpatches = Patch_extractionallslices2d(Originalimage, mask, patchsize, val) Xtrain_set = [] for l in range(nbpatches): Xtrain_set.append(tl.tensor(Setofpatches[:, :, l])) max_iter = 100 period = 3 Nonnegative = True epsilon = np.power(10, -3, dtype=float) step = np.power(10, -5, dtype=float) #np.power(10,-6,dtype=float) Setting = "Single" nbepochs = 1 #5 Reprojectornot = False Minibatchsize = [] Pre_existingfactors = [] #Coretensorsize=[Rank,Rank,Rank] penaltylasso = alpha * theta Pre_existingG_settrain = [] for l in range(nbpatches): Pre_existingG_settrain.append( tl.tensor( np.maximum( np.random.normal(loc=0, scale=1, size=Coretensorsize), 0))) Pre_existingfactors = [ tl.tensor( np.maximum( np.random.normal(loc=0, scale=1, size=(patchsize, Coretensorsize[0])), 0)), tl.tensor( np.maximum( np.random.normal(loc=0, scale=1, size=(patchsize, Coretensorsize[1])), 0)) ] Pre_existingP = [ tl.tensor( np.random.normal(loc=0, scale=1, size=(patchsize, Coretensorsize[0]))), tl.tensor( np.random.normal(loc=0, scale=1, size=(patchsize, Coretensorsize[1]))) ] Pre_existingQ = [ tl.tensor( np.random.normal(loc=0, scale=1, size=(Coretensorsize[0], Coretensorsize[0]))), tl.tensor( np.random.normal(loc=0, scale=1, size=(Coretensorsize[1], Coretensorsize[1]))) ] #Dictionarymatrices,listobjectivefunctionvalues,Objectivefunction_per_epoch=CyclicBlocCoordinateTucker_setWithPredefinedEpochs(Xtrain_set,Coretensorsize,Pre_existingfactors,Pre_existingG_settrain,backendchoice,Pre_existingP,Pre_existingQ,Nonnegative,Reprojectornot,Setting,Minibatchsize,step,alpha,theta,max_iter,epsilon,period,nbepochs,pool) Starttime = time.time() Gresult4, Dictionarymatrices = ALTO_setWithpredefinedEpochs( Xtrain_set, Coretensorsize, Pre_existingfactors, 5, pool, 1, nbepochs) Endtime = time.time() Runningtime = Endtime - Starttime print("The running time is") print(Runningtime) pdb.set_trace() Dm = list(Dictionarymatrices) Dictionarymatricesconverted = Operations_listmatrices( Dictionarymatrices, "Arrayconversion") mask = Definemasksingleslice2d(Originalimage, val) #plt.imshow(mask) #plt.show() #pdb.set_trace() for i in range(nbpatches): for j in range(nbpatches): #indx=np.array(np.linspace(i*patchsize+1,(i+1)*patchsize,patchsize),dtype=int) #indy=np.array(np.linspace(j*patchsize+1,(j+1)*patchsize,patchsize),dtype=int) #pdb.set_trace() #patchmask=mask[i*patchsize+1:(i+1)*patchsize,j*patchsize+1:(j+1)*patchsize]#[indx,indy] #Aux=Originalimage[i*patchsize+1:(i+1)*patchsize,j*patchsize+1:(j+1)*patchsize,:]#[indx,indy,:] patchmask = mask[i * patchsize:(i + 1) * patchsize, j * patchsize:(j + 1) * patchsize] #[indx,indy] #plt.imshow(patchmask) #plt.show() #print(patchmask) Aux = Originalimage[i * patchsize:(i + 1) * patchsize, j * patchsize:(j + 1) * patchsize] #[indx,indy,:] Aux = np.resize(Aux, np.size(Aux)) #patchmask=Repetition(patchmask,K) Auxmask = np.resize(patchmask, np.size(patchmask)) Ind = np.where(Auxmask != val)[0] #np.nonzero(Auxmask)[0] yy = Aux[Ind] Dictionarymatrix = np.kron(Dictionarymatricesconverted[0], Dictionarymatricesconverted[1]) Dma = Dictionarymatrix[Ind, :] clf = linear_model.Lasso(alpha=penaltylasso, fit_intercept=False, positive=False) #fit_intercept=False #print(Dma.shape) #print(yy.shape) clf.fit(Dma, yy) #print(clf.coef_.shape) #pdb.set_trace() Activationcoeff = np.reshape(clf.coef_, (Rank, Rank)) Restore = Tensor_matrixproduct(Activationcoeff, Dm) Restore = mxnet_backend.to_numpy(Restore) #plt.imshow(Originalimage[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize,:]) #plt.show() #plt.imshow(Restore) #plt.show() Listrestauration.append(Restore) #plt.imshow(Restore) #plt.show() #plt.imshow(Originalimage[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize,:]) #plt.show() #pdb.set_trace() #print(np.argwhere(Restore==0)) #print(i*patchsize) #print((i+1)*patchsize) #print(j*patchsize) #print((j+1)*patchsize) Imrestored[i * patchsize:(i + 1) * patchsize, j * patchsize:(j + 1) * patchsize] = Restore #print(Restore) #pdb.set_trace() #print("The values of i and j are") #print(i,j) return Imrestored, Listrestauration
def Mean_relative_errorsingle(args): error=np.power(tl.norm(args[0][args[3]]-Tensor_matrixproduct(args[1][args[3]],args[2]),2),2) error=error/np.power(tl.norm(args[0][args[3]],2),2) return error
def Mean_relative_error(X,G,listoffactors,setting,pool): if(setting=="Single"): return np.power(tl.norm(X-Tensor_matrixproduct(G,listoffactors),2),2)/np.power(tl.norm(X,2),2) if(setting=="MiniBatch"): Mean_errorslist=pool.map(Mean_relative_errorsingle,[[X,G,listoffactors,l] for l in range(len(X))]) return np.mean(np.array(Mean_errorslist))
def Reconstruction(Originalimage,patchsize,Rank,alpha,theta,val,pool): [I,J]=np.array(Originalimage.shape,dtype=int) Listrestauration=[] n0=np.min([I,J]) Imrestored=np.zeros((I,J)) #nbx=int(np.floor(n0/patchsize)) nbpatches=int(np.floor(n0/patchsize)) Setofpatches=np.zeros((patchsize,patchsize,nbpatches)) mask=Definemasksingleslice2d(Originalimage,val) Setofpatches=Patch_extractionallslices2d(Originalimage,mask,patchsize,val) Xtrain=np.zeros((nbpatches,patchsize,patchsize)) for l in range(nbpatches): Xtrain[l,:,:]=tl.tensor(Setofpatches[:,:,l]) max_iter=500#0 period=3 Nonnegative=False epsilon=np.power(10,-3,dtype=float) step=np.power(10,-5,dtype=float)#np.power(10,-5,dtype=float) Setting="Single" nbepochs=3 Reprojectornot=False Minibatchsize=[] Pre_existingfactors=[] Coretensorsize=[Rank,Rank] penaltylasso=alpha*theta Ginittrain=np.zeros((nbpatches,Coretensorsize[0],Coretensorsize[1])) for l in range(nbpatches): Ginittrain[l,:,:]=tl.tensor(np.maximum(np.random.normal(loc=0,scale=1,size=Coretensorsize),0)) listoffactorsinit=[tl.tensor(np.identity(nbpatches)),tl.tensor(np.maximum(np.random.normal(loc=0,scale=1,size=(patchsize,Coretensorsize[0])),0)),tl.tensor(np.maximum(np.random.normal(loc=0,scale=1,size=(patchsize,Coretensorsize[1])),0))] start_timetraining=time.clock() Dictionarymatrices,errorlist,listobjectivefunctionvalues,nbiter=TuckerBatch(Xtrain,[nbpatches,Coretensorsize[0],Coretensorsize[1]],max_iter,listoffactorsinit,Ginittrain,Nonnegative,backendchoice,Reprojectornot,alpha,theta,step,epsilon,pool) #Dictionarymatrices,errorlist,listobjectivefunctionlist,Objectivefnction_per_epoch,Time_per_epoch,nbiter=TuckerBatch(Xtrain,[nbpatches,Coretensorsize[0],Coretensorsize[1]],max_iter,listoffactorsinit,Ginittrain,Nonnegative,backendchoice,Reprojectornot,alpha,theta,step,epsilon,pool) #pdb.set_trace() end_timetraining=time.clock() Runningtime=end_timetraining-start_timetraining print("The running time") print(Runningtime) pdb.set_trace() #adress='/Users/Traoreabraham/Desktop/OnlineTensorDictionaryLearning/Hyperspecimpainting/Objectivefunctions/TuckerObjectivefunction'+str(Rank) #adress='/home/scr/etu/sil821/traorabr/ImageInpainting/Objectivefunctions/TuckerObjectivefunction'+str(Rank) #np.savez_compressed(adress,Objectivefunction=listobjectivefunctionlist,Objectivefnctionperepoch=Objectivefnction_per_epoch,Timeperepoch=Time_per_epoch) #pdb.set_trace() Dm=list(Dictionarymatrices[1:3]) Dictionarymatricesconverted=Operations_listmatrices(Dictionarymatrices[1:3],"Arrayconversion") mask=Definemasksingleslice2d(Originalimage,val) #plt.imshow(mask) #plt.show() #pdb.set_trace() for i in range(nbpatches): for j in range(nbpatches): #indx=np.array(np.linspace(i*patchsize+1,(i+1)*patchsize,patchsize),dtype=int) #indy=np.array(np.linspace(j*patchsize+1,(j+1)*patchsize,patchsize),dtype=int) #pdb.set_trace() #patchmask=mask[i*patchsize+1:(i+1)*patchsize,j*patchsize+1:(j+1)*patchsize]#[indx,indy] #Aux=Originalimage[i*patchsize+1:(i+1)*patchsize,j*patchsize+1:(j+1)*patchsize,:]#[indx,indy,:] patchmask=mask[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize]#[indx,indy] #plt.imshow(patchmask) #plt.show() #print(patchmask) Aux=Originalimage[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize]#[indx,indy,:] Aux=np.resize(Aux,np.size(Aux)) #patchmask=Repetition(patchmask,K) Auxmask=np.resize(patchmask,np.size(patchmask)) Ind=np.where(Auxmask!=val)[0] #np.nonzero(Auxmask)[0] yy=Aux[Ind] Dictionarymatrix=np.kron(Dictionarymatricesconverted[0],Dictionarymatricesconverted[1]) Dma=Dictionarymatrix[Ind,:] clf=linear_model.Lasso(alpha=penaltylasso,fit_intercept=False,positive=True)#fit_intercept=False #print(Dma.shape) #print(yy.shape) clf.fit(Dma,yy) #print(clf.coef_.shape) #pdb.set_trace() Activationcoeff=np.reshape(clf.coef_,(Rank,Rank)) Restore=Tensor_matrixproduct(Activationcoeff,Dm) Restore=mxnet_backend.to_numpy(Restore) #plt.imshow(Originalimage[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize,:]) #plt.show() #plt.imshow(Restore) #plt.show() Listrestauration.append(Restore) #plt.imshow(Restore) #plt.show() #plt.imshow(Originalimage[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize,:]) #plt.show() #pdb.set_trace() #print(np.argwhere(Restore==0)) #print(i*patchsize) #print((i+1)*patchsize) #print(j*patchsize) #print((j+1)*patchsize) Imrestored[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize]=Restore #print(Restore) #pdb.set_trace() #print("The values of i and j are") #print(i,j) return Imrestored,Listrestauration,Runningtime
def Reconstruction(Originalimage,patchsize,Coretensorsize,alpha,theta,val,pool): [I,J,K]=np.array(Originalimage.shape,dtype=int) Listrestauration=[] n0=np.min([I,J]) Imrestored=np.zeros((I,J,K)) nbpatches=int(np.floor(n0/patchsize)) Setofpatches=np.zeros((patchsize,patchsize,3,nbpatches)) slicenumber=0 mask=Definemasksingleslice(Originalimage,val,slicenumber) Setofpatches=Patch_extractionallslices(Originalimage,mask,patchsize,val) Xtrain_set=[] for l in range(nbpatches): Xtrain_set.append(tl.tensor(Setofpatches[:,:,:,l])) max_iter=100 period=3 Nonnegative=True epsilon=np.power(10,-3,dtype=float) step=np.power(10,-6,dtype=float) Setting="Single" nbepochs=10 Reprojectornot=False Minibatchsize=[] Pre_existingfactors=[] penaltylasso=alpha*theta Pre_existingG_settrain=[] for l in range(nbpatches): Pre_existingG_settrain.append(tl.tensor(np.maximum(np.random.normal(loc=0,scale=1/4,size=Coretensorsize),0))) Pre_existingfactors=[tl.tensor(np.maximum(np.random.normal(loc=0,scale=1/4,size=(patchsize,Coretensorsize[0])),0)),tl.tensor(np.maximum(np.random.normal(loc=0,scale=1/4,size=(patchsize,Coretensorsize[1])),0)),tl.tensor(np.maximum(np.random.normal(loc=0,scale=1/4,size=(K,Coretensorsize[2])),0))] Pre_existingP=[tl.tensor(np.random.normal(loc=0,scale=2,size=(patchsize,Coretensorsize[0]))),tl.tensor(np.random.normal(loc=0,scale=2,size=(patchsize,Coretensorsize[1]))),tl.tensor(np.random.normal(loc=0,scale=2,size=(K,Coretensorsize[2])))] Pre_existingQ=[tl.tensor(np.random.normal(loc=0,scale=2,size=(Coretensorsize[0],Coretensorsize[0]))),tl.tensor(np.random.normal(loc=0,scale=2,size=(Coretensorsize[1],Coretensorsize[1]))),tl.tensor(np.random.normal(loc=0,scale=2,size=(Coretensorsize[2],Coretensorsize[2])))] Dictionarymatrices,listobjectivefunctionvalues,Objectivefunction_per_epoch=CyclicBlocCoordinateTucker_setWithPredefinedEpochs(Xtrain_set,Coretensorsize,Pre_existingfactors,Pre_existingG_settrain,backendchoice,Pre_existingP,Pre_existingQ,Nonnegative,Reprojectornot,Setting,Minibatchsize,step,alpha,theta,max_iter,epsilon,period,nbepochs,pool) Dm=list(Dictionarymatrices) Dictionarymatricesconverted=Operations_listmatrices(Dictionarymatrices,"Arrayconversion") mask=Definemasksingleslice(Originalimage,val,0) for i in range(nbpatches): for j in range(nbpatches): patchmask=mask[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize]#[indx,indy] Aux=Originalimage[i*patchsize:(i+1)*patchsize,j*patchsize:(j+1)*patchsize,:]#[indx,indy,:] Aux=np.resize(Aux,np.size(Aux)) patchmask=Repetition(patchmask,K) Auxmask=np.resize(patchmask,np.size(patchmask)) Ind=np.where(Auxmask!=val)[0] #np.nonzero(Auxmask)[0] yy=Aux[Ind] Dictionarymatrix=np.kron(Dictionarymatricesconverted[0],Dictionarymatricesconverted[1]) Dictionarymatrix=np.kron(Dictionarymatrix,Dictionarymatricesconverted[2]) Dma=Dictionarymatrix[Ind,:] clf=linear_model.Lasso(alpha=penaltylasso,fit_intercept=False,positive=True)#fit_intercept=False clf.fit(Dma,yy) Activationcoeff=np.reshape(clf.coef_,Coretensorsize) Restore=Tensor_matrixproduct(Activationcoeff,Dm) Restore=mxnet_backend.to_numpy(Restore) Listrestauration.append(Restore) return Imrestored,Listrestauration #val=255 #value which allows to define the missing pixels #ratio=0.4 #ratio of missing pixels #Hyperspectralimg=np.array(Image.open("Lena.png")) #[W,H,K]=np.array(Hyperspectralimg.shape,dtype=int) #Hyperspectralimg=Hyperspectralimg+np.maximum(np.random.normal(loc=0,scale=1,size=(W,H,K)),0)/10 #plt.imshow(Hyperspectralimgpixelsdropped) #plt.show() #pdb.set_trace() #patchsize=16 #Rank=16 #alpha=0.001 #theta=0.1 #[width,length,spectralbands]=np.array(Hyperspectralimg.shape,dtype=int) #Commonsize=np.min(np.array([width,length,spectralbands])) #Corentensorsize=[int(Rank),int(Rank),int(Rank)] #pool=Pool(20) #Imrestored,Listrestauration=Reconstruction(Hyperspectralimgpixelsdropped,patchsize,Corentensorsize,alpha,theta,val,pool) #pdb.set_trace()
def OnlineTensorlearningsingleblock(Choleskysimilaritymatrix, Oldparamtensor, Oldloadingmatrices, ResponsetensorX, PredictortensorZ, alpha, M, K, Coretensorsize, Methodname): #All the operations, i.e. the variable change, the update and ALTO application are performed in the function #In this function, we assume that ALTO has already been applied to the former parameter tensor and the loading matrices already recovered Responsetensor = np.zeros(ResponsetensorX.shape) Newparametertensor = np.zeros(Oldparamtensor.shape) Listoffactormarices = [] R = Coretensorsize[0] for m in range(M): #The parameter change is performed Responsetensor[:, :, m] = np.dot(np.linalg.inv(Choleskysimilaritymatrix), ResponsetensorX[:, :, m]) #We update the parameter tensor Newparametertensor[:, :, m] = ( 1 - alpha) * Oldparamtensor[:, :, m] + alpha * np.dot( Responsetensor[:, :, m], np.linalg.pinv(PredictortensorZ[:, :, m])) if (Methodname == "Online"): Setting = "Single" [I, J, K] = np.array(Newparametertensor.shape, dtype=int) Pre_existingfactors = [ np.random.normal(loc=0, scale=1 / 2, size=(I, R)), np.random.normal(loc=0, scale=1 / 2, size=(J, R)), np.random.normal(loc=0, scale=1 / 2, size=(K, R)) ] Pre_existingG_set = np.random.normal(loc=0, scale=1 / 2, size=(R, R, R)) Pre_existingP = [ np.random.normal(loc=0, scale=1 / 2, size=(I, R)), np.random.normal(loc=0, scale=1 / 2, size=(J, R)), np.random.normal(loc=0, scale=1 / 2, size=(K, R)) ] Pre_existingQ = [ np.random.normal(loc=0, scale=1 / 2, size=(R, R)), np.random.normal(loc=0, scale=1 / 2, size=(R, R)), np.random.normal(loc=0, scale=1 / 2, size=(R, R)) ] Nonnegative = False Reprojectornot = True Minibatchnumber = [] step = np.power(10, -18, dtype=float) alpha = np.power(10, 2, dtype=float) #np.power(10,-2,dtype=float) theta = np.power(10, -2, dtype=float) max_iter = 20 epsilon = np.power(10, -3, dtype=float) period = 2 nbepochs = 1 pool = Pool(10) Core, Listoffactormarices = CyclicBlocCoordinateTucker_setWithPredefinedEpochs( [Newparametertensor], Coretensorsize, Pre_existingfactors, [Pre_existingG_set], Pre_existingP, Pre_existingQ, Nonnegative, Reprojectornot, Setting, Minibatchnumber, step, alpha, theta, max_iter, epsilon, period, nbepochs, pool) Newparametertensor = Tensor_matrixproduct( tl.tensor(Core[0]), Operations_listmatrices(Listoffactormarices, "Tensorize")) if (Methodname == "Tucker"): Ginit = np.random.normal(loc=0, scale=1 / 2, size=(R, R, R)) [I, J, K] = np.array(Newparametertensor.shape, dtype=int) Reprojectornot = True max_iter = 20 step = np.power(10, -18, dtype=float) alpha = np.power(10, 2, dtype=float) theta = np.power(10, -2, dtype=float) epsilon = np.power(10, -3, dtype=float) listoffactorsinit = [ np.random.normal(loc=0, scale=1, size=(I, R)), np.random.normal(loc=0, scale=1 / 2, size=(J, R)), np.random.normal(loc=0, scale=1 / 2, size=(K, R)) ] X = Newparametertensor Nonnegative = False Core, Listoffactormarices, errorlist, nbiter = TuckerBatch( X, Coretensorsize, max_iter, listoffactorsinit, Ginit, Nonnegative, Reprojectornot, alpha, theta, step, epsilon) Newparametertensor = Tensor_matrixproduct( tl.tensor(Core), Operations_listmatrices(Listoffactormarices, "Tensorize")) if (Methodname == "Tucker2"): [I, J, K] = np.array(Newparametertensor.shape, dtype=int) Ginit = np.random.normal(loc=0, scale=1 / 2, size=(I, R, R)) Reprojectornot = True max_iter = 20 step = np.power(10, -18, dtype=float) alpha = np.power(10, 2, dtype=float) theta = np.power(10, -2, dtype=float) epsilon = np.power(10, -3, dtype=float) listoffactorsinit = [ np.eye(I), np.random.normal(loc=0, scale=1 / 2, size=(J, R)), np.random.normal(loc=0, scale=1 / 2, size=(K, R)) ] X = Newparametertensor Nonnegative = False Core, Listoffactormarices, errorlist, nbiter = TuckerBatch( X, Coretensorsize, max_iter, listoffactorsinit, Ginit, Nonnegative, Reprojectornot, alpha, theta, step, epsilon) Newparametertensor = Tensor_matrixproduct( tl.tensor(Core), Operations_listmatrices(Listoffactormarices, "Tensorize")) #Listoffactormarices: tensor type #Newparametertensor: tensor type return Newparametertensor, Listoffactormarices
def Reconstruction(Originalimage, patchsize, Coretensorsize, alpha, theta, pool): [I, J, K] = np.array(Originalimage.shape, dtype=int) Listrestauration = [] n0 = np.min([I, J]) Imrestored = np.zeros((I, J, K)) nbpatches = int(np.floor(n0 / patchsize)) Setofpatches = np.zeros((patchsize, patchsize, 3, nbpatches)) Setofpatches = Patch_extractionallslices(Originalimage, patchsize) Xtrain_set = [] for l in range(nbpatches): Xtrain_set.append(tl.tensor(Setofpatches[:, :, :, l])) max_iter = 5 period = 20 Nonnegative = False epsilon = np.power(10, -3, dtype=float) step = np.power(10, -5, dtype=float) Setting = "Single" nbepochs = 1 Reprojectornot = False Minibatchsize = [] Pre_existingfactors = [] Pre_existingG_settrain = [] for l in range(nbpatches): Pre_existingG_settrain.append( tl.tensor( np.maximum( np.random.normal(loc=0, scale=1, size=Coretensorsize), 0))) Pre_existingfactors = [ tl.tensor( np.maximum( np.random.normal(loc=0, scale=1, size=(patchsize, Coretensorsize[0])), 0)), tl.tensor( np.maximum( np.random.normal(loc=0, scale=1 / 4, size=(patchsize, Coretensorsize[1])), 0)), tl.tensor( np.maximum( np.random.normal(loc=0, scale=1 / 4, size=(K, Coretensorsize[2])), 0)) ] Pre_existingP = [ tl.tensor( np.random.normal(loc=0, scale=1, size=(patchsize, Coretensorsize[0]))), tl.tensor( np.random.normal(loc=0, scale=1, size=(patchsize, Coretensorsize[1]))), tl.tensor(np.random.normal(loc=0, scale=1, size=(K, Coretensorsize[2]))) ] Pre_existingQ = [ tl.tensor( np.random.normal(loc=0, scale=1, size=(Coretensorsize[0], Coretensorsize[0]))), tl.tensor( np.random.normal(loc=0, scale=2, size=(Coretensorsize[1], Coretensorsize[1]))), tl.tensor( np.random.normal(loc=0, scale=1, size=(Coretensorsize[2], Coretensorsize[2]))) ] Dictionarymatrices, listobjectivefunctionvalues, Objectivefunction_per_epoch = CyclicBlocCoordinateTucker_setWithPredefinedEpochs( Xtrain_set, Coretensorsize, Pre_existingfactors, Pre_existingG_settrain, backendchoice, Pre_existingP, Pre_existingQ, Nonnegative, Reprojectornot, Setting, Minibatchsize, step, alpha, theta, max_iter, epsilon, period, nbepochs, pool) #Dm=list(Dictionarymatrices) #Dictionarymatricesconverted=Operations_listmatrices(Dictionarymatrices,"Arrayconversion") #pdb.set_trace() Nonnegative = False for i in range(nbpatches): for j in range(nbpatches): Noisypatch = Originalimage[i * patchsize:(i + 1) * patchsize, j * patchsize:(j + 1) * patchsize, :] G_init = tl.tensor( np.random.normal(loc=0, scale=1, size=Coretensorsize)) Activationcoeff = Sparse_code(Noisypatch, G_init, Dictionarymatrices, Nonnegative, step, max_iter, alpha, theta, epsilon)[0] Restore = Tensor_matrixproduct(Activationcoeff, Dictionarymatrices) Imrestored[i * patchsize:(i + 1) * patchsize, j * patchsize:(j + 1) * patchsize, :] = Restore #pdb.set_trace() #Aux=np.resize(Aux,np.size(Aux)) #patchmask=Repetition(patchmask,K) #Auxmask=np.resize(patchmask,np.size(patchmask)) #Ind=np.where(Auxmask!=val)[0] #np.nonzero(Auxmask)[0] #yy=Aux#[Ind] #Dictionarymatrix=np.kron(Dictionarymatricesconverted[0],Dictionarymatricesconverted[1]) #Dictionarymatrix=np.kron(Dictionarymatrix,Dictionarymatricesconverted[2]) #Dma=Dictionarymatrix #pdb.set_trace() #clf=linear_model.Lasso(alpha=penaltylasso,fit_intercept=False,positive=False)#fit_intercept=False #clf.fit(Dma,yy) #Activationcoeff=np.reshape(clf.coef_,Coretensorsize) #Restore=Tensor_matrixproduct(Activationcoeff,Dm) #Restore=mxnet_backend.to_numpy(Restore) Listrestauration.append(Restore) return Imrestored, Listrestauration