示例#1
0
文件: conv_models.py 项目: mczhu/hdl
    def display_whitening(self,save_string=None,save=True,normalize_A=True,max_factors=256,zerophasewhiten=True):
        output = {}

        nfilt, nchannels, szy, szx = self.convwhitenfiltershp

        if zerophasewhiten:
            A = self.zerophasewhitenmatrix
            if hasattr(A,'get_value'):
                A = A.get_value()
            Afilter = self.convwhitenfilter.reshape((nfilt*nchannels,szy*szx))
            Adefilter = self.convdewhitenfilter.reshape((nfilt*nchannels,szy*szx))
            A = np.vstack((Afilter,Adefilter,A))
        else:
            A = self.whitenmatrix
            if hasattr(A,'get_value'):
                A = A.get_value()

        A = A.T # make the columns the filters

        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(A.shape[1],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['whitenmatrix'] = array
        plt.draw()

        if save:
            from hdl.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
示例#2
0
文件: conv_models.py 项目: mczhu/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 not zerophasewhiten:
            Areshaped = np.reshape(A.T,self.kshp)
            Aoutput = self._scipy_convolve4d(Areshaped,self.convdewhitenfilter,mode='same')
            A = np.reshape(Aoutput,(self.kshp[0],np.prod(self.kshp[1:]))).T

        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 hdl.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
def test_3Dvideo():
    from matplotlib import pyplot as plt

    test_name = '3Dvideo'
    psz = 16
    l = SGD(model=SparseSlowModel(patch_sz=psz,D=6*psz*psz,N=400,T=48),datasource='3Dvideo_color',batchsize=48)

    batchsize = 48
    databatch = l.get_databatch(batchsize)

    batchsize = 1000
    databatch = l.get_databatch(batchsize)

    from hdl.config import tests_dir, tstring
    savepath = os.path.join(tests_dir,test_name,tstring())
    if not os.path.isdir(savepath): os.makedirs(savepath)

    vidind = np.random.randint(l.video_buffer)
    video = l.videos[vidind]
    for tt in range(video.shape[0]):

        plt.figure(1)
        plt.clf()
        plt.subplot(1,2,1)
        frame = np.uint8(video[tt,:3,:,:].T)
        plt.imshow(frame,interpolation='nearest')
        plt.subplot(1,2,2)
        frame = np.uint8(video[tt,3:,:,:].T)
        plt.imshow(frame,interpolation='nearest')
        fname = os.path.join(savepath, 'vid_' + str(vidind) + '_frame_%04d'%tt + '.png')
        plt.savefig(fname)

    batchsize = 48
    databatch = l.get_databatch(batchsize)
    for tt in range(batchsize):

        plt.figure(1)
        plt.clf()
        plt.subplot(1,2,1)
        frame_both = np.uint8(databatch[...,tt].reshape(6,l.model.patch_sz,l.model.patch_sz)).T
        frame = frame_both[...,:3]
        plt.imshow(frame,interpolation='nearest')
        plt.subplot(1,2,2)
        frame = frame_both[...,3:]
        plt.imshow(frame,interpolation='nearest')
        fname = os.path.join(savepath, 'databatch_frame_%04d'%tt + '.png')
        plt.savefig(fname)

    # test multiple loads
    for tt in range(1000):
        if not tt%100: print tt,
        databatch = l.get_databatch(batchsize)
示例#4
0
def test_YouTubeFaces():
    from matplotlib import pyplot as plt

    test_name = 'YouTubeFaces'
    l = SGD(model=SparseSlowModel(patch_sz=48,N=400,T=64),datasource='YouTubeFaces_aligned',batchsize=64)

    batchsize = 64
    databatch = l.get_databatch(batchsize)

    batchsize = 1000
    databatch = l.get_databatch(batchsize)

    from hdl.config import tests_dir, tstring
    savepath = os.path.join(tests_dir,test_name,tstring())
    if not os.path.isdir(savepath): os.makedirs(savepath)

    vidind = int(np.floor(np.random.rand()*l.YouTubeInfo['num_videos']))
    video = l.YouTubeInfo['videos'][vidind]
    hval = np.abs(video).max()
    for tt in range(video.shape[2]):

        plt.figure(1)
        plt.clf()
        plt.imshow(video[:,:,tt],cmap=plt.cm.gray,vmin=-hval,vmax=hval,interpolation='nearest')
        fname = os.path.join(savepath, 'vid_' + str(vidind) + '_frame_%04d'%tt + '.png')
        plt.savefig(fname)

    batchsize = 64
    databatch = l.get_databatch(batchsize)
    hval = np.abs(databatch).max()
    for tt in range(batchsize):

        plt.figure(1)
        plt.clf()
        plt.imshow(databatch[...,tt].reshape(l.model.patch_sz,l.model.patch_sz),cmap=plt.cm.gray,vmin=-hval,vmax=hval,interpolation='nearest')
        fname = os.path.join(savepath, 'databatch_frame_%04d'%tt + '.png')
        plt.savefig(fname)

    # test multiple loads
    for tt in range(100):
        databatch = l.get_databatch(batchsize)
示例#5
0
def test_vid075():
    from matplotlib import pyplot as plt

    test_name = 'vid075'
    l = SGD(model=SparseSlowModel(patch_sz=20,N=400,T=64),datasource='vid075-chunks',batchsize=64)

    batchsize = 64
    databatch = l.get_databatch(batchsize)

    batchsize = 1000
    databatch = l.get_databatch(batchsize)

    from hdl.config import tests_dir, tstring
    savepath = os.path.join(tests_dir,test_name,tstring())
    if not os.path.isdir(savepath): os.makedirs(savepath)

    vidind = np.floor(np.random.rand()*l.nvideos)
    hval = np.abs(l.videos[vidind,...]).max()
    for tt in range(l.videot):

        plt.figure(1)
        plt.clf()
        plt.imshow(l.videos[vidind,:,:,tt],cmap=plt.cm.gray,vmin=-hval,vmax=hval,interpolation='nearest')
        fname = os.path.join(savepath, 'vid_' + str(vidind) + '_frame_'+ str(tt) + '.png')
        plt.savefig(fname)

    batchsize = 64
    databatch = l.get_databatch(batchsize)
    hval = np.abs(databatch).max()
    for tt in range(batchsize):

        plt.figure(1)
        plt.clf()
        plt.imshow(databatch[...,tt].reshape(l.model.patch_sz,l.model.patch_sz),cmap=plt.cm.gray,vmin=-hval,vmax=hval,interpolation='nearest')
        fname = os.path.join(savepath, 'databatch_' + str(vidind) + '_frame_'+ str(tt) + '.png')
        plt.savefig(fname)
示例#6
0
import os
import hdl
reload(hdl)

from hdl.models import SparseSlowModel
from hdl.hdl import HDL

from hdl.config import tstring, state_dir

timestring = tstring()
model_base_name = 'HDL_loga_' + timestring + '/layer_%s'

m1 = SparseSlowModel()
layer1_name = 'SparseSlowModel_patchsz020_N512_NN512_l2_subspacel1_dist_2012-05-24_17-33-06/model.model'
fname = os.path.join(state_dir,layer1_name)
m1.load(fname)
m1.model_name = model_base_name % '1'

model_sequence = [
    m1,
    SparseSlowModel(patch_sz=None, N=512,  T=48, sparse_cost='subspacel1', slow_cost='dist', perc_var=99.9, tstring=timestring, model_name=model_base_name % '2'),
    SparseSlowModel(patch_sz=None, N=256,  T=48, sparse_cost='l1', slow_cost=None, perc_var=99.9, tstring=timestring, model_name=model_base_name % '3')]

hdl_learner  = HDL(model_sequence=model_sequence,datasource='PLoS09_Cars_Planes',output_function='proj_loga')

hdl_learner.learn(layer_start=1)