Skip to content

Instantly share code, notes, and snippets.

View mjbhobe's full-sized avatar

Manish Bhobe mjbhobe

  • Mumbai, India
  • 06:40 (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
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))
# shuffle the training dataset & set aside val_perc % of rows as validation data
for _ in range(5):
indexes = np.random.permutation(len(train_data))
# randomly sorted!
train_data = train_data[indexes]
train_labels_cat = train_labels_cat[indexes]
# now we will set-aside val_perc% of the train_data/labels as cross-validation sets
val_perc = 0.10
# convert the data into float32 types
train_data = train_data.astype('float32')
test_data = test_data.astype('float32')
# mean-normalize the image data (not the labels!)
# we take the mean & stdev of the training dataset along 4 dimensions
mean = np.mean(train_data,axis=(0,1,2,3))
std = np.std(train_data,axis=(0,1,2,3))
train_data = (train_data-mean)/(std)
# we apply the training set's mean & stdev to test dataset
from keras.datasets.cifar10 import load_data
# load the data from keras datasets library
(train_data, train_labels), (test_data, test_labels) = load_data()
print(train_data.shape, train_labels.shape, test_data.shape, test_labels.shape)
# 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