python-3.x 视觉变换器:可视化要素Map

ercv8c1e  于 2023-06-25  发布在  Python
关注(0)|答案(1)|浏览(140)

我正在可视化我的视觉转换器的特征图,但我无法可视化特征图。当我打印model.children()时,它显示了卷积层,但我仍然无法验证if语句。

list(model.children())

输出

[OverlapPatchEmbed(
   (proj): Conv2d(3, 64, kernel_size=(7, 7), stride=(4, 4), padding=(3, 3))
   (norm): LayerNorm((64,), eps=1e-05, elementwise_affine=True)
 ),
 OverlapPatchEmbed(
   (proj): Conv2d(64, 128, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
   (norm): LayerNorm((128,), eps=1e-05, elementwise_affine=True)
 ),
 OverlapPatchEmbed(
   (proj): Conv2d(128, 256, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
   (norm): LayerNorm((256,), eps=1e-05, elementwise_affine=True)
 ),
 OverlapPatchEmbed(
   (proj): Conv2d(256, 512, kernel_size=(3, 3), stride=(2, 2), padding=(1, 1))
   (norm): LayerNorm((512,), eps=1e-05, elementwise_affine=True)
 ),
 ModuleList(
   (0): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=64)
   (1): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=64)
   (2): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), groups=64)
   (3): Conv2d(64, 64, kernel_size=(3, 3), stride=(1, 1), paddin...

我想访问Conv2d并可视化特征图,但我无法这样做type(model_children[i]) == Conv2d不是True,我不知道为什么?

model_children = list(model.children())
# counter to keep count of the conv layers
counter = 0
# append all the conv layers and their respective wights to the list
for i in range(len(model_children)):
    if type(model_children[i]) == Conv2d:
        counter += 1
        model_weights.append(model_children[i].weight)
        conv_layers.append(model_children[i])
    elif type(model_children[i]) == nn.Sequential:
        for j in range(len(model_children[i])):
            for child in model_children[i][j].children():
                if type(child) == nn.Conv2d:
                    counter += 1
                    model_weights.append(child.weight)
                    conv_layers.append(child)
print(f"Total convolution layers: {counter}")
print("conv_layers")
ha5z0ras

ha5z0ras1#

实际上model_children[i].weight不包含weight属性。在OverlapPatchEmbed内部,proj图层包含Conv2dConv2d包含weight属性。你可以在下面纠正它。

if model_children[i] == model.patch_embed1:
    counter += 1
    weigh = model_children[i].proj
    model_weights.append(weigh.weight)
    conv_layers.append(model_children[i].proj)
elif model_children[i] == model.patch_embed2:
    counter += 1
    weigh = model_children[i].proj
    model_weights.append(weigh.weight)
    conv_layers.append(model_children[i].proj)

相关问题