tensorflowobject-detectionobject-detection-apitensorflow-slim

get feature vector from Fast-RCNN


I am trying to identify and group similar images, I followed this tutorial: https://douglasduhaime.com/posts/identifying-similar-images-with-tensorflow.html.

The problem is that I am using faster_rcnn_resnet_101 and I found out that feature vectors in fast-rcnn get dropped after SecondStageBoxPredictor. I used https://gist.github.com/markdtw/02ece6b90e75832bd44787c03a664e8d to get the vectores before being dropped.

feat_avg = graph.get_tensor_by_name('SecondStageBoxPredictor/AvgPool:0')

np.savetxt('output1/' + "test" + '.npz',feat_vector,delimiter=',')

However when I try to save the vector I get an error:

ValueError: Expected 1D or 2D array, got 4D array instead

I printed the extracted feature vector to see the result:

    Tensor("SecondStageFeatureExtractor/resnet_v1_101/block4/unit_3/bottleneck_v1/Relu:0", shape=(?, 7, 7, 2048), dtype=float32) 
 [[[[0.         0.         0.         ... 0.         0.
    0.        ]
   [0.         0.         0.         ... 0.         0.
    0.        ]
   [0.         0.         0.         ... 0.         0.
    0.        ]
   ...
   [2.9170244  0.         0.33220196 ... 0.         0.
    0.        ]
   [0.         0.         0.         ... 0.         0.
    0.        ]
   [0.         0.         0.         ... 0.         0.
    0.        ]].....

I am a novice in TensorFlow and CV, what I want to do is extract the feature vectors and then use TSNE clusterring. What exactly is wrong with the feature vector I extracted


Solution

  • Feature vector is 4D sensor: [Batch, Height, Width, Channels]. np.savetxt waits 1D or 2D array. You can slice feature vector to 2D arrays or use some other functional for save it as 4D.