pytorch进行fashion mnist数据集分类
标题1.数据集介绍(1)MNISTMNIST是深度学习最基本的数据集之一,由CNN鼻祖yann lecun建立的一个手写字符数据集,包含60000张训练图像和10000张测试图像,包含数字0-9共10个类别.(2)FASHION MNIST由于MNIST数据集太简单,简单的网络就可以达到99.7%的top one准确率,也就是说在这个数据集上表现较好的网络,在别的任务上表现不一定好。因此...
1.数据集介绍
最近在撸pytorch框架,这里参考深度学习经典数据集mnist的“升级版”fashion mnist,来做图像分类,主要目的是熟悉pytorch框架,代码中包含了大量的pytorch使用相关的注释。
(1)MNIST
MNIST是深度学习最基本的数据集之一,由CNN鼻祖yann lecun建立的一个手写字符数据集,包含60000张训练图像和10000张测试图像,包含数字0-9共10个类别.
(2)FASHION MNIST
由于MNIST数据集太简单,简单的网络就可以达到99%以上的top one准确率,也就是说在这个数据集上表现较好的网络,在别的任务上表现不一定好。因此zalando research的工作人员建立了fashion mnist数据集,该数据集由衣服、鞋子等服饰组成,包含70000张图像,其中60000张训练图像加10000张测试图像,图像大小为28x28,单通道,共分10个类,如下图,每3行表示一个类。
数据集信息如下:
数据集共分10个类,类别描述如下:
标题2. pytorch进行分类
pytorch中提供了这个数据集的下载接口,下面分别使用全连接网络和CNN网络来进行分类
(1)FC网络
输入图像大小为28x28,设计如下全连接网络,代码命名为02_fashion_mnist_fc.py
FC1(784) + Relu(1000) + FC2(500) + Relu + FC3(200) + Relu3 + FC4(10) + log_softmax
from __future__ import print_function # 从future版本导入print函数功能
import argparse # 加载处理命令行参数的库
import torch # 引入相关的包
import torch.nn as nn # 指定torch.nn别名nn
import torch.nn.functional as F # 引用神经网络常用函数包,不具有可学习的参数
import torch.optim as optim
from torchvision import datasets, transforms # 加载pytorch官方提供的dataset
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.fc1 = nn.Linear(784, 1000) # 784表示输入神经元数量,1000表示输出神经元数量
self.fc2 = nn.Linear(1000, 500)
self.fc3 = nn.Linear(500, 200)
self.fc4 = nn.Linear(200, 10)
def forward(self, x):
x = x.view(-1, 28*28)
x = F.relu(self.fc1(x))
x = F.relu(self.fc2(x))
x = F.relu(self.fc3(x))
x = self.fc4(x)
return F.log_softmax(x, dim=1) #Applies a softmax followed by a logarithm, output batch * classes tensor
def train(args, model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target) # negative log likelihood loss(nll_loss), sum up batch cross entropy
loss.backward()
optimizer.step() # 根据parameter的梯度更新parameter的值
#print(epoch, batch_idx, type(batch_idx))
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def test(args, model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad(): #无需计算梯度
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
def main():
# Training settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--batch-size', type=int, default=64, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
help='input batch size for testing (default: 1000)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
help='learning rate (default: 0.01)')
parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
help='SGD momentum (default: 0.5)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--save-model', action='store_true', default=True,
help='For Saving the current Model')
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
train_loader = torch.utils.data.DataLoader(
datasets.FashionMNIST('./fashionmnist_data/', train=True, download=True,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.FashionMNIST('./fashionmnist_data/', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=args.test_batch_size, shuffle=True, **kwargs)
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum) #optimizer存储了所有parameters的引用,每个parameter都包含gradient
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[12, 24], gamma=0.1) #学习率按区间更新
for epoch in range(1, args.epochs + 1):
train(args, model, device, train_loader, optimizer, epoch)
test(args, model, device, test_loader)
if (args.save_model):
torch.save(model.state_dict(), "mnist_fc.pt")
# 当.py文件直接运行时,该语句及以下的代码被执行,当.py被调用时,该语句及以下的代码不被执行
if __name__ == '__main__':
main()
python 02_fashion_mnist_fc.py --epochs=36
备注:
数据集下载比较慢,第一次训练时,train_loader中download设置为True,后面再训练时改为False
F.log_softmax只是对输出结果做softmax后再取log
optimizer存储了所有parameters的引用,每个parameter都包含gradient
scheduler根据设置的epoch区间来调整学习率大小,调整率为gamma
训练过程中,随机选择1个batch的数据显示,如下:
训练结果如下,top1准确率为88%,网络参数大小为5.1M
2.2 CNN网络
FC网络参数量太大,而CNN网络考虑到图像的局部关联特性,使用卷积网络,参数量大小减小,设计如下CNN,代码全名为02_fashion_mnist_cnn.py
conv(1, 20, 5) + Relu + conv(20, 50, 5) + flatten + Relu + FC(10) + log_softmax
from __future__ import print_function # 从future版本导入print函数功能
import argparse # 加载处理命令行参数的库
import torch # 引入相关的包
import torch.nn as nn # 指定torch.nn别名nn
import torch.nn.functional as F # 引用神经网络常用函数包,不具有可学习的参数
import torch.optim as optim
from torchvision import datasets, transforms # 加载pytorch官方提供的dataset
class Net(nn.Module):
def __init__(self):
super(Net, self).__init__()
self.conv1 = nn.Conv2d(1, 20, 5, 1) # 1表示输入通道,20表示输出通道,5表示conv核大小,1表示conv步长
self.conv2 = nn.Conv2d(20, 50, 5, 1)
self.fc1 = nn.Linear(4 * 4 * 50, 500)
self.fc2 = nn.Linear(500, 10)
def forward(self, x):
x = F.relu(self.conv1(x))
x = F.max_pool2d(x, 2, 2)
x = F.relu(self.conv2(x))
x = F.max_pool2d(x, 2, 2)
x = x.view(-1, 4 * 4 * 50)
x = F.relu(self.fc1(x))
x = self.fc2(x)
return F.log_softmax(x, dim=1)
def train(args, model, device, train_loader, optimizer, epoch):
model.train()
for batch_idx, (data, target) in enumerate(train_loader):
data, target = data.to(device), target.to(device)
optimizer.zero_grad()
output = model(data)
loss = F.nll_loss(output, target)
loss.backward()
optimizer.step()
if batch_idx % args.log_interval == 0:
print('Train Epoch: {} [{}/{} ({:.0f}%)]\tLoss: {:.6f}'.format(
epoch, batch_idx * len(data), len(train_loader.dataset),
100. * batch_idx / len(train_loader), loss.item()))
def test(args, model, device, test_loader):
model.eval()
test_loss = 0
correct = 0
with torch.no_grad():
for data, target in test_loader:
data, target = data.to(device), target.to(device)
output = model(data)
test_loss += F.nll_loss(output, target, reduction='sum').item() # sum up batch loss
pred = output.argmax(dim=1, keepdim=True) # get the index of the max log-probability
correct += pred.eq(target.view_as(pred)).sum().item()
test_loss /= len(test_loader.dataset)
print('\nTest set: Average loss: {:.4f}, Accuracy: {}/{} ({:.0f}%)\n'.format(
test_loss, correct, len(test_loader.dataset),
100. * correct / len(test_loader.dataset)))
def main():
# Training settings
parser = argparse.ArgumentParser(description='PyTorch MNIST Example')
parser.add_argument('--batch-size', type=int, default=64, metavar='N',
help='input batch size for training (default: 64)')
parser.add_argument('--test-batch-size', type=int, default=1000, metavar='N',
help='input batch size for testing (default: 1000)')
parser.add_argument('--epochs', type=int, default=10, metavar='N',
help='number of epochs to train (default: 10)')
parser.add_argument('--lr', type=float, default=0.01, metavar='LR',
help='learning rate (default: 0.01)')
parser.add_argument('--momentum', type=float, default=0.5, metavar='M',
help='SGD momentum (default: 0.5)')
parser.add_argument('--no-cuda', action='store_true', default=False,
help='disables CUDA training')
parser.add_argument('--seed', type=int, default=1, metavar='S',
help='random seed (default: 1)')
parser.add_argument('--log-interval', type=int, default=10, metavar='N',
help='how many batches to wait before logging training status')
parser.add_argument('--save-model', action='store_true', default=False,
help='For Saving the current Model')
args = parser.parse_args()
use_cuda = not args.no_cuda and torch.cuda.is_available()
torch.manual_seed(args.seed)
device = torch.device("cuda" if use_cuda else "cpu")
kwargs = {'num_workers': 1, 'pin_memory': True} if use_cuda else {}
train_loader = torch.utils.data.DataLoader(
datasets.FashionMNIST('./fashionmnist_data/', train=True, download=False,
transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=args.batch_size, shuffle=True, **kwargs)
test_loader = torch.utils.data.DataLoader(
datasets.FashionMNIST('./fashionmnist_data/', train=False, transform=transforms.Compose([
transforms.ToTensor(),
transforms.Normalize((0.1307,), (0.3081,))
])),
batch_size=args.test_batch_size, shuffle=True, **kwargs)
model = Net().to(device)
optimizer = optim.SGD(model.parameters(), lr=args.lr, momentum=args.momentum)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer, milestones=[12, 24], gamma=0.1)
for epoch in range(1, args.epochs + 1):
train(args, model, device, train_loader, optimizer, epoch)
test(args, model, device, test_loader)
if (args.save_model):
torch.save(model.state_dict(), "mnist_cnn.pt")
# 当.py文件直接运行时,该语句及以下的代码被执行,当.py被调用时,该语句及以下的代码不被执行
if __name__ == '__main__':
main()
python 02_fashion_mnist_cnn.py --epochs=36
训练结果如下,top1准确率为91%。
标题3. references
[1] https://github.com/zalandoresearch/fashion-mnist
[2] https://pytorch.org/docs/stable/_modules/torch/optim/lr_scheduler.html
[3] https://github.com/zalandoresearch/fashion-mnist
开放原子开发者工作坊旨在鼓励更多人参与开源活动,与志同道合的开发者们相互交流开发经验、分享开发心得、获取前沿技术趋势。工作坊有多种形式的开发者活动,如meetup、训练营等,主打技术交流,干货满满,真诚地邀请各位开发者共同参与!
更多推荐
所有评论(0)