这是我编写的执行单个卷积并输出形状的代码。
使用来自的公式
http://cs231n.github.io/convolutional-networks/
要计算输出大小:
你可以说服自己正确的计算公式
许多神经元__fit_由(w__f+2p)/s+1给出。
计算输出大小的公式如下所示:
def output_size(w , f , stride , padding) :
return (((w - f) + (2 * padding)) / stride) + 1
问题是
output_size
计算2690.5的大小,与1350的卷积结果不同:
%reset -f
import torch
import torch.nn.functional as F
import numpy as np
from PIL import Image
import torch.nn as nn
import torchvision
import torchvision.transforms as transforms
from pylab import plt
plt.style.use('seaborn')
%matplotlib inline
width = 60
height = 30
kernel_size_param = 5
stride_param = 2
padding_param = 2
img = Image.new('RGB', (width, height), color = 'red')
in_channels = 3
out_channels = 3
class ConvNet(nn.Module):
def __init__(self):
super(ConvNet, self).__init__()
self.layer1 = nn.Sequential(
nn.Conv2d(in_channels,
out_channels,
kernel_size=kernel_size_param,
stride=stride_param,
padding=padding_param))
def forward(self, x):
out = self.layer1(x)
return out
def output_size(w , f , stride , padding) :
return (((w - f) + (2 * padding)) / stride) + 1
w = width * height * in_channels
f = kernel_size_param * kernel_size_param
print('output size :' , output_size(w , f , stride_param , padding_param))
model = ConvNet()
criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.Adam(model.parameters(), lr=.001)
img_a = np.array(img)
img_pt = torch.tensor(img_a).float()
result = model(img_pt.view(3, width , height).unsqueeze_(0))
an = result.view(30 , 15 , out_channels).data.numpy()
print(30 * 15 * out_channels)
我是否正确实现了输出大小?如何修改此模型,使结果
Conv2d
形状与结果相同
输出大小
?