-
Notifications
You must be signed in to change notification settings - Fork 69
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #480 from ModECI/keras_interface
Keras interface
- Loading branch information
Showing
39 changed files
with
286,427 additions
and
368 deletions.
There are no files selected for viewing
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
Binary file not shown.
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
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) |
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
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.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Oops, something went wrong.