gpt4 book ai didi

neural-network - PyTorch:如何将 CNN 中的预训练 FC 层转换为 Conv 层

转载 作者:行者123 更新时间:2023-12-04 17:16:05 25 4
gpt4 key购买 nike

我想在 Pytorch 中将预训练的 CNN(如 VGG-16)转换为完全卷积网络。我怎么能这样做?

最佳答案

您可以按如下方式执行此操作(有关说明,请参阅注释):

import torch
import torch.nn as nn
from torchvision import models

# 1. LOAD PRE-TRAINED VGG16
model = models.vgg16(pretrained=True)

# 2. GET CONV LAYERS
features = model.features

# 3. GET FULLY CONNECTED LAYERS
fcLayers = nn.Sequential(
# stop at last layer
*list(model.classifier.children())[:-1]
)

# 4. CONVERT FULLY CONNECTED LAYERS TO CONVOLUTIONAL LAYERS

### convert first fc layer to conv layer with 512x7x7 kernel
fc = fcLayers[0].state_dict()
in_ch = 512
out_ch = fc["weight"].size(0)

firstConv = nn.Conv2d(in_ch, out_ch, 7, 7)

### get the weights from the fc layer
firstConv.load_state_dict({"weight":fc["weight"].view(out_ch, in_ch, 7, 7),
"bias":fc["bias"]})

# CREATE A LIST OF CONVS
convList = [firstConv]

# Similarly convert the remaining linear layers to conv layers
for layer in enumerate(fcLayers[1:]):
if isinstance(module, nn.Linear):
# Convert the nn.Linear to nn.Conv
fc = module.state_dict()
in_ch = fc["weight"].size(1)
out_ch = fc["weight"].size(0)
conv = nn.Conv2d(in_ch, out_ch, 1, 1)

conv.load_state_dict({"weight":fc["weight"].view(out_ch, in_ch, 1, 1),
"bias":fc["bias"]})

convList += [conv]
else:
# Append other layers such as ReLU and Dropout
convList += [layer]

# Set the conv layers as a nn.Sequential module
convLayers = nn.Sequential(*convList)

关于neural-network - PyTorch:如何将 CNN 中的预训练 FC 层转换为 Conv 层,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44146655/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com