Skip to content

Commit

Permalink
add examples cnn and onnx configs simple dnn, correct plotting layout
Browse files Browse the repository at this point in the history
  • Loading branch information
nprouvost committed Apr 18, 2024
1 parent a8c58c1 commit f5a48f2
Show file tree
Hide file tree
Showing 45 changed files with 19,830 additions and 20 deletions.
3 changes: 0 additions & 3 deletions examples/cnn/conv_2d_inputs.pb

This file was deleted.

101 changes: 101 additions & 0 deletions examples/cnn/create_small_cnn.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
# coding: utf-8

"""
Test script to create a simple DNN model.
Best to be executed in a CMSSW environment with TensorFlow and cmsml installed.
Will install tf2onnx for the user if not present in environment.
Signature: f32(64) -> f32(8)
"""

import os
import subprocess
import importlib.util

import cmsml


def create_model(
model_dir: str,
postfix: str = r"l{n_layers}k{kernel_size}f{n_filters}",
n_in_1: int = 28,
n_in_2: int = 28,
n_out: int = 8,
n_layers: int = 1,
kernel_size: int = 3,
n_filters: int = 4,
batch_norm: bool = True,
pooling: bool = True,
) -> None:
# get tensorflow
tf, _, tf_version = cmsml.tensorflow.import_tf()
print("creating simple cnn model")
print(f"location : {model_dir}")
print(f"TF version: {'.'.join(map(str, tf_version))}")

# set random seeds to get deterministic results for testing
tf.keras.utils.set_random_seed(1)

# define input layer
x = tf.keras.Input(shape=(n_in_1, n_in_2, 1), dtype=tf.float32, name="input")

# model layers
a = tf.keras.layers.BatchNormalization(axis=1, renorm=True)(x) if batch_norm else x
for _ in range(n_layers):
a = tf.keras.layers.Conv2D(n_filters, kernel_size, padding="same", activation="elu")(a)
if pooling:
a = tf.keras.layers.MaxPooling2D(pool_size=(2, 2), strides=(1, 1), padding="same")(a)
if batch_norm:
a = tf.keras.layers.BatchNormalization(axis=1, renorm=True)(a)

# output layer
b = tf.keras.layers.Flatten()(a)
y = tf.keras.layers.Dense(n_out, activation="softmax", name="output", dtype=tf.float32)(b)

# define the model
model = tf.keras.Model(inputs=[x], outputs=[y])

# test evaluation
print(model([tf.constant([[[[i] for i in range(n_in_2)] for _ in range(n_in_1)]], dtype=tf.float32)]))

# save it as a frozen graph
_postfix = postfix.format(n_in_1=n_in_1, n_in_2=n_in_2, n_out=n_out, n_layers=n_layers, kernel_size=kernel_size,
n_filters=n_filters, batch_norm=batch_norm)
cmsml.tensorflow.save_graph(
os.path.join(model_dir, f"frozen_graph_{_postfix}.pb"),
model,
variables_to_constants=True,
)

# create a SavedModel
tf.saved_model.save(
model,
os.path.join(model_dir, f"saved_model_{_postfix}"),
)

# convert SavedModel to onnx
# install tf2onnx if necessary
if importlib.util.find_spec("tf2onnx") is None:
subprocess.run("pip3 install --user tf2onnx", shell=True)

# convert
subprocess.run(
f"""
python3 -m tf2onnx.convert \
--saved-model saved_model_{_postfix} \
--output onnx_graph_{_postfix}.onnx \
""",
shell=True,
)


def main():
this_dir = os.path.dirname(os.path.abspath(__file__))
create_model(this_dir, n_layers=1, kernel_size=1)
create_model(this_dir, n_layers=1, kernel_size=3)
create_model(this_dir, n_layers=5, kernel_size=1)
create_model(this_dir, n_layers=5, kernel_size=3)


if __name__ == "__main__":
main()
3 changes: 3 additions & 0 deletions examples/cnn/frozen_graph_l1k1f4.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/frozen_graph_l1k3f4.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/frozen_graph_l5k1f4.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/frozen_graph_l5k3f4.pb
Git LFS file not shown
11 changes: 11 additions & 0 deletions examples/cnn/model_onnx_l1k1f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model:
name: simple_cnn_onnx
label: ONNX 1x4x1
version: "1.0.0"
inference_engine: onnx
file: ./onnx_graph_l1k1f4.onnx
inputs:
- name: input
shape: [28, 28, 1]
outputs:
- name: output
11 changes: 11 additions & 0 deletions examples/cnn/model_onnx_l1k3f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model:
name: simple_cnn_onnx
label: ONNX 1x4x3
version: "1.0.0"
inference_engine: onnx
file: ./onnx_graph_l1k3f4.onnx
inputs:
- name: input
shape: [28,28,1]
outputs:
- name: output
11 changes: 11 additions & 0 deletions examples/cnn/model_onnx_l5k1f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model:
name: simple_cnn_onnx
label: ONNX 5x4x1
version: "1.0.0"
inference_engine: onnx
file: ./onnx_graph_l5k1f4.onnx
inputs:
- name: input
shape: [28,28,1]
outputs:
- name: output
11 changes: 11 additions & 0 deletions examples/cnn/model_onnx_l5k3f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model:
name: simple_cnn_onnx
label: ONNX 5x4x3
version: "1.0.0"
inference_engine: onnx
file: ./onnx_graph_l5k3f4.onnx
inputs:
- name: input
shape: [28,28,1]
outputs:
- name: output
13 changes: 0 additions & 13 deletions examples/cnn/model_tf.yaml

This file was deleted.

11 changes: 11 additions & 0 deletions examples/cnn/model_tf_l1k1f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model:
name: simple_cnn_tf
label: TF 1x4x1
version: "1.0.0"
inference_engine: tf
file: ./frozen_graph_l1k1f4.pb
inputs:
- name: input
shape: [28,28,1]
outputs:
- name: Identity
11 changes: 11 additions & 0 deletions examples/cnn/model_tf_l1k3f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model:
name: simple_cnn_tf
label: TF 1x4x3
version: "1.0.0"
inference_engine: tf
file: ./frozen_graph_l1k3f4.pb
inputs:
- name: input
shape: [28,28,1]
outputs:
- name: Identity
11 changes: 11 additions & 0 deletions examples/cnn/model_tf_l5k1f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model:
name: simple_cnn_tf
label: TF 5x4x1
version: "1.0.0"
inference_engine: tf
file: ./frozen_graph_l5k1f4.pb
inputs:
- name: input
shape: [28,28,1]
outputs:
- name: Identity
11 changes: 11 additions & 0 deletions examples/cnn/model_tf_l5k3f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
model:
name: simple_cnn_tf
label: TF 5x4x3
version: "1.0.0"
inference_engine: tf
file: ./frozen_graph_l5k3f4.pb
inputs:
- name: input
shape: [28,28,1]
outputs:
- name: Identity
12 changes: 12 additions & 0 deletions examples/cnn/model_tfaot_l1k1f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
model:
name: simple_cnn_tfaot
label: AOT 1x4x1
version: "1.0.0"
inference_engine: tfaot
saved_model: ./saved_model_l1k1f4
serving_key: serving_default

compilation:
batch_sizes: [1]
tf_xla_flags: []
xla_flags: []
12 changes: 12 additions & 0 deletions examples/cnn/model_tfaot_l1k3f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
model:
name: simple_cnn_tfaot
label: AOT 1x4x3
version: "1.0.0"
inference_engine: tfaot
saved_model: ./saved_model_l1k3f4
serving_key: serving_default

compilation:
batch_sizes: [1]
tf_xla_flags: []
xla_flags: []
12 changes: 12 additions & 0 deletions examples/cnn/model_tfaot_l5k1f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
model:
name: simple_cnn_tfaot
label: AOT 5x4x1
version: "1.0.0"
inference_engine: tfaot
saved_model: ./saved_model_l5k1f4
serving_key: serving_default

compilation:
batch_sizes: [1]
tf_xla_flags: []
xla_flags: []
12 changes: 12 additions & 0 deletions examples/cnn/model_tfaot_l5k3f4.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
model:
name: simple_cnn_tfaot
label: AOT 5x4x3
version: "1.0.0"
inference_engine: tfaot
saved_model: ./saved_model_l5k3f4
serving_key: serving_default

compilation:
batch_sizes: [1]
tf_xla_flags: []
xla_flags: []
12 changes: 12 additions & 0 deletions examples/cnn/model_tfaot_l5k3f4_bu.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
model:
name: simple_cnn_tfaot
label: AOT 5x4x3 BS
version: "1.0.0"
inference_engine: tfaot
saved_model: ./saved_model_l5k3f4
serving_key: serving_default

compilation:
batch_sizes: [1, 2, 4, 8, 16, 32, 64, 128, 256]
tf_xla_flags: []
xla_flags: []
Binary file added examples/cnn/onnx_graph_l1k1f4.onnx
Binary file not shown.
Binary file added examples/cnn/onnx_graph_l1k3f4.onnx
Binary file not shown.
Binary file added examples/cnn/onnx_graph_l5k1f4.onnx
Binary file not shown.
Binary file added examples/cnn/onnx_graph_l5k3f4.onnx
Binary file not shown.
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l1k1f4/fingerprint.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l1k1f4/saved_model.pb
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l1k1f4/variables/variables.index
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l1k3f4/fingerprint.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l1k3f4/saved_model.pb
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l1k3f4/variables/variables.index
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l5k1f4/fingerprint.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l5k1f4/saved_model.pb
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l5k1f4/variables/variables.index
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l5k3f4/fingerprint.pb
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l5k3f4/saved_model.pb
Git LFS file not shown
Git LFS file not shown
3 changes: 3 additions & 0 deletions examples/cnn/saved_model_l5k3f4/variables/variables.index
Git LFS file not shown
Binary file added examples/simple_dnn/onnx_graph_l10u128.onnx
Binary file not shown.
Binary file added examples/simple_dnn/onnx_graph_l10u256.onnx
Binary file not shown.
Loading

0 comments on commit f5a48f2

Please sign in to comment.