实现从MINIST 数据集 到 FashionMinist数据集的迁移学习代码
时间: 2024-01-22 11:01:47 浏览: 69
MINIST深度学习机器学习用到的手写字符识别数据集资源
以下是使用PyTorch实现从MNIST数据集到FashionMNIST数据集的迁移学习的代码示例:
```python
import torch
import torch.nn as nn
import torch.optim as optim
from torch.utils.data import DataLoader
from torchvision.datasets import MNIST, FashionMNIST
from torchvision.transforms import ToTensor, Normalize
from tqdm import tqdm
# 加载MNIST数据集
mnist_trainset = MNIST(root='./data', train=True, download=True, transform=ToTensor())
mnist_testset = MNIST(root='./data', train=False, download=True, transform=ToTensor())
mnist_trainloader = DataLoader(mnist_trainset, batch_size=64, shuffle=True)
mnist_testloader = DataLoader(mnist_testset, batch_size=64, shuffle=False)
# 加载FashionMNIST数据集
fashion_trainset = FashionMNIST(root='./data', train=True, download=True, transform=ToTensor())
fashion_testset = FashionMNIST(root='./data', train=False, download=True, transform=ToTensor())
fashion_trainloader = DataLoader(fashion_trainset, batch_size=64, shuffle=True)
fashion_testloader = DataLoader(fashion_testset, batch_size=64, shuffle=False)
# 定义模型
class LeNet(nn.Module):
def __init__(self):
super(LeNet, self).__init__()
self.conv1 = nn.Conv2d(1, 6, kernel_size=5)
self.pool1 = nn.MaxPool2d(kernel_size=2)
self.conv2 = nn.Conv2d(6, 16, kernel_size=5)
self.pool2 = nn.MaxPool2d(kernel_size=2)
self.fc1 = nn.Linear(16 * 4 * 4, 120)
self.fc2 = nn.Linear(120, 84)
self.fc3 = nn.Linear(84, 10)
def forward(self, x):
x = self.pool1(torch.relu(self.conv1(x)))
x = self.pool2(torch.relu(self.conv2(x)))
x = x.view(-1, 16 * 4 * 4)
x = torch.relu(self.fc1(x))
x = torch.relu(self.fc2(x))
x = self.fc3(x)
return x
# 加载预训练的模型
pretrained_model = LeNet()
pretrained_model.load_state_dict(torch.load('mnist_model.pt'))
# 添加新的全连接层
pretrained_model.fc4 = nn.Linear(84, 10)
# 冻结已经训练好的模型的所有层
for param in pretrained_model.parameters():
param.requires_grad = False
# 定义优化器和损失函数
optimizer = optim.Adam(pretrained_model.fc4.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
# 训练新添加的全连接层
for epoch in range(10):
running_loss = 0.0
for i, data in tqdm(enumerate(fashion_trainloader), total=len(fashion_trainloader)):
inputs, labels = data
optimizer.zero_grad()
outputs = pretrained_model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print('Epoch %d, loss: %.3f' % (epoch + 1, running_loss / len(fashion_trainloader)))
# 解冻已经训练好的模型的所有层
for param in pretrained_model.parameters():
param.requires_grad = True
# 定义优化器和损失函数
optimizer = optim.Adam(pretrained_model.parameters(), lr=0.001)
criterion = nn.CrossEntropyLoss()
# 端到端的微调
for epoch in range(10):
running_loss = 0.0
for i, data in tqdm(enumerate(fashion_trainloader), total=len(fashion_trainloader)):
inputs, labels = data
optimizer.zero_grad()
outputs = pretrained_model(inputs)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
running_loss += loss.item()
print('Epoch %d, loss: %.3f' % (epoch + 1, running_loss / len(fashion_trainloader)))
# 在测试集上进行测试
total = 0
correct = 0
with torch.no_grad():
for data in tqdm(fashion_testloader, total=len(fashion_testloader)):
images, labels = data
outputs = pretrained_model(images)
_, predicted = torch.max(outputs.data, 1)
total += labels.size(0)
correct += (predicted == labels).sum().item()
print('Accuracy of the network on the %d test images: %.2f%%' % (total, 100.0 * correct / total))
# 保存模型
torch.save(pretrained_model.state_dict(), 'fashion_model.pt')
```
在该代码中,我们首先加载MNIST和FashionMNIST数据集,并且定义了LeNet模型。然后,我们加载了在MNIST数据集上训练好的LeNet模型,并且添加了一个新的全连接层用于适应FashionMNIST数据集。接下来,我们冻结了已经训练好的模型的所有层,并且只训练了新添加的全连接层。训练完成后,我们解冻了已经训练好的模型的所有层,并且进行了端到端的微调。最后,我们在FashionMNIST测试集上对模型进行了测试,并且保存了模型的参数。
阅读全文