Search code examples
pythonnumpyneural-networkcafferesnet

How to reshape the res5c layer of ResNet (3D to 2D)?


I extract the features of an image with ResNet of the 'res5c' layer, resulting of a numpy array of shape (2048, 14, 14)

I have trouble manipulating these dimensions. I understand there is 14*14 features of size 2048. I would like to iterate over to access every feature at a time.

Therefore, how I can reshape this to an array of (14*14, 2048) without mistakes and then easily iterate over it with a for loop?


Solution

  • You can read the features after net.forward():

    feat = net.blobs['res5c'].data.cop() # copy to be on the safe side.
    

    As you describe, feat is an np.array with shape = (2048, 14, 14).
    You can reshape it:

    feat.reshape((2048,-1)) # fix the first dimension to 2048, -1 set the number of features to match that of `feat`.
    

    Now you can iterate over features:

    for fi in xrange(feat.shape[1]):
        f = feat[:,fi] # get the fi-th feature
        # do somethinf to the feature f