Skip to content

Commit

Permalink
Merge pull request #480 from ModECI/keras_interface
Browse files Browse the repository at this point in the history
Keras interface
  • Loading branch information
pgleeson authored Sep 15, 2023
2 parents 3f71c5a + 3b24a67 commit 91855d5
Show file tree
Hide file tree
Showing 39 changed files with 286,427 additions and 368 deletions.
9 changes: 9 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -278,6 +278,7 @@ Thumbs.db #thumbnail cache on Windows
/examples/NeuroML/arm64
/examples/MDF/RNN/iaf.net
/examples/MDF/RNN/iaf.net2
/examples/TensorFlow/Keras/keras_to_MDF
/examples/NeuroML/PyNN/All*
/examples/NeuroML/PyNN/*dat
/examples/NeuroML/PyNN/*1.png
Expand All @@ -288,6 +289,10 @@ Thumbs.db #thumbnail cache on Windows
/examples/NeuroML/PyNN/*.mdf
/examples/NeuroML/PyNN/*.spikes
*_code.gen.c
examples/TensorFlow/Keras/Keras_to_MDF_IRIS/keras_to_MDF
examples/TensorFlow/Keras/keras_to_MDF
/examples/TensorFlow/Keras/Keras_MDF/Keras_to_MDF_IRIS/keras_to_MDF
/examples/TensorFlow/Keras/Keras_MDF/keras_to_MDF
/examples/NeuroML/PyNN/HH.yaml
/examples/NeuroML/PyNN/InputWeights.yaml
/examples/NeuroML/PyNN/Net1.yaml
Expand All @@ -298,3 +303,7 @@ Thumbs.db #thumbnail cache on Windows
/examples/NeuroML/PyNN/SimSimpleNet.yaml
/examples/NeuroML/PyNN/SimpleNet.yaml
/examples/NeuroML/PyNN/SimInputWeights.yaml
/examples/TensorFlow/Keras/IRIS/keras_to_MDF
/examples/TensorFlow/Keras/MNIST/keras_to_MDF
/examples/TensorFlow/Keras/MNIST/keras_to_MDF.1
/examples/TensorFlow/Keras/IRIS/keras_to_MDF.1
Binary file removed examples/TensorFlow/Keras/3.jpeg
Binary file not shown.
34 changes: 34 additions & 0 deletions examples/TensorFlow/Keras/IRIS/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# Keras to MDF example: IRIS dataset

**For more details on Keras and the current state of the Keras->MDF mapping see the [MNIST example](../MNIST).**

This model uses the [IRIS dataset](https://en.wikipedia.org/wiki/Iris_flower_data_set) in the [trained Keras model](keras_model.py) and the MDF equivalent.

### Summarize Model

Below is the summary image of the trained Keras model. We can clearly see the output shape and number of weights in each layer:

![summary](summary.png)


### Keras Model

Visualization of the model from Keras:

<p align="center"><img src="model_on_iris_plot.png"/></p>
<br>

### MDF Model

Graphviz is used to generate visualization for the MDF graph. Below is the visualization of the MDF graph after converting the keras model to MDF.

![keras_to_MDF](keras_to_MDF.1.png)

More detailed graphical representation of the MDF:

<p align="center"><img src="keras_to_MDF.png" width="400"/></p>

##### Netron
Below is the visualization of this model using netron

![keras-model-to-netron](layers_netron.png)
91 changes: 91 additions & 0 deletions examples/TensorFlow/Keras/IRIS/keras_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import sys
import tensorflow as tf

# tf.__version__

from keras.layers import Dense
from keras.utils.vis_utils import plot_model
from keras.models import Sequential

# from keras_visualizer import visualizer
from keras import layers

import matplotlib.pyplot as plt
import numpy as np

import matplotlib.pyplot as plt
import numpy as np

from sklearn.datasets import load_iris
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import StandardScaler

# Controlling randomness, to facilitate testing/reproducibility
# based on https://keras.io/examples/keras_recipes/reproducibility_recipes/
from keras.utils import set_random_seed

set_random_seed(1234)
tf.config.experimental.enable_op_determinism()

print("Loading data")
iris = load_iris()

X, y = iris.data, iris.target

X_train, X_test, y_train, y_test = train_test_split(
X, y, test_size=0.2, random_state=42
)

# standardize the data
scaler = StandardScaler()
X_train = scaler.fit_transform(X_train)
X_test = scaler.fit_transform(X_test)


# Build Model
print("Building the Model")
model = tf.keras.models.Sequential()
model.add(
tf.keras.layers.Dense(32, activation=tf.nn.relu, input_dim=4, name="first_layer")
) # hidden layers
model.add(tf.keras.layers.Dense(64, activation=tf.nn.relu, name="second_layer"))
model.add(tf.keras.layers.Dense(64, activation=tf.nn.relu, name="third_layer"))
model.add(
tf.keras.layers.Dense(3, activation=tf.nn.softmax, name="fourth_layer")
) # output layer

# Compile Model
print("Compiling the model")
model.compile(
optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"]
)

# train the model
print("Training the Model")
model.fit(X_train, y_train, epochs=3)


# check if the model actually generalize the data.
print("Accuracy of our model is:")
val_loss, val_acc = model.evaluate(X_test, y_test)
print(val_loss, val_acc)


# Print summary
model.summary()

# plot the model
print("Plotting the model")
plot_model(
model, to_file="model_on_iris_plot.png", show_shapes=True, show_layer_names=True
)


# Saving model in h5 for Ploting Nuetron visual
model.save("keras_model_on_iris.h5")


# predict example for index 0
print("Predict value at index 0:")
predictions = model.predict([X_test])
print("The predicted number at index 0 is", np.argmax(predictions[0]))
Binary file not shown.
Binary file added examples/TensorFlow/Keras/IRIS/keras_to_MDF.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 91855d5

Please sign in to comment.