def learn_dictionary (self, images, npatches=50000, niter=1000, njobs=-1): """ Learn a Sparse Code dictionary for this ScSPM. This method trains a sparse codes dictionary for the ScSPM descriptor object. This only needs to be run once before multiple calls to the extract() method can be made. Arguments: images: list, a list of paths to images to use for training. npatches: int (default 50000) number of SIFT patches to extract from the images to use for training the dictionary. niter: int (default 1000), the number of iterations of dictionary learning (Lasso) to perform. njobs: int (default -1), the number of threads to use. -1 means the number of threads will be equal to the number of cores. """ # Get SIFT training patches print('Getting training patches...') patches = sw.training_patches(images, npatches, self.psize, self.maxdim, verbose=True) patches = pch.norm_patches(patches) print('{0} patches requested, {1} patches found.'.format(npatches, patches.shape[0])) time.sleep(3) # Give people a chance to see this message # Learn dictionary print('Learning dictionary...') self.dic = trainDL(np.asfortranarray(patches.T, np.float64), mode=0, K=self.dsize, lambda1=0.15, iter=niter, numThreads=njobs) print('done.')
def learn_dictionary(self, images, npatches=50000, niter=1000, njobs=-1): """ Learn a Sparse Code dictionary for this ScSPM. This method trains a sparse codes dictionary for the ScSPM descriptor object. This only needs to be run once before multiple calls to the extract() method can be made. Arguments: images: list, a list of paths to images to use for training. npatches: int (default 50000) number of SIFT patches to extract from the images to use for training the dictionary. niter: int (default 1000), the number of iterations of dictionary learning (Lasso) to perform. njobs: int (default -1), the number of threads to use. -1 means the number of threads will be equal to the number of cores. """ # Get SIFT training patches print('Getting training patches...') patches = sw.training_patches(images, npatches, self.psize, self.maxdim, verbose=True) patches = pch.norm_patches(patches) print('{0} patches requested, {1} patches found.'.format( npatches, patches.shape[0])) time.sleep(3) # Give people a chance to see this message # Learn dictionary print('Learning dictionary...') self.dic = trainDL(np.asfortranarray(patches.T, np.float64), mode=0, K=self.dsize, lambda1=0.15, iter=niter, numThreads=njobs) print('done.')
def test_norm_patches (self): """ Test patch contrast normalisation/unitisation. """ diff = np.abs(np.ones((self.tpatch.shape[0],1)) - np.sqrt((patch.norm_patches(self.tpatch)**2).sum(axis=1))) self.assertTrue((diff < 1e-15).all())