1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

前提

すでにopenvinoが使えるものとする

tensorflowでMNISTのモデルを学習

tensorflow==1.15を使用

train.py
import tensorflow as tf
from tensorflow.keras.datasets import mnist
from tensorflow.keras.utils import to_categorical

# Load and preprocess the MNIST dataset
(x_train, y_train), (x_test, y_test) = mnist.load_data()
x_train = x_train.reshape(-1, 28, 28, 1).astype('float32') / 255.0
x_test = x_test.reshape(-1, 28, 28, 1).astype('float32') / 255.0
y_train = to_categorical(y_train, 10)
y_test = to_categorical(y_test, 10)

# Build the model
model = tf.keras.models.Sequential([
    tf.keras.layers.Conv2D(32, kernel_size=(3, 3), activation='relu', input_shape=(28, 28, 1)),
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
    tf.keras.layers.Conv2D(64, kernel_size=(3, 3), activation='relu'),
    tf.keras.layers.MaxPooling2D(pool_size=(2, 2)),
    tf.keras.layers.Flatten(),
    tf.keras.layers.Dense(128, activation='relu'),
    tf.keras.layers.Dense(10, activation='softmax')
])

# Compile and train the model
model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy'])
model.fit(x_train, y_train, batch_size=128, epochs=10, validation_data=(x_test, y_test))

# Save the model's checkpoints
checkpoint_path = "./saved_model/model_checkpoint.ckpt"
model.save_weights(checkpoint_path)

# Save the model as a frozen graph
sess = tf.keras.backend.get_session()

# Get the graph definition
output_node_names = [node.op.name for node in model.outputs]
graph_def = sess.graph.as_graph_def()

# Convert variables to constants and remove training-only operations
from tensorflow.python.framework import graph_util
frozen_graph_def = graph_util.convert_variables_to_constants(sess, graph_def, output_node_names)

# Write the frozen graph to a file
with tf.gfile.GFile("./saved_model/frozen_graph.pb", "wb") as f:
    f.write(frozen_graph_def.SerializeToString())

print("Frozen graph saved as 'frozen_graph.pb'")

IR形式に変換

$ cd  /opt/intel/openvino_2021/deployment_tools/model_optimizer/
$ python3.7 mo.py --input_model "/root/MNIST/saved_model/frozen_graph.pb" --output_dir "/root/MNIST/IR/" --input_shape [1,28,28,1]

出力

Model Optimizer arguments:
Common parameters:
        - Path to the Input Model:      /root/MNIST/saved_model/frozen_graph.pb
        - Path for generated IR:        /root/MNIST/IR/
        - IR output name:       frozen_graph
        - Log level:    ERROR
        - Batch:        Not specified, inherited from the model
        - Input layers:         Not specified, inherited from the model
        - Output layers:        Not specified, inherited from the model
        - Input shapes:         [1,28,28,1]
        - Mean values:  Not specified
        - Scale values:         Not specified
        - Scale factor:         Not specified
        - Precision of IR:      FP32
        - Enable fusing:        True
        - Enable grouped convolutions fusing:   True
        - Move mean values to preprocess section:       None
        - Reverse input channels:       False
TensorFlow specific parameters:
        - Input model in text protobuf format:  False
        - Path to model dump for TensorBoard:   None
        - List of shared libraries with TensorFlow custom layers implementation:        None
        - Update the configuration file with input/output node names:   None
        - Use configuration file used to generate the model with Object Detection API:  None
        - Use the config file:  None
Model Optimizer version:        2021.2.0-1877-176bdf51370-releases/2021/2

[ SUCCESS ] Generated IR version 10 model.
[ SUCCESS ] XML file: /root/MNIST/IR/frozen_graph.xml
[ SUCCESS ] BIN file: /root/MNIST/IR/frozen_graph.bin
[ SUCCESS ] Total execution time: 3.27 seconds.
[ SUCCESS ] Memory consumed: 263 MB.
It's been a while, check for a new version of Intel(R) Distribution of OpenVINO(TM) toolkit here https://software.intel.com/content/www/us/en/develop/tools/openvino-toolkit/choose-download.html?cid=other&source=Prod&campid=ww_2021_bu_IOTG&content=upg_pro&medium=organic_uid_agjj or on the GitHub*
1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?