Code annotations for the Swin transformer detector in mmdetection

First of all, let me talk about the two questions asked by a small partner before:

Why do you need to write the forward() function every time you define a network, and it is not a special function of python?

Answer: Because our network parent class is nn.Module class (the latest version of mmdetection is BaseModule class), and nn.Module class defines the __call__() method, which calls the forward() function. Therefore, when module(data) is executed, the __call__() function is automatically called.

class Module(nn.Module):
    def __init__(self):
        super(Module, self).__init__()
        # ......
    def forward(self, x):
        # ......
        return x

data = .....  #输入数据
module = Module()# 实例化一个对象
module(data)  # 前向传播
"""
    而不是使用module.forward(data)
    而实际上 module(data) 和 module.forward(data) 是等价的
"""

Note: In Python, anything that can directly apply () to itself and execute it is called a callable object. If the __call__() method is implemented in the class, the instance object of the class can be made a callable object. For callable objects y

Guess you like

Origin blog.csdn.net/qq_42308217/article/details/123470974