for name in ["conv1", "layer1", "layer2", "layer3", "layer4"]:
fm = feats[name] # Get the feature map for the <<name>> layer
#idx = idx # Get the index of the channel with the highest mean activation
sel = fm[:, idx:idx+1] # Select the channel with the highest mean activation
sel = fm[:, idx:idx+1] # Upsample the feature map to 224x224
sel = fm[:, idx:idx+1] # Normalize the feature map
upsampled.append(sel), # Append the upsampled feature map to the list
I need help to Get the index of the channel with the highest mean activation
I haven’t gotten to that assignment yet, but the first question is identifying the dimensions of the tensors. Then it will involve first taking the mean over the appropriate dimension(s) and then using argmax on the channel dimension to return the index of the channel that’s got the highest mean value. Surely by the time we get to Course 3 of this series we will have seen examples of the use of torch.argmax at some point. If not, then the old saying “google is your friend” will apply. ![]()
I am not a mentor for this course.
Hope this example to get the channel with highest mean (channels last) helps:
In [1]: import torch
In [2]: a = torch.randn(2, 3, 4)
In [3]: torch.mean(a[..., 0])
Out[3]: tensor(-0.0547)
In [4]: torch.mean(a[..., 1])
Out[4]: tensor(-0.0964)
In [5]: torch.mean(a[..., 2])
Out[5]: tensor(0.4814)
In [6]: torch.mean(a[..., 3])
Out[6]: tensor(-0.2637)
In [7]: torch.mean(a, dim=(0, 1))
Out[7]: tensor([-0.0547, -0.0964, 0.4814, -0.2637])
In [8]: torch.argmax(torch.mean(a, dim=(0, 1)))
Out[8]: tensor(2)
In addition to that question, shouldn’t we use in addition torch.abs to the mean to find the highest?.
I also suggest team to to look at the function signature:
def feature_map_strip(img, model):
“”"
Forward-pass an image through pretrained ResNet-50, capture the feature map
with the highest mean activation at five depths, upsample them and returns a
list of five upsampled tensors.
Parameters
----------
img : torch.Tensor
Tensor of an RGB image (will be centre-cropped to 224Ă—224).
model : torch.nn.Module
Pretrained model to use for feature extraction.
Returns
-------
Dict[str, torch.Tensor]
Dictionary of raw feature maps for keys
{"conv1", "layer1", "layer2", "layer3", "layer4"}.
"""
return is list but the documentation of function state that the return is dictionary.
if I am not incorrect, you can also safely call the same call directly from tensor object, instead of torch.mean(a, dim=(0,1), you can do a.mean(dim=(0,1)), same for argmax function as well.
Yes you can use the instance methods as well.