FashionMNIST_CNN with pytorch (accuracy: 97.58%/90.1%)

FashionMNIST_CNN with pytorch (accuracy: 97.58%/90.1%)

接上篇
手工简单复现经典CNN网络测试(LeNet5、AlexNet、VGG16、GoogLeNet、ResNe)
想进行各种CCN经典网络的效果对比,因为个人电脑配置原因失败,不得以科学上网,注册了kaggle进行训练,整体训练分两部分:

1 创建CNN网络训练(网络简单,大概后台运行了5h);

2 使用后台运行产生的模型,进行测试集预测

结果如下:

#1 ----------- first/第一步:kaggle后台训练模型 -------------------------

import torch
import torch.nn as nn
from torch.utils.data import Dataset,DataLoader,TensorDataset
from torch.nn import CrossEntropyLoss
from torch.optim import SGD,Adam

# from torchsummary import summary

from torchvision.datasets import ImageFolder,FashionMNIST
import  torchvision.transforms as transforms


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import joblib

# 读入数据
train_data = pd.read_csv('/kaggle/input/fashionmnist/fashion-mnist_train.csv')
train_y = torch.tensor(train_data['label'])
train_x = torch.tensor(train_data.iloc[:,1:].values.reshape(60000,1,28,28))/255

# 创建批量数据加载器
dataset = TensorDataset(train_x,train_y)
batch_size= 5000 # 批次大小
dataloader = DataLoader(dataset=dataset,batch_size=batch_size,shuffle=True)

# 创建模型
class Net(nn.Module):
    
    def __init__(self):
        super(Net,self).__init__()
        self.conv1 = nn.Conv2d(in_channels=1,out_channels=16,kernel_size=5,stride=1,padding=0)
#         self.maxpool1 = nn.MaxPool2d(kernel_size=2,stride=2,padding=0) 注释原因图片28*28,支持不了太大的网络
        
        self.conv2 = nn.Conv2d(in_channels=16,out_channels=32,kernel_size=3,stride=1,padding=0)
        self.maxpool2 = nn.MaxPool2d(kernel_size=2,stride=2,padding=0)
        
        self.conv3 = nn.Conv2d(in_channels=32,out_channels=64,kernel_size=3,stride=1,padding=0)
        self.conv4 = nn.Conv2d(in_channels=64,out_channels=128,kernel_size=3,stride=1,padding=0)
        self.maxpool3 = nn.MaxPool2d(kernel_size=2,stride=2,padding=0)
        
        self.bn1 = nn.BatchNorm2d(num_features=16)
        self.bn2 = nn.BatchNorm2d(num_features=32)
        self.bn3 = nn.BatchNorm2d(num_features=128)
        
        self.linear1 = nn.Linear(in_features=128*3*3,out_features=1024,device=None,dtype=None)
        self.linear2 = nn.Linear(in_features=1024,out_features=1024,device=None,dtype=None)
        self.linear3 = nn.Linear(in_features=1024,out_features=10,device=None,dtype=None)
        
    def forward(self,x):
        x = self.maxpool2(torch.relu(self.bn2(self.conv2(torch.relu(self.bn1(self.conv1(x)))))))
        x = self.maxpool3(torch.relu(self.bn3(self.conv4(torch.relu(self.conv3(x))))))
        x = x.view(-1,128*3*3)
        z1 = self.linear1(x)
        z2 = self.linear2(z1)
        z3 = self.linear3(z2)
        
        return z3
    
# 设置损失函数、优化器
net = Net()
loss = CrossEntropyLoss()

opt = Adam(params=net.parameters(),lr=0.001)

# 训练
epochs = 200 # 时长大概5h
res = []

for epoch in range(epochs):
    for x,y in dataloader:
        z = net(x)
        loss_value = loss(z,y)
        opt.zero_grad()
        loss_value.backward()
        opt.step()
    print(f'loss:{loss_value}')
    res.append(loss_value.item())
   
# 保存模型和训练过程损失值()
joblib.dump(value=res,filename='./res.pkl')
print('loss_values保存成功')
torch.save(net,f='./model.pkl')
print('模型保存成功')

第2步:

#2 second/第二步 : 读取离线训练好的模型,进行testdatesets的predict

import joblib
import matplotlib.pyplot as plt
import pandas as pd

import torch
import torch.nn as nn
import torchvision.transforms as transforms
from torchvision.datasets import FashionMNIST

from sklearn.metrics import accuracy_score

# 加载并查看训练loss结果
loss = joblib.load('../input/fashionmnist-cnn-with-pytorch-accuracy-97-58/res.pkl')
plt.plot(range(len(loss)),loss)

# 加载训练好的模型
net = torch.load(f='../input/fashionmnist-cnn-with-pytorch-accuracy-97-58/model.pkl')
net

# 计算训练模型的accuracy_score
accuracy_score(torch.argmax(input=net(test_x),dim=1),test_y) # 0.901


备注1:

## 这里有个小疑问,将保存模型下载,使用本地FasHionMNIST数据集test,结果比kaggle上数据集的效果好
# 重新加载test数据集,不知道(自己的离线版本和kaggle在线的数据集是否有差异)
# 备注 RuntimeError: expected scalar type Byte but found Float transforms.ToTensor()只进行了归一化,没有进行float()
test_data = FashionMNIST(root=r"D:\appData\jupyter_lab\2021pytorch课件\WEEK 3、4\MINST-FASHION数据集"
             ,train=False,transform=transforms.ToTensor())

accuracy_score(torch.argmax(input=net(test_data.data.unsqueeze(dim=1).float()).detach(),dim=1),test_y) #0.9758

备注2:

FashionMNIST_CNN with pytorch (accuracy: 97.58%/90.1%)_第1张图片

你可能感兴趣的:(深度学习,神经网络,深度学习,机器学习)