美文网首页我爱编程
2018-03-11-How to run the traine

2018-03-11-How to run the traine

作者: 老王_5b2d | 来源:发表于2018-03-12 00:29 被阅读0次

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!

相关文章

  • 2018-03-11-How to run the traine

    The problem encountered is that the company needs to make...

  • 【论文笔记】当Bert炼丹不是玄学而是哲学:Mengzi模型

    论文标题:Mengzi: Towards Lightweight yet Ingenious Pre-traine...

  • 迁移学习:如何将预训练CNN当成特征提取器

    本文为 AI 研习社编译的技术博客,原标题: Transfer Leaning: How a Pre-Traine...

  • tips总结

    如何print一个迭代器的所有值: transfer learning 需要改的内容(1) load traine...

  • R语言20练习题【完整版】

    刘小泽重新写于2018.9.3练习题来自生信技能树jimmy,http://www.bio-info-traine...

  • run,run,run

    you are the light in my life for that, i keep chasing all...

  • Run Run Run

    这周是重新搬回0号楼的第一周,总体来说过的好不错。跑步离操场也进了,跑着更方便了。 跑步?就提上了日程安排。感觉最...

  • 03-30AI

    话说,之前人工智能研究组织OpenAI发表了新一代的语言模型GPT-3(Generative Pre-traine...

  • I will run run run

    办公室的老师们 夸完我之后 一致挥手 让我逃!! 认为 我值得拥有 更好的选择 工作和人生! 嗯 可是说是 与我心...

  • Run ,Run

    斜斜的夹着凉意的丝丝细雨落在黑漆漆的跑道上,跨上去啪嗒啪嗒带着响,提起脚,黑色的沙砾子也跟着扬到了鞋子,裤腿...

网友评论

    本文标题:2018-03-11-How to run the traine

    本文链接:https://www.haomeiwen.com/subject/hcpgfftx.html