DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional或Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测

本文主要是介绍DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional或Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional或Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测

目录

利用卷积神经网络算法(2→2,基于Keras的API-Functional)利用MNIST(手写数字图片识别)数据集实现多分类预测

输出结果

设计思路

核心代码

利用卷积神经网络算法(2→2,基于Keras的API-Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测

输出结果

设计思路

核心代码


利用卷积神经网络算法(2→2,基于Keras的API-Functional)利用MNIST(手写数字图片识别)数据集实现多分类预测

输出结果

下边两张图对应查看,可知,数字0有965个是被准确识别到!

1.10.0
Size of:
- Training-set:		55000
- Validation-set:	5000
- Test-set:		10000
Epoch 1/1128/55000 [..............................] - ETA: 14:24 - loss: 2.3439 - acc: 0.0938256/55000 [..............................] - ETA: 14:05 - loss: 2.2695 - acc: 0.1016384/55000 [..............................] - ETA: 13:20 - loss: 2.2176 - acc: 0.1302512/55000 [..............................] - ETA: 13:30 - loss: 2.1608 - acc: 0.2109640/55000 [..............................] - ETA: 13:29 - loss: 2.0849 - acc: 0.2500768/55000 [..............................] - ETA: 13:23 - loss: 2.0309 - acc: 0.2734896/55000 [..............................] - ETA: 13:30 - loss: 1.9793 - acc: 0.29461024/55000 [..............................] - ETA: 13:23 - loss: 1.9105 - acc: 0.33691152/55000 [..............................] - ETA: 13:22 - loss: 1.8257 - acc: 0.3776
……
53760/55000 [============================>.] - ETA: 18s - loss: 0.2106 - acc: 0.9329
53888/55000 [============================>.] - ETA: 16s - loss: 0.2103 - acc: 0.9330
54016/55000 [============================>.] - ETA: 14s - loss: 0.2100 - acc: 0.9331
54144/55000 [============================>.] - ETA: 13s - loss: 0.2096 - acc: 0.9333
54272/55000 [============================>.] - ETA: 11s - loss: 0.2092 - acc: 0.9334
54400/55000 [============================>.] - ETA: 9s - loss: 0.2089 - acc: 0.9335 
54528/55000 [============================>.] - ETA: 7s - loss: 0.2086 - acc: 0.9336
54656/55000 [============================>.] - ETA: 5s - loss: 0.2082 - acc: 0.9337
54784/55000 [============================>.] - ETA: 3s - loss: 0.2083 - acc: 0.9337
54912/55000 [============================>.] - ETA: 1s - loss: 0.2082 - acc: 0.9337
55000/55000 [==============================] - 837s 15ms/step - loss: 0.2080 - acc: 0.933832/10000 [..............................] - ETA: 21s160/10000 [..............................] - ETA: 8s 288/10000 [..............................] - ETA: 6s448/10000 [>.............................] - ETA: 5s576/10000 [>.............................] - ETA: 5s736/10000 [=>............................] - ETA: 4s864/10000 [=>............................] - ETA: 4s1024/10000 [==>...........................] - ETA: 4s1152/10000 [==>...........................] - ETA: 4s1312/10000 [==>...........................] - ETA: 4s1440/10000 [===>..........................] - ETA: 4s1600/10000 [===>..........................] - ETA: 3s1728/10000 [====>.........................] - ETA: 3s
……3008/10000 [========>.....................] - ETA: 3s3168/10000 [========>.....................] - ETA: 3s3296/10000 [========>.....................] - ETA: 3s3456/10000 [=========>....................] - ETA: 2s
……5248/10000 [==============>...............] - ETA: 2s5376/10000 [===============>..............] - ETA: 2s5536/10000 [===============>..............] - ETA: 2s5664/10000 [===============>..............] - ETA: 1s5792/10000 [================>.............] - ETA: 1s
……7360/10000 [=====================>........] - ETA: 1s7488/10000 [=====================>........] - ETA: 1s7648/10000 [=====================>........] - ETA: 1s7776/10000 [======================>.......] - ETA: 1s7936/10000 [======================>.......] - ETA: 0s8064/10000 [=======================>......] - ETA: 0s8224/10000 [=======================>......] - ETA: 0s
……9760/10000 [============================>.] - ETA: 0s9920/10000 [============================>.] - ETA: 0s
10000/10000 [==============================] - 4s 449us/step
loss 0.05686537345089018
acc 0.982
acc: 98.20%
[[ 965    0    4    0    0    0    4    1    2    4][   0 1128    3    0    0    0    0    1    3    0][   0    0 1028    0    0    0    0    1    3    0][   0    0   10  991    0    2    0    2    3    2][   0    0    3    0  967    0    1    1    1    9][   2    0    1    7    1  863    5    1    4    8][   2    3    0    0    3    2  946    0    2    0][   0    1   17    1    1    0    0  987    2   19][   2    0    9    2    0    1    0    1  955    4][   1    4    3    2    8    0    0    0    1  990]]_________________________________________________________________
Layer (type)                 Output Shape              Param #   
=================================================================
input_1 (InputLayer)         (None, 784)               0         
_________________________________________________________________
reshape (Reshape)            (None, 28, 28, 1)         0         
_________________________________________________________________
layer_conv1 (Conv2D)         (None, 28, 28, 16)        416       
_________________________________________________________________
max_pooling2d (MaxPooling2D) (None, 14, 14, 16)        0         
_________________________________________________________________
layer_conv2 (Conv2D)         (None, 14, 14, 36)        14436     
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 7, 7, 36)          0         
_________________________________________________________________
flatten (Flatten)            (None, 1764)              0         
_________________________________________________________________
dense (Dense)                (None, 128)               225920    
_________________________________________________________________
dense_1 (Dense)              (None, 10)                1290      
=================================================================
Total params: 242,062
Trainable params: 242,062
Non-trainable params: 0
_________________________________________________________________
(5, 5, 1, 16)
(1, 28, 28, 16)

设计思路

核心代码

后期更新……

path_model = 'Functional_model.keras'  from tensorflow.python.keras.models import load_model  
model2_1 = load_model(path_model)      model_weights_path = 'Functional_model_weights.keras'
model2_1.save_weights(model_weights_path )                  
model2_1.load_weights(model_weights_path, by_name=True ) 
model2_1.load_weights(model_weights_path)  result = model.evaluate(x=data.x_test,y=data.y_test)for name, value in zip(model.metrics_names, result):print(name, value)
print("{0}: {1:.2%}".format(model.metrics_names[1], result[1]))y_pred = model.predict(x=data.x_test) 
cls_pred = np.argmax(y_pred, axis=1)   
plot_example_errors(cls_pred)        
plot_confusion_matrix(cls_pred)     images = data.x_test[0:9]                      
cls_true = data.y_test_cls[0:9]                 
y_pred = model.predict(x=images)               
cls_pred = np.argmax(y_pred, axis=1)            
title = 'MNIST(Sequential Model): plot predicted example, resl VS predict'
plot_images(title, images=images,               cls_true=cls_true,cls_pred=cls_pred)

利用卷积神经网络算法(2→2,基于Keras的API-Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测

输出结果

1.10.0
Size of:
- Training-set:		55000
- Validation-set:	5000
- Test-set:		10000
Epoch 1/1128/55000 [..............................] - ETA: 15:39 - loss: 2.3021 - acc: 0.0703256/55000 [..............................] - ETA: 13:40 - loss: 2.2876 - acc: 0.1172384/55000 [..............................] - ETA: 14:24 - loss: 2.2780 - acc: 0.1328512/55000 [..............................] - ETA: 13:57 - loss: 2.2613 - acc: 0.1719640/55000 [..............................] - ETA: 13:57 - loss: 2.2414 - acc: 0.1828768/55000 [..............................] - ETA: 13:58 - loss: 2.2207 - acc: 0.2135896/55000 [..............................] - ETA: 14:01 - loss: 2.1926 - acc: 0.24671024/55000 [..............................] - ETA: 13:34 - loss: 2.1645 - acc: 0.27251152/55000 [..............................] - ETA: 13:38 - loss: 2.1341 - acc: 0.29691280/55000 [..............................] - ETA: 13:40 - loss: 2.0999 - acc: 0.32731408/55000 [..............................] - ETA: 13:37 - loss: 2.0555 - acc: 0.3629
……
54016/55000 [============================>.] - ETA: 15s - loss: 0.2200 - acc: 0.9350
54144/55000 [============================>.] - ETA: 13s - loss: 0.2198 - acc: 0.9350
54272/55000 [============================>.] - ETA: 11s - loss: 0.2194 - acc: 0.9351
54400/55000 [============================>.] - ETA: 9s - loss: 0.2191 - acc: 0.9352 
54528/55000 [============================>.] - ETA: 7s - loss: 0.2189 - acc: 0.9352
54656/55000 [============================>.] - ETA: 5s - loss: 0.2185 - acc: 0.9354
54784/55000 [============================>.] - ETA: 3s - loss: 0.2182 - acc: 0.9354
54912/55000 [============================>.] - ETA: 1s - loss: 0.2180 - acc: 0.9355
55000/55000 [==============================] - 863s 16ms/step - loss: 0.2177 - acc: 0.935632/10000 [..............................] - ETA: 22s160/10000 [..............................] - ETA: 8s 288/10000 [..............................] - ETA: 6s416/10000 [>.............................] - ETA: 5s544/10000 [>.............................] - ETA: 5s672/10000 [=>............................] - ETA: 5s800/10000 [=>............................] - ETA: 5s928/10000 [=>............................] - ETA: 4s1056/10000 [==>...........................] - ETA: 4s1184/10000 [==>...........................] - ETA: 4s1312/10000 [==>...........................] - ETA: 4s1440/10000 [===>..........................] - ETA: 4s
……9088/10000 [==========================>...] - ETA: 0s9216/10000 [==========================>...] - ETA: 0s9344/10000 [===========================>..] - ETA: 0s9472/10000 [===========================>..] - ETA: 0s9600/10000 [===========================>..] - ETA: 0s9728/10000 [============================>.] - ETA: 0s9856/10000 [============================>.] - ETA: 0s9984/10000 [============================>.] - ETA: 0s
10000/10000 [==============================] - 5s 489us/step
loss 0.060937872195523234
acc 0.9803
acc: 98.03%
[[ 963    0    0    1    1    0    4    1    4    6][   0 1128    0    2    0    1    2    0    2    0][   2    9 1006    1    1    0    0    3   10    0][   1    0    2  995    0    3    0    5    2    2][   0    1    0    0  977    0    0    1    0    3][   2    0    0    7    0  874    3    1    1    4][   2    3    0    0    6    1  943    0    3    0][   0    5    7    3    1    1    0  990    1   20][   4    1    3    3    2    1    7    2  944    7][   4    6    0    4    9    1    0    1    1  983]]

设计思路

后期更新……

核心代码

后期更新……

result = model.evaluate(x=data.x_test,y=data.y_test)for name, value in zip(model.metrics_names, result):print(name, value)
print("{0}: {1:.2%}".format(model.metrics_names[1], result[1]))y_pred = model.predict(x=data.x_test) 
cls_pred = np.argmax(y_pred, axis=1)   
plot_example_errors(cls_pred)        
plot_confusion_matrix(cls_pred)     images = data.x_test[0:9]                      
cls_true = data.y_test_cls[0:9]                 
y_pred = model.predict(x=images)               
cls_pred = np.argmax(y_pred, axis=1)            
title = 'MNIST(Sequential Model): plot predicted example, resl VS predict'
plot_images(title, images=images,               cls_true=cls_true,cls_pred=cls_pred)

这篇关于DL之CNN:利用卷积神经网络算法(2→2,基于Keras的API-Functional或Sequential)利用MNIST(手写数字图片识别)数据集实现多分类预测的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/629675

相关文章

SpringBoot3实现Gzip压缩优化的技术指南

《SpringBoot3实现Gzip压缩优化的技术指南》随着Web应用的用户量和数据量增加,网络带宽和页面加载速度逐渐成为瓶颈,为了减少数据传输量,提高用户体验,我们可以使用Gzip压缩HTTP响应,... 目录1、简述2、配置2.1 添加依赖2.2 配置 Gzip 压缩3、服务端应用4、前端应用4.1 N

SpringBoot实现数据库读写分离的3种方法小结

《SpringBoot实现数据库读写分离的3种方法小结》为了提高系统的读写性能和可用性,读写分离是一种经典的数据库架构模式,在SpringBoot应用中,有多种方式可以实现数据库读写分离,本文将介绍三... 目录一、数据库读写分离概述二、方案一:基于AbstractRoutingDataSource实现动态

Python FastAPI+Celery+RabbitMQ实现分布式图片水印处理系统

《PythonFastAPI+Celery+RabbitMQ实现分布式图片水印处理系统》这篇文章主要为大家详细介绍了PythonFastAPI如何结合Celery以及RabbitMQ实现简单的分布式... 实现思路FastAPI 服务器Celery 任务队列RabbitMQ 作为消息代理定时任务处理完整

Java枚举类实现Key-Value映射的多种实现方式

《Java枚举类实现Key-Value映射的多种实现方式》在Java开发中,枚举(Enum)是一种特殊的类,本文将详细介绍Java枚举类实现key-value映射的多种方式,有需要的小伙伴可以根据需要... 目录前言一、基础实现方式1.1 为枚举添加属性和构造方法二、http://www.cppcns.co

使用Python实现快速搭建本地HTTP服务器

《使用Python实现快速搭建本地HTTP服务器》:本文主要介绍如何使用Python快速搭建本地HTTP服务器,轻松实现一键HTTP文件共享,同时结合二维码技术,让访问更简单,感兴趣的小伙伴可以了... 目录1. 概述2. 快速搭建 HTTP 文件共享服务2.1 核心思路2.2 代码实现2.3 代码解读3.

使用C#代码在PDF文档中添加、删除和替换图片

《使用C#代码在PDF文档中添加、删除和替换图片》在当今数字化文档处理场景中,动态操作PDF文档中的图像已成为企业级应用开发的核心需求之一,本文将介绍如何在.NET平台使用C#代码在PDF文档中添加、... 目录引言用C#添加图片到PDF文档用C#删除PDF文档中的图片用C#替换PDF文档中的图片引言在当

详解C#如何提取PDF文档中的图片

《详解C#如何提取PDF文档中的图片》提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使用,下面我们就来看看如何使用C#通过代码从PDF文档中提取图片吧... 当 PDF 文件中包含有价值的图片,如艺术画作、设计素材、报告图表等,提取图片可以将这些图像资源进行单独保存,方便后续在不同的项目中使

MySQL双主搭建+keepalived高可用的实现

《MySQL双主搭建+keepalived高可用的实现》本文主要介绍了MySQL双主搭建+keepalived高可用的实现,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、测试环境准备二、主从搭建1.创建复制用户2.创建复制关系3.开启复制,确认复制是否成功4.同

Java实现文件图片的预览和下载功能

《Java实现文件图片的预览和下载功能》这篇文章主要为大家详细介绍了如何使用Java实现文件图片的预览和下载功能,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... Java实现文件(图片)的预览和下载 @ApiOperation("访问文件") @GetMapping("

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定