In [183]:
import numpy as np
np.random.seed(1337) # for reproducibility
from keras.datasets import mnist
from keras.models import Sequential, model_from_json
from keras.layers.core import Dense, Dropout, Activation
from keras.optimizers import SGD, Adam, RMSprop
from keras.utils import np_utils
from PIL import Image, ImageOps, ImageEnhance
import matplotlib.pyplot as plt
import redis
import json
import h5py
In [184]:
batch_size = 128
nb_classes = 10
nb_epoch = 20
In [185]:
# the data, shuffled and split between train and test sets
(X_train, y_train), (X_test, y_test) = mnist.load_data()
In [186]:
X_train = X_train.reshape(60000, 784)
X_test = X_test.reshape(10000, 784)
X_train = X_train.astype("float32")
X_test = X_test.astype("float32")
X_train /= 255
X_test /= 255
print(X_train.shape[0], "train samples")
print(X_test.shape[0], "test samples")
In [187]:
# convert class vectors to binary class matrices
Y_train = np_utils.to_categorical(y_train, nb_classes)
Y_test = np_utils.to_categorical(y_test, nb_classes)
In [188]:
model = Sequential()
model.add(Dense(512, input_shape=(784,)))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(512))
model.add(Activation("relu"))
model.add(Dropout(0.2))
model.add(Dense(10))
model.add(Activation("softmax"))
In [189]:
model.summary()
In [190]:
model.compile(loss="categorical_crossentropy",
optimizer=RMSprop(),
metrics=["accuracy"])
In [191]:
history = model.fit(X_train, Y_train,
batch_size=batch_size, nb_epoch=nb_epoch,
verbose=1, validation_data=(X_test, Y_test))
In [192]:
score = model.evaluate(X_test, Y_test, verbose=0)
In [193]:
print("Test score:", score[0])
print("Test accuracy:", score[1])
In [194]:
print("Test score:", score[0])
print("Test accuracy:", score[1])
In [16]:
json_string = model.to_json()
open("mnist_mlp.json", "w").write(json_string)
model.save_weights("mnist_mlp.h5")
In [27]:
r = redis.StrictRedis(host="localhost", port=6379, db=0)
In [29]:
r = redis.StrictRedis(host="redis.local.pcfdev.io", port=55098, password="c2aa5c33-9099-4d37-a27f-fbb8af882e2d")
In [30]:
r.info()
Out[30]:
In [18]:
r.set("model", json_string)
Out[18]:
In [19]:
weights = h5py.File("mnist_mlp.h5", "r")
In [20]:
for i in weights:
print(i, weights[i])
In [21]:
# trick to save the weights to redis and the to load it on CF
with open("mnist_mlp.h5", "rb") as f:
r.set("weights", f.read())
In [22]:
weights = r.get("weights")
In [23]:
with open("mnist_mlp_weights.h5", "wb") as f:
f.write(weights)
In [24]:
model = model_from_json(r.get("model").decode("UTF-8"))
In [25]:
model.load_weights("mnist_mlp_weights.h5")
In [28]:
r.keys()
Out[28]:
In [43]:
model = model_from_json(open("mnist_mlp.json").read())
model.load_weights("mnist_mlp.h5")
model.compile(loss="categorical_crossentropy",
optimizer=RMSprop())
In [44]:
y_train
Out[44]:
In [45]:
plt.imshow(X_train[1].reshape(28,28), cmap="Greys_r")
plt.show()
In [149]:
img = Image.open("four_test.png").convert("L")
In [150]:
img
Out[150]:
In [151]:
data = np.asarray(img, dtype="int32")
In [152]:
inverted_img = ImageOps.invert(img)
In [153]:
inverted_img
Out[153]:
In [154]:
data = np.asarray(inverted_img, dtype="int32")
In [7]:
rescaled_data = (data/255).reshape(28,28)
In [48]:
plt.imshow(rescaled_data, cmap="Greys_r")
plt.show()
In [49]:
model.predict_classes(np.vstack([rescaled_data.reshape(-1)]))
Out[49]:
In [9]:
rescaled_data.sum()
Out[9]:
In [10]:
stacked_data = np.vstack([rescaled_data.reshape(-1)])
In [12]:
stacked_data.sum()
Out[12]:
In [416]:
img = Image.open("four.png").convert("RGBA")
In [417]:
img
Out[417]:
In [418]:
img = np.asarray(img, dtype="int32")[:,:,3]
In [419]:
img = Image.fromarray(img).convert("L")
In [420]:
resized_image = img.resize((28,28), Image.ANTIALIAS)
In [421]:
resized_image = np.asarray(resized_image, dtype="int32")
In [422]:
# 255 * 5
Image.fromarray(resized_image * 5).convert("L")
Out[422]:
In [423]:
# maybe add another rescaling factor
rescaled_data = (resized_image/255)
In [424]:
rescaled_data.reshape(-1).sum()
Out[424]:
In [425]:
model.predict(np.vstack([rescaled_data.reshape(-1)]))
Out[425]:
In [426]:
model.predict_classes(np.vstack([resized_image.reshape(-1) / 255]))
Out[426]:
In [427]:
resized_image = Image.fromarray(resized_image).convert("L")
In [428]:
brightness = ImageEnhance.Brightness(resized_image)
In [429]:
brightness.enhance(2)
Out[429]:
In [434]:
rescaled_data = (np.asarray(brightness.enhance(2), dtype="int32") / 255)
In [435]:
model.predict_classes(np.vstack([rescaled_data.reshape(-1)]))
Out[435]:
In [436]:
rescaled_data.reshape(-1).sum()
Out[436]:
In [ ]: