2018-03-11-How to run the traine
The problem encountered is that the company needs to make online predictions of the trained model, which i trained with Linux & Keras. In order to make a faster calibration, the original image from the fisheye camera will be calibrated and prepossessed on the Halcon software, which must be called in Windows platform.
The C# interface should be designed to connect Halcon (Calibration & Image Preprocessing), axis motor and the network.
Now it is necessary to add the trained model to the C# interface.
reference,Tensorflow win10 c++ run trained model from python。
It is mainly divided into the following three steps.
1.formerly the library function of Keras model.save(filepath) be used to save the model。now the model should be saved as model of Tensorflow。
2.change the variables of the model into constants,for example use “freeze graph” to export the model as a file, so that we can call the model in c++。
3.Load trained tensorflow model in windows environment using tensorflow's c++ interface。
Solution of 1.
Referencekeras with tensorflow Hybrid Programming。
import tensorflow as tf
from keras import backend as K
from keras.layers import Dense
from keras.objectives import categorical_crossentropy
from keras.metrics import categorical_accuracy as accuracy
from tensorflow.examples.tutorials.mnist import input_data
sess = tf.Session()
K.set_session(sess)
#Create a TensorFlow session and sign up for Keras.
with tf.name_scope('input'):
# this place holder is the same with input layer in keras
img = tf.placeholder(tf.float32, shape=(None, 784))
labels = tf.placeholder(tf.float32, shape=(None, 10))
mnist_data = input_data.read_data_sets('MNIST_data', one_hot=True)
def feed_dict(train):
if train:
xs, ys = mnist_data.train.next_batch(50)
else:
xs, ys = mnist_data.test.images, mnist_data.test.labels
#not suit for my case, input should be changed as keras input
return {img:xs, labels:ys}# keras layers can be called on tensorflow tensors
with tf.name_scope('NN'):
#before we call the keras function, we should add with tf.name_scope
x = Dense(128, activation='relu')(img)
x = Dense(128, activation='relu')(x)
preds = Dense(10, activation='softmax')(x)
with tf.name_scope('loss'):
loss = tf.reduce_mean(categorical_crossentropy(labels, preds))
# do it in master thesis!
writer = tf.summary.FileWriter('./keras_tensorflow_log/')
outloss = tf.summary.scalar('loss', loss)
merged = tf.summary.merge([outloss])with tf.name_scope('train'):
train_step = tf.train.GradientDescentOptimizer(0.5).minimize(loss)
# initialize all variables
init_op = tf.global_variables_initializer()
sess.run(init_op)with sess.as_default():
for i in range(1000):
summary, loss = sess.run([merged, train_step],
feed_dict=feed_dict(True))
writer.add_summary(summary, global_step=i)
writer.close()
reference githubdirect export keras into tensorflow。
save keras model as Binary model of tensorflow
best way!not only convert the weights of keras model, but alsoconvert_variables_to_constants,that means frezze weights。
# coding=utf-8
import sys
from keras.models import load_model
import tensorflow as tf
import os
import os.path as osp
from keras import backend as K
def freeze_session(session, keep_var_names=None, output_names=None, clear_devices=True):
"""
Freezes the state of a session into a prunned computation graph.
Creates a new computation graph where variable nodes are replaced by
constants taking their current value in the session. The new graph will be
prunned so subgraphs that are not neccesary to compute the requested
outputs are removed.
@param session The TensorFlow session to be frozen.
@param keep_var_names A list of variable names that should not be frozen,
or None to freeze all the variables in the graph.
@param output_names Names of the relevant graph outputs.
@param clear_devices Remove the device directives from the graph for better portability.
@return The frozen graph definition.
"""
from tensorflow.python.framework.graph_util import convert_variables_to_constants
graph = session.graph
with graph.as_default():
freeze_var_names = list(set(v.op.name for v in tf.global_variables()).difference(keep_var_names or []))
output_names = output_names or []
output_names += [v.op.name for v in tf.global_variables()]
input_graph_def = graph.as_graph_def()
if clear_devices:
for node in input_graph_def.node:
node.device = ""
frozen_graph = convert_variables_to_constants(session, input_graph_def,
output_names, freeze_var_names)
return frozen_graph
input_fld = sys.path[0]
weight_file = 'your_model.h5'
output_graph_name = 'tensor_model.pb'
output_fld = input_fld + '/tensorflow_model/'
if not os.path.isdir(output_fld):
os.mkdir(output_fld)
weight_file_path = osp.join(input_fld, weight_file)
K.set_learning_phase(0)
net_model = load_model(weight_file_path)
print('input is :', net_model.input.name)
print ('output is:', net_model.output.name)
sess = K.get_session()
frozen_graph = freeze_session(K.get_session(), output_names=[net_model.output.op.name])
from tensorflow.python.framework import graph_io
graph_io.write_graph(frozen_graph, output_fld, output_graph_name, as_text=False)
print('saved the constant graph (ready for inference) at: ', osp.join(output_fld, output_graph_name))
Solution of 2.
already found in 1!
Solution of 3.
reference githubinstall tensorflow in windows。
at least we need python 3.5 64X or python 3.6 64X in windows
so at first, we need install the python 3.5 + tensorflow (maybe need git in windows)
then we should find a C++ Tensorflow interface or C++ TF API, so that TF model could be called in C++.
to be continued!