Beispiel #1
0
    def get_features_from_scale(self,
                     ymin=None, ymax=None,
                     xmin=None, xmax=None):
        tic = time.time()
        
        if ymin == None or xmin == None or ymax == None or xmax == None:
            feature_grid = vivid.group_cell_histograms(
               self.block_hist,
               int(self.feature_opt.blocks_per_win[0]),
               int(self.feature_opt.blocks_per_win[1]),
               1)
        else:
            feature_grid = vivid.group_cell_histograms(
                self.block_hist[
                    ymin:(ymax - 1) + int(self.feature_opt.blocks_per_win[0]),
                    xmin:(xmax - 1) + int(self.feature_opt.blocks_per_win[1]),
                    :],
                int(self.feature_opt.blocks_per_win[0]),
                int(self.feature_opt.blocks_per_win[1]),
                1)

        if self.feature_opt.feature_normalization:
            # if we normalized the blocks, we know the norm already
            #if self.feature_opt.block.block_normalization == 'l2':
            #    fea_sum = np.sum(feature_grid * feature_grid,axis=2)
            #   
            #    feature_mag = np.sqrt(
            #        self.feature_opt.blocks_per_win[0] *
            #        self.feature_opt.blocks_per_win[1])
            #    feature_grid /= feature_mag
            #else:
            feature_mag = (
                np.sqrt(np.sum(feature_grid * feature_grid, axis=2)) +
                EPSILON)
            
            #feature_sums = np.sqrt( (feature_grid * feature_grid).sum(axis=2)
                # + EPSILON)
            feature_grid = feature_grid / feature_mag[:, :, np.newaxis]
        # feature_sums[:,:,np.newaxis]

        #print "Feature compile time %f" % (time.time() - tic)

        #print "feature size: %d MB" % (feature_grid.nbytes / 1024 / 1024)
        #print feature_grid.shape

        return feature_grid
Beispiel #2
0
    def _init_scale(self, scale_ind, dbg=False):
        """
        frame_size is the real image size

        frame_size + (margins in each direction) is the zero padded image size

        We calculate the cell histograms only on the cells that are fully
        inside the real image.  Otherwise the cell histograms are considered
        all zeros.

        First we need to figure out the size & the boundaries of the cell grid
         on the padded image and the real image.

        """
        if scale_ind >= len(self.scales):
            raise EndOfScales

        frame_size = [self.scaled_sources[scale_ind].get_frame(
                        self.feature_frame).height,
                      self.scaled_sources[scale_ind].get_frame(
                        self.feature_frame).width]

        #self.ff.origin = self.scaled_sources[scale_ind]
        self.assignment_source.origin = self.scaled_sources[scale_ind]

        # This is the entire cell grid.
        # The ranges can be outside the image boundaries to account for
        # the cases when the window is partially outside the image
        x_start = -self.margins[2]
        x_stop = (frame_size[1] + self.margins[3] -
                  self.feature_opt.cell.cell_size + 1)

        y_start = -self.margins[0]
        y_stop = (frame_size[0] + self.margins[1] -
                  self.feature_opt.cell.cell_size + 1)
        cell_grid = np.meshgrid(np.arange(x_start, x_stop, 
                                          self.feature_opt.cell.cell_size),
                                np.arange(y_start, y_stop, 
                                          self.feature_opt.cell.cell_size))

#        import pdb;pdb.set_trace()

        cell_grid_start_x = -self.margins[2]
        cell_grid_start_y = -self.margins[0]
        cell_grid_stop_x = np.max(cell_grid[0][0])
        cell_grid_stop_y = np.max(cell_grid[1][:, 0])

        num_cells_x = cell_grid[1].shape[1]
        num_cells_y = cell_grid[0].shape[0]

        # The cells that fall within the image and can be populated with
        # histograms
        min_offset = self.feature_opt.cell.patch_size / 2

        cell_grid_start_x_image = np.min(
            cell_grid[0][cell_grid[0] >= min_offset])
        cell_grid_start_y_image = np.min(
            cell_grid[1][cell_grid[1] >= min_offset])

        cell_grid_stop_x_image = np.max(cell_grid[0][
             cell_grid[0] < frame_size[1] -
             self.feature_opt.cell.cell_size])

        cell_grid_stop_y_image = np.max(cell_grid[1][
             cell_grid[1] < frame_size[0] -
             self.feature_opt.cell.cell_size])

        # Indices of the image cells in the cell grid
        cell_ind_start_y = ((cell_grid_start_y_image - cell_grid_start_y) /
            self.feature_opt.cell.cell_size)

        cell_ind_start_x = ((cell_grid_start_x_image - cell_grid_start_x) /
            self.feature_opt.cell.cell_size)

        cell_ind_stop_y = ((cell_grid_stop_y_image - cell_grid_start_y) /
            self.feature_opt.cell.cell_size) + 1  # non inclusive range

        cell_ind_stop_x = ((cell_grid_stop_x_image - cell_grid_start_x) /
            self.feature_opt.cell.cell_size) + 1  # non inclusive range

        #patch dictionary assignments
        tic = time.time()
        assignments, weights = self.get_assignments(self.assignment_source,
                                               self.feature_frame)

        if PRINT_PROFILE:
            print "Filtering time %f" % (time.time() - tic)

        #cell histograms
        tic = time.time()

        cell_hist = np.zeros((num_cells_y, num_cells_x,
                              self.feature_opt.cell.dictionary_size),
                              dtype='float32')

        cell_hist[cell_ind_start_y:cell_ind_stop_y,
                  cell_ind_start_x:cell_ind_stop_x,
                  :] = vivid.cell_histogram_dense(
                        assignments,  # assignments
                        weights,  # similarities
                        self.feature_opt.cell.dictionary_size,
                        self.feature_opt.cell.cell_size,
                        [cell_grid_start_y_image, cell_grid_start_x_image],
                        [cell_grid_stop_y_image +
                            self.feature_opt.cell.cell_size,
                         cell_grid_stop_x_image +
                            self.feature_opt.cell.cell_size])

        if self.feature_opt.fea_type == 'lbp':
            cell_hist[:,:,0] = 0

        if PRINT_PROFILE:
            print "Cell histogram time %f" % (time.time() - tic)

        #block histograms
        tic = time.time()
        if self.feature_opt.block.cell_grouping == 'concatenate':
            #block_mags = vivid.group_cell_histograms(
            #    np.abs(cell_hist),
            #    self.feature_opt.block.block_size,
            #    self.feature_opt.block.block_size,
            #    1)
            #
            #block_mags = np.sqrt(block_mags)
            #
            #n_blocks = block_mags.shape
            #
            #cell_hist_temp = np.zeros_like(cell_hist)
            #
            #import pdb; pdb.set_trace()

            #for by in range(self.feature_opt.block.block_size):
            #    for bx in range(self.feature_opt.block.block_size):
            #        cell_hist_temp[by:by+n_blocks[0], bx : bx + n_blocks[1]] += (
            #            cell_hist[by:by+n_blocks[0], bx : bx + n_blocks[1]] /
            #            block_mags )
                        
            self.block_hist = vivid.group_cell_histograms(
                            cell_hist,
                            self.feature_opt.block.block_size,
                            self.feature_opt.block.block_size,
                            1)

        elif self.feature_opt.block.cell_grouping == 'add':
            self.block_hist = vivid.add_cell_histograms(
                            cell_hist,
                            self.feature_opt.block.block_size,
                            self.feature_opt.block.block_size,
                            1)

        if self.feature_opt.block.block_normalization == 'l2':
            cell_mags = np.reshape((cell_hist * cell_hist).sum(axis=2),
                                   (cell_hist.shape[0], cell_hist.shape[1], 1))
            block_mags = vivid.group_cell_histograms(
                            cell_mags,
                            self.feature_opt.block.block_size,
                            self.feature_opt.block.block_size, 1)

            block_mags = np.sqrt(block_mags.sum(axis=2)) # + EPSILON
            block_mags[block_mags == 0] = 1.0
            self.block_mags = block_mags

            self.block_hist = self.block_hist / block_mags[:, :, np.newaxis]

        elif self.feature_opt.block.block_normalization == 'l1sqrt':
            block_mags = vivid.group_cell_histograms(
                            np.abs(cell_hist),
                            self.feature_opt.block.block_size,
                            self.feature_opt.block.block_size,
                            1)

            block_mags = block_mags.sum(axis=2)
            block_mags[block_mags == 0] = 1.0
            self.block_hist = self.block_hist / block_mags[:, :, np.newaxis]
            self.block_hist = np.sqrt(self.block_hist)

        if PRINT_PROFILE:
            print "Block histogram time %f" % (time.time() - tic)

        window_grid = [cell_grid[1][:-self.feature_opt.blocks_per_win[0],
                                    :-self.feature_opt.blocks_per_win[1]],
                       cell_grid[0][:-self.feature_opt.blocks_per_win[0],
                                    :-self.feature_opt.blocks_per_win[1]]]

        #chmag = np.sqrt(np.sum(self.ch * self.ch, axis=2))
        #print("C: {0}, {1}".format(chmag.min(),chmag.max()))
        #print("B: {0}, {1}".format(self.block_mags.min(),self.block_mags.max()))

        return window_grid