Created
February 6, 2017 16:28
-
-
Save chakkritte/12c692a04af1633e0abce64e4f292a4d to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from __future__ import print_function | |
from keras.datasets import cifar10 | |
from keras.models import Sequential | |
from keras.layers import Dense, Dropout, Activation, Flatten | |
from keras.layers import Convolution2D, MaxPooling2D | |
from keras.utils import np_utils | |
batch_size = 128 | |
nb_classes = 10 | |
nb_epoch = 20 | |
# input image dimensions | |
img_rows, img_cols = 32, 32 | |
# The CIFAR10 images are RGB. | |
img_channels = 3 | |
# The data, shuffled and split between train and test sets: | |
(X_train, y_train), (X_test, y_test) = cifar10.load_data() | |
print('X_train shape:', X_train.shape) | |
print(X_train.shape[0], 'train samples') | |
print(X_test.shape[0], 'test samples') | |
# Convert class vectors to binary class matrices. | |
Y_train = np_utils.to_categorical(y_train, nb_classes) | |
Y_test = np_utils.to_categorical(y_test, nb_classes) | |
model = Sequential() | |
model.add(Convolution2D(32, 3, 3, border_mode='same',input_shape=X_train.shape[1:])) | |
model.add(Activation('relu')) | |
model.add(Convolution2D(32, 3, 3)) | |
model.add(Activation('relu')) | |
model.add(MaxPooling2D(pool_size=(2, 2))) | |
model.add(Dropout(0.25)) | |
model.add(Convolution2D(64, 3, 3, border_mode='same')) | |
model.add(Activation('relu')) | |
model.add(Convolution2D(64, 3, 3)) | |
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(nb_classes)) | |
model.add(Activation('softmax')) | |
model.compile(loss='categorical_crossentropy', | |
optimizer='sgd', | |
metrics=["accuracy"]) | |
X_train = X_train.astype('float32') | |
X_test = X_test.astype('float32') | |
X_train /= 255 | |
X_test /= 255 | |
model.fit(X_train, Y_train, | |
batch_size=batch_size, | |
nb_epoch=nb_epoch, | |
validation_data=(X_test, Y_test), | |
shuffle=True) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment