大家好,又见面了,我是你们的朋友全栈君。
在上一篇文章:CNN训练前的准备:pytorch处理自己的图像数据(Dataset和Dataloader),大致介绍了怎么利用pytorch把猫狗图片处理成CNN需要的数据,今天就用该数据对自己定义的CNN模型进行训练及测试。
- 首先导入需要的包:
import torch
from torch import optim
import torch.nn as nn
from torch.autograd import Variable
from torchvision import transforms
from torch.utils.data import Dataset, DataLoader
from PIL import Image
- 定义自己的CNN网络
class cnn(nn.Module):
def __init__(self):
super(cnn, self).__init__()
self.relu = nn.ReLU()
self.sigmoid = nn.Sigmoid()
self.conv1 = nn.Sequential(
nn.Conv2d(
in_channels=3,
out_channels=16,
kernel_size=3,
stride=2,
),
nn.BatchNorm2d(16),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
#
self.conv2 = nn.Sequential(
nn.Conv2d(
in_channels=16,
out_channels=32,
kernel_size=3,
stride=2,
),
nn.BatchNorm2d(32),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
#
self.conv3 = nn.Sequential(
nn.Conv2d(
in_channels=32,
out_channels=64,
kernel_size=3,
stride=2,
),
nn.BatchNorm2d(64),
nn.ReLU(),
nn.MaxPool2d(kernel_size=2),
)
self.fc1 = nn.Linear(3 * 3 * 64, 64)
self.fc2 = nn.Linear(64, 10)
self.out = nn.Linear(10, 2)
def forward(self, x):
x = self.conv1(x)
x = self.conv2(x)
x = self.conv3(x)
# print(x.size())
x = x.view(x.shape[0], -1)
x = self.relu(self.fc1(x))
x = self.relu(self.fc2(x))
x = self.out(x)
return x
- 训练(GPU)
def train():
train_loader, test_loader = load_data()
print('train...')
epoch_num = 15
# GPU
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = cnn().to(device)
optimizer = optim.Adam(model.parameters(), lr=0.0008)
criterion = nn.CrossEntropyLoss().to(device)
for epoch in range(epoch_num):
for batch_idx, (data, target) in enumerate(train_loader, 0):
data, target = Variable(data).to(device), Variable(target.long()).to(device)
optimizer.zero_grad()
output = model(data)
loss = criterion(output, target)
loss.backward()
optimizer.step()
if batch_idx % 10 == 0:
print('Epoch: {} [{}/{} ({:.0f}%)]tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
torch.save(model.state_dict(), "model/cnn.pkl")
一共训练三轮,训练的步骤如下:
- 初始化模型:
model = cnn().to(device)
- 选择优化器以及优化算法,这里选择了Adam:
optimizer = optim.Adam(model.parameters(), lr=0.00005)
- 选择损失函数,这里选择了交叉熵:
criterion = nn.CrossEntropyLoss().to(device)
- 对每一个batch里的数据,先将它们转成能被GPU计算的类型:
data, target = Variable(data).to(device), Variable(target.long()).to(device)
- 梯度清零、前向传播、计算误差、反向传播、更新参数:
optimizer.zero_grad() # 梯度清0
output = model(data)[0] # 前向传播
loss = criterion(output, target) # 计算误差
loss.backward() # 反向传播
optimizer.step() # 更新参数
- 测试(GPU)
def test():
train_loader, test_loader = load_data()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
model = torch.load('cnn.pkl') # load model
total = 0
current = 0
for data in test_loader:
images, labels = data
images, labels = images.to(device), labels.to(device)
outputs = model(images)[0]
predicted = torch.max(outputs.data, 1)[1].data
total = labels.size(0)
current = (predicted == labels).sum()
print('Accuracy: %d %%' % (100 * current / total))
一开始只是进行了3轮训练,结果惨不忍睹:
随后训练20轮:
训练30轮:
如果想继续提高精度,可以再次增加训练轮数。
完整代码及数据我放在了GitHub,各位下载时麻烦给个follow和star!!感谢!! 链接:cnn-dogs-vs-cats
发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/131132.html原文链接:https://javaforall.cn