예제 #1
0
파일: models.py 프로젝트: npinto/hdl
 def __init__(self,**kargs):
     self.patch_sz = kargs.get('patch_sz',4)
     self.tstring = kargs.get('tstring', tstring()) # used for saving
     self.model_name = kargs.get('model_name','BaseModel')
     if self.patch_sz:
         self.D = kargs.get('D',self.patch_sz*self.patch_sz)
     else:
         self.D = kargs.get('D',None)
예제 #2
0
파일: models.py 프로젝트: npinto/hdl
    def display(self,save_string=None,save=True,normalize_A=True,max_factors=256,zerophasewhiten=True):
        output = {}
        if hasattr(self.A,'get_value'):
            A = self.A.get_value()
        else:
            A = self.A

        if self.whiten:
            A = np.dot(self.dewhitenmatrix,A)
            if zerophasewhiten:
                A = np.dot(self.zerophasewhitenmatrix,A)

        if self.patch_sz:
            psz = self.patch_sz
        else:
            psz = int(np.ceil(np.sqrt(A.shape[0])))
            if not psz**2 == A.shape[0]:
                A = np.vstack((A,np.zeros((psz**2 - A.shape[0],A.shape[1]))))

        # plot the vectors in A
        NN = min(self.NN,max_factors)
        buf = 1
        sz = int(np.sqrt(NN))
        hval = np.max(np.abs(A))
        array = -np.ones(((psz+buf)*sz+buf,(psz+buf)*sz+buf))
        Aind = 0
        for r in range(sz):
            for c in range(sz):
                if normalize_A:
                    hval = np.max(np.abs(A[:,Aind]))
                Avalues = A[:,Aind].reshape(psz,psz)/hval
                array[buf+(psz+buf)*c:buf+(psz+buf)*c+psz,buf+(psz+buf)*r:buf+(psz+buf)*r+psz] = Avalues
                Aind += 1
        hval = 1.
        plt.figure(1)
        plt.clf()
        plt.imshow(array,vmin=-hval,vmax=hval,interpolation='nearest',cmap=plt.cm.gray)
        plt.colorbar()

        output['A'] = array

        plt.draw()

        if save:
            from config import state_dir, tstring
            savepath = os.path.join(state_dir,self.model_name + '_' + self.tstring)
            if not os.path.isdir(savepath): os.makedirs(savepath)

            if save_string is None:
                save_string = tstring()
            plt.figure(1)
            fname = os.path.join(savepath, 'A_' + save_string + '.png')
            plt.savefig(fname)

        return output
예제 #3
0
파일: models.py 프로젝트: npinto/hdl
    def display(self,save_string=None,save=True,normalize_A=True,max_factors=64,zerophasewhiten=True):

        if self.patch_sz is None:
            raise NotImplemented

        from display import display_color_patches

        output = {}
        if hasattr(self.A,'get_value'):
            A = self.A.get_value()
        else:
            A = self.A

        if self.whiten:
            A = np.dot(self.dewhitenmatrix,A)
            if zerophasewhiten:
                A = np.dot(self.zerophasewhitenmatrix,A)

        # plot the vectors in A
        NN = min(self.NN,2*max_factors)

        patches = np.zeros((self.patch_sz,self.patch_sz,3,2*NN))
        count = 0
        for nn in range(NN):
            if normalize_A:
                Ann = A[...,nn]
                Ann /= np.abs(Ann).max() / 127.5
                Ann += 127.5
            else:
                Ann = A[...,nn]
            patches[...,count  ] = Ann.reshape(6,self.patch_sz,self.patch_sz)[:3,...].T
            patches[...,count+1] = Ann.reshape(6,self.patch_sz,self.patch_sz)[3:,...].T
            count += 2

        patches = patches.reshape((self.patch_sz*self.patch_sz*3,2*NN))

        array = display_color_patches(patches,self.patch_sz,fig_num=1,normalize=False)

        output['A'] = array

        plt.draw()

        if save:
            from config import state_dir, tstring
            savepath = os.path.join(state_dir,self.model_name + '_' + self.tstring)
            if not os.path.isdir(savepath): os.makedirs(savepath)

            if save_string is None:
                save_string = tstring()
            plt.figure(1)
            fname = os.path.join(savepath, 'A_' + save_string + '.png')
            plt.savefig(fname)

        return output
예제 #4
0
파일: models.py 프로젝트: npinto/hdl
    def display_whitening(self,save_string=None,save=True,normalize_A=True,max_factors=64,zerophasewhiten=True):

        if self.patch_sz is None:
            raise NotImplemented

        from display import display_color_patches

        output = {}
        if zerophasewhiten:
            A = self.zerophasewhitenmatrix
        else:
            A = self.whitenmatrix
        if hasattr(A,'get_value'):
            A = A.get_value()

        A = A.T # make the columns the filters

        # plot the vectors in A
        NN = min(A.shape[1],2*max_factors)
        patches = np.zeros((self.patch_sz,self.patch_sz,3,2*NN))
        count = 0
        for nn in range(NN):
            patches[...,count  ] = A[...,nn].reshape(6,self.patch_sz,self.patch_sz)[:3,...].T
            patches[...,count+1] = A[...,nn].reshape(6,self.patch_sz,self.patch_sz)[3:,...].T
            count += 2

        patches = patches.reshape((self.patch_sz*self.patch_sz*3,2*NN))
        array = display_color_patches(patches,self.patch_sz,fig_num=1,normalize=normalize_A)

        output['whitenmatrix'] = array
        plt.draw()

        if save:
            from config import state_dir, tstring
            savepath = os.path.join(state_dir,self.model_name + '_' + self.tstring)
            if not os.path.isdir(savepath): os.makedirs(savepath)

            if save_string is None:
                save_string = tstring()
            plt.figure(1)
            fname = os.path.join(savepath, 'whitenmatrix_' + save_string + '.png')
            plt.savefig(fname)

        return output