Skip to content

Commit

Permalink
Fix Mnist example running in two browsers at the same time (streamlit…
Browse files Browse the repository at this point in the history
…#216)

* update mnist demo to use TF 2.0 api
* upgrade tensorflow to 2.0.0
  • Loading branch information
jrhone authored Oct 1, 2019
1 parent bf8e883 commit 8d7f4a1
Show file tree
Hide file tree
Showing 4 changed files with 311 additions and 185 deletions.
30 changes: 16 additions & 14 deletions examples/mnist-cnn.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,27 +24,27 @@
import streamlit as st
from streamlit import config

from keras.datasets import mnist
from keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten
from keras.models import Sequential
from keras.optimizers import SGD
from tensorflow.keras.datasets import mnist
from tensorflow.keras.layers import Conv2D, MaxPooling2D, Dropout, Dense, Flatten
from tensorflow.keras.models import Sequential
from tensorflow.keras.optimizers import SGD
from keras.utils import np_utils
import keras
from tensorflow import keras
import math
import numpy as np
import pandas as pd
import time

# https://kobkrit.com/using-allow-growth-memory-option-in-tensorflow-and-keras-dc8c8081bc96
from keras.backend.tensorflow_backend import set_session
import tensorflow as tf

tf_config = tf.ConfigProto()
# dynamically grow the memory used on the GPU
# this option is fine on non gpus as well.
tf_config = tf.compat.v1.ConfigProto()
tf_config.gpu_options.allow_growth = True
tf_config.log_device_placement = True
set_session(tf.Session(config=tf_config))

# https://kobkrit.com/using-allow-growth-memory-option-in-tensorflow-and-keras-dc8c8081bc96
tf.compat.v1.keras.backend.set_session(tf.compat.v1.Session(config=tf_config))


class MyCallback(keras.callbacks.Callback):
Expand All @@ -67,10 +67,12 @@ def on_epoch_begin(self, epoch, logs=None):

def on_batch_end(self, batch, logs=None):
rows = pd.DataFrame(
[[logs["loss"], logs["accuracy"]]], columns=["loss", "accuracy"])
[[logs["loss"], logs["accuracy"]]], columns=["loss", "accuracy"]
)
if batch % 10 == 0:
self._epoch_chart.add_rows({"loss": [logs["loss"]],
"accuracy": [logs["accuracy"]]})
self._epoch_chart.add_rows(
{"loss": [logs["loss"]], "accuracy": [logs["accuracy"]]}
)
if batch % 100 == 99:
self._summary_chart.add_rows(rows)
percent_complete = logs["batch"] * logs["size"] / self.params["samples"]
Expand All @@ -96,6 +98,7 @@ def on_epoch_end(self, epoch, logs=None):
% {"epoch": epoch, "summary": summary}
)


st.title("MNIST CNN")

(x_train, y_train), (x_test, y_test) = mnist.load_data()
Expand Down Expand Up @@ -134,8 +137,7 @@ def on_epoch_end(self, epoch, logs=None):
model.add(Dense(8, activation="relu"))
model.add(Dense(num_classes, activation="softmax"))

model.compile(
loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])
model.compile(loss="categorical_crossentropy", optimizer=sgd, metrics=["accuracy"])

show_terminal_output = not config.get_option("server.liveSave")
model.fit(
Expand Down
2 changes: 1 addition & 1 deletion lib/Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ bokeh = "*"
graphviz = "*"
parameterized = "*"
pydot = "*"
tensorflow = "*"
tensorflow = ">=2.0.0"
seaborn = "*"
prometheus-client = "*"
opencv-python = "*"
Expand Down
Loading

0 comments on commit 8d7f4a1

Please sign in to comment.