import numpy as np
np.random.randint(0,49,3)

# -*- coding:utf-8 -*-
import keras
from keras.models import Sequential
from keras.layers import Dense,Activation
from keras.layers import Convolution2D,MaxPooling2D,Flatten
from keras.optimizers import Adam,Adadelta
from keras.utils import np_utils #utilities
import matplotlib.pyplot as plt
%matplotlib inline

####引用CIFAR10的数据集
from keras.datasets import cifar10
(train_x,train_y),(test_x,test_y)=cifar10.load_data()

print(train_x.shape,train_y.shape,test_x.shape,test_y.shape)

##把训练的目标值转为one-hot编码
# 1->(0,1,0,0,0,0,0,0,0,0)
n_classes=10
train_Y=keras.utils.to_categorical(train_y,n_classes)
test_Y=keras.utils.to_categorical(test_y,n_classes)

print(train_Y.shape,test_Y.shape)

### visualization
###显示训练数据集train_x(50000,32,32,3)中的前64张图像,
##显示成8*8的形式,并且加入title(label:Truth type)

plt.figure(figsize=(15,15))###显示的每张图像为15*15大小
for i in range(64):
plt.subplot(8,8,(i+1))
plt.imshow(train_x[i])
plt.title("label:{0}".format(train_y[i]))
plt.axis('off')
plt.show()

## 1.构造CNN,分为3层,
# #1(kernel=3*3*32,s=1,p='same',acti='relu')
# #1(pool_size=2,s=2,p='same')
# #1 Dropout(0.2)

# #2(kernel=3*3*64,s=1,p='same',acti='relu')
# #2(pool_size=2,s=2,p='same')
# #2 Dropout(0.2)

# #1(kernel=3*3*128,s=1,p='same',acti='relu')
# #1(pool_size=2,s=2,p='same')
# #2 Dropout(0.2)

from keras.layers import Dropout
model=Sequential()
##layer 1
model.add(Convolution2D(filters=32,kernel_size=(3,3),input_shape=(32,32,3),strides=(1,1),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same'))
model.add(Dropout(0.2))

##layer 2
model.add(Convolution2D(filters=64,kernel_size=(3,3),strides=(1,1),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same'))
model.add(Dropout(0.2))

##layer 3
model.add(Convolution2D(filters=128,kernel_size=(3,3),strides=(1,1),padding='same'))
model.add(Activation('relu'))
model.add(MaxPooling2D(pool_size=(2,2),strides=(2,2),padding='same'))
model.add(Flatten())
model.add(Dropout(0.2))

### Fully connected layer 1
model.add(Dense(units=128,activation='relu'))
model.add(Dropout(0.5))

### Fully connected layer 2
model.add(Dense(units=256,activation='relu'))
model.add(Dropout(0.5))

### Fully connected layer 3
model.add(Dense(units=n_classes,activation='softmax'))

## conpile
model.compile(optimizer=Adadelta(),loss='categorical_crossentropy',metrics=['accuracy'])

model.summary()

import time
s_time=time.time()
model.fit(train_x,train_Y,epochs=30,batch_size=256,verbose=1)
e_time=time.time()
print("running time%.4f"%(e_time-s_time))

e=model.evaluate(test_x,test_Y,batch_size=256,verbose=1)
print("loss:%.4f"%(e[0]),"accuracy:%.4f"%(e[1]))

from keras.models import load_model
model.save("cifar10_30.h5")###you should install pyh5
del model # deletes the existing model
model.predict(test_x[0],batch_size=1,verbose=0)##报错
##加载模型
model=load_model("cifar10_30.h5")
test_img=test_x[0][np.newaxis,:]
model.predict_classes(test_img,batch_size=1,verbose=0)
#test_img.shape
test_y[0]

吴裕雄 python神经网络(7)的更多相关文章

  1. 吴裕雄 python神经网络 花朵图片识别(10)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Image, ImageChopsfrom skim ...

  2. 吴裕雄 python神经网络 花朵图片识别(9)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom PIL import Image, ImageChopsfrom skim ...

  3. 吴裕雄 python神经网络 手写数字图片识别(5)

    import kerasimport matplotlib.pyplot as pltfrom keras.models import Sequentialfrom keras.layers impo ...

  4. 吴裕雄 python神经网络 水果图片识别(4)

    # coding: utf-8 # In[1]:import osimport numpy as npfrom skimage import color, data, transform, io # ...

  5. 吴裕雄 python神经网络 水果图片识别(3)

    import osimport kerasimport timeimport numpy as npimport tensorflow as tffrom random import shufflef ...

  6. 吴裕雄 python神经网络 水果图片识别(2)

    import osimport numpy as npimport matplotlib.pyplot as pltfrom skimage import color,data,transform,i ...

  7. 吴裕雄 python 神经网络——TensorFlow 循环神经网络处理MNIST手写数字数据集

    #加载TF并导入数据集 import tensorflow as tf from tensorflow.contrib import rnn from tensorflow.examples.tuto ...

  8. 吴裕雄 python 神经网络——TensorFlow 使用卷积神经网络训练和预测MNIST手写数据集

    import tensorflow as tf import numpy as np from tensorflow.examples.tutorials.mnist import input_dat ...

  9. 吴裕雄 python 神经网络——TensorFlow 训练过程的可视化 TensorBoard的应用

    #训练过程的可视化 ,TensorBoard的应用 #导入模块并下载数据集 import tensorflow as tf from tensorflow.examples.tutorials.mni ...

  10. 吴裕雄 python 神经网络——TensorFlow实现搭建基础神经网络

    import numpy as np import tensorflow as tf import matplotlib.pyplot as plt def add_layer(inputs, in_ ...

随机推荐

  1. DNS(bind)服务器安装和配置

    一.前言 DNS 域名系统(英文:Domain Name System,缩写:DNS)是因特网的一项服务.它作为将域名和IP地址相互映射的一个分布式数据库,能够使人更方便地访问互联网.DNS使用TCP ...

  2. 《Linux 性能及调优指南》1.1 Linux进程管理

    https://blog.csdn.net/ljianhui/article/details/46718835 本文为IBM RedBook的Linux Performanceand Tuning G ...

  3. vue获取dom

    //使用ref属性来获取当前的div的dom属性 <div class="list" ref="wrapper"></div> //在j ...

  4. RBAC表

    --权限管理1 CREATE TABLE SystemLog--日志表 ( Id ,) PRIMARY KEY,--主键id UserName ) NOT NULL,--用户名称,创建日志的用户名称 ...

  5. mongo数据库的安装与使用

    下载mongoDB安装包.https://pan.baidu.com/s/1cvSJtc 默认安装.会在系统盘的program Files文件夹下法相一个MongoDB的文件夹,这个就是软件安装的位置 ...

  6. PHP的几种缓存方式

    1.文件缓存: 2.Memcached;  是一个高性能的分布式内存对象缓存系统,用于动态Web应用以减轻数据库负载.它通过在内存中缓存数据和对象来减少读取数据库的次数,从而提高动态.数据库驱动网站的 ...

  7. 浮动ip cz

  8. 36.scrapy框架采集全球玻璃网数据

    1.采集目标地址 https://www.glass.cn/gongying/sellindex.aspx 网站比较简单,没什么大的需要注意的问题.2.通过分析测试 https://www.glass ...

  9. python学习笔记_week24

    note 内容回顾: Model - 数据库操作 on_delete Query_set select_related 跨表数据一次性拿过来,不增加sql查询次数.帮助跨表,后面参数只能加连表字段 f ...

  10. webapi_uploadfile_gdal_to_geojson_and_unzipfile

    using ICSharpCode.SharpZipLib.Zip; using OSGeo.GDAL; using OSGeo.OGR; using System; using System.Col ...