还是针对天气处理的图片的四个分类,使用这个网络模型resnet18
import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import numpy as np
import matplotlib.pyplot as plt
%matplotlib inline
import torchvision
from torchvision import transforms
import os
base_dir = r'你的图片文件地址'
train_dir = os.path.join(base_dir , 'train')#测试数据集目录
test_dir = os.path.join(base_dir , 'test') #检测的数据集目录
transform = transforms.Compose([
transforms.Resize((192, 192)),
transforms.ToTensor(),
transforms.Normalize(mean=[.5, .5, .5], std=[.5, .5, .5])
])
train_ds = torchvision.datasets.ImageFolder(
train_dir,
transform=transform
)#测试数据集
test_ds = torchvision.datasets.ImageFolder(
test_dir,
transform=transform
)#检测数据集,将图片格式转化成dataset
BTACH_SIZE = 32#可调节参数,显存不足的时候一定要调节参数
train_dl = torch.utils.data.DataLoader(
train_ds,
batch_size=BTACH_SIZE,
shuffle=True
)
test_dl = torch.utils.data.DataLoader(
test_ds,
batch_size=BTACH_SIZE,
)
#选择resnet18进行训练
model = torchvision.models.resnet18(pretrained=True)
for param in model.parameters():
param.requires_grad = False#上面所有的参数保持不动
in_f = model.fc.in_features#保证最后这个参数准确的传入
model.fc = nn.Linear(in_f, 4)#替换掉最后的fc的层,分成4种类型
if torch.cuda.is_available():
model.to('cuda')
loss_fn = nn.CrossEntropyLoss()
#选择adam优化器,选择这个的话正确率相对比较高一些
optimizer = torch.optim.Adam(model.fc.parameters(), lr=0.0001)
#然后就是训练就好了
def fit(epoch, model, trainloader, testloader):
correct = 0
total = 0
running_loss = 0
model.train()
for x, y in trainloader:
if torch.cuda.is_available():
x, y = x.to('cuda'), y.to('cuda')
y_pred = model(x)
loss = loss_fn(y_pred, y)
optimizer.zero_grad()
loss.backward()
optimizer.step()
with torch.no_grad():
y_pred = torch.argmax(y_pred, dim=1)
correct += (y_pred == y).sum().item()
total += y.size(0)
running_loss += loss.item()
# exp_lr_scheduler.step()
epoch_loss = running_loss / len(trainloader.dataset)
epoch_acc = correct / total
test_correct = 0
test_total = 0
test_running_loss = 0
model.eval()
with torch.no_grad():
for x, y in testloader:
if torch.cuda.is_available():
x, y = x.to('cuda'), y.to('cuda')
y_pred = model(x)
loss = loss_fn(y_pred, y)
y_pred = torch.argmax(y_pred, dim=1)
test_correct += (y_pred == y).sum().item()
test_total += y.size(0)
test_running_loss += loss.item()
epoch_test_loss = test_running_loss / len(testloader.dataset)
epoch_test_acc = test_correct / test_total
print('epoch: ', epoch,
'loss: ', round(epoch_loss, 3),
'accuracy:', round(epoch_acc, 3),
'test_loss: ', round(epoch_test_loss, 3),
'test_accuracy:', round(epoch_test_acc, 3)
)
return epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc
epochs = 50
train_loss = []
train_acc = []
test_loss = []
test_acc = []
for epoch in range(epochs):
epoch_loss, epoch_acc, epoch_test_loss, epoch_test_acc = fit(epoch,
model,
train_dl,
test_dl)
train_loss.append(epoch_loss)
train_acc.append(epoch_acc)
test_loss.append(epoch_test_loss)
test_acc.append(epoch_test_acc)
训练模型的内部结构会专门写一下的,使用这个网络模型进行图片处理的训练,主要是更改最后输出的全连接层,改成自己的即可,其他的还是原来的训练方式。
本内容借鉴于日月光华。