使用 MaxViT 进行迁移学习时我的分类器应该是什么?

问题描述 投票:0回答:1

我正在尝试使用自定义数据集在 Pytorch 预训练模型上进行迁移学习。我已经能够使用 SqueezeNet 成功执行迁移学习。

对于 Squeezenet,我的分类器是,层源

model.classifier = nn.Sequential(
    nn.Dropout(p=0.2),
    nn.Conv2d(512, len(class_names), kernel_size=1),
    nn.ReLU(inplace=True),
    nn.AdaptiveAvgPool2d((1, 1)))

对于 Efficientnet,我的分类器是,层源

model.classifier = torch.nn.Sequential(
    torch.nn.Dropout(p=0.2, inplace=True),
    torch.nn.Linear(in_features=1280,
                    out_features=output_shape,
                    bias=True))

类似地,我一直在尝试为 MaxViT 做,我浏览了源代码,发现参数中有

block_channels[-1]
。我最近开始使用这个,但我不知道它们是什么,layers source

self.classifier = nn.Sequential(
    nn.AdaptiveAvgPool2d(1),
    nn.Flatten(),
    nn.LayerNorm(block_channels[-1]),
    nn.Linear(block_channels[-1], block_channels[-1]),
    nn.Tanh(),
    nn.Linear(block_channels[-1], num_classes, bias=False),
)

作为参考,如果需要的话,以下是我使用squeezenet执行迁移学习的完整代码。

weights = torchvision.models.SqueezeNet1_0_Weights.DEFAULT
model = torchvision.models.squeezenet1_0(weights=weights).to(device)
auto_transforms = weights.transforms()
train_dataloader, test_dataloader, class_names = data_setup.create_dataloaders(train_dir=d1,
                                                                               test_dir=d2,
                                                                               transform=auto_transforms,
                                                                               batch_size=32)
for param in model.features.parameters():
    param.requires_grad = False

torch.manual_seed(42)
torch.cuda.manual_seed(42)
output_shape = len(class_names)

model.classifier = nn.Sequential(
    nn.Dropout(p=0.2),
    nn.Conv2d(512, len(class_names), kernel_size=1),
    nn.ReLU(inplace=True),
    nn.AdaptiveAvgPool2d((1, 1))).to(device)

loss_fn = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
torch.manual_seed(42)
torch.cuda.manual_seed(42)
results = engine.train(model=model,
                       train_dataloader=train_dataloader,
                       test_dataloader=test_dataloader,
                       optimizer=optimizer,
                       loss_fn=loss_fn,
                       epochs=15,
                       device=device)

我的 MaxViT 分类器应该是什么?

machine-learning deep-learning pytorch transfer-learning torchvision
1个回答
0
投票

来自

MaxVit
Args 参数
block_channels (List[int]): Number of channels in each block
来源

分类器来源

self.classifier = nn.Sequential(
        nn.AdaptiveAvgPool2d(1),
        nn.Flatten(),
        nn.LayerNorm(block_channels[-1]),
        nn.Linear(block_channels[-1], block_channels[-1]),
        nn.Tanh(),
        nn.Linear(block_channels[-1], num_classes, bias=False),
    )

由于

block_channels
是一个列表,`block_channels[-1] 返回列表中的最后一项,在以下情况下为 512 Source

return _maxvit(
    stem_channels=64,
    block_channels=[64, 128, 256, 512],
    block_layers=[2, 2, 5, 2],
    head_dim=32,
    stochastic_depth_prob=0.2,
    partition_size=7,
    weights=weights,
    progress=progress,
    **kwargs,
)

.

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.