本文共 5354 字,大约阅读时间需要 17 分钟。
在本节中,我们将学习如何使用卷积神经网络来改进图像分类模型。
在之前的实验中 ,使用了包含了三个层的深度神经网络进行时尚图像识别-输入层(以输入数据的形状)、输出层(以及所需输出的形状)和一个隐藏层,
为方便起见,先运行DNN的代码并打印出测试精度。
import tensorflow as tffashion_mnist = tf.keras.datasets.fashion_mnist(training_images,training_labels),(test_images,test_labels) = fashion_mnist.load_data()trainging_images = training_images / 255.0test_images = test_images / 255.0model = tf.keras.models.Sequential([ tf.keras.layers.Flatten(), tf.keras.layers.Dense(128,activation="relu"), tf.keras.layers.Dense(10,activation="softmax")])model.compile(optimizer="adam",loss='sparse_categorical_crossentropy', metrics=['accuracy'])model.fit(trainging_images,training_labels,epochs=5)test_loss, test_accuracy = model.evaluate(test_images, test_labels)print ('Test loss: {}, Test accuracy: {}'.format(test_loss, test_accuracy*100))
Epoch 1/560000/60000 [==============================] - 4s 72us/sample - loss: 0.4982 - acc: 0.8257Epoch 2/560000/60000 [==============================] - 4s 74us/sample - loss: 0.3746 - acc: 0.8649Epoch 3/560000/60000 [==============================] - 5s 77us/sample - loss: 0.3388 - acc: 0.8765Epoch 4/560000/60000 [==============================] - 4s 74us/sample - loss: 0.3133 - acc: 0.8858Epoch 5/560000/60000 [==============================] - 4s 73us/sample - loss: 0.2991 - acc: 0.890510000/10000 [==============================] - 0s 28us/sample - loss: 0.3888 - acc: 0.8607Test loss: 0.38882760289907453, Test accuracy: 86.0700011253357
DNN测试集的准确率为86%。
import tensorflow as tfprint(tf.__version__)fashion_mnist = tf.keras.datasets.fashion_mnist(trainging_images,training_labels),(test_images,test_labels) = fashion_mnist.load_data()training_images = trainging_images.reshape(60000,28,28,1)trainging_images = trainging_images / 255.0test_images = test_images.reshape(10000,28,28,1)test_images = test_images / 255.0model = tf.keras.models.Sequential([ tf.keras.layers.Conv2D(64,(3,3),activation="relu",input_shape=(28,28,1)), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Conv2D(64,(3,3),activation="relu"), tf.keras.layers.MaxPooling2D(2,2), tf.keras.layers.Flatten(), tf.keras.layers.Dense(128,activation="relu"), tf.keras.layers.Dense(10,activation="softmax")])model.compile(optimizer='adam', loss='sparse_categorical_crossentropy', metrics=['accuracy'])model.summary()model.fit(training_images,training_labels,epochs=5)test_loss, test_accuracy = model.evaluate(test_images, test_labels)print ('Test loss: {}, Test accuracy: {}'.format(test_loss, test_accuracy*100))
1.13.1_________________________________________________________________Layer (type) Output Shape Param # =================================================================conv2d_4 (Conv2D) (None, 26, 26, 64) 640 _________________________________________________________________max_pooling2d_4 (MaxPooling2 (None, 13, 13, 64) 0 _________________________________________________________________conv2d_5 (Conv2D) (None, 11, 11, 64) 36928 _________________________________________________________________max_pooling2d_5 (MaxPooling2 (None, 5, 5, 64) 0 _________________________________________________________________flatten_2 (Flatten) (None, 1600) 0 _________________________________________________________________dense_4 (Dense) (None, 128) 204928 _________________________________________________________________dense_5 (Dense) (None, 10) 1290 =================================================================Total params: 243,786Trainable params: 243,786Non-trainable params: 0_________________________________________________________________Epoch 1/560000/60000 [==============================] - 75s 1ms/sample - loss: 13.4033 - acc: 0.1681Epoch 2/560000/60000 [==============================] - 74s 1ms/sample - loss: 14.5063 - acc: 0.1000Epoch 3/560000/60000 [==============================] - 72s 1ms/sample - loss: 14.5063 - acc: 0.1000Epoch 4/560000/60000 [==============================] - 73s 1ms/sample - loss: 14.5063 - acc: 0.1000Epoch 5/560000/60000 [==============================] - 73s 1ms/sample - loss: 14.5063 - acc: 0.100010000/10000 [==============================] - 4s 364us/sample - loss: 8.4485 - acc: 0.1000Test loss: 8.44854741897583, Test accuracy: 10.000000149011612
import matplotlib.pyplot as pltf, axarr = plt.subplots(3,4)FIRST_IMAGE=0SECOND_IMAGE=23THIRD_IMAGE=28CONVOLUTION_NUMBER = 6from tensorflow.keras import modelslayer_outputs = [layer.output for layer in model.layers]activation_model = tf.keras.models.Model(inputs = model.input, outputs = layer_outputs)for x in range(0,4): f1 = activation_model.predict(test_images[FIRST_IMAGE].reshape(1, 28, 28, 1))[x] axarr[0,x].imshow(f1[0, : , :, CONVOLUTION_NUMBER], cmap='inferno') axarr[0,x].grid(False) f2 = activation_model.predict(test_images[SECOND_IMAGE].reshape(1, 28, 28, 1))[x] axarr[1,x].imshow(f2[0, : , :, CONVOLUTION_NUMBER], cmap='inferno') axarr[1,x].grid(False) f3 = activation_model.predict(test_images[THIRD_IMAGE].reshape(1, 28, 28, 1))[x] axarr[2,x].imshow(f3[0, : , :, CONVOLUTION_NUMBER], cmap='inferno') axarr[2,x].grid(False)
转载地址:http://yczdf.baihongyu.com/