keras CNN对CIFAR10图像分类

from keras.datasets import cifar10
from keras.utils import np_utils
from keras.models import Sequential
from keras.layers import Conv2D, Dense, AveragePooling2D, Flatten, BatchNormalization
from keras.optimizers import Adam
import numpy as np
import tensorflow as tf

(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
print(Y_train.shape[0])
Y_train = Y_train.reshape(Y_train.shape[0])
Y_test = Y_test.reshape(Y_test.shape[0])
Y_train = np_utils.to_categorical(Y_train, 10)
Y_test = np_utils.to_categorical(Y_test, 10)

model = Sequential()
model.add(Conv2D(32, 5, strides=1, padding='same', input_shape=(32, 32, 3), activation=tf.nn.relu))
model.add(AveragePooling2D(3, 2, padding='same'))
model.add(BatchNormalization())

model.add(Conv2D(65, 5, strides=1, padding='same', activation=tf.nn.relu))
model.add(BatchNormalization())
model.add(AveragePooling2D(3, 2, padding='same'))
model.add(Flatten())

model.add(Dense(384, activation=tf.nn.relu))
model.add(Dense(192, activation=tf.nn.relu))
model.add(Dense(10, activation=tf.nn.softmax))

adam = Adam(lr=0.001, beta_1=0.9, beta_2=0.999, epsilon=1e-8)
model.compile(optimizer=adam, loss='categorical_crossentropy', metrics=['accuracy'])
print('Training')
result = model.fit(X_train, Y_train, epochs=20, batch_size=64)

print('Testing')
loss, accuracy = model.evaluate(X_test, Y_test)
print('loss, accuracy', loss, accuracy)

训练结果:

163s 3ms/step - loss: 0.0641 - acc: 0.9790

测试结果:

loss, accuracy 2.004339290237427 0.6988

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值