<jupyter_start><jupyter_text>Converting Models to OpenVINO IR<jupyter_code>import os
from tensorflow.python.util import deprecation
deprecation._PRINT_DEPRECATION_WARNINGS = False
import warnings

import numpy as np
import tensorflow as tf
tf.compat.v1.logging.set_verbosity(tf.compat.v1.logging.ERROR)

from tensorflow.python.framework import graph_util
from tensorflow.python.framework import graph_io
from keras.layers import *
from keras.models import load_model
import tensorflow.keras.backend as K
import shutil, sys

import torch
from pathlib import PurePath
from keras.models import model_from_json, Sequential<jupyter_output>Using TensorFlow backend.<jupyter_text>Section 1.1: Convert Keras Models<jupyter_code>vggModel = '../vgg.json'
vggWeights = '../vgg.h5'
mlpModel = '../gsbMLP.json'
mlpWeights = '../gsbMLP.h5'

jsonModelFilePath, weightsFilePath = mlpModel, mlpWeights
# jsonModelFilePath, weightsFilePath = vggModel, vggWeights

# load model
jsonModelFile = open(jsonModelFilePath, 'r' )
base_model = jsonModelFile.read()
jsonModelFile.close()
model = model_from_json(base_model)
model.load_weights(weightsFilePath)
model.compile( loss='binary_crossentropy', optimizer='sgd', metrics=[ 'accuracy' ] )
# path_to_keras = str(assets_path.joinpath('fseg-60.hdf5'))
# model = load_model(path_to_keras, custom_objects={'cross_entropy_balanced': cross_entropy_balanced})
assets_path = PurePath('.')
frozen_model_path = str(assets_path)
frozen_model_name = 'mlp_sfd.pb'
tf.get_logger().setLevel('INFO')
K.set_image_data_format('channels_last')

output_node_names = [node.op.name for node in model.outputs]
sess = K.get_session()
constant_graph = graph_util.convert_variables_to_constants(sess,
                                                           sess.graph.as_graph_def(),
                                                           output_node_names)
graph_io.write_graph(constant_graph, frozen_model_path, 
                     frozen_model_name, as_text=False)<jupyter_output><empty_output><jupyter_text>Section 1.2: Convert Tensorflow Frozen Graphs<jupyter_code># os_version = 'open_seismic:v0.0.1'
# model_optimizer_cmd = f"""
# docker run {os_version} /bin/bash executables/mo.sh -h
# """
# ! {model_optimizer_cmd}

! {"mo.py -h"}
# Model expects a 5-dimensional input
model.input.get_shape().as_list()
# phys_mnt_vol = str(PurePath(os.getcwd()))
# docker_mnt_vol = '/mnt_vol'
# docker_frozen_model_path = f'{docker_mnt_vol}/{frozen_model_name}'
# input_shape = str([1] + model.input.get_shape().as_list()[1:]).replace(" ", "")
# data_type = 'FP32'
# docker_output_model_path = f'{docker_mnt_vol}/IR_sdf/'
# ov_model_name = 'mlp_sdf'
# configs = f"--input_model {docker_frozen_model_path} \
#             --input_shape {input_shape} \
#             --data_type {data_type} \
#             --disable_nhwc_to_nchw \
#             --output_dir {docker_output_model_path}  \
#             --model_name {ov_model_name}"

# model_optimizer_docker_cmd = f"docker run -v {phys_mnt_vol}:{docker_mnt_vol} {os_version} /bin/bash executables/mo.sh {configs}"
# model_optimizer_cmd # Checking the command to see if this works
# ! {model_optimizer_docker_cmd}
phys_mnt_vol = str(PurePath(os.getcwd()))
phys_frozen_model_path = f'{phys_mnt_vol}/{frozen_model_name}'
input_shape = str([1] + model.input.get_shape().as_list()[1:]).replace(" ", "")
data_type = 'FP32'
phys_output_model_path = f'{phys_mnt_vol}/IR_sdf_local/'
ov_model_name = 'mlp_sdf'
configs = f"--input_model {phys_frozen_model_path} \
            --input_shape {input_shape} \
            --data_type {data_type} \
            --disable_nhwc_to_nchw \
            --output_dir {phys_output_model_path}  \
            --model_name {ov_model_name}"

mo_local_cmd = f"mo.py {configs}"
mo_local_cmd
! {mo_local_cmd}<jupyter_output>Model Optimizer arguments:
Common parameters:
	- Path to the Input Model: 	/home/sdp/freyes/workspace/official_openseismic/updated_open_seismic/SFD-CNN-TL/models/tensorflow_conversion/mlp_sfd.pb
	- Path for generated IR: 	/home/sdp/freyes/workspace/official_openseismic/updated_open_seismic/SFD-CNN-TL/models/tensorflow_conversion/IR_sdf_local/
	- IR output name: 	mlp_sdf
	- 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,45,45,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: 	False
	- Reverse input channels: 	False
TensorFlow specific parameters:
	- Input model in text protobuf format: 	False
	- Path to model dump for TensorBoard: 	None
	- List of[...]<jupyter_text>Testing the Model<jupyter_code>from openvino.inference_engine import IECore
xml_path = './IR_sdf_local/mlp_sdf.xml'
bin_path = './IR_sdf_local/mlp_sdf.bin'
ie = IECore()
pre_model = ie.read_network(model=xml_path, weights=bin_path)
model = ie.load_network(network=pre_model, device_name="CPU", num_requests=1)
input_layer = next(iter(pre_model.input_info))
n, c, h, w = pre_model.input_info[input_layer].input_data.shape
n, c, h, w
list(pre_model.outputs.values())[0].shape<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11445217064461567, 'fraction_numerical': 0.006201841759067845, 'mean_word_length': 4.4753086419753085, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 3}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '513978', 'license': 'MIT', 'n_tokens_mistral': 1947, 'n_tokens_neox': 1888, 'n_words': 355, 'path': 'models/tensorflow_conversion/keras2tf.ipynb', 'repo_name': 'fcr3/SFD-CNN-TL', 'size': '5321'}