Classification of Fashion MNIST dataset based on Pytorch-CNN sliding product neural network

Frame: Pytorch

Data set: Fashion MNIST (advanced handwritten digit recognition)

Network: Convolutional Neural Network (CNN), also known as sliding neural network

Purpose: Familiar with the Pytorch framework and build a simple sliding product neural network

 

structure:

Image for post

 

Code:

Quote

import torch 
import torch.nn as nn
import torchvision.datasets as dsets
from skimage import transform
import torchvision.transforms as transforms
from torch.autograd import Variable
import pandas as pd;
import numpy as np;
from torch.utils.data import Dataset, DataLoader
from vis_utils import *
import random;
import math;

 

Hyperparameter 

num_epochs = 5;
batch_size = 100;
learning_rate = 0.001;

这块黑色的我不知道怎么去掉~哎呀呀,小黑屏幕,哎呀呀,你也去不掉呀,你是咋出来的呢,我也不知道呀!小白菜呀,地里黄呀,小黑屏呀,去不掉呀。嘿嘿!嘿嘿!嘿嘿嘿!

 

Define the data set

The built-in data set in Pytorch does not include Fashion MNIST, so you need to customize the Fashion MNIST data set. The custom data set needs to inherit the parent class Dataset and override the two private member functions in the parent class members.

Where __len__ should return the size of the data set , and __getitem__ should write a function that supports data indexing.

def getitem(self, index):
def len(self):
class FashionMNISTDataset(Dataset):
    '''Fashion MNIST Dataset'''

    def __init__(self, csv_file, transform=None): #初始化函数,主要用于数据的加载,pandas读取数据为dataframe,转成numpy数组来进行索引 。
        """
        Args:
            csv_file (string): csv file地址
            transform (callable): Optional transform to apply to sample
        """

        '''iloc:分割矩阵,data:表格数据。X:样本,Y:标签且在0列。
           取样本:行全取、列从第1到结束。取标签:行全取,列取0。'''
        data = pd.read_csv(csv_file); #从csv文件加载数据,使用Pandas读入
        self.X = np.array(data.iloc[:, 1:]).reshape(-1, 1, 28, 28).astype(float);
        self.Y = np.array(data.iloc[:, 0]); #数据转换为numpy数组从而索引

        del data;  # 结束data对数据的引用,节省空间
        self.transform = transform; #transform默认值为None

    def __len__(self): #样本长度,即数据集总数,dataloader需使用。
        return len(self.X);

    def __getitem__(self, idx):  #返回单张图片。输入索引,返回该样本及其标签。如果定义transform,转换样本并返回。
        item = self.X[idx];
        label = self.Y[idx];

        if self.transform:
            item = self.transform(item);

        return (item, label); #样本和标签

 

Divide the sample set and test set

train_dataset = FashionMNISTDataset(csv_file='fashionmnist/fashion-mnist_train.csv');
test_dataset = FashionMNISTDataset(csv_file='fashionmnist/fashion-mnist_test.csv')

 

Download Data

train_loader = torch.utils.data.DataLoader(dataset=train_dataset,
                                           batch_size=batch_size,
                                           shuffle=True);
test_loader = torch.utils.data.DataLoader(dataset=test_dataset,
                                          batch_size=batch_size,
                                          shuffle=True);

Building a sliding product network

The network inherits nn.Module, writes __init__ and forward functions. Use the nn module to define the network layer: (convolution-normalization-activation-pooling)-(convolution-normalization-activation-pooling)-fully connected layer (10 category output)

class CNN(nn.Module):
    def __init__(self):
        super(CNN, self).__init__()
        self.layer1 = nn.Sequential(
            nn.Conv2d(1, 16, kernel_size=5, padding=2),
            nn.BatchNorm2d(16),
            nn.ReLU(),
            nn.MaxPool2d(2))
        self.layer2 = nn.Sequential(
            nn.Conv2d(16, 32, kernel_size=5, padding=2),
            nn.BatchNorm2d(32),
            nn.ReLU(),
            nn.MaxPool2d(2))
        self.fc = nn.Linear(7*7*32, 10)
        
    def forward(self, x):
        out = self.layer1(x)
        out = self.layer2(out)
        out = out.view(out.size(0), -1)
        out = self.fc(out)
        return out

 

Network instantiation

cnn = CNN();

Define loss function

criterion = nn.CrossEntropyLoss();

Choose an optimizer

  Multi-classification uses Softmax regression to turn the results obtained by the forward propagation of the neural network into a probability distribution, so cross-entropy loss is used. Pytorch to  nn.LogSoftmax() and  nn.NLLLoss()integrated into NN.CrossEntropyLoss () is the cross-entropy.

optimizer = torch.optim.Adam(cnn.parameters(), lr=learning_rate);

Model training

Note: loss.data[0] is the version code of pytorch0.3.1, the version above 0.5 will report an error and should be replaced with loss.data.item(). The following code has been replaced.

One of the most important concepts in the figure below is Variable.

losses = [];  #记录损失函数
for epoch in range(num_epochs):
    for i, (images, labels) in enumerate(train_loader):
        images = Variable(images.float())
        labels = Variable(labels)
        
        # Forward + Backward + Optimize
        
        #清零
        optimizer.zero_grad()
      
        #前向
        outputs = cnn(images)

        #损失函数
        loss = criterion(outputs, labels)

        #后向传播
        loss.backward()

        #优化器迭代
        optimizer.step()
        
        #损失函数矩阵
        #losses.append(loss.data.item());
        losses.append(loss.data.item());

        #打印
        if (i+1) % 100 == 0:
            print ('Epoch : %d/%d, Iter : %d/%d,  Loss: %.4f' 
                   %(epoch+1, num_epochs, i+1, len(train_dataset)//batch_size, loss.data.item()))

 

Guess you like

Origin blog.csdn.net/weixin_42133481/article/details/114400394