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]