Skip to content

Instantly share code, notes, and snippets.

@johntips
Last active July 27, 2017 04:36
Show Gist options
  • Save johntips/10e4a5cd9c3cc936c9804ac49d794c50 to your computer and use it in GitHub Desktop.
Save johntips/10e4a5cd9c3cc936c9804ac49d794c50 to your computer and use it in GitHub Desktop.
import keras
from keras.datasets import cifar10
import numpy as np
from keras.applications.inception_v3 import InceptionV3, preprocess_input
import scipy
from scipy import misc
import os
from keras.utils import np_utils
from keras.callbacks import ModelCheckpoint
from keras.models import Sequential
from keras.layers import Dense, Dropout, Conv2D, GlobalAveragePooling2D
model = Sequential()
model.add(Conv2D(filters=100, kernel_size=2, input_shape=features.shape[1:]))
model.add(Dropout(0.4))
model.add(GlobalAveragePooling2D())
model.add(Dropout(0.3))
model.add(Dense(10, activation='softmax'))
model.summary()
model.compile(loss='categorical_crossentropy', optimizer='rmsprop', metrics=['accuracy'])
checkpointer = ModelCheckpoint(filepath='model.best.hdf5',
verbose=1, save_best_only=True)
model.fit(features, y_train, batch_size=50, epochs=50,
validation_split=0.2, callbacks=[checkpointer],
verbose=2, shuffle=True)
# load the weights that yielded the best validation accuracy
model.load_weights('model.best.hdf5')
# evaluate test accuracy
score = model.evaluate(features_test, y_test, verbose=0)
accuracy = 100*score[1]
# print test accuracy
print('Test accuracy: %.4f%%' % accuracy)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment