Ejemplo n.º 1
0
    def train(self, images, annotations, metadata=None, **kwargs):
        ## load images
        ntrain = len(images)
        xs = images
        zs = annotations
        if metadata is None:
            metas = [None for i in range(ntrain)]
        else:
            metas = metadata

        ## initial w
        w0 = kwargs.pop('w0', 1)
        w = w0

        ## initial latent variable
        y0s = kwargs.pop('init_latents', None)

        ## initial objective
        svm_objective0 = 0

        for niter in range(1, self.niter_max + 1):
            logger.debug('iteration (latent) #{}'.format(niter))

            ## annotation consistent inference
            logger.debug('annotation consistent inference (with w={})'\
                         .format(self.print_w(w)))
            ys = self.all_aci(w, xs, zs, metas, y0s=y0s)

            ## convec struct svm
            logger.debug('start struct svm')
            struct_svm = StructSVM(loss_function=self.user_loss,
                                   psi=self.user_psi,
                                   most_violated_constraint=self.user_mvc,
                                   **self.struct_params)
            w, xi, struct_info = struct_svm.train(xs,
                                                  ys,
                                                  metadata=metas,
                                                  w0=w,
                                                  **kwargs)

            ## Stop condition
            svm_objective = struct_svm.objective(w, xi)
            strw = self.print_w(w)
            if (svm_objective - svm_objective0) < self.epsilon:
                logger.debug('latent svm stopping with: w={}, xi={:.04}'\
                             .format(strw, float(xi)))
                info = {'converged': True}
                return w, xi, ys, info

            ## end current iteration
            logger.debug('done iteration #{}, with: w={}, xi={:.04}'.format(
                niter, strw, float(xi)))
            logger.debug('objective={:.04}'.format(svm_objective))
            svm_objective0 = svm_objective

        else:
            info = {'converged': False}
            logger.info("max number of iterations reached ({})".format(
                self.niter_max))
            return w, xi, ys, info
Ejemplo n.º 2
0
    def train(self, x, z, balanced=True, **kwargs):
        import struct_svm
        struct_svm.logger = struct_svm.utils_logging.get_logger(
            'svm', struct_svm.utils_logging.INFO)
        from struct_svm import StructSVM

        C = kwargs.pop('C', 1.)

        if balanced:
            n0 = np.where(np.asarray(z) == 0)[0]
            n1 = np.where(np.asarray(z) == 1)[0]
            n = np.minimum(len(n0), len(n1))
            in0 = np.random.permutation(n0)[:n]
            in1 = np.random.permutation(n1)[:n]
            S = [(x[i], z[i]) for i in in0]
            S += [(x[i], z[i]) for i in in1]
        else:
            S = [(x[i], z[i]) for i in range(len(z))]

        svm = StructSVM(
            S,
            self.loss,
            self.psi,
            self.mvc,
            C=C,
        )

        ## train svm
        w, xi, info = svm.train()
        self.w = w
        self.xi = xi
Ejemplo n.º 3
0
    def train(self, images, annotations, metadata=None, **kwargs):
        ## load images
        ntrain = len(images)
        xs = images
        zs = annotations
        if metadata is None:
            metas = [None for i in range(ntrain)]
        else: metas = metadata            

        ## initial w
        w0 = kwargs.pop('w0',1)
        w = w0
       
        ## initial latent variable
        y0s = kwargs.pop('init_latents', None)
       
        ## initial objective
        svm_objective0 = 0
        
        for niter in range(1,self.niter_max+1):
            logger.debug('iteration (latent) #{}'.format(niter))
            
            ## annotation consistent inference
            logger.debug('annotation consistent inference (with w={})'\
                         .format(self.print_w(w)))
            ys = self.all_aci(w, xs, zs, metas, y0s=y0s)
           
            ## convec struct svm
            logger.debug('start struct svm')
            struct_svm = StructSVM(
                loss_function=self.user_loss,
                psi=self.user_psi,
                most_violated_constraint=self.user_mvc,
                **self.struct_params
                )
            w,xi,struct_info = struct_svm.train(xs, ys, metadata=metas, w0=w, **kwargs)

            ## Stop condition
            svm_objective = struct_svm.objective(w,xi)
            strw = self.print_w(w)
            if (svm_objective - svm_objective0) < self.epsilon:
                logger.debug('latent svm stopping with: w={}, xi={:.04}'\
                             .format(strw, float(xi)))
                info = {'converged': True}
                return w, xi, ys, info

            ## end current iteration 
            logger.debug('done iteration #{}, with: w={}, xi={:.04}'.format(niter, strw, float(xi)))
            logger.debug('objective={:.04}'.format(svm_objective))
            svm_objective0 = svm_objective

        else:
            info = {'converged': False}
            logger.info("max number of iterations reached ({})".format(self.niter_max))
            return w, xi, ys, info
Ejemplo n.º 4
0
    def train(self, x, z, balanced=True, **kwargs):
        import struct_svm 
        struct_svm.logger = struct_svm.utils_logging.get_logger('svm',struct_svm.utils_logging.INFO)
        from struct_svm import StructSVM
        
        C = kwargs.pop('C', 1.)

        if balanced:
            n0 = np.where(np.asarray(z)==0)[0]
            n1 = np.where(np.asarray(z)==1)[0]
            n = np.minimum(len(n0), len(n1))
            in0 = np.random.permutation(n0)[:n]
            in1 = np.random.permutation(n1)[:n]
            S  = [(x[i],z[i]) for i in in0]
            S += [(x[i],z[i]) for i in in1]
        else:   
            S = [(x[i],z[i]) for i in range(len(z))]
        
        svm = StructSVM(S, self.loss, self.psi, self.mvc,C=C,)
         
        ## train svm        
        w,xi,info = svm.train()
        self.w = w
        self.xi = xi