当前位置:网站首页>Classification of cifar100 data set based on convolutional neural network

Classification of cifar100 data set based on convolutional neural network

2022-04-23 17:53:00 Stephen_ Tao

CIFAR100 Data set introduction

CIFAR100 The dataset has 100 Categories , Each category contains 600 A picture , And each category has 500 Training pictures and 100 Test pictures .CIFAR100 Data sets 100 The three categories are divided into 20 A superclass . Each image has a " fine " label ( The class it belongs to ) And a " Rough " label ( The superclass it belongs to ).
 Insert picture description here

Code implementation

Reading data sets

#  Import dataset 
from tensorflow.python.keras.datasets import cifar100

class CNNMnist(object):
	    def __init__(self):
	    #  Reading data sets 
        (self.train,self.train_label),(self.test,self.test_label) = cifar100.load_data()
        #  Normalize the data set 
        self.train = self.train.reshape(-1,32,32,3) / 255.0
        self.test = self.test.reshape(-1,32,32,3) / 255.0

Build a network model

  1. Convolution layer :32 individual 5*5 Convolution kernel , The step size is set to 1, The activation function uses relu
  2. Pooling layer : The pool size is 2, The step size is set to 2
  3. Convolution layer :64 individual 5*5 Convolution kernel , The step size is set to 1, The activation function uses relu
  4. Pooling layer : The pool size is 2, The step size is set to 2
  5. Fully connected layer : Set up 1024 Neurons , The activation function is relu
  6. Fully connected layer : Set up 100 Neurons , The activation function is softmax
#  Import necessary packages 
from tensorflow.python.keras import layers,losses,optimizers
from tensorflow.python.keras.models import Sequential
import tensorflow as tf

class CNNMnist(object):
    model = Sequential([
        layers.Conv2D(32,kernel_size=5,strides=1,padding='same',data_format='channels_last',activation=tf.nn.relu),
        layers.MaxPool2D(pool_size=2,strides=2,padding='same'),
        layers.Conv2D(64,kernel_size=5,strides=1,padding='same',data_format='channels_last',activation=tf.nn.relu),
        layers.MaxPool2D(pool_size=2,strides=2,padding='same'),
        layers.Flatten(),
        layers.Dense(1024,activation=tf.nn.relu),
        layers.Dense(100,activation=tf.nn.softmax)
    ])

Network model compilation

class CNNMnist(object):
    def compile(self):
        CNNMnist.model.compile(optimizer=optimizers.adam_v2.Adam(),
                               loss=losses.sparse_categorical_crossentropy,
                               metrics=['accuracy'])
        return None

model training

class CNNMnist(object):
    def fit(self):
        CNNMnist.model.fit(self.train,self.train_label,epochs=1,batch_size=32)
        return None

Model to evaluate

class CNNMnist(object):
    def evaluate(self):
        train_loss,train_acc = CNNMnist.model.evaluate(self.train,self.train_label)
        test_loss,test_acc = CNNMnist.model.evaluate(self.test,self.test_label)
        print("train_loss:",train_loss)
        print("train_acc:",train_acc)
        print("test_loss:",test_loss)
        print("test_acc:",test_acc)
        return None

The model runs

if __name__ == '__main__':
    cnn = CNNMnist()
    cnn.compile()
    cnn.fit()
    cnn.evaluate()

Model running results

1563/1563 [==============================] - 199s 126ms/step - loss: 3.5098 - accuracy: 0.1748
1563/1563 [==============================] - 56s 35ms/step - loss: 2.8101 - accuracy: 0.3094
313/313 [==============================] - 11s 33ms/step - loss: 2.9732 - accuracy: 0.2672
train_loss: 2.81014084815979
train_acc: 0.3094399869441986
test_loss: 2.9731905460357666
test_acc: 0.2671999931335449

You can see from the results , The accuracy obtained is still relatively low . Because the loss of convolutional neural networks does not decline as fast as fully connected neural networks , And the above code only iterates once . However, compared with fully connected neural networks, convolutional neural networks , Reduced training parameters , It can reduce the requirements for computing power and performance of equipment , Therefore, in pattern recognition 、 Object detection has a wide range of applications .

summary

This paper focuses on how to build a convolutional neural network model , No necessary improvements have been made to the model .
notes : The code resource of this article comes from the dark horse programmer course

版权声明
本文为[Stephen_ Tao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/04/202204230548468570.html