<jupyter_start><jupyter_text>Saving the model<jupyter_code>json_string = model.to_json()
open("mnist_mlp.json", "w").write(json_string)
model.save_weights("mnist_mlp.h5")<jupyter_output>[WARNING] mnist_mlp.h5 already exists - overwrite? [y/n]n<jupyter_text>Saving to redis<jupyter_code>r = redis.StrictRedis(host="localhost", port=6379, db=0)
r = redis.StrictRedis(host="redis.local.pcfdev.io", port=55098, password="<PASSWORD>")
r.info()
r.set("model", json_string)
weights = h5py.File("mnist_mlp.h5", "r")
for i in weights:
    print(i, weights[i])
# 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())
weights = r.get("weights")
with open("mnist_mlp_weights.h5", "wb") as f:
    f.write(weights)
model = model_from_json(r.get("model").decode("UTF-8"))
model.load_weights("mnist_mlp_weights.h5")
r.keys()<jupyter_output><empty_output><jupyter_text>Loading the model<jupyter_code>model = model_from_json(open("mnist_mlp.json").read())
model.load_weights("mnist_mlp.h5")
model.compile(loss="categorical_crossentropy",
              optimizer=RMSprop())<jupyter_output><empty_output><jupyter_text>Prediction<jupyter_code>y_train
plt.imshow(X_train[1].reshape(28,28), cmap="Greys_r")
plt.show()
img = Image.open("four_test.png").convert("L")
img
data = np.asarray(img, dtype="int32")
inverted_img = ImageOps.invert(img)
inverted_img
data = np.asarray(inverted_img, dtype="int32")
rescaled_data = (data/255).reshape(28,28)
plt.imshow(rescaled_data, cmap="Greys_r")
plt.show()
model.predict_classes(np.vstack([rescaled_data.reshape(-1)]))
rescaled_data.sum()
stacked_data = np.vstack([rescaled_data.reshape(-1)])
stacked_data.sum()<jupyter_output><empty_output><jupyter_text>With sketch.js<jupyter_code>img = Image.open("four.png").convert("RGBA")
img
img = np.asarray(img, dtype="int32")[:,:,3]
img = Image.fromarray(img).convert("L")
resized_image = img.resize((28,28), Image.ANTIALIAS)
resized_image = np.asarray(resized_image, dtype="int32")
# 255 * 5
Image.fromarray(resized_image * 5).convert("L")
# maybe add another rescaling factor
rescaled_data = (resized_image/255)
rescaled_data.reshape(-1).sum()
model.predict(np.vstack([rescaled_data.reshape(-1)]))
model.predict_classes(np.vstack([resized_image.reshape(-1) / 255]))<jupyter_output>1/1 [==============================] - 0s<jupyter_text>Increase the brightness<jupyter_code>resized_image = Image.fromarray(resized_image).convert("L")
brightness = ImageEnhance.Brightness(resized_image)
brightness.enhance(2)
rescaled_data = (np.asarray(brightness.enhance(2), dtype="int32") / 255)
model.predict_classes(np.vstack([rescaled_data.reshape(-1)]))
rescaled_data.reshape(-1).sum()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.18022619481940896, 'fraction_numerical': 0.026267785479751914, 'mean_word_length': 8.326530612244898, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 24, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '89663', 'license': 'MIT', 'n_tokens_mistral': 1138, 'n_tokens_neox': 1092, 'n_words': 139, 'path': 'exploration/MNIST_MLP.ipynb', 'repo_name': 'pivotalsoftware/cf-demo', 'size': '2767'}