def extract_features(self, image, settings={}, must_preserve_size=False):
        sett = self.bedges_settings().copy()
        sett['radius'] = 0
        sett['preserve_size'] = False or must_preserve_size
        if 1:
            unspread_edges = ag.features.bedges(image, **sett)
        else:
            # LEAVE-BEHIND: From multi-channel images
            unspread_edges = ag.features.bedges_from_image(image, **sett)

        # Now do spreading
        edges = ag.features.bspread(unspread_edges,
                                    spread=self.bedges_settings()['spread'],
                                    radius=self.bedges_settings()['radius'])

        feats = self.extract_parts(edges, unspread_edges, settings=settings)

        sett = self.settings
        sett.update(settings)
        psize = sett.get('subsample_size', (1, 1))
        feats = gv.sub.subsample(feats, psize)

        buf = tuple(image.shape[i] - feats.shape[i] * psize[i]
                    for i in range(2))
        lower = (buf[0] // 2, buf[1] // 2)
        upper = tuple(image.shape[i] - (buf[i] - lower[i]) for i in range(2))

        return gv.ndfeature(feats, lower=lower, upper=upper)
    def _response_map(self, feats, mixcomp, strides=(1, 1)):
        sh = self.svms[mixcomp]['weights'].shape
        pmult = self.settings.get('padding_multiple_of_object', 0.5)
        padding = (int(sh[0] * pmult), int(sh[1] * pmult), 0)

        if min(feats.shape[:2]) < 2:
            return np.array([]), None, padding

        bigger = gv.ndfeature.zeropad(feats, padding)

        from .fast import multifeature_real_correlate2d
        #index = 26
        #res = multifeature_correlate2d(bigger[...,index:index+1], weights[...,index:index+1].astype(np.float64))

        weights = self.svms[mixcomp]['weights']
        if bigger.shape[0] < weights.shape[0] or bigger.shape[
                1] < weights.shape[1]:
            return np.zeros((0, 0, 0)), None, padding

        res = self.svms[mixcomp]['intercept'] + \
              multifeature_real_correlate2d(bigger.astype(np.float64), weights, strides=strides)

        lower, upper = gv.ndfeature.inner_frame(bigger, (sh[0] / 2, sh[1] / 2))
        res = gv.ndfeature(res, lower=lower, upper=upper)

        return res, bigger, padding
    def _response_map(self, feats, mixcomp, strides=(1, 1)):
        sh = self.svms[mixcomp]['weights'].shape
        pmult = self.settings.get('padding_multiple_of_object', 0.5)
        padding = (int(sh[0]*pmult), int(sh[1]*pmult), 0)

        if min(feats.shape[:2]) < 2:
            return np.array([]), None, padding

        bigger = gv.ndfeature.zeropad(feats, padding)


        from .fast import multifeature_real_correlate2d
        #index = 26 
        #res = multifeature_correlate2d(bigger[...,index:index+1], weights[...,index:index+1].astype(np.float64)) 

        weights = self.svms[mixcomp]['weights']
        if bigger.shape[0] < weights.shape[0] or bigger.shape[1] < weights.shape[1]:
            return np.zeros((0, 0, 0)), None, padding 

        res = self.svms[mixcomp]['intercept'] + \
              multifeature_real_correlate2d(bigger.astype(np.float64), weights, strides=strides)

        lower, upper = gv.ndfeature.inner_frame(bigger, (sh[0]/2, sh[1]/2))
        res = gv.ndfeature(res, lower=lower, upper=upper)

        return res, bigger, padding
    def extract_features(self, image, settings={}, must_preserve_size=False):
        sett = self.bedges_settings().copy()
        sett['radius'] = 0
        sett['preserve_size'] = False or must_preserve_size
        if 1:
            unspread_edges = ag.features.bedges(image, **sett)
        else:
            # LEAVE-BEHIND: From multi-channel images
            unspread_edges = ag.features.bedges_from_image(image, **sett)
    

        # Now do spreading
        edges = ag.features.bspread(unspread_edges, spread=self.bedges_settings()['spread'], radius=self.bedges_settings()['radius'])  

        feats = self.extract_parts(edges, unspread_edges, settings=settings)

        sett = self.settings
        sett.update(settings)
        psize = sett.get('subsample_size', (1, 1))
        feats = gv.sub.subsample(feats, psize)

        buf = tuple(image.shape[i] - feats.shape[i] * psize[i] for i in range(2))
        lower = (buf[0]//2, buf[1]//2)
        upper = tuple(image.shape[i] - (buf[i]-lower[i]) for i in range(2))

        return gv.ndfeature(feats, lower=lower, upper=upper)
    def extract_features(self, image, settings={}, must_preserve_size=False):
        sett = self.settings
        sett.update(settings)

        radii = sett.get('spread_radii', (0, 0))
        psize = sett.get('subsample_size', (1, 1))

        spread_shape = (radii[0] * 2 + 1, radii[1] * 2 + 1)

        self._net.layers[-1]._shape = spread_shape 
        self._net.layers[-1]._strides = psize

        feats = self._net.extract(image[np.newaxis])[0]

        buf = tuple(image.shape[i] - feats.shape[i] * psize[i] for i in range(2))
        lower = (buf[0]//2, buf[1]//2)
        upper = tuple(image.shape[i] - (buf[i]-lower[i]) for i in range(2))

        return gv.ndfeature(feats, lower=lower, upper=upper)
Beispiel #6
0
    def extract_features(self, image, settings={}, must_preserve_size=False):
        sett = self.settings
        sett.update(settings)

        radii = sett.get('spread_radii', (0, 0))
        psize = sett.get('subsample_size', (1, 1))

        spread_shape = (radii[0] * 2 + 1, radii[1] * 2 + 1)

        self._net.layers[-1]._shape = spread_shape
        self._net.layers[-1]._strides = psize

        feats = self._net.extract(image[np.newaxis])[0]

        buf = tuple(image.shape[i] - feats.shape[i] * psize[i]
                    for i in range(2))
        lower = (buf[0] // 2, buf[1] // 2)
        upper = tuple(image.shape[i] - (buf[i] - lower[i]) for i in range(2))

        return gv.ndfeature(feats, lower=lower, upper=upper)
    def extract_features(self,
                         image,
                         settings={},
                         must_preserve_size=False,
                         dropout=None):
        sett = self.bedges_settings().copy()
        sett['radius'] = 0
        if 1:
            #unspread_edges = ag.features.bedges(image, **sett)
            #unspread_edges = gv.gradients.extract(image, orientations=8)
            unspread_edges = _extract_edges(
                self.bedges_settings(),
                self.settings,
                image,
                must_preserve_size=must_preserve_size)
        else:
            # LEAVE-BEHIND: From multi-channel images
            unspread_edges = ag.features.bedges_from_image(image, **sett)

        # Now do spreading
        edges = ag.features.bspread(unspread_edges,
                                    spread=self.bedges_settings()['spread'],
                                    radius=self.bedges_settings()['radius'])

        # TODO Temporary
        #sett['preserve_size'] = True
        #unspread_edges = ag.features.bedges(image, **sett)
        th = self.threshold_in_counts(self.settings['threshold'],
                                      edges.shape[-1],
                                      sett['contrast_insensitive'])

        # TODO : Since we're using a hard-coded tau
        if self.settings.get('tau', 0) == 0:
            sett = self.settings.copy()
            sett.update(settings)
            psize = sett.get('subsample_size', (1, 1))

            ORI = self.settings.get('orientations', 1)
            POL = self.settings.get('polarities', 1)
            P = ORI * POL
            H = P // 2

            if POL == 2 and not self.settings.get('no_collapse', False):
                part_to_feature = np.zeros(self.parts.shape[0], dtype=np.int64)
                for f in range(part_to_feature.shape[0]):
                    thepart = f // P
                    ori = f % H
                    v = thepart * H + ori

                    part_to_feature[f] = v
            else:
                part_to_feature = np.arange(self.parts.shape[0],
                                            dtype=np.int64)

            # Rotation spreading?
            rotspread = sett.get('rotation_spreading_radius', 0)
            if rotspread == 0:
                between_feature_spreading = None
            else:
                between_feature_spreading = np.zeros(
                    (self.num_parts, rotspread * 2 + 1), dtype=np.int32)

                for f in range(self.num_parts):
                    thepart = f // ORI
                    ori = f % ORI
                    for i in range(rotspread * 2 + 1):
                        between_feature_spreading[
                            f, i] = thepart * ORI + (ori - rotspread + i) % ORI

            feats = ag.features.extract_parts(
                edges,
                unspread_edges,
                self._log_parts,
                self._log_invparts,
                th,
                self.settings['patch_frame'],
                spread_radii=sett.get('spread_radii', (0, 0)),
                subsample_size=psize,
                part_to_feature=part_to_feature,
                stride=self.settings.get('part_coding_stride', 1),
                between_feature_spreading=between_feature_spreading)

            buf = tuple(image.shape[i] - feats.shape[i] * psize[i]
                        for i in range(2))
            lower = (buf[0] // 2, buf[1] // 2)
            upper = tuple(image.shape[i] - (buf[i] - lower[i])
                          for i in range(2))

        else:
            sett = self.settings.copy()
            sett.update(settings)

            feats = self.extract_parts(edges,
                                       unspread_edges,
                                       settings=sett,
                                       dropout=dropout)

            psize = sett.get('subsample_size', (1, 1))
            feats = gv.sub.subsample(feats, psize)

            buf = tuple(image.shape[i] - feats.shape[i] * psize[i]
                        for i in range(2))
            lower = (buf[0] // 2, buf[1] // 2)
            upper = tuple(image.shape[i] - (buf[i] - lower[i])
                          for i in range(2))

            # Now collapse the polarities
            #feats = feats.reshape((int(feats.shape[0]//2), 2) + feats.shape[1:])
            feats = feats.reshape(feats.shape[:2] + (feats.shape[2] // 2, 2))
            feats = feats.max(axis=-1)

        # TODO: Experiment
        if 0:
            Q = np.load('Q.npy')
            new_feats_shape = feats.shape
            new_feats = np.empty(new_feats_shape, dtype=np.uint8)
            for i, j in itr.product(range(feats.shape[0]),
                                    range(feats.shape[1])):
                # Transform the basis 0.572
                #new_feats[i,j] = np.dot(Q[:,-ARTS:].T, feats[i,j])
                new_feats[i, j] = (np.fabs(
                    np.dot(Q.T, feats[i, j].astype(float))) > 15)
            feats = new_feats

        return gv.ndfeature(feats, lower=lower, upper=upper)
    def extract_features(self, image, settings={}, dropout=None):
        sett = self.bedges_settings().copy()
        sett['radius'] = 0
        if 1:
            #unspread_edges = ag.features.bedges(image, **sett)
            #unspread_edges = gv.gradients.extract(image, orientations=8)
            unspread_edges = self._extract_edges(image) 
        else:
            # LEAVE-BEHIND: From multi-channel images
            unspread_edges = ag.features.bedges_from_image(image, **sett)
    
        # Now do spreading
        edges = ag.features.bspread(unspread_edges, spread=self.bedges_settings()['spread'], radius=self.bedges_settings()['radius'])  

        # TODO Temporary
        #sett['preserve_size'] = True
        #unspread_edges = ag.features.bedges(image, **sett)

        th = self.threshold_in_counts(self.settings['threshold'], edges.shape[-1])

        # TODO : Since we're using a hard-coded tau
        if self.settings.get('tau', 0) == 0:
            sett = self.settings.copy()
            sett.update(settings)
            psize = sett.get('subsample_size', (1, 1))
            if 0:
                feats = ag.features.extract_parts_adaptive_EXPERIMENTAL(edges, unspread_edges, 
                                                  self._log_parts,
                                                  self._log_invparts,
                                                  th,
                                                  self.settings['patch_frame'],
                                                  spread_radii=sett.get('spread_radii', (0, 0)),
                                                  subsample_size=psize,
                                                  collapse=2,
                                                  accept_threshold=15)
            elif 1:
                # These are experiments with the new edge type
                #import gv.fast
                #th = self.settings['amp_threshold']
                #feats = gv.fast.extract_parts(edges, unspread_edges, amps,
                feats = ag.features.extract_parts(edges, unspread_edges,
                                                  self._log_parts,
                                                  self._log_invparts,
                                                  th,
                                                  self.settings['patch_frame'],
                                                  spread_radii=sett.get('spread_radii', (0, 0)),
                                                  subsample_size=psize,
                                                  part_to_feature=np.arange(self.num_features*2)//2,
                                                  stride=self.settings.get('part_coding_stride', 1))

            else:
                all_feats = []
                for i in range(10):
                    feats = ag.features.extract_parts(edges, unspread_edges,
                                                      self._log_parts,
                                                      self._log_invparts,
                                                      th,
                                                      self.settings['patch_frame'],
                                                      spread_radii=sett.get('spread_radii', (0, 0)),
                                                      subsample_size=psize,
                                                      collapse=2,
                                                      accept_threshold=-100000)
                    all_feats.append(feats)
                all_feats = np.asarray(all_feats)

                feats = all_feats.max(axis=0)
            
            buf = tuple(image.shape[i] - feats.shape[i] * psize[i] for i in range(2))
            lower = (buf[0]//2, buf[1]//2)
            upper = tuple(image.shape[i] - (buf[i]-lower[i]) for i in range(2))

        else:
            sett = self.settings.copy()
            sett.update(settings)

            feats = self.extract_parts(edges, unspread_edges, settings=sett, dropout=dropout)

            psize = sett.get('subsample_size', (1, 1))
            feats = gv.sub.subsample(feats, psize)

            buf = tuple(image.shape[i] - feats.shape[i] * psize[i] for i in range(2))
            lower = (buf[0]//2, buf[1]//2)
            upper = tuple(image.shape[i] - (buf[i]-lower[i]) for i in range(2))

            # Now collapse the polarities
            #feats = feats.reshape((int(feats.shape[0]//2), 2) + feats.shape[1:])
            feats = feats.reshape(feats.shape[:2] + (feats.shape[2]//2, 2))
            feats = feats.max(axis=-1)

        # TODO: Experiment
        if 0:
            Q = np.load('Q.npy')
            new_feats_shape = feats.shape
            new_feats = np.empty(new_feats_shape, dtype=np.uint8)
            #import pdb; pdb.set_trace()
            for i, j in itr.product(range(feats.shape[0]), range(feats.shape[1])):
                # Transform the basis 0.572
                #new_feats[i,j] = np.dot(Q[:,-ARTS:].T, feats[i,j])
                new_feats[i,j] = (np.fabs(np.dot(Q.T, feats[i,j].astype(float))) > 15)
            feats = new_feats

        return gv.ndfeature(feats, lower=lower, upper=upper)
    def extract_features(self, image, settings={}, must_preserve_size=False, dropout=None):
        sett = self.bedges_settings().copy()
        sett['radius'] = 0
        if 1:
            #unspread_edges = ag.features.bedges(image, **sett)
            #unspread_edges = gv.gradients.extract(image, orientations=8)
            unspread_edges = _extract_edges(self.bedges_settings(), self.settings, image, must_preserve_size=must_preserve_size) 
        else:
            # LEAVE-BEHIND: From multi-channel images
            unspread_edges = ag.features.bedges_from_image(image, **sett)
    
        # Now do spreading
        edges = ag.features.bspread(unspread_edges, spread=self.bedges_settings()['spread'], radius=self.bedges_settings()['radius'])  

        # TODO Temporary
        #sett['preserve_size'] = True
        #unspread_edges = ag.features.bedges(image, **sett)
        th = self.threshold_in_counts(self.settings['threshold'], edges.shape[-1], sett['contrast_insensitive'])

        # TODO : Since we're using a hard-coded tau
        if self.settings.get('tau', 0) == 0:
            sett = self.settings.copy()
            sett.update(settings)
            psize = sett.get('subsample_size', (1, 1))

            ORI = self.settings.get('orientations', 1)
            POL = self.settings.get('polarities', 1)
            P = ORI * POL 
            H = P // 2

            if POL == 2 and not self.settings.get('no_collapse', False):
                part_to_feature = np.zeros(self.parts.shape[0], dtype=np.int64)
                for f in range(part_to_feature.shape[0]):
                    thepart = f // P
                    ori = f % H
                    v = thepart * H + ori

                    part_to_feature[f] = v 
            else:
                part_to_feature = np.arange(self.parts.shape[0], dtype=np.int64)

            # Rotation spreading?
            rotspread = sett.get('rotation_spreading_radius', 0)
            if rotspread == 0:
                between_feature_spreading = None
            else:
                between_feature_spreading = np.zeros((self.num_parts, rotspread*2 + 1), dtype=np.int32)

                for f in range(self.num_parts):
                    thepart = f // ORI
                    ori = f % ORI 
                    for i in range(rotspread*2 + 1):
                        between_feature_spreading[f,i] = thepart * ORI + (ori - rotspread + i) % ORI

            feats = ag.features.extract_parts(edges, unspread_edges,
                                              self._log_parts,
                                              self._log_invparts,
                                              th,
                                              self.settings['patch_frame'],
                                              spread_radii=sett.get('spread_radii', (0, 0)),
                                              subsample_size=psize,
                                              part_to_feature=part_to_feature,
                                              stride=self.settings.get('part_coding_stride', 1),
                                              between_feature_spreading=between_feature_spreading)

            
            buf = tuple(image.shape[i] - feats.shape[i] * psize[i] for i in range(2))
            lower = (buf[0]//2, buf[1]//2)
            upper = tuple(image.shape[i] - (buf[i]-lower[i]) for i in range(2))

        else:
            sett = self.settings.copy()
            sett.update(settings)

            feats = self.extract_parts(edges, unspread_edges, settings=sett, dropout=dropout)

            psize = sett.get('subsample_size', (1, 1))
            feats = gv.sub.subsample(feats, psize)

            buf = tuple(image.shape[i] - feats.shape[i] * psize[i] for i in range(2))
            lower = (buf[0]//2, buf[1]//2)
            upper = tuple(image.shape[i] - (buf[i]-lower[i]) for i in range(2))

            # Now collapse the polarities
            #feats = feats.reshape((int(feats.shape[0]//2), 2) + feats.shape[1:])
            feats = feats.reshape(feats.shape[:2] + (feats.shape[2]//2, 2))
            feats = feats.max(axis=-1)

        # TODO: Experiment
        if 0:
            Q = np.load('Q.npy')
            new_feats_shape = feats.shape
            new_feats = np.empty(new_feats_shape, dtype=np.uint8)
            for i, j in itr.product(range(feats.shape[0]), range(feats.shape[1])):
                # Transform the basis 0.572
                #new_feats[i,j] = np.dot(Q[:,-ARTS:].T, feats[i,j])
                new_feats[i,j] = (np.fabs(np.dot(Q.T, feats[i,j].astype(float))) > 15)
            feats = new_feats

        return gv.ndfeature(feats, lower=lower, upper=upper)