ReDet icon indicating copy to clipboard operation
ReDet copied to clipboard

ReResNet在e2cnn的example的e2wrn程序的测试中没有等变性

Open Martin0Li opened this issue 3 years ago • 10 comments

如题,使用re_resnet.py中的ReResNet替换e2wrn中的Wide_ResNet,测试的结果全部为NO。

Martin0Li avatar Dec 17 '21 09:12 Martin0Li

请附上测试代码。我们在实现ReResNet之后曾验证过其等变性。

csuhan avatar Dec 19 '21 15:12 csuhan

首先感谢您的回复,下面将附上我在re_resnet.py基础上修改的测试代码:

删除526行,修改527行,改为:

class ReResNet(nn.Module):

其他与re_resnet.py一样

后面补充下文:

m = ReResNet(depth=50)

m.eval()
x = torch.randn(3, 3, 224, 224)

x_fv = x.flip(dims=[3])
x_fh = x.flip(dims=[2])
x90 = x.rot90(1, (2, 3))
x90_fh = x.flip(dims=[2]).rot90(1, (2, 3))

y = m(x)
y_fv = m(x_fv)
y_fh = m(x_fh)
y90 = m(x90)
y90_fh = m(x90_fh)

print()
print('TESTING INVARIANCE:                    ')
print('REFLECTIONS along the VERTICAL axis:   ' + ('YES' if torch.allclose(y, y_fv, atol=1e-6) else 'NO'))
print('REFLECTIONS along the HORIZONTAL axis: ' + ('YES' if torch.allclose(y, y_fh, atol=1e-6) else 'NO'))
print('90 degrees ROTATIONS:                  ' + ('YES' if torch.allclose(y, y90, atol=1e-6) else 'NO'))
print('REFLECTIONS along the 45 degrees axis: ' + ('YES' if torch.allclose(y, y90_fh, atol=1e-6) else 'NO'))

Martin0Li avatar Dec 19 '21 16:12 Martin0Li

感谢测试。目前代码里的ReResNet输出的tensor确实不是严格等变的。 对比wide_resnet,其网络的输出添加了pooling和linear: https://github.com/QUVA-Lab/e2cnn/blob/cbe6454d93edd551819809f7131ac580e88cc235/examples/e2wrn.py#L386-L390

b, c, w, h = out.shape
out = F.avg_pool2d(out, (w, h))

out = out.view(out.size(0), -1)
out = self.linear(out)

我们早期版本里曾尝试在每个ResLayer后添加gpooling:

def ennGPool(inplanes):
    in_type = FIELD_TYPE['regular'](gspace, inplanes)
    return enn.GroupPooling(in_type)

然后在输出层添加pooling和linear,其误差约为torch.allclose(y, y_fv, atol=1e-3)=True 主要原因在于ResNet多次dilation和pooling的操作,导致feature map的尺寸在奇/偶数之间变换,目前尚未有合适的解决办法,参考:https://github.com/QUVA-Lab/e2cnn/issues/36

不过我们认为其不影响整体的等变性。目前的测试是随机初始化参数,网络输出并不稳定;当网络充分训练后,这种误差会得到减小。

csuhan avatar Dec 21 '21 04:12 csuhan

@Martin0Li ,前几天我们对ReResNet的等变性做了重新的验证,以及重新实现了ReResNet V2,参考这个commit

目前ReResNet的等变性已经能够验证,参考: https://github.com/csuhan/ReDet/blob/3eae28f784f771fee8e2305f17a69ac8e84567b0/mmcls/models/backbones/re_resnet.py#L625-L659

csuhan avatar Apr 04 '22 09:04 csuhan

@csuhan 我还有一个疑问,为什么ReResNet比ResNet的参数量少呢?期待您的回复,感谢。

Martin0Li avatar Apr 13 '22 02:04 Martin0Li

为什么输入的x的h和w非得是513 801这样的数呢?如果是256 256会怎样啊?

hcy226 avatar Apr 17 '22 07:04 hcy226

为什么输入的x的h和w非得是513 801这样的数呢?如果是256 256会怎样啊?

以偶数特征图输入,旋转等变性就会变差,貌似是e2cnn框架的问题。

Martin0Li avatar Apr 17 '22 09:04 Martin0Li

为什么输入的x的h和w非得是513 801这样的数呢?如果是256 256会怎样啊?

以偶数特征图输入,旋转等变性就会变差,貌似是e2cnn框架的问题。

那您在redet里面用的输入都是奇数么?如果我目前的工作要求必须输入是偶数就没法用e2cnn了么?

hcy226 avatar Apr 17 '22 09:04 hcy226

@csuhan 我还有一个疑问,为什么ReResNet比ResNet的参数量少呢?期待您的回复,感谢。

在ReResNet中,大小为[32, 3, 3]的卷积核通过group convolution产生的特征图与常规卷积核[32*8, 3, 3]是一致的;即可以简单的理解为ReResNet中卷积核经过一定的变换,复制了N次(N=8),因此其所需参数较少,仅在运行时进行卷积核变换。

csuhan avatar Apr 18 '22 05:04 csuhan

为什么输入的x的h和w非得是513 801这样的数呢?如果是256 256会怎样啊?

以偶数特征图输入,旋转等变性就会变差,貌似是e2cnn框架的问题。

那您在redet里面用的输入都是奇数么?如果我目前的工作要求必须输入是偶数就没法用e2cnn了么?

下面这张图(https://arxiv.org/pdf/2004.09691.pdf)大概可以解释: image

这个问题目前似乎是一个无法调节的矛盾:ResNet等都需要偶数的输入图像(如32*N),且均包含有down sample的模块,但e2cnn这类等变网络在降采样时等变性就会被破坏。

仅针对目标检测问题,相较于来自其他模块的误差(bbox坐标取整, RoI Align线性插值采样),我们认为这里的误差是相对较小的,可接受的。

csuhan avatar Apr 18 '22 05:04 csuhan