Skip to content

Instantly share code, notes, and snippets.

Last active June 18, 2018 09:46
What would you like to do?
pre-process the data in order to be ready for Keras model
from keras.utils import np_utils
# Training and validation files
files = ['training/train-y', 'training/train-x',
'validation/test-y', 'validation/test-x']
# Load training labels
with open(input_path+files[0], 'rb') as lbpath:
y_train = pickle.load(lbpath, encoding='bytes')
# Load training samples
with open(input_path+files[1], 'rb') as imgpath:
x_train = pickle.load(imgpath, encoding='bytes')
# Load validation labels
with open(input_path+files[2], 'rb') as lbpath:
y_test = pickle.load(lbpath, encoding='bytes')
# Load validation samples
with open(input_path+files[3], 'rb') as imgpath:
x_test = pickle.load(imgpath, encoding='bytes')
# Transofrm them to a float32 type
x_train = x_train.astype('float32')
x_test = x_test.astype('float32')
# Normalize the input
x_train /= 255
x_test /= 255
# One-hot Encoding
num_classes = 10
y_train = np_utils.to_categorical(y_train, num_classes)
y_test = np_utils.to_categorical(y_test, num_classes)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment