Exemplo n.º 1
0
    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.')
Exemplo n.º 2
0
    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.')
Exemplo n.º 3
0
    def test_DSIFT_training_patches (self):
        """ Test getting SIFT patches for training dictionaries. """ 

        tpatches = siftwrap.training_patches(self.tilist, 50, 16)
        self.assertEqual(tpatches.shape[1], 128)
        self.assertTrue(40 < tpatches.shape[0] < 60) # Not exact, but that's ok