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

Question regarding InvertedResidual block implementation #31

Open
AndyAtCMU opened this issue Aug 11, 2022 · 2 comments
Open

Question regarding InvertedResidual block implementation #31

AndyAtCMU opened this issue Aug 11, 2022 · 2 comments

Comments

@AndyAtCMU
Copy link

感谢fire717老师分享的代码实现和解析文章。我拜读之后感到受益匪浅。

这边对于mobilenet_v2的代码实现有一点细节方面的问题,不知道老师您能否为我解惑?
这是本repo对于InvertedResidual模块的实现

def forward(self, x):
    x = self.conv1(x)
    for _ in range(self.n):
         x = x + self.conv2(x)
    return x

以下是参考torchvision中的mobilenet_v2实现中对应的部分:

# building inverted residual blocks
for t, c, n, s in inverted_residual_setting:
    output_channel = _make_divisible(c * width_mult, round_nearest)
    for i in range(n):
        stride = s if i == 0 else 1
        features.append(block(input_channel, output_channel, stride, expand_ratio=t, norm_layer=norm_layer))
        input_channel = output_channel

我目前理解的两者之间的区别是,本实现版本中的conv2部分是网络结构相同且共用相同权重的InvertedResidual block,输入通过conv1后将通过(n-1)个权重相同的模块。而在torchvision的实现版本中,因为block会调用InvertedResidua的constructor,生成的是网络结构相同但不共用权重的InvertedResidualBlock。不知道我这样的理解是否正确?想请教一下原版本movenet的实现也采用了类似的设计吗?

@fire717
Copy link
Owner

fire717 commented Aug 12, 2022

这个问题问的很好,这里的n应该代表了InvertedResidualBlock中的残差模块重复次数,残差本身是对应像素直接相加,因为其特征图是同样尺寸且包含同样语义,那么借助卷积核的共享权重思想,这里的整个模块理论上也是可以共享权重的。

这种方案优点应该是可以降低模型复杂度,减少过拟合,缺点可能就是学习能力相对差一些。

文章中也提到当时我复现的时候没有用现有的模型代码,而是自己一层一层照着结构搭建出来的,这里的复用最开始只是方便代码编写,因此我建议你也可以试试原始这种不共享权重的方式来训练对比看看是否能提升精度。

@fire717
Copy link
Owner

fire717 commented Aug 13, 2022

测试了下修改不复用后,acc提升了0.005个点,已经更新。
当时的确没想那么多,感谢指出。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants