实战keras——用CNN实现cifar10图像分类
原文:https://blog.csdn.net/zzulp/article/details/76358694
import keras
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense, Dropout, Activation, Flatten
from keras.layers import Conv2D, MaxPooling2D num_classes = 10
model_name = 'cifar10.h5' # The data, shuffled and split between train and test sets:
(x_train, y_train), (x_test, y_test) = cifar10.load_data() x_train = x_train.astype('float32')/255
x_test = x_test.astype('float32')/255 # Convert class vectors to binary class matrices.
y_train = keras.utils.to_categorical(y_train, num_classes)
y_test = keras.utils.to_categorical(y_test, num_classes) model = Sequential() model.add(Conv2D(32, (3, 3), padding='same', input_shape=x_train.shape[1:]))
model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25)) model.add(Conv2D(64, (3, 3), padding='same'))
model.add(Activation('relu')) model.add(MaxPooling2D(pool_size=(2, 2)))
model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512))
model.add(Activation('relu'))
model.add(Dropout(0.5)) model.add(Dense(num_classes))
model.add(Activation('softmax')) model.summary() # initiate RMSprop optimizer
opt = keras.optimizers.rmsprop(lr=0.001, decay=1e-6) # train the model using RMSprop
model.compile(loss='categorical_crossentropy', optimizer=opt, metrics=['accuracy']) hist = model.fit(x_train, y_train, epochs=40, shuffle=True)
model.save(model_name) # evaluate
loss, accuracy = model.evaluate(x_test, y_test)
print(loss, accuracy)
实验结果:
Downloading data from http://www.cs.toronto.edu/~kriz/cifar-10-python.tar.gz
170475520/170498071 [============================>.] - ETA: 0s_________________________________________________________________
Layer (type) Output Shape Param #
=================================================================
conv2d_1 (Conv2D) (None, 32, 32, 32) 896
_________________________________________________________________
activation_1 (Activation) (None, 32, 32, 32) 0
_________________________________________________________________
max_pooling2d_1 (MaxPooling2 (None, 16, 16, 32) 0
_________________________________________________________________
dropout_1 (Dropout) (None, 16, 16, 32) 0
_________________________________________________________________
conv2d_2 (Conv2D) (None, 16, 16, 64) 18496
_________________________________________________________________
activation_2 (Activation) (None, 16, 16, 64) 0
_________________________________________________________________
max_pooling2d_2 (MaxPooling2 (None, 8, 8, 64) 0
_________________________________________________________________
dropout_2 (Dropout) (None, 8, 8, 64) 0
_________________________________________________________________
flatten_1 (Flatten) (None, 4096) 0
_________________________________________________________________
dense_1 (Dense) (None, 512) 2097664
_________________________________________________________________
activation_3 (Activation) (None, 512) 0
_________________________________________________________________
dropout_3 (Dropout) (None, 512) 0
_________________________________________________________________
dense_2 (Dense) (None, 10) 5130
_________________________________________________________________
activation_4 (Activation) (None, 10) 0
=================================================================
Total params: 2,122,186
Trainable params: 2,122,186
Non-trainable params: 0
_________________________________________________________________
Epoch 1/40
50000/50000 [==============================] - 189s - loss: 1.5264 - acc: 0.4558
Epoch 2/40
50000/50000 [==============================] - 185s - loss: 1.2152 - acc: 0.5769
Epoch 3/40
50000/50000 [==============================] - 192s - loss: 1.1367 - acc: 0.6118
Epoch 4/40
50000/50000 [==============================] - 183s - loss: 1.1145 - acc: 0.6241
Epoch 5/40
50000/50000 [==============================] - 189s - loss: 1.1131 - acc: 0.6273
Epoch 6/40
50000/50000 [==============================] - 192s - loss: 1.1175 - acc: 0.6313
Epoch 7/40
50000/50000 [==============================] - 202s - loss: 1.1309 - acc: 0.6299
Epoch 8/40
50000/50000 [==============================] - 187s - loss: 1.1406 - acc: 0.6278
Epoch 9/40
50000/50000 [==============================] - 190s - loss: 1.1583 - acc: 0.6221
Epoch 10/40
50000/50000 [==============================] - 188s - loss: 1.1689 - acc: 0.6199
Epoch 11/40
50000/50000 [==============================] - 183s - loss: 1.1896 - acc: 0.6134
Epoch 12/40
50000/50000 [==============================] - 188s - loss: 1.2032 - acc: 0.6101
Epoch 13/40
50000/50000 [==============================] - 186s - loss: 1.2246 - acc: 0.6011
Epoch 14/40
50000/50000 [==============================] - 192s - loss: 1.2405 - acc: 0.6000
Epoch 15/40
50000/50000 [==============================] - 170s - loss: 1.2514 - acc: 0.5958
Epoch 16/40
50000/50000 [==============================] - 172s - loss: 1.2627 - acc: 0.5912
Epoch 17/40
50000/50000 [==============================] - 177s - loss: 1.2835 - acc: 0.5838
Epoch 18/40
50000/50000 [==============================] - 179s - loss: 1.2876 - acc: 0.5809
Epoch 19/40
50000/50000 [==============================] - 180s - loss: 1.3085 - acc: 0.5782
Epoch 20/40
50000/50000 [==============================] - 180s - loss: 1.3253 - acc: 0.5695
Epoch 21/40
50000/50000 [==============================] - 180s - loss: 1.3375 - acc: 0.5651
Epoch 22/40
50000/50000 [==============================] - 183s - loss: 1.3483 - acc: 0.5623
Epoch 23/40
50000/50000 [==============================] - 177s - loss: 1.3567 - acc: 0.5599
Epoch 24/40
50000/50000 [==============================] - 178s - loss: 1.3697 - acc: 0.5541
Epoch 25/40
50000/50000 [==============================] - 178s - loss: 1.3722 - acc: 0.5518
Epoch 26/40
50000/50000 [==============================] - 181s - loss: 1.3848 - acc: 0.5479
Epoch 27/40
50000/50000 [==============================] - 181s - loss: 1.3916 - acc: 0.5474
Epoch 28/40
50000/50000 [==============================] - 183s - loss: 1.4081 - acc: 0.5403
Epoch 29/40
50000/50000 [==============================] - 172s - loss: 1.4229 - acc: 0.5387
Epoch 30/40
50000/50000 [==============================] - 190s - loss: 1.4153 - acc: 0.5383
Epoch 31/40
50000/50000 [==============================] - 183s - loss: 1.4355 - acc: 0.5324
Epoch 32/40
50000/50000 [==============================] - 191s - loss: 1.4667 - acc: 0.5251
Epoch 33/40
50000/50000 [==============================] - 169s - loss: 1.4690 - acc: 0.5188
Epoch 34/40
50000/50000 [==============================] - 168s - loss: 1.4798 - acc: 0.5176
Epoch 35/40
50000/50000 [==============================] - 181s - loss: 1.5152 - acc: 0.5054
Epoch 36/40
50000/50000 [==============================] - 173s - loss: 1.4985 - acc: 0.5067
Epoch 37/40
50000/50000 [==============================] - 182s - loss: 1.5030 - acc: 0.5098
Epoch 38/40
50000/50000 [==============================] - 178s - loss: 1.5298 - acc: 0.4967
Epoch 39/40
50000/50000 [==============================] - 181s - loss: 1.5237 - acc: 0.5014
Epoch 40/40
50000/50000 [==============================] - 181s - loss: 1.4933 - acc: 0.5103
9952/10000 [============================>.] - ETA: 0s1.80146283646 0.3274
实战keras——用CNN实现cifar10图像分类的更多相关文章
- CNN眼中的世界:利用Keras解释CNN的滤波器
转载自:https://keras-cn.readthedocs.io/en/latest/legacy/blog/cnn_see_world/ 文章信息 本文地址:http://blog.keras ...
- 基于Pre-Train的CNN模型的图像分类实验
基于Pre-Train的CNN模型的图像分类实验 MatConvNet工具包提供了好几个在imageNet数据库上训练好的CNN模型,可以利用这个训练好的模型提取图像的特征.本文就利用其中的 “im ...
- 深度学习之 cnn 进行 CIFAR10 分类
深度学习之 cnn 进行 CIFAR10 分类 import torchvision as tv import torchvision.transforms as transforms from to ...
- keras训练cnn模型时loss为nan
keras训练cnn模型时loss为nan 1.首先记下来如何解决这个问题的:由于我代码中 model.compile(loss='categorical_crossentropy', optimiz ...
- 数据挖掘入门系列教程(十二)之使用keras构建CNN网络识别CIFAR10
简介 在上一篇博客:数据挖掘入门系列教程(十一点五)之CNN网络介绍中,介绍了CNN的工作原理和工作流程,在这一篇博客,将具体的使用代码来说明如何使用keras构建一个CNN网络来对CIFAR-10数 ...
- Keras框架下使用CNN进行CIFAR-10的识别测试
有手册,然后代码不知道看一下:https://keras-cn.readthedocs.io/en/latest/ 首先是下载数据集,下载太慢了就从网盘上下载: 链接:https://pan.baid ...
- 入门项目数字手写体识别:使用Keras完成CNN模型搭建(重要)
摘要: 本文是通过Keras实现深度学习入门项目——数字手写体识别,整个流程介绍比较详细,适合初学者上手实践. 对于图像分类任务而言,卷积神经网络(CNN)是目前最优的网络结构,没有之一.在面部识别. ...
- CNN训练Cifar-10技巧
关于数据集 Cifar-10是由Hinton的两个大弟子Alex Krizhevsky.Ilya Sutskever收集的一个用于普适物体识别的数据集.Cifar是加拿大政府牵头投资的一个先进科学项目 ...
- 使用Keras搭建cnn+rnn, BRNN,DRNN等模型
Keras api 提前知道: BatchNormalization, 用来加快每次迭代中的训练速度 Normalize the activations of the previous layer a ...
随机推荐
- MySQL--------SQL优化审核工具实战
1. 背景 SQLAdvisor是由美团点评公司技术工程部DBA团队(北京)开发维护的一个分析SQL给出索引优化建议的工具.它基于MySQL原生态词法解析,结合分析SQL中的where条件.聚合条件. ...
- redistempalate的超时设置的操作更新
redistempalate的超时设置时,一定要每次用set写入时,更新超时,默认是不会自动更新的. 例如: int tempTime = this.redisTemplate.getExpire(& ...
- 配置NFS共享
NFS(网络文件系统)-------> linux与linux平台 服务器端: 1.安装软件nfs-utils(服务:nfs-server) 2.创建共享目录:mkdir /nfs_dir 3. ...
- [转]vue-router各个属性的作用及用法
转自以下网址,当备忘使用:https://www.cnblogs.com/goloving/p/9211358.html vue-router是vue单页面开发的路由,就是决定页面跳转的! <r ...
- Navicat连接虚拟机上的mysql
刚刚在虚拟主机上安装mysql, 想使用Navicat 操作mysql. 但是连接不上 报错: 2003 - can't connect to MySQL server on '192.168.1 ...
- .net实现浏览器大文件分片上传
以ASP.NET Core WebAPI 作后端 API ,用 Vue 构建前端页面,用 Axios 从前端访问后端 API ,包括文件的上传和下载. 准备文件上传的API #region 文件上传 ...
- 阿里云Ubuntu安装LNMP环境之PHP7
在QQ群很多朋友问阿里云服务器怎么安装LNMP环境,怎么把项目放到服务器上面去,在这里,我就从头开始教大家怎么在阿里云服务器安装LNMP环境. 在这之前,我们先要知道什么是LNMP. L: 表示的是L ...
- Python地理信息数据可视化
地图 基础铺垫 定义 地图(map):是指按一定的比例运用符号.颜色.文字标记等描绘显示地球表面的自然地理.行政区域.社会经济状况的图形. 地图绘制步骤 绘制需要展示的地图,获取地图对象,获取每个区域 ...
- redis服务及其配置与应用(window 环境下)
一.redis简介:Redis是一个开源的使用ANSI C语言编写.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API(源自百度百科),个人更喜欢下面的解释:我们 ...
- vsftp 上传550 Permission denied解决办法
在linux服务器上安装和配置了vsftp,添加用户然后现在用这个用户登录ftp成功,能下载,但是不能上传和删除,明明这个用户的主目录给了777权限了,但是始终无法上传 解决办法:是在/var/ftp ...