This article is participating in Python Theme Month. See the link to the event for more details

How can a simple convolutional neural network (CNN) be trained to classify CIFAR images

# This tutorial demonstrates how to train a simple convolutional neural network (CNN) to classify CIFAR images. Since this tutorial uses the Keras sequential API, it only takes a few lines of code to create and train our model. import tensorflow as tf from tensorflow.keras import datasets, layers, Models Import Matplotlib. pyplot as PLT # matplotlib is a 2D plot library for Python # download and prepare the CIFAR10 dataset # CIFAR10 dataset contains 60000 color images from 10 classes, There are 6000 images in each class. # The data set is divided into 50,000 training images and 10,000 test images. These classes are mutually exclusive; there is no overlap between them. (train_images, train_labels), (test_images, Test_labels) = datasets.cifar10.load_data() # Normalize pixel values to be between 0 and 1 Test_images = train_images / 255.0, test_images / 255.0Copy the code

Run: need to download the data set, very slow, copy the link below to thunderbolt download, very fast

http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
Copy the code

Place the downloaded file in ~/.keras/datasets/ and rename it to cifar-10-batches-py.tar.gz

To verify that the data set looks correct, let's draw the first 25 images in the training set and display the class name below each image. class_names = ['airplane', 'automobile', 'bird', 'cat', 'deer', 'dog', 'frog', 'horse', 'ship', Figure (figure size=(10,10)) for I in range(25): Subplot (5,5, I +1) plt.xticks([]) plt.yticks([]) plt.grid(False) plt.imshow(train_images[I], Cmap =plt.cm.binary) # The CIFAR labels happen to be arrays, # which is why you need the extra index plt.xlabel(class_names[train_labels[I][0]]) plt.show()Copy the code

Running result:

The following 6 lines of code define the contional base using a common pattern: the stack of layers Conv2D and MaxPooling2D as input, # CNN takes shape tensors (image height, image width, color channel) and ignores the batch size. If you are not familiar with these dimensions, the color channels refer to (R, G, B). # In this example, you will configure our CNN to handle shape (32, 32, 3) input, # this is the CIFAR image format. You can do this by passing the input_shape parameter to the first layer. model = models.Sequential() model.add(layers.Conv2D(32, (3, 3), activation='relu', input_shape=(32, 32, 3))) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) model.add(layers.MaxPooling2D((2, 2))) model.add(layers.Conv2D(64, (3, 3), activation='relu')) # Let's display the architecture of our model so far. model.summary()Copy the code

Running result:

Above, you can see that the output of each Conv2D and MaxPooling2D layers is a THREE-DIMENSIONAL shape tensor (height, width, channel). As the network deepens, the width and height dimensions tend to shrink. The number of output channels for each Conv2D layer is controlled by the first parameter (for example, 32 or 64). In general, you can add more output channels to each Conv2D layer (by calculation) as the width and height decrease.

Add a dense layer at the top

To complete our model, you will feed the last output tensor from the convolution basis (shape (4,4,64)) into one or more dense layers to perform classification. Dense layers take vectors as inputs (one dimensional), while the current output is a three dimensional tensor. First, flatten (or expand) the THREE-DIMENSIONAL output to 1D, then add one or more dense layers at the top. CIFAR has 10 output classes, so the last dense layer is used, which has 10 outputs and one SoftMax activation.

Add (layer.flatten ()) model.add(layer.dense (64, activation='relu')) model.add(layer.dense (10)) # model.summary()Copy the code



As you can see, our (4,4,64) output is flattened into a shape vector (1024) before passing through two dense layers.

Compile and train the model

model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

history = model.fit(train_images, train_labels, epochs=10, 
                    validation_data=(test_images, test_labels))

Copy the code

Evaluate the model

plt.plot(history.history['accuracy'], label='accuracy')
plt.plot(history.history['val_accuracy'], label = 'val_accuracy')
plt.xlabel('Epoch')
plt.ylabel('Accuracy')
plt.ylim([0.5, 1])
plt.legend(loc='lower right')

test_loss, test_acc = model.evaluate(test_images,  test_labels, verbose=2)
print(test_acc)  

Copy the code

Results:





Our simple CNN has achieved over 70% test accuracy. Not bad for a few lines of code!