CVND_Exercises icon indicating copy to clipboard operation
CVND_Exercises copied to clipboard

Implementing HOG: Incorrect average gradient computation

Open puhach opened this issue 6 years ago • 0 comments

There seems to be a mistake in the "Implementing HOG" notebook and the related one with HOG examples. Namely, I am referring to this code fragment:

# Add up all the histograms for each cell and count the number of histograms per cell
for i in range (num_cells_per_block[0]):
    for j in range(num_cells_per_block[1]):
        ave_grad[i:tot_by + i,
                 j:tot_bx + j] += hog_descriptor_reshaped[:, :, i, j, :]
        
        hist_counter[i:tot_by + i,
                     j:tot_bx + j] += 1

I want to draw your attention to the fact that average gradient array is initialized as

ave_grad = np.zeros((y_cells, x_cells, num_bins))

whereas num_cells_per_block is a number of cells in (x, y) direction.

Thus, the code computing the average gradients is mixing x and y dimension, because i is iterating over num_cells_per_block[0], which refers to x dimension, but then i : tot_by+i expression is used as an index in the y dimension.

The code doesn't produce any errors as long as num_cells_per_block = (2,2), i.e. number of cells is the same in x and y dimension. But once we try to change that to something like (4,2) it results in broadcasting error due to dimensions mismatch.

I suggest changing it to:

for i in range (num_cells_per_block[0]): #x
    for j in range(num_cells_per_block[1]): #y      
        ave_grad[j:tot_by + j, i:tot_bx + i] += hog_descriptor_reshaped[:, :, i, j, :]
        hist_counter[j:tot_by+j, i:tot_bx+i] += 1

puhach avatar Jan 17 '20 07:01 puhach