Last active
April 3, 2020 09:34
-
-
Save smeschke/e486ce21a7d88c8d3672e5d81926328f to your computer and use it in GitHub Desktop.
predict juggling patterns using keras
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
import numpy as np, os, cv2 | |
from keras.utils import to_categorical | |
from keras.models import load_model | |
#reads images from folder (images must be labeled 0.png, 1.png, etc...) | |
def read_from_folder(folder, pattern, image_number, stop): | |
images = [] | |
while image_number < stop: | |
path = folder + pattern + '/' + str(image_number) + '.png' | |
img = cv2.imread(path, 0) | |
images.append(img) | |
image_number+=1 | |
return images | |
#flattens a list of images, returns an array range 0,1 | |
def flatten(dimData, images): | |
images = np.array(images) | |
images = images.reshape(len(images), dimData) | |
images = images.astype('float32') | |
images /=255 | |
return images | |
#-------------------get training data and training labels--------------- | |
train_start, train_stop = 120,800 #only 4GB RAM :( | |
test_start, test_stop = 800,1200 | |
folder = '/home/stephen/Desktop/juggling/' | |
#the perprocessed images are stored in 5 folders | |
patterns = ['cascade', '423', 'columns', '2inlh', '2inrh'] | |
#Training Images | |
train_images = [] | |
for pattern in patterns: train_images += read_from_folder(folder, pattern, train_start, train_stop) | |
#image dimentions | |
h,w = train_images[0].shape | |
dimData = np.prod(h*w) | |
#list of images --> array of flattened iamges | |
train_data = flatten(dimData, train_images) | |
#make training_labels | |
train_labels = [] | |
for pattern in patterns: | |
for i in range(train_stop - train_start): train_labels.append(patterns.index(pattern)) | |
train_labels = np.array(train_labels) | |
# integer --> categorical data | |
train_labels_one_hot = to_categorical(train_labels) | |
#-----------------get testing data and testing labels------------------- | |
#Test images | |
test_images = [] | |
for pattern in patterns: test_images += read_from_folder(folder, pattern, test_start, test_stop) | |
#list of images --> array of flattened iamges | |
test_data = flatten(dimData, test_images) | |
#make test_labels | |
test_labels = [] | |
for pattern in patterns: | |
for i in range(test_stop - test_start): test_labels.append(patterns.index(pattern)) | |
test_labels = np.array(test_labels) | |
# integer --> categorical data | |
test_labels_one_hot = to_categorical(test_labels) | |
#------------------make keras model-------------------------------- | |
# Find the unique numbers from the train labels | |
classes = np.unique(train_labels) | |
nClasses = len(classes) | |
from keras.models import Sequential | |
from keras.layers import Dense | |
model = Sequential() | |
model.add(Dense(512, activation='relu', input_shape=(dimData,))) | |
model.add(Dense(512, activation='relu')) | |
model.add(Dense(nClasses, activation='softmax')) | |
model.compile(optimizer='rmsprop', loss='categorical_crossentropy', metrics=['accuracy']) | |
#fit model - this is where the magic happens | |
history = model.fit(train_data, train_labels_one_hot, batch_size=256, epochs=2, verbose=1, | |
validation_data=(test_data, test_labels_one_hot)) | |
#test model | |
[test_loss, test_acc] = model.evaluate(test_data, test_labels_one_hot) | |
print("Evaluation result on Test Data : Loss = {}, accuracy = {}".format(test_loss, test_acc)) | |
#save model | |
model.save('/home/stephen/Desktop/juggling/my_model.h5') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment