AlexNet模型

概述


代码实现

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
import torch
from torch import nn
from d2l import torch as d2l
from torch.utils import data
from torchvision import transforms
import torchvision
%matplotlib inline

d2l.use_svg_display()

net = nn.Sequential(
nn.Conv2d(1, 96, kernel_size=11, stride=4, padding=1), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(96, 256, kernel_size=5, padding=2), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2),
nn.Conv2d(256, 384, kernel_size=3, padding=1), nn.ReLU(),
nn.Conv2d(384, 384, kernel_size=3, padding=1), nn.ReLU(),
nn.Conv2d(384, 256, kernel_size=3, padding=1), nn.ReLU(),
nn.MaxPool2d(kernel_size=3, stride=2), nn.Flatten(),
nn.Linear(6400, 4096), nn.ReLU(), nn.Dropout(p=0.5),
nn.Linear(4096, 4096), nn.ReLU(), nn.Dropout(p=0.5),
nn.Linear(4096, 10)
)

1
2
3
4
5
X = torch.randn(1, 1, 224, 224)
for layer in net:
X = layer(X)
print(layer.__class__.__name__, "Output shape:\t", X.shape)

Conv2d Output shape:     torch.Size([1, 96, 54, 54])
ReLU Output shape:     torch.Size([1, 96, 54, 54])
MaxPool2d Output shape:     torch.Size([1, 96, 26, 26])
Conv2d Output shape:     torch.Size([1, 256, 26, 26])
ReLU Output shape:     torch.Size([1, 256, 26, 26])
MaxPool2d Output shape:     torch.Size([1, 256, 12, 12])
Conv2d Output shape:     torch.Size([1, 384, 12, 12])
ReLU Output shape:     torch.Size([1, 384, 12, 12])
Conv2d Output shape:     torch.Size([1, 384, 12, 12])
ReLU Output shape:     torch.Size([1, 384, 12, 12])
Conv2d Output shape:     torch.Size([1, 256, 12, 12])
ReLU Output shape:     torch.Size([1, 256, 12, 12])
MaxPool2d Output shape:     torch.Size([1, 256, 5, 5])
Flatten Output shape:     torch.Size([1, 6400])
Linear Output shape:     torch.Size([1, 4096])
ReLU Output shape:     torch.Size([1, 4096])
Dropout Output shape:     torch.Size([1, 4096])
Linear Output shape:     torch.Size([1, 4096])
ReLU Output shape:     torch.Size([1, 4096])
Dropout Output shape:     torch.Size([1, 4096])
Linear Output shape:     torch.Size([1, 10])

拿Fashion-MINST的数据集跑一下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
def get_dataloader_workers():
return 4


def load_data_fashion_mnist(batch_size, resize=None):
"""下载Fashion-MNIST数据集,然后将其加载到内存中"""
trans = [transforms.ToTensor()]
if resize:
trans.insert(0, transforms.Resize(resize))
trans = transforms.Compose(trans)
mnist_train = torchvision.datasets.FashionMNIST(
root="./data", train=True, transform=trans, download=True)
mnist_test = torchvision.datasets.FashionMNIST(
root="./data", train=False, transform=trans, download=True)
return (data.DataLoader(mnist_train, batch_size, shuffle=True,
num_workers=get_dataloader_workers()),
data.DataLoader(mnist_test, batch_size, shuffle=False,
num_workers=get_dataloader_workers()))


batch_size = 128
train_iter, test_iter = load_data_fashion_mnist(
batch_size=batch_size, resize=224)
# 因为Fashion-MNIST图像的分辨率,低于ImageNet图像,我们将它们强行拉长到224×224

1
2
3
lr, num_epochs = 0.05, 10
d2l.train_ch6(net, train_iter=train_iter, test_iter=test_iter, num_epochs=num_epochs, lr=lr, device=d2l.try_gpu())

由于手头上的电脑没有独立显卡,这是沐神的截图

(鼠鼠暂时没有能跑cuda的机器,只能拿李沐老师的运行效果图充数了QAQ)
今晚就去给拯救者配置cuda环境(逃)

2022/8/18日更新

我回来辣
成功在游戏本上安装pytorch运行环境,并且可以使用cuda
电脑所支持的cuda版本

电脑所装的的cuda版本

从pytorch官网装的是pytorch11.6版本(向下兼容)

我们来看一下运行效果(装了一下午加一晚上人整麻了)

运行时间