【深度学习 走进tensorflow2.0】训练简单的卷积神经网络(CNN)对CIFAR图像进行分类

本教程使用Keras Sequential API,因此创建和训练模型仅需几行代码。

基本目录如下:

1、导入tensorflow
2、下载cifar图片数据集
3、归一化
4、创建序惯模型
5、 编译和训练模型
6、保存模型
7、评估模型

CIFAR10数据集包含10类60,000张彩色图像,每类6,000张图像。数据集分为50,000个训练图像和10,000个测试图像。这些类是互斥的,并且它们之间没有重叠。

可用linux 下载数据集

 wget https://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz

CNN对CIFAR图像进行分类demo:

# -*- coding: utf-8 -*-


from __future__ import absolute_import, division, print_function, unicode_literals

import tensorflow as tf

from tensorflow.keras import datasets, layers, models


(train_images, train_labels), (test_images, test_labels) = datasets.cifar10.load_data()

# Normalize pixel values to be between 0 and 1
train_images, test_images = train_images / 255.0, test_images / 255.0


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'))

model.add(layers.Flatten())
model.add(layers.Dense(64, activation='relu'))
model.add(layers.Dense(10, activation='softmax'))



model.summary()

# 编译和训练模型
model.compile(optimizer='adam',loss='sparse_categorical_crossentropy',metrics=['accuracy'])

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


# 将整个模型保存为HDF5文件
model.save('./model/cifar_my_model.h5')


loss, acc = model.evaluate(test_images,  test_labels, verbose=2)
print("Restored model, accuracy: {:5.2f}%".format(100*acc)

Model: "sequential"
_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
conv2d (Conv2D)              (None, 30, 30, 32)        896       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 15, 15, 32)        0         
_________________________________________________________________
conv2d_1 (Conv2D)            (None, 13, 13, 64)        18496     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 6, 6, 64)          0         
_________________________________________________________________
conv2d_2 (Conv2D)            (None, 4, 4, 64)          36928     
_________________________________________________________________
flatten (Flatten)            (None, 1024)              0         
_________________________________________________________________
dense (Dense)                (None, 64)                65600     
_________________________________________________________________
dense_1 (Dense)              (None, 10)                650       
=================================================================
Total params: 122,570
Trainable params: 122,570
Non-trainable params: 0
_________________________________________________________________
Train on 50000 samples, validate on 10000 samples
Epoch 1/100
50000/50000 [==============================] - 9s 183us/sample - loss: 1.5363 - accuracy: 0.4397 - val_loss: 1.3163 - val_accuracy: 0.5313
Epoch 2/100
50000/50000 [==============================] - 8s 166us/sample - loss: 1.1822 - accuracy: 0.5801 - val_loss: 1.1045 - val_accuracy: 0.6034
Epoch 3/100
50000/50000 [==============================] - 8s 167us/sample - loss: 1.0237 - accuracy: 0.6398 - val_loss: 1.0022 - val_accuracy: 0.6437
Epoch 4/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.9163 - accuracy: 0.6790 - val_loss: 0.9329 - val_accuracy: 0.6702
Epoch 5/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.8403 - accuracy: 0.7053 - val_loss: 0.9708 - val_accuracy: 0.6578
Epoch 6/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.7841 - accuracy: 0.7244 - val_loss: 0.8902 - val_accuracy: 0.6943
Epoch 7/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.7389 - accuracy: 0.7417 - val_loss: 0.8770 - val_accuracy: 0.6977
Epoch 8/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.6919 - accuracy: 0.7557 - val_loss: 0.8673 - val_accuracy: 0.7034
Epoch 9/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.6565 - accuracy: 0.7699 - val_loss: 0.9305 - val_accuracy: 0.6932
Epoch 10/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.6159 - accuracy: 0.7848 - val_loss: 0.8748 - val_accuracy: 0.7045
Epoch 11/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.5761 - accuracy: 0.7986 - val_loss: 0.9147 - val_accuracy: 0.6985
Epoch 12/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.5486 - accuracy: 0.8065 - val_loss: 0.9423 - val_accuracy: 0.7031
Epoch 13/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.5132 - accuracy: 0.8182 - val_loss: 0.9583 - val_accuracy: 0.6966
Epoch 14/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.4833 - accuracy: 0.8277 - val_loss: 0.9543 - val_accuracy: 0.7046
Epoch 15/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.4555 - accuracy: 0.8402 - val_loss: 0.9745 - val_accuracy: 0.7111
Epoch 16/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.4285 - accuracy: 0.8497 - val_loss: 1.0080 - val_accuracy: 0.6986
Epoch 17/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.3973 - accuracy: 0.8585 - val_loss: 1.1170 - val_accuracy: 0.6934
Epoch 18/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.3792 - accuracy: 0.8655 - val_loss: 1.0824 - val_accuracy: 0.7009
Epoch 19/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.3547 - accuracy: 0.8729 - val_loss: 1.1968 - val_accuracy: 0.6924
Epoch 20/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.3309 - accuracy: 0.8826 - val_loss: 1.2272 - val_accuracy: 0.6973
Epoch 21/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.3125 - accuracy: 0.8890 - val_loss: 1.2925 - val_accuracy: 0.6774
Epoch 22/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.2938 - accuracy: 0.8955 - val_loss: 1.2561 - val_accuracy: 0.6993
Epoch 23/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.2792 - accuracy: 0.8996 - val_loss: 1.3547 - val_accuracy: 0.6916
Epoch 24/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.2599 - accuracy: 0.9071 - val_loss: 1.3564 - val_accuracy: 0.7034
Epoch 25/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.2488 - accuracy: 0.9115 - val_loss: 1.4790 - val_accuracy: 0.6861
Epoch 26/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.2438 - accuracy: 0.9127 - val_loss: 1.4812 - val_accuracy: 0.6912
Epoch 27/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.2200 - accuracy: 0.9196 - val_loss: 1.6117 - val_accuracy: 0.6867
Epoch 28/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.2226 - accuracy: 0.9196 - val_loss: 1.6241 - val_accuracy: 0.6870
Epoch 29/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.2149 - accuracy: 0.9222 - val_loss: 1.7127 - val_accuracy: 0.6783
Epoch 30/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1984 - accuracy: 0.9291 - val_loss: 1.7735 - val_accuracy: 0.6890
Epoch 31/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1914 - accuracy: 0.9314 - val_loss: 1.7460 - val_accuracy: 0.6826
Epoch 32/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.1858 - accuracy: 0.9334 - val_loss: 1.7483 - val_accuracy: 0.6863
Epoch 33/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1736 - accuracy: 0.9375 - val_loss: 1.8251 - val_accuracy: 0.6768
Epoch 34/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1677 - accuracy: 0.9403 - val_loss: 1.9756 - val_accuracy: 0.6827
Epoch 35/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1733 - accuracy: 0.9377 - val_loss: 1.9917 - val_accuracy: 0.6851
Epoch 36/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1598 - accuracy: 0.9429 - val_loss: 2.0564 - val_accuracy: 0.6844
Epoch 37/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1545 - accuracy: 0.9441 - val_loss: 2.1996 - val_accuracy: 0.6598
Epoch 38/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.1554 - accuracy: 0.9446 - val_loss: 2.2076 - val_accuracy: 0.6747
Epoch 39/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1450 - accuracy: 0.9486 - val_loss: 2.1895 - val_accuracy: 0.6907
Epoch 40/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1481 - accuracy: 0.9470 - val_loss: 2.1972 - val_accuracy: 0.6789
Epoch 41/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1433 - accuracy: 0.9496 - val_loss: 2.2059 - val_accuracy: 0.6843
Epoch 42/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1358 - accuracy: 0.9524 - val_loss: 2.3514 - val_accuracy: 0.6700
Epoch 43/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1410 - accuracy: 0.9520 - val_loss: 2.3809 - val_accuracy: 0.6797
Epoch 44/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1238 - accuracy: 0.9552 - val_loss: 2.5317 - val_accuracy: 0.6756
Epoch 45/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1321 - accuracy: 0.9543 - val_loss: 2.5433 - val_accuracy: 0.6741
Epoch 46/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1223 - accuracy: 0.9567 - val_loss: 2.5977 - val_accuracy: 0.6726
Epoch 47/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1317 - accuracy: 0.9551 - val_loss: 2.6235 - val_accuracy: 0.6735
Epoch 48/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1229 - accuracy: 0.9574 - val_loss: 2.7012 - val_accuracy: 0.6660
Epoch 49/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1208 - accuracy: 0.9592 - val_loss: 2.5341 - val_accuracy: 0.6807
Epoch 50/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1188 - accuracy: 0.9597 - val_loss: 2.6736 - val_accuracy: 0.6771
Epoch 51/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1319 - accuracy: 0.9552 - val_loss: 2.5732 - val_accuracy: 0.6798
Epoch 52/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1072 - accuracy: 0.9625 - val_loss: 2.6446 - val_accuracy: 0.6784
Epoch 53/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1110 - accuracy: 0.9613 - val_loss: 2.7991 - val_accuracy: 0.6806
Epoch 54/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1162 - accuracy: 0.9605 - val_loss: 2.7622 - val_accuracy: 0.6753
Epoch 55/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1131 - accuracy: 0.9621 - val_loss: 2.7666 - val_accuracy: 0.6768
Epoch 56/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1069 - accuracy: 0.9634 - val_loss: 2.7999 - val_accuracy: 0.6798
Epoch 57/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1147 - accuracy: 0.9607 - val_loss: 2.9075 - val_accuracy: 0.6690
Epoch 58/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1121 - accuracy: 0.9622 - val_loss: 2.7893 - val_accuracy: 0.6788
Epoch 59/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.0972 - accuracy: 0.9658 - val_loss: 3.0507 - val_accuracy: 0.6758
Epoch 60/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1243 - accuracy: 0.9587 - val_loss: 2.9327 - val_accuracy: 0.6770
Epoch 61/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.0995 - accuracy: 0.9657 - val_loss: 2.8374 - val_accuracy: 0.6777
Epoch 62/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.1027 - accuracy: 0.9660 - val_loss: 2.9794 - val_accuracy: 0.6761
Epoch 63/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.1026 - accuracy: 0.9655 - val_loss: 2.9922 - val_accuracy: 0.6729
Epoch 64/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1091 - accuracy: 0.9650 - val_loss: 3.0998 - val_accuracy: 0.6673
Epoch 65/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0968 - accuracy: 0.9668 - val_loss: 3.0342 - val_accuracy: 0.6709
Epoch 66/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1017 - accuracy: 0.9659 - val_loss: 3.1683 - val_accuracy: 0.6747
Epoch 67/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1076 - accuracy: 0.9649 - val_loss: 3.0948 - val_accuracy: 0.6778
Epoch 68/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.1000 - accuracy: 0.9672 - val_loss: 3.0447 - val_accuracy: 0.6715
Epoch 69/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0953 - accuracy: 0.9680 - val_loss: 3.1502 - val_accuracy: 0.6790
Epoch 70/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0991 - accuracy: 0.9677 - val_loss: 3.1486 - val_accuracy: 0.6759
Epoch 71/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0960 - accuracy: 0.9687 - val_loss: 3.2751 - val_accuracy: 0.6765
Epoch 72/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.1008 - accuracy: 0.9663 - val_loss: 3.1812 - val_accuracy: 0.6751
Epoch 73/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0918 - accuracy: 0.9699 - val_loss: 3.3328 - val_accuracy: 0.6750
Epoch 74/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0928 - accuracy: 0.9693 - val_loss: 3.2500 - val_accuracy: 0.6755
Epoch 75/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.1040 - accuracy: 0.9661 - val_loss: 3.2569 - val_accuracy: 0.6725
Epoch 76/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0868 - accuracy: 0.9719 - val_loss: 3.2508 - val_accuracy: 0.6751
Epoch 77/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.0913 - accuracy: 0.9697 - val_loss: 3.4788 - val_accuracy: 0.6713
Epoch 78/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.0996 - accuracy: 0.9674 - val_loss: 3.3437 - val_accuracy: 0.6750
Epoch 79/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.0811 - accuracy: 0.9734 - val_loss: 3.3015 - val_accuracy: 0.6622
Epoch 80/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0946 - accuracy: 0.9702 - val_loss: 3.2744 - val_accuracy: 0.6773
Epoch 81/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0834 - accuracy: 0.9731 - val_loss: 3.4619 - val_accuracy: 0.6689
Epoch 82/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0933 - accuracy: 0.9705 - val_loss: 3.4450 - val_accuracy: 0.6720
Epoch 83/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0857 - accuracy: 0.9730 - val_loss: 3.6283 - val_accuracy: 0.6666
Epoch 84/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0989 - accuracy: 0.9683 - val_loss: 3.5437 - val_accuracy: 0.6708
Epoch 85/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.0948 - accuracy: 0.9707 - val_loss: 3.4481 - val_accuracy: 0.6689
Epoch 86/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0808 - accuracy: 0.9737 - val_loss: 3.4770 - val_accuracy: 0.6754
Epoch 87/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0881 - accuracy: 0.9707 - val_loss: 3.4255 - val_accuracy: 0.6713
Epoch 88/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0901 - accuracy: 0.9709 - val_loss: 3.5193 - val_accuracy: 0.6686
Epoch 89/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0889 - accuracy: 0.9715 - val_loss: 3.5919 - val_accuracy: 0.6695
Epoch 90/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0868 - accuracy: 0.9730 - val_loss: 3.6907 - val_accuracy: 0.6657
Epoch 91/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0911 - accuracy: 0.9717 - val_loss: 3.5573 - val_accuracy: 0.6723
Epoch 92/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0819 - accuracy: 0.9731 - val_loss: 3.6163 - val_accuracy: 0.6704
Epoch 93/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0777 - accuracy: 0.9746 - val_loss: 3.5383 - val_accuracy: 0.6753
Epoch 94/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0924 - accuracy: 0.9711 - val_loss: 3.6594 - val_accuracy: 0.6741
Epoch 95/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0833 - accuracy: 0.9739 - val_loss: 3.7672 - val_accuracy: 0.6689
Epoch 96/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0762 - accuracy: 0.9757 - val_loss: 3.7906 - val_accuracy: 0.6782
Epoch 97/100
50000/50000 [==============================] - 8s 167us/sample - loss: 0.0815 - accuracy: 0.9739 - val_loss: 3.8269 - val_accuracy: 0.6720
Epoch 98/100
50000/50000 [==============================] - 8s 165us/sample - loss: 0.0850 - accuracy: 0.9734 - val_loss: 3.6840 - val_accuracy: 0.6728
Epoch 99/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0821 - accuracy: 0.9747 - val_loss: 3.9175 - val_accuracy: 0.6700
Epoch 100/100
50000/50000 [==============================] - 8s 166us/sample - loss: 0.0816 - accuracy: 0.9744 - val_loss: 3.6780 - val_accuracy: 0.6722
10000/1 - 1s - loss: 4.5844 - accuracy: 0.6722
Restored model, accuracy: 67.22%

已标记关键词 清除标记
©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页