博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
TensorFlow(keras)入门课程--04 卷积神经网络
阅读量:1887 次
发布时间:2019-04-26

本文共 5354 字,大约阅读时间需要 17 分钟。

目录

  • 1 简介
  • 2 使用卷积提高计算机视觉准确度
  • 3 可视化卷积核池

1 简介

在本节中,我们将学习如何使用卷积神经网络来改进图像分类模型。

2 使用卷积提高计算机视觉准确度

在之前的实验中 ,使用了包含了三个层的深度神经网络进行时尚图像识别-输入层(以输入数据的形状)、输出层(以及所需输出的形状)和一个隐藏层,

为方便起见,先运行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

3 可视化卷积和池化

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/

你可能感兴趣的文章
【经验分享】RT-Thread UART设备驱动框架初体验(中断方式接收带\r\n的数据)
查看>>
RT-Thread 编程风格指南
查看>>
95后高校电子教师,软硬兼修有趣有料!
查看>>
Cache 的基本概念与工作原理
查看>>
Android程序员必备!面试一路绿灯Offer拿到手软,Android面试题及解析
查看>>
Android开发知识体系!腾讯+字节+阿里面经真题汇总,成功入职阿里
查看>>
typescript学习(进阶)
查看>>
三天敲一个前后端分离的员工管理系统
查看>>
EL表达式、JSTL标签库、文件上传和下载
查看>>
Cookie、Session
查看>>
表单重复提交
查看>>
Filter
查看>>
微服务架构实施原理详解
查看>>
必须了解的mysql三大日志-binlog、redo log和undo log
查看>>
Uniform Grid Quadtree kd树 Bounding Volume Hierarchy R树 搜索
查看>>
局部敏感哈希Locality Sensitive Hashing归总
查看>>
图像检索中为什么仍用BOW和LSH
查看>>
图˙谱˙马尔可夫过程˙聚类结构----by林达华
查看>>
深度学习读书笔记之AE(自动编码AutoEncoder)
查看>>
深度学习读书笔记之RBM
查看>>