Naren
Naren

Reputation: 33

Extracting a part of the convolutional neural network feature through application of a mask

So i have my final feature of shape (1, 512, 90, 160) with 512 as the depth(3rd dimension). I have created a 2-D binary mask from the ground truth image of shape (90, 160). I am trying to apply this mask to the feature to extract only a part of my feature manually. However due to the mismatch in the shapes of feature and the mask, I get the index error.

With np.expand_dims(), I have made shape of the mask to be (1,1,90,160). Now, How can I get to stack the mask to get the shape (1, 512, 90, 160)?

Upvotes: 2

Views: 67

Answers (1)

Dinari
Dinari

Reputation: 2557

You have 2 options:

Create a 3d mask-

final = np.ones((1,512,90,160))
final2 = np.copy(final)
mask = np.random.randint(1,10,size = (1,90,160)) > np.random.randint(1,10,size = (1,90,160))

masked = np.copy(final)
masked[:,0,:,:] = np.logical_and(masked[:,0,:,:],mask)
masked = np.logical_and.accumulate(masked,axis = 1)
np.putmask(final,masked == False, 0)

The above will create a 3d mask, and use it to mask final.

the other option, much simpler, is to just multiply:

np.multiply(final,mask)

NP handles the dimensions, and will give you the masked version.

You can verify this by:

(np.multiply(final2,mask) == final).all()

Upvotes: 1

Related Questions