Keras快速搭建简易CNN训练Cifar10数据集

Keras很适合快速搭建网络,验证想法。下面使用Keras快速搭建一个简单的卷及神经网

import keras as K
from keras.datasets import cifar10
from keras.models import Sequential
from keras.layers import Dense,Conv2D,MaxPool2D,Flatten,Activation
import numpy as np
(x_train, y_train), (x_test, y_test) = cifar10.load_data()

x_train = x_train.astype('float32')###没有归一化训练无法收敛
x_test = x_test.astype('float32')
x_train /= 255
x_test /= 255
label_train=np.zeros((50000,10,1,1),dtype='uint8')
label_test=np.zeros((10000,10,1,1),dtype='uint8')
for i in range(50000):
    label_train[i,y_train[i],0,0]=1
for i in range(10000):
    label_test[i,y_test[i],0,0]=1
label_train=np.reshape(label_train,(50000,10))
label_test=np.reshape(label_test,(10000,10))
model=Sequential()
model.add(Conv2D(64,3,padding='same',activation='relu'))
model.add(Conv2D(128,3,padding='same',activation='relu'))
model.add(MaxPool2D(strides=2))
model.add(Conv2D(256,3,padding='same',activation='relu'))
model.add(Conv2D(256,3,padding='same',activation='relu'))
model.add(MaxPool2D(strides=2))
model.add(Flatten())
model.add(Dense(512,activation='relu'))
model.add(Dense(1024,activation='relu'))
model.add(Dense(10,activation='softmax'))
model.compile(optimizer='sgd',loss='categorical_crossentropy',metrics=['accuracy'])
model.fit(x_train,label_train, epochs=80,batch_size=350)
model.save('final.h5')
score=model.evaluate(x_test,label_test)
print('test score:',score[0])
print('test accurace:',score[1])

络来训练Cifar10数据集。

你可能感兴趣的:(Keras快速搭建简易CNN训练Cifar10数据集)