Classification of cifar-10 based on Kears

I. Introduction to CIFAR-10 data set CIFAR-10 data set contains 60000 32 * 32 color images in 10 categories, 6000 images in each category, all of whi...
I. Introduction to CIFAR-10 data set

CIFAR-10 data set contains 60000 32 * 32 color images in 10 categories, 6000 images in each category, all of which have been labeled. There are 50000 training pictures and 10000 test pictures, and 10 categories are respectively:

  • airplane
  • automobile
  • Bird bird
  • Cat cat
  • Deer deer
  • Dog dog
  • Frog frog
  • Horse horse
  • Ship ship
  • Truck truck

II. The code is as follows
# Import package from __future__ import print_function import keras from keras.models import Sequential from keras.layers import Conv2D, MaxPool2D, Flatten from keras.layers import Dense, Dropout from keras.preprocessing.image import ImageDataGenerator import os
# Initialization parameters batch_size = 32 epochs = 100 # Iteration times num_classes = 10 # Classification number data_augmentation = False # Data enhancement model_dir = os.path.join(os.getcwd(), 'saved_models') # Save path model_name = 'keras_cifar10_trained_model.h5' # file name
# Load and check datasets (x_train, y_train), (x_test, y_test) = keras.datasets.cifar10() print('x_train.shape = ' + str(x_train.shape)) print('y_train.shape = ' + str(x_test.shape)) print('train samples = ' + str(x_train.shape[0])) print('test samples = ' + str(x_test.shape[0]))
# Data preprocessing y_train = keras.utils.to_categorical(y_train, num_classes) y_test = keras.utils.to_categorical(y_test, num_classes) x_train = x_train.astype('float32') x_train /= 255. x_test = x_test.astype('float32') x_test /= 255.
# Build Sequential model model = keras.Sequential() # conv -> conv -> maxpool -> dropout -> conv -> conv -> maxpool -> dropout -> flatten -> fc -> dropout -> fc -> softmax model.add(Conv2D(32, (3, 3), padding='same', activation='relu', input_shape=x_train.shape[1:])) model.add(Conv2D(32, (3, 3), activation='relu')) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.5)) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(Conv2D(64, (3, 3), activation='relu')) model.add(MaxPool2D(pool_size=(2, 2))) model.add(Dropout(0.25)) model.add(Flatten()) model.add(Dense(512, activation='relu')) model.add(Dropout(0.5)) model.add(Dense(num_classes, activation='softmax'))
# Model compilation opt = keras.optimizers.rmsprop(lr=0.0001, decay=1e-6) # Optimizer, choose to use model.compile(optimizer=opt, loss='categorical_crossentropy', metrics=['accuracy'])
# Training model if data_augmentation: print("Not using data augementation") model.fit(x_train, y_train, batch_size=batch_size, epochs=epochs, validation_data=(x_test, y_test), shuffle=True) else: print("Using real-time data augmentation") # Use real-time data increase data_generate = ImageDataGenerator( featurewise_center=False, # Set the mean value of the input data to 0 samplewise_center=False, # Set the mean value of each sample to 0 featurewise_std_normalization=False, # Divide the input by the standard deviation of data, and carry out feature by feature samplewise_std_normalization=False, # Divide each output by its standard deviation zca_epsilon=1e-6, # epsilon value of ZCA whitening, default is 1e-6 zca_whitening=False, # Whether ZCA whitening is applied rotation_range=0, # Degree range of random rotation, input as integer width_shift_range=0.1, # Translation left and right, input as floating point number, output as pixel value when greater than 1 height_shift_range=0.1, # Translation up and down, input as floating point number, output as pixel value when greater than 1 shear_range=0., # Shear strength, input as floating point number zoom_range=0., # Random scaling, input as floating point number channel_shift_range=0., # Random channel conversion range, input as floating point number fill_mode='nearest', # There are three filling methods: constant, reflect and wrap cval=0., # The value used for filling, which takes effect when fill mode = constant ' horizontal_flip=True, # Random horizontal flip vertical_flip=False, # Random vertical flip rescale=None, # Replay factor, no scaling when None or 0 preprocessing_function=None, # Functions applied to each input data_format=None, # Image data format, the default is channels? Last validation_split=0.0 ) data_generate.fit(x_train) # Using real-time data enhanced batch to fit the model: model.fit_generator(data_generate.flow(x_train, y_train, batch_size), steps_per_epoch=len(x_train)/32, epochs=epochs)
# Preservation model if not os.path.isdir(model_dir): # Check whether the vehicle directory exists, if not, create the corresponding directory os.makedirs(model_dir) model_path = os.path.join(model_dir, model_name) model.save(model_path) print("Saved trained model at: " + str(model_path))
# model prediction scores = model.evaluate(x_test, y_test, batch_sizeb=batch_size, verbose=1) # Show iterations as progress bars print("Test loss is: " + str(scores[0])) print("Test accuracy is: " + str(scores[1]))
III. operation results

Generally speaking, when running on the CPU, it takes several hours to load the data set and run the results. A total of 100 iterations are needed. The intermediate results are as follows:

It should be noted that when using the fit ﹣ generator iterator for data fitting, steps ﹣ per ﹣ epoch needs to be set, otherwise the result is likely to report an error, as shown below:

Please specify `steps_per_epoch` or use the `keras.utils.Sequence` class.

The final experimental results are as follows:

The loss value of the final test set is 0.748, and the prediction accuracy is only 0.7543

The saved model is as shown above, which can be loaded and used directly next time

4 December 2019, 11:07 | Views: 6284

Add new comment

For adding a comment, please log in
or create account

0 comments