Pytorch 新手來了!我正在嘗試微調 VGG16 模型以預測 3 個不同的類別。我的部分工作涉及將 FC 層轉換為 CONV 層。但是,我的預測值不在 0 到 2(3 個類別)之間。有人能給我指出一個關于如何計算最后一層正確尺寸的好資源嗎?以下是 VGG16 的原始 fC 層:(classifier): Sequential( (0): Linear(in_features=25088, out_features=4096, bias=True) (1): ReLU(inplace) (2): Dropout(p=0.5) (3): Linear(in_features=4096, out_features=4096, bias=True) (4): ReLU(inplace) (5): Dropout(p=0.5) (6): Linear(in_features=4096, out_features=1000, bias=True) )我將 FC 層轉換為 CONV 的代碼: def convert_fc_to_conv(self, fc_layers): # Replace first FC layer with CONV layer fc = fc_layers[0].state_dict() in_ch = 512 out_ch = fc["weight"].size(0) first_conv = nn.Conv2d(512, out_ch, kernel_size=(1, 1), stride=(1, 1)) conv_list = [first_conv] for idx, layer in enumerate(fc_layers[1:]): if isinstance(layer, nn.Linear): fc = layer.state_dict() in_ch = fc["weight"].size(1) out_ch = fc["weight"].size(0) if idx == len(fc_layers)-4: in_ch = 3 conv = nn.Conv2d(out_ch, in_ch, kernel_size=(1, 1), stride=(1, 1)) conv_list += [conv] else: conv_list += [layer] gc.collect() avg_pool = nn.AvgPool2d(kernel_size=2, stride=1, ceil_mode=False) conv_list += [avg_pool, nn.Softmax()] top_layers = nn.Sequential(*conv_list) return top_layers
添加回答
舉報
0/150
提交
取消