请用Anaconda3 写python代码: 设计自定义的ResNet 数据:MINST <导入必要的PyTorch包 1.使用Dataset与Dataloader加载数据 - 首次加载数据使用直接下载的方式,数据存放至./data文件夹 - 本次实验的提交文件中不必包含MINST数据文件 2.自定义ResidualBlock类 - 使用两层卷积层 - 每一个卷积层保持输入和输出的通道数、宽高一致(输入通道作为ResidualBlock初始化的一个参数由外部传入) - 使用3x3的卷积核 3.定义前面给出的模型结构并实例化网络模型、交叉熵损失、SGD优化器 4.定义单次训练/测试的函数训练阶段每300个batch打印一下损失 5.设置训练周期为10次,打印每个周期中训练过程中的模型损失及测试过程中的准确率 6.定义一个函数get_n_params,来计算上述模型中的参数个数 ```def get_n_params(model)``` Hint: 调研model.parameters()的使用 7.保存训练好的模型到文件resnet.pt
时间: 2024-02-09 09:11:04 浏览: 73
好的,以下是代码实现:
```python
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
from torch.utils.data import DataLoader, Dataset
# 定义自定义的ResidualBlock类
class ResidualBlock(nn.Module):
def __init__(self, in_channels):
super(ResidualBlock, self).__init__()
self.conv1 = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(in_channels)
self.relu = nn.ReLU(inplace=True)
self.conv2 = nn.Conv2d(in_channels, in_channels, kernel_size=3, stride=1, padding=1, bias=False)
self.bn2 = nn.BatchNorm2d(in_channels)
def forward(self, x):
identity = x
out = self.conv1(x)
out = self.bn1(out)
out = self.relu(out)
out = self.conv2(out)
out = self.bn2(out)
out += identity
out = self.relu(out)
return out
# 定义自定义的ResNet模型
class ResNet(nn.Module):
def __init__(self):
super(ResNet, self).__init__()
self.conv1 = nn.Conv2d(1, 64, kernel_size=3, stride=1, padding=1, bias=False)
self.bn1 = nn.BatchNorm2d(64)
self.relu = nn.ReLU(inplace=True)
self.layer1 = nn.Sequential(
ResidualBlock(64),
ResidualBlock(64)
)
self.layer2 = nn.Sequential(
nn.Conv2d(64, 128, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(128),
nn.ReLU(inplace=True),
ResidualBlock(128),
ResidualBlock(128)
)
self.layer3 = nn.Sequential(
nn.Conv2d(128, 256, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(256),
nn.ReLU(inplace=True),
ResidualBlock(256),
ResidualBlock(256)
)
self.layer4 = nn.Sequential(
nn.Conv2d(256, 512, kernel_size=3, stride=2, padding=1, bias=False),
nn.BatchNorm2d(512),
nn.ReLU(inplace=True),
ResidualBlock(512),
ResidualBlock(512)
)
self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
self.fc = nn.Linear(512, 10)
def forward(self, x):
x = self.conv1(x)
x = self.bn1(x)
x = self.relu(x)
x = self.layer1(x)
x = self.layer2(x)
x = self.layer3(x)
x = self.layer4(x)
x = self.avgpool(x)
x = x.view(x.size(0), -1)
x = self.fc(x)
return x
# 定义自定义的MINST数据集
class CustomMNISTDataset(Dataset):
def __init__(self, train=True, transform=None):
self.train = train
self.transform = transform
if self.train:
self.data, self.labels = torch.utils.data.Subset(torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transforms.ToTensor()), list(range(0, 50000)))
else:
self.data, self.labels = torch.utils.data.Subset(torchvision.datasets.MNIST(root='./data', train=True, download=True, transform=transforms.ToTensor()), list(range(50000, 60000)))
def __getitem__(self, index):
img, target = self.data[index], self.labels[index]
if self.transform is not None:
img = self.transform(img)
return img, target
def __len__(self):
return len(self.data)
# 定义数据预处理方式
transform = transforms.Compose([
transforms.Normalize((0.1307,), (0.3081,))
])
# 加载训练集和测试集
train_dataset = CustomMNISTDataset(train=True, transform=transform)
train_loader = DataLoader(train_dataset, batch_size=128, shuffle=True)
test_dataset = CustomMNISTDataset(train=False, transform=transform)
test_loader = DataLoader(test_dataset, batch_size=128, shuffle=False)
# 创建ResNet模型
model = ResNet()
# 定义损失函数和优化器
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.1, momentum=0.9, weight_decay=5e-4)
# 定义单次训练函数
def train_one_epoch(model, train_loader, criterion, optimizer, epoch):
model.train()
running_loss = 0.0
for i, data in enumerate(train_loader, 0):
inputs, labels = data
optimizer.zero_grad()
outputs = model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
if i % 300 == 299: # 每300个batch打印一下损失
print('[Epoch %d, Batch %d] loss: %.3f' % (epoch + 1, i + 1, running_loss / 300))
running_loss = 0.0
# 定义测试函数
def test(model, test_loader):
model.eval()
correct = 0
total = 0
with torch.no_grad():
for data in test_loader:
images, labels = data
outputs = model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
accuracy = 100 * correct / total
return accuracy
# 训练模型
for epoch in range(10):
train_one_epoch(model, train_loader, criterion, optimizer, epoch)
test_accuracy = test(model, test_loader)
print('[Epoch %d] test accuracy: %.2f%%' % (epoch + 1, test_accuracy))
# 定义函数计算模型参数个数
def get_n_params(model):
return sum([p.numel() for p in model.parameters()])
# 打印模型参数个数
print('Number of parameters: %d' % get_n_params(model))
# 保存模型
torch.save(model.state_dict(), 'resnet.pt')
```
注意:这段代码在第一次运行时会直接下载MINST数据集,存放至./data文件夹下。如果之后再次运行需要手动删除./data文件夹下的所有文件。同时,为了减小训练集大小,代码中只使用前50000个样本作为训练集,后10000个样本作为测试集。如果需要使用完整的训练集,可以将`self.data, self.labels`的赋值语句改为`torchvision.datasets.MNIST(root='./data', train=self.train, download=True, transform=transforms.ToTensor())`。
阅读全文