Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How to output feature maps #570

Closed
greattBILL opened this issue Nov 30, 2021 · 7 comments · Fixed by #593
Closed

How to output feature maps #570

greattBILL opened this issue Nov 30, 2021 · 7 comments · Fixed by #593
Labels
help wanted Extra attention is needed Planned feature

Comments

@greattBILL
Copy link

greattBILL commented Nov 30, 2021

I use resnet101 and now i want to output feature maps. i write the linear_head.py but it doest work

 def __init__(self,
                 num_classes,
                 in_channels,
                 init_cfg=dict(type='Normal', layer='Linear', std=0.01),
                 *args,
                 **kwargs):
        super(LinearClsHead, self).__init__(init_cfg=init_cfg, *args, **kwargs)

        self.in_channels = in_channels
        self.num_classes = num_classes

        if self.num_classes <= 0:
            raise ValueError(
                f'num_classes={num_classes} must be a positive integer')

        self._init_layers()

    def _init_layers(self):
        self.fc = nn.Linear(self.in_channels, self.num_classes)

    def init_weights(self):
        normal_init(self.fc, mean=0, std=0.01, bias=0)

    def simple_test(self, img):
        """Test without augmentation."""
        cls_score = self.fc(img)
        x = self.extract_feat(img)
        print(type(x))
        #mmcv.imwrite("test.img",img)
        #x = self.extract_feat(img)

        if isinstance(cls_score, list):
            cls_score = sum(cls_score) / float(len(cls_score))
        pred = F.softmax(cls_score, dim=1) if cls_score is not None else None

        on_trace = is_tracing()
        if torch.onnx.is_in_onnx_export() or on_trace:
            return pred
        pred = list(pred.detach().cpu().numpy())
        return pred

how to do?thz

@greattBILL greattBILL added the help wanted Extra attention is needed label Nov 30, 2021
@mzr1996
Copy link
Member

mzr1996 commented Dec 1, 2021

Hello, if you just want the feature map, you can build the backbone only.
For example,

>>> import torch
>>> from mmcv import Config
>>> from mmcls.models import build_classifier
>>> 
>>> cfg = Config.fromfile("./configs/_base_/models/resnet101.py")
>>> cfg.model.backbone.out_indices = (0, 1, 2, 3)  # Output the feature map of all stages.
>>> 
>>> inputs = torch.rand(8, 3, 224, 224)
>>> resnet = build_classifier(cfg.model)
>>> outputs = resnet.extract_feat(inputs)  # with neck
>>> for out in outputs:
...     print(out.shape)
torch.Size([8, 256])
torch.Size([8, 512])
torch.Size([8, 1024])
torch.Size([8, 2048])
>>> outputs = resnet.backbone(inputs)      # without neck
>>> for out in outputs:
...     print(out.shape)
torch.Size([8, 256, 56, 56])
torch.Size([8, 512, 28, 28])
torch.Size([8, 1024, 14, 14])
torch.Size([8, 2048, 7, 7])

@greattBILL
Copy link
Author

hello sir thank you very much but i still have some question
1.inputs = torch.rand(8, 3, 224, 224) what the 8 means?
2.i still want the Activation value(after fc,before softmax). do you have the ways?
wish u have a happy day

@mzr1996
Copy link
Member

mzr1996 commented Dec 1, 2021

hello sir thank you very much but i still have some question 1.inputs = torch.rand(8, 3, 224, 224) what the 8 means? 2.i still want the Activation value(after fc,before softmax). do you have the ways? wish u have a happy day

  1. The 8 is just batch size.
  2. Well, we don't have the final feature extraction API by now, we will add it later. For now, you can modify the simple_test function like:
    def simple_test(self, x):
        """Test without augmentation."""
        if isinstance(x, tuple):
            x = x[-1]
        cls_score = self.fc(x)
        return cls_score

@greattBILL
Copy link
Author

The part of final feature extraction is too hard for me .I really look forward to this API!Thank you for your contribution

@greattBILL
Copy link
Author

@mzr1996
i still have some questions about your backbone.

cfg = Config.fromfile("./configs/base/models/resnet101.py")
abs it onlyhave cfg,i want to use my checkpoints(such as epoch_100.pth),how do i do?
thank!

@mzr1996
Copy link
Member

mzr1996 commented Dec 7, 2021

Please try:

cfg.model.init_cfg = dict(type='Pretrained', checkpoint='./epoch_100.pth')
resnet = build_classifier(cfg.model)

@mzr1996 mzr1996 linked a pull request Dec 8, 2021 that will close this issue
6 tasks
@greattBILL
Copy link
Author

greattBILL commented Dec 17, 2021 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
help wanted Extra attention is needed Planned feature
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants