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)
def test_convsparsenet(lam_sparse=.1,N=16,perc_var=100.): from hdl.models import SparseSlowModel from hdl.learners import SGD whitenpatches = 1000 #model = ConvWhitenInputModel(imshp=(10,1,32,32),convwhitenfiltershp=(7,7),perc_var=100.) model = ConvSparseSlowModel(imshp=(10,1,28,28),convwhitenfiltershp=(7,7),N=N,kshp=(7,7),perc_var=perc_var,lam_sparse=lam_sparse) l = SGD(model=model,datasource='vid075-chunks',display_every=1000,save_every=10000,batchsize=model.imshp[0]) print 'whitenpatches', whitenpatches print 'model.imshp', model.imshp print 'model.convwhitenfiltershp', model.convwhitenfiltershp databatch = l.get_databatch(whitenpatches) l.model.learn_whitening(databatch) l.model.setup() l.learn(iterations=20000) l.change_target(.5) l.learn(iterations=5000) l.change_target(.5) l.learn(iterations=5000) #l.learn(iterations=160000) #l.change_target(.5) #l.learn(iterations=20000) #l.change_target(.5) #l.learn(iterations=20000) from hdl.display import display_final display_final(l.model)
def test_imageshape(): from hdl.learners import SGD whitenpatches = 100 model = ConvSparseSlowModel(imshp=(whitenpatches,1,64,64),convwhitenfiltershp=(7,7),N=16,kshp=(5,5),perc_var=100.) l = SGD(model=model,datasource='vid075-chunks',display_every=100,save_every=10000,batchsize=model.imshp[0]) print 'whitenpatches', whitenpatches print 'model.imshp', model.imshp print 'model.convwhitenfiltershp', model.convwhitenfiltershp databatch = l.get_databatch(whitenpatches) from matplotlib import pyplot as plt images = np.transpose(databatch).reshape(l.model.imshp) plt.figure(1) for ind in range(100): plt.subplot(10,10,ind) im = np.squeeze(images[ind,0,:,:]) plt.imshow(im,interpolation='nearest',cmap=plt.cm.gray) plt.axis('off') plt.draw() plt.show()
def debug_convsparsenet(lam_sparse=.1,N=16,perc_var=100.,stride=1,kshp=(7,7),batchsize=4,imsz=(64,64)): from hdl.learners import SGD whitenpatches = 100 if isinstance(kshp,int): kshp = (kshp,kshp) if isinstance(stride,int): stride = (stride,stride) imszr = imsz[0] imszc = imsz[1] #model = ConvWhitenInputModel(imshp=(10,1,32,32),convwhitenfiltershp=(7,7),perc_var=100.) model = ConvSparseSlowModel(imshp=(batchsize,1,imszr,imszc),convwhitenfiltershp=(7,7),N=N,kshp=kshp,stride=stride, perc_var=perc_var,lam_sparse=lam_sparse) l = SGD(model=model,datasource='vid075-chunks',display_every=50,save_every=10000,batchsize=model.imshp[0]) print 'whitenpatches', whitenpatches print 'model.imshp', model.imshp print 'model.convwhitenfiltershp', model.convwhitenfiltershp databatch = l.get_databatch(whitenpatches) l.model.learn_whitening(databatch) l.model.setup() l.learn(iterations=10) l.model.center_basis_functions = False l.learn(iterations=10)
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)
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)
def test_convsparsenet_subspace(lam_sparse=1.,lam_slow=1.,N=8,perc_var=100.,stride=(1,1)): from hdl.models import ConvSparseSlowModel from hdl.learners import SGD whitenpatches = 1000 psz = 48 ksz = 16 convwhitenfiltershp=(15,15) #model = ConvWhitenInputModel(imshp=(10,1,32,32),convwhitenfiltershp=(7,7),perc_var=100.) model = ConvSparseSlowModel(imshp=(4,1,psz,psz),convwhitenfiltershp=convwhitenfiltershp,N=N,kshp=(ksz,ksz),stride=stride, sparse_cost='subspacel1', perc_var=perc_var, lam_sparse=lam_sparse, lam_slow=lam_slow, mask=True, force_subspace_orthogonal=True) l = SGD(model=model,datasource='vid075-chunks',display_every=50,save_every=10000, eta_target_maxupdate=0.5, batchsize=model.imshp[0]) print 'whitenpatches', whitenpatches print 'model.imshp', model.imshp print 'model.convwhitenfiltershp', model.convwhitenfiltershp databatch = l.get_databatch(whitenpatches) l.model.learn_whitening(databatch) l.model.setup() l.learn(iterations=1000) l.model.center_basis_functions=False l.learn(iterations=9000) l.change_target(.5) l.learn(iterations=5000) l.change_target(.5) l.learn(iterations=5000) #l.learn(iterations=160000) #l.change_target(.5) #l.learn(iterations=20000) #l.change_target(.5) #l.learn(iterations=20000) from hdl.display import display_final display_final(l.model)
def test_convsparsenet(lam_sparse=.1,N=16,perc_var=100.,stride=1,kshp=(7,7),batchsize=4): from hdl.learners import SGD whitenpatches = 1000 if isinstance(kshp,int): kshp = (kshp,kshp) if isinstance(stride,int): stride = (stride,stride) imszr = 5*stride[0] + kshp[0] - 1 imszc = 5*stride[1] + kshp[1] - 1 #model = ConvWhitenInputModel(imshp=(10,1,32,32),convwhitenfiltershp=(7,7),perc_var=100.) model = ConvSparseSlowModel(imshp=(batchsize,1,imszr,imszc),convwhitenfiltershp=(7,7),N=N,kshp=kshp,stride=stride, perc_var=perc_var,lam_sparse=lam_sparse) l = SGD(model=model,datasource='vid075-chunks',display_every=50,save_every=10000,batchsize=model.imshp[0]) print 'whitenpatches', whitenpatches print 'model.imshp', model.imshp print 'model.convwhitenfiltershp', model.convwhitenfiltershp databatch = l.get_databatch(whitenpatches) l.model.learn_whitening(databatch) l.model.setup() l.learn(iterations=5000) l.model.center_basis_functions = False l.learn(iterations=15000) l.change_target(.5) l.learn(iterations=5000) l.change_target(.5) l.learn(iterations=5000) #l.learn(iterations=160000) #l.change_target(.5) #l.learn(iterations=20000) #l.change_target(.5) #l.learn(iterations=20000) from hdl.display import display_final display_final(l.model)
def test_sparsenet_subspace(lam_sparse=1.,lam_slow=1.,N=8,perc_var=100.): from hdl.models import SparseSlowModel from hdl.learners import SGD whitenpatches = 10000 psz = 16 #model = ConvWhitenInputModel(imshp=(10,1,32,32),convwhitenfiltershp=(7,7),perc_var=100.) model = SparseSlowModel(patch_sz=psz,N=N, sparse_cost='subspacel1', perc_var=perc_var, lam_sparse=lam_sparse, lam_slow=lam_slow,T=48) l = SGD(model=model,datasource='vid075-chunks',display_every=100,save_every=10000,batchsize=model.T) print 'whitenpatches', whitenpatches databatch = l.get_databatch(whitenpatches) l.model.learn_whitening(databatch) l.model.setup() l.learn(iterations=5000) l.change_target(.5) l.learn(iterations=5000) l.change_target(.5) l.learn(iterations=5000) #l.learn(iterations=160000) #l.change_target(.5) #l.learn(iterations=20000) #l.change_target(.5) #l.learn(iterations=20000) from hdl.display import display_final display_final(l.model)
l.model.patch_sz = new_patch_sz l.model.D = new_patch_sz**2 databatch = l.get_databatch(whitenpatches) l.model.learn_whitening(databatch) newA = np.dot(l.model.whitenmatrix,newA) newA = l.model.normalize_A(newA) l.model.A.set_value(newA.astype(hdl.models.theano.config.floatX)) l.model.reset_functions() return l initial_target = l.eta_target_maxupdate databatch = l.get_databatch(whitenpatches) l.model.learn_whitening(databatch) l.model.setup() l.eta_target_maxupdate iterations = 2000 l = learn_loop(l,iterations=iterations) display_final(l.model,save_string='doubling_0') doublings = 4 for doubling in range(doublings): iterations *= 2 l.eta_target_maxupdate = initial_target print 'Doubling model...' l = double_patch_sz(l) display_final(l.model,save_string='doubling_%d_before_learning'%(doubling+1))
from hdl.models import SparseSlowModel from hdl.learners import SGD, autoSGD debug_grad = False # Create auotSGD class # Create fixedSGD class # write metric evaluation # run test for all three that measures time, and loss. -> then plots loss_setsize = 10000 whitenpatches = 40000 ldefault = SGD(model=SparseSlowModel(patch_sz=16,N=256,T=1,sparse_cost='l1',slow_cost=None,u_init_method='proj'),datasource='vid075-chunks',display_every=100000,save_every=100000,batchsize=1) databatch = ldefault.get_databatch(whitenpatches) ldefault.model.learn_whitening(databatch) ldefault.model.setup() l = autoSGD(model=SparseSlowModel(patch_sz=16,N=256,T=1,sparse_cost='l1',slow_cost=None,u_init_method='proj'),datasource='vid075-chunks',display_every=100000,save_every=100000,batchsize=1) l.model.inputmean = ldefault.model.inputmean.copy() l.model.whitenmatrix = ldefault.model.whitenmatrix.copy() l.model.dewhitenmatrix = ldefault.model.dewhitenmatrix.copy() l.model.zerophasewhitenmatrix = ldefault.model.zerophasewhitenmatrix.copy() l.model.M = ldefault.model.M l.model.D = ldefault.model.D l.model.setup() l.model.A.set_value(ldefault.model.A.get_value()) if debug_grad: