1. 程式人生 > >實戰keras——用CNN實現cifar10影象分類

實戰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)
View Code

實驗結果:

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