0
$\begingroup$

Given a pre-trained CNN model, I extract feature vector of 3450 reference images FV_R as follows:

FV_R = [       [-8.2, -52.2, 9.07, -1.1, -0.08, -9.1, ........, -4.11], 
               [7.8, -3.8, 6.4, -4.27, -2.2, -5.0, ............., 3.6], 
               [-1.2, -0.8, 49.3, 1.73, -1.74, -7.1, ..........., 2.41],
               [-1.2, -.8, 49.3, 0.6, -1.24, -1.04, .........., -2.06],
               .
               .
               .
               [-1.2, -.8, 49.3, 12.77. -2.2, -5.0, .........., -51.1]
       ]

and FV_Q for 1200 query images :

FV_Q = [       [-0.13, 2.6, -3.7, -0.5, -1.02, -0.6, ........, -0.11], 
               [0.3, -3.8, 6.4, -1.6, -2.2, -5.0, ............., 0.97], 
               [-6.4, -0.08, 8.0, 7.3, -8.07, -5.6, ..........., 0.01],
               [-6.09, -.8, 0.5, -8.9, -0.74, -0.08, .........., -8.9],
               .
               .
               .
               [-1.2, -.8, 49.3, 12.77. -2.2, -5.0, .........., -51.1]
       ]

The size info:

>>> FV_R.shape
(3450, 64896)

Query images:

>>> FV_Q.shape
(1200, 64896)

I would like to binarize the CNN feature vectors (descriptors) and calculate Hamming Distance. I am already aware of this answer to probably use np.count_nonzero(a!=b)(if a.shape == b.shape) but does anyone know a method to binarize a feature vector with different size?

Cheers,

$\endgroup$
1
  • $\begingroup$ Programming issues are off-topic here. Please, read ai.stackexchange.com/help/on-topic for more details and to understand which questions are on-topic here. If you have a programming issue, the best place to ask your question is Stack Overflow or, alternatively, Data Science SE. $\endgroup$ – nbro Jun 2 '20 at 22:16
0
$\begingroup$

If you mean to binarize the vector such that all positive values become 1 and the rest becoming 0, then you can do this.

bin_arr = np.zeros_like(FV_R)
bin_arr[FV_R > 0] = 1.

As an example,

In [7]: arr                                                                                                                                                                                                        
Out[7]: 
array([[ 0.15, -0.52],
       [ 1.  , -0.43]])

In [8]: bin_arr = np.zeros_like(arr)                                                                                                                                                                               

In [9]: bin_arr[arr > 0] = 1                                                                                                                                                                                       

In [10]: bin_arr                                                                                                                                                                                                   
Out[10]: 
array([[1., 0.],
       [1., 0.]])
```
$\endgroup$
0
$\begingroup$

I fixed the problem as follows:

import numpy as np

def binarize(FV):
    return np.where(FV > 0, 1, 0).astype(int)

def Hamming_Distance(ref, query):
    b_rf, b_qu = binarize(ref), binarize(query)
    H = np.count_nonzero(b_qu[:, None, :] != b_rf, axis=2)
    return H
$\endgroup$

Not the answer you're looking for? Browse other questions tagged or ask your own question.