Binarize ConvNet Feature vector 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:

&gt;&gt;&gt; FV_R.shape
(3450, 64896)


Query images:

&gt;&gt;&gt; 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,
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 &gt; 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 &gt; 0] = 1                                                                                                                                                                                       

In [10]: bin_arr                                                                                                                                                                                                   
Out[10]: 
array([[1., 0.],
       [1., 0.]])
```