resnet18图像分类 pytorch
时间: 2023-11-03 19:05:29 浏览: 111
ResNet18是ResNet系列中的一种卷积神经网络模型,它是由18层卷积层和全连接层组成的。ResNet18的主要特点是使用了残差块(residual block)来解决深度神经网络训练过程中的梯度消失问题,从而可以训练更深的网络。在图像分类任务中,ResNet18可以达到较高的准确率。在Pytorch中,可以使用预训练的ResNet18模型进行图像分类任务,也可以根据自己的需求进行微调或重新训练。
相关问题
resnet18代码复现pytorch图像分类
### 使用 PyTorch 实现 ResNet18 进行图像分类
为了实现一个完整的 ResNet18 图像分类器,可以按照以下部分构建代码:
#### 导入必要的库
```python
import torch
from torchvision import datasets, transforms, models
from torch.utils.data import DataLoader
import torch.nn as nn
import torch.optim as optim
```
#### 定义数据预处理和加载函数
定义用于训练集和验证集的数据转换操作。对于 CIFAR-10 数据集来说,通常会应用一些增强技术来提高泛化能力。
```python
transform_train = transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
transform_test = transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.4914, 0.4822, 0.4465), (0.2023, 0.1994, 0.2010)),
])
trainset = datasets.CIFAR10(root='./data', train=True, download=True, transform=transform_train)
testset = datasets.CIFAR10(root='./data', train=False, download=True, transform=transform_test)
trainloader = DataLoader(trainset, batch_size=128, shuffle=True, num_workers=2)
testloader = DataLoader(testset, batch_size=100, shuffle=False, num_workers=2)
```
#### 加载预训练的 ResNet18 模型并修改最后一层全连接层以适应 CIFAR-10 的类别数
这里使用 `models.resnet18(pretrained=True)` 来获取带有 ImageNet 预训练权重的 ResNet18 模型,并调整其最后的线性层以匹配 CIFAR-10 中的十个类别[^1]。
```python
model = models.resnet18(pretrained=True)
num_ftrs = model.fc.in_features
model.fc = nn.Linear(num_ftrs, 10) # 修改为适合CIFAR-10的输出维度
device = 'cuda' if torch.cuda.is_available() else 'cpu'
model.to(device)
```
#### 设置损失函数与优化器
采用交叉熵作为损失函数,并选用 Adam 或 SGD 作为优化算法。
```python
criterion = nn.CrossEntropyLoss()
optimizer = optim.Adam(model.parameters(), lr=0.001)
scheduler = optim.lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)
```
#### 训练过程
编写循环来进行多个 epoch 的迭代,在每次迭代过程中更新参数并通过验证集评估性能。
```python
def train_model(model, criterion, optimizer, scheduler, num_epochs=25):
best_acc = 0.0
for epoch in range(num_epochs):
print(f'Epoch {epoch}/{num_epochs - 1}')
print('-' * 10)
# Each epoch has a training and validation phase
for phase in ['train', 'val']:
if phase == 'train':
dataloaders = {'train': trainloader}
model.train() # Set model to training mode
else:
dataloaders = {'val': testloader}
model.eval() # Set model to evaluate mode
running_loss = 0.0
running_corrects = 0
# Iterate over data.
for inputs, labels in dataloaders[phase]:
inputs = inputs.to(device)
labels = labels.to(device)
# zero the parameter gradients
optimizer.zero_grad()
# forward
with torch.set_grad_enabled(phase=='train'):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)
# backward + optimize only if in training phase
if phase == 'train':
loss.backward()
optimizer.step()
# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
if phase == 'train':
scheduler.step()
epoch_loss = running_loss / len(dataloaders[phase].dataset)
epoch_acc = running_corrects.double() / len(dataloaders[phase].dataset)
print('{} Loss: {:.4f} Acc: {:.4f}'.format(
phase, epoch_loss, epoch_acc))
# deep copy the model
if phase == 'val' and epoch_acc > best_acc:
best_acc = epoch_acc
print('Best val Acc: {:4f}'.format(best_acc))
return model
```
调用上述定义好的 `train_model` 函数即可启动训练流程。
resnet18 cifar100 pytorch
ResNet18是一种深度卷积神经网络模型,适用于图像分类任务。CIFAR100是一个包含100个类别的图像分类数据集。在PyTorch中,可以使用ResNet18模型对CIFAR100数据集进行训练和测试。
使用ResNet18对CIFAR100数据集进行训练的参数和代码如下:
```
# 导入必要的库
import torch
import torch.nn as nn
import torch.optim as optim
import torchvision.transforms as transforms
import torchvision.datasets as datasets
from torch.utils.data import DataLoader
# 加载数据集
train_dataset = datasets.CIFAR100(root='./data', train=True, download=True, transform=transforms.Compose([
transforms.RandomCrop(32, padding=4),
transforms.RandomHorizontalFlip(),
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
]))
test_dataset = datasets.CIFAR100(root='./data', train=False, download=True, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
]))
train_loader = DataLoader(train_dataset, batch_size=1024, shuffle=True)
test_loader = DataLoader(test_dataset, batch_size=1024, shuffle=False)
# 定义模型
class ResNet18(nn.Module):
def __init__(self, num_classes=100):
super(ResNet18, self).__init__()
self.resnet18 = torch.hub.load('pytorch/vision:v0.6.0', 'resnet18', pretrained=False)
self.resnet18.fc = nn.Linear(512, num_classes)
def forward(self, x):
x = self.resnet18(x)
return x
# 训练模型
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu')
model = ResNet18().to(device)
criterion = nn.CrossEntropyLoss()
optimizer = optim.SGD(model.parameters(), lr=0.002, momentum=0.9, weight_decay=5e-4)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[80, 120], gamma=0.1)
num_epochs = 200
best_acc = 0.0
for epoch in range(num_epochs):
model.train()
train_loss = 0.0
train_acc = 0.0
for i, (images, labels) in enumerate(train_loader):
images = images.to(device)
labels = labels.to(device)
optimizer.zero_grad()
outputs = model(images)
loss = criterion(outputs, labels)
loss.backward()
optimizer.step()
train_loss += loss.item() * images.size(0)
_, preds = torch.max(outputs, 1)
train_acc += torch.sum(preds == labels.data)
train_loss = train_loss / len(train_loader.dataset)
train_acc = train_acc / len(train_loader.dataset)
model.eval()
test_loss = 0.0
test_acc = 0.0
for i, (images, labels) in enumerate(test_loader):
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
loss = criterion(outputs, labels)
test_loss += loss.item() * images.size(0)
_, preds = torch.max(outputs, 1)
test_acc += torch.sum(preds == labels.data)
test_loss = test_loss / len(test_loader.dataset)
test_acc = test_acc / len(test_loader.dataset)
if test_acc > best_acc:
best_acc = test_acc
torch.save(model.state_dict(), './res/best_model.pth')
print('Epoch [{}/{}], Train Loss: {:.4f}, Train Acc: {:.4f}, Test Loss: {:.4f}, Test Acc: {:.4f}'.format(
epoch + 1, num_epochs, train_loss, train_acc, test_loss, test_acc))
scheduler.step()
# 测试模型
model.load_state_dict(torch.load('./res/best_model.pth'))
model.eval()
test_acc = 0.0
for i, (images, labels) in enumerate(test_loader):
images = images.to(device)
labels = labels.to(device)
outputs = model(images)
_, preds = torch.max(outputs, 1)
test_acc += torch.sum(preds == labels.data)
test_acc = test_acc / len(test_loader.dataset)
print('Test Acc: {:.4f}'.format(test_acc))
```
阅读全文