Skip to content

Instantly share code, notes, and snippets.

View mjbhobe's full-sized avatar

Manish Bhobe mjbhobe

  • Mumbai, India
  • 12:02 (UTC +05:30)
View GitHub Profile
from keras.models import Sequential
from keras.layers import Conv2D, MaxPooling2D, Flatten, Dense, Dropout
def build_model():
model = Sequential()
# add Convolutional layers
model.add(Conv2D(filters=32, kernel_size=(3,3), activation='relu', padding='same',
input_shape=(image_height, image_width, num_channels)))
model.add(MaxPooling2D(pool_size=(2,2)))
model.add(Conv2D(filters=64, kernel_size=(3,3), activation='relu', padding='same'))
# shuffle the training dataset (5 times!)
for _ in range(5):
indexes = np.random.permutation(len(train_data))
train_data = train_data[indexes]
train_labels_cat = train_labels_cat[indexes]
# now set-aside 10% of the train_data/labels as the
# cross-validation sets
val_perc = 0.10
# some variables...
image_height = train_digits.shape[1]
image_width = train_digits.shape[2]
num_channels = 1 # we have grayscale images
# NOTE: image_height == image_width == 28
# re-shape the images data
train_data = np.reshape(train_digits, (train_digits.shape[0], image_height, image_width, num_channels))
test_data = np.reshape(test_digits, (test_digits.shape[0],image_height, image_width, num_channels))
from keras.datasets.mnist import load_data
# load the data - it returns 2 tuples of digits & labels - one for
# the train set & the other for the test set
(train_digits, train_labels), (test_digits, test_labels) = load_data()
# display 14 random images from the training set
import numpy as np
np.random.seed(123)
# let's pick 20 random values from the test data
rand_idx = np.random.randint(0, test_data.shape[0],20)
rand_images = test_data[rand_idx]
rand_labels_true = test_labels_cat[rand_idx]
# decode the labels (reverse on-hot encoding)
y_true = np.argmax(rand_labels_true, axis=1)
y_true
# predictions
# let's pick 20 random values from the test data
rand_idx = np.random.randint(0, test_data.shape[0],20)
rand_images = test_data[rand_idx]
rand_labels_true = test_labels_cat[rand_idx]
# one-hot-decode the labels (reverse on-hot encoding)
y_true = np.argmax(rand_labels_true, axis=1)
print(y_true)
# predictions
test_loss, test_accuracy = kr_base_model.evaluate(test_data, test_labels_cat, batch_size=batch_size)
print('Test loss: %8.4f accuracy: %.3f' % (test_loss, test_accuracy))
kr_history = kr_base_model.fit(train_data2, train_labels_cat2,
epochs=15,
batch_size=100,
validation_data=(val_data,val_labels_cat))
from keras.models import Sequential
from keras.layers import Dense, MaxPooling2D, Conv2D, Flatten
def build_keras_model():
model = Sequential()
# CNN layer
model.add(Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='elu',
input_shape=(image_height, image_width, num_channels)))
model.add(Conv2D(filters=32, kernel_size=3, strides=1, padding='same', activation='elu'))
model.add(MaxPooling2D(pool_size=2))
# some more globals...
num_features = train_data.shape[1]
num_epochs = 15
batch_size = 100
ksize, kernel_size = 3, 3
psize, pool_size = 2, 2
strides = 1