链接
卷积神经网络基础
卷积神经网络高级部分
刘二大人笔记链接
刘二大人视频链接
补充
卷积conv2d
- convolution中的卷积核数量N和输入Channels相同,每N个卷积核计算组成一个输出数据output,每N个卷积核作为一个filters,M个filters组成M维outputs
-
一共需要
M
(
输
出
C
h
a
n
n
e
l
s
)
?
N
(
输
入
C
h
a
n
n
e
l
s
)
M(输出Channels)*N(输入Channels)
M(输出Channels)?N(输入Channels)个卷积核 -
一次卷积后输出的每个Channels的数据大小会变成
(
i
n
p
u
t
s
w
i
d
t
h
?
k
e
r
n
e
l
s
i
z
e
+
1
)
?
(
i
n
p
u
t
s
h
e
i
g
h
t
?
k
e
r
n
e
l
s
i
z
e
+
1
)
(inputs_{width}-kernel_{size}+1)*(inputs_{height}-kernel_{size}+1)
(inputswidth??kernelsize?+1)?(inputsheight??kernelsize?+1) -
可以用padding参数使每个Channels数据大小保持不变,padding = 1 表示增加一圈,就是边缘的两行两列。 -
卷积(convolution)后,C(Channels)可变可不变(一般都变),W(width)和H(Height)可变可不变,取决于是否padding和kernel的大小。
池化MaxPool2d
torch.nn.MaxPool2d(2)
- subsampling(或pooling)后,Channels不变,W和H变
整体流程
代码实现
代码
import torch
from torchvision import transforms
from torchvision import datasets
from torch.utils.data import DataLoader
import torch.nn.functional as F
import torch.optim as optim
batch_size = 64
transform = transforms.Compose([transforms.ToTensor(), transforms.Normalize((0.1307,), (0.3081,))])
train_dataset = datasets.MNIST(root=r'D:\code_management\pythonProject\dataset/mnist/', train=True, download=False,
transform=transform)
train_loader = DataLoader(train_dataset, shuffle=True, batch_size=batch_size)
test_dataset = datasets.MNIST(root=r'D:\code_management\pythonProject\dataset/dataset/mnist/', train=False,
download=False, transform=transform)
test_loader = DataLoader(test_dataset, shuffle=False, batch_size=batch_size)
class Net(torch.nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = torch.nn.Conv2d(1, 10, kernel_size=5)
self.conv2 = torch.nn.Conv2d(10, 20, kernel_size=5)
self.pooling = torch.nn.MaxPool2d(2)
self.fc = torch.nn.Linear(320, 10)
def forward(self, x):
batch_size = x.size(0)
x = F.relu(self.pooling(self.conv1(x)))
x = F.relu(self.pooling(self.conv2(x)))
x = x.view(batch_size, -1)
x = self.fc(x)
return x
model = Net()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model.to(device)
criterion = torch.nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.01, momentum=0.5)
def train(epoch):
running_loss = 0.0
for batch_idx, data in enumerate(train_loader, 0):
inputs, target = data
inputs, target = inputs.to(device), target.to(device)
outputs = model(inputs)
loss = criterion(outputs, target)
optimizer.zero_grad()
loss.backward()
optimizer.step()
running_loss += loss.item()
if batch_idx % 300 == 299:
print('[%d, %5d] loss: %.3f' % (epoch + 1, batch_idx + 1, running_loss / 300))
running_loss = 0.0
def test():
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images)
_, predicted = torch.max(outputs.data, dim=1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('accuracy on test set: %d %% ' % (100 * correct / total))
if __name__ == '__main__':
for epoch in range(10):
train(epoch)
test()
GoogleNet
卷积核大小为1的操作能通过减小维度较少计算量,但是有信息损失。
- 16,24表示Channels,注意要保持输出大小width,height保持不变,通过padding和kernelsize大小来保持。
|