2

I have five python Classes in different files. All are in one folder.

Folder
-main.py
-main_class.py
-sub_of_first.py
-sub_of_second.py
-sub_of_third.py

main.py start calls MainClass

main.py
def main():
   model = MainClass(backbone, )

MainClass is inheritance of FirstClass

main_class.py
from .sub_of_first import FirstClass
class MainClass(FirstClass):
    """Implementation of `Fast R-CNN <https://arxiv.org/abs/1504.08083>`_"""

    def __init__(self,
                 backbone,
                 roi_head,
                 train_cfg,
                 test_cfg,
                 neck=None,
                 pretrained=None,
                 init_cfg=None):
        super(MainClass, self).__init__(
            backbone=backbone,
            neck=neck,
            roi_head=roi_head,
            train_cfg=train_cfg,
            test_cfg=test_cfg,
            pretrained=pretrained,
            init_cfg=init_cfg)
    def method1(self):
        test=1

Then

sub_of_first.py
from .base import BaseDetector
from .sub_of_second import FirstSubClass
from .sub_of_third import SecondSubClass

class FirstClass(BaseDetector):
    """Base class for two-stage detectors.
    Two-stage detectors typically consisting of a region proposal network and a
    task-specific regression head.
    """

    def __init__(self,
                 backbone,
                 neck=None,
                 rpn_head=None,
                 roi_head=None,
                 train_cfg=None,
                 test_cfg=None,
                 pretrained=None,
                 init_cfg=None):
        super(FirstClass, self).__init__(init_cfg)
        if pretrained:
            warnings.warn('DeprecationWarning: pretrained is deprecated, '
                          'please use "init_cfg" instead')
            backbone.pretrained = pretrained
        args = backbone.copy()
        args.pop('type')
        self.backbone = FirstSubClass(args)
        self.roi_head = SecondSubClass(bbox_ext, bbox_head, train_cfg, test_cfg)

Then FirstSubClass is

sub_of_second.py
class FirstSubClass(nn.Module):
    def __init__(self,):super().__init__()

    def somemethod(self):
       test=1

SecondSubClass is in

sub_of_third.py
class SecondSubClass(StandardRoIHead):
        def __init__(self,
                     bbox_ext,
                     bbox_head,
                     train_cfg,
                     test_cfg):
            super(SecondSubClass, self).__init__(
                    bbox_roi_extractor=bbox_ext,
                    bbox_head=bbox_head,
                    train_cfg=train_cfg,
                    test_cfg=test_cfg)
        def someothersmethod(self):
             test=1

When I print model in main.py, the model shows

MainClass(
  (backbone): FirstSubClass(
  

   )
)

It has no roi_head. I need to print model.roi_head to see roi_head. roi_head is not in MainClass and printing in debug gives

p model.roi_head

<Folder.SecondSubClass object at 0x7fd1dc88f210>

Why SecondSubClass in not in MainClass?

What I like to have is

MainClass(
  (backbone): FirstSubClass(


   )
  (roi_head): SecondSubClass(


   )
)

How can I do that? What could be the difference as the first one is included in MainClass but second one is not?

batuman
  • 7,066
  • 26
  • 107
  • 229
  • how do you have 2 files both called `sub_of_first.py`? also you have the line `self.rot_head = SecondSubClass(...)` so I'm not sure what you are confused about, it looks like the code is behaving as I'd expect it to – Tadhg McDonald-Jensen Nov 10 '22 at 04:12
  • Sorry I made mistake. It is updated. How can I do to include `roi_head` in `MainClass`? – batuman Nov 10 '22 at 04:44

0 Answers0