I'm using the Mask-RCNN Model that given by Pytorch for my project on Colab.
The model was adjusted to the data that i have(transfer learning) and train process was completed.
To the next step, i wanted to extract the feature maps of some input data from FPN.
So, i've tried to use 'hook' and made a code for that purpose like below.
load the weights of Mask-RCNN
state_dict = torch.load("~~~.path")
models.load_state_dict(state_dict)
function for hook
def get_activation(name):
def hook(models, input, output):
activation[name] = output
return hook
Code to save feature maps of each input data
activation = {}
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
models.to(device)
for i in range(1, 4):
test_img = Image.open("{path for input data}.png".format(i)).convert('RGB')
test_img = test_img.resize((800, 800))
test_img = tr.functional.to_tensor(test_img)
test_img = test_img.reshape(1, 3, 800, 800)
with torch.no_grad():
models.eval()
activation["fm{}".format(i)] = models.backbone.fpn.extra_blocks.register_forward_hook(get_activation("fm{}".format(i)))
test_img = test_img.to(device)
models(test_img)
But the feature map of just one input data was appended to every keys.
Like {"fm1" : a, "fm2" : a, "fm3" : a}
I don't know what is the reason and fault of this code.
Please help me know what should i try.