Deep-Learning-Approach-for-Surface-Defect-Detection icon indicating copy to clipboard operation
Deep-Learning-Approach-for-Surface-Defect-Detection copied to clipboard

how to save pbfile

Open ZOUYIyi opened this issue 5 years ago • 12 comments

I find the function of save pb file is incompleted , I really can't make it ,wish your help , please...

ZOUYIyi avatar May 29 '19 09:05 ZOUYIyi

sorry ,I am busy those days. It's easy and You just need to modify the “name” of input and output nodes. I believe you can do it.

ShuaiLYU avatar Jun 04 '19 05:06 ShuaiLYU

yes,I have done that.it works perfectly.thank you.

---Original--- From: "Wslsdx"[email protected] Date: Tue, Jun 4, 2019 13:55 PM To: "Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection"Deep-Learning-Approach-for-Surface-Defect-Detection@noreply.github.com; Cc: "ZOUYIyi"[email protected];"Author"[email protected]; Subject: Re: [Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection] how to save pbfile (#3)

sorry ,I am busy those days. It's easy and You just need to modify the “name” of input and output nodes. I believe you can do it.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

ZOUYIyi avatar Jun 04 '19 06:06 ZOUYIyi

主要是确定输入输出节点的名字,然后按照网上的例子先做个简单的网络freeze,就可以了。

------------------ 原始邮件 ------------------ 发件人: "gulingfengze"[email protected]; 发送时间: 2019年6月11日(星期二) 上午9:19 收件人: "Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection"Deep-Learning-Approach-for-Surface-Defect-Detection@noreply.github.com; 抄送: "邹易"[email protected];"Mention"[email protected]; 主题: Re: [Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection] howto save pbfile (#3)

@ZOUYIyi Can you tell me how to implement model freeze? Thanks a lot.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ZOUYIyi avatar Jun 11 '19 01:06 ZOUYIyi

@ZOUYIyi What are the input and output node names? I'm not sure.

gulingfengze avatar Jun 11 '19 09:06 gulingfengze

input_name = "Image" output_name = "segment/Sigmoid"

------------------ 原始邮件 ------------------ 发件人: "gulingfengze"[email protected]; 发送时间: 2019年6月11日(星期二) 下午5:13 收件人: "Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection"Deep-Learning-Approach-for-Surface-Defect-Detection@noreply.github.com; 抄送: "邹易"[email protected]; "Mention"[email protected]; 主题: Re: [Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection] howto save pbfile (#3)

@ZOUYIyi What are the input and output node names? I'm not sure.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ZOUYIyi avatar Jun 11 '19 09:06 ZOUYIyi

@ZOUYIyi Thank you very much for your reply, but I still encountered some problems. I use the following script to go back to the pb file, and according to the output node name you provided, execute the script reminder: AssertionError: segment/Sigmoid is not in graph, the node name is not seen in the code, do I need to add it manually? If so, where do you add it? `import os import argparse import tensorflow as tf from tensorflow.python.framework import graph_util

def freeze_graph(model_dir, output_node_names):

if not tf.gfile.Exists(model_dir):
    raise AssertionError(
        "Export directory doesn't exists. Please specify an export "
        "directory: %s" % model_dir)

if not output_node_names:
    print("You need to supply the name of a node to --output_node_names.")
    return -1


checkpoint = tf.train.get_checkpoint_state(model_dir)
input_checkpoint = checkpoint.model_checkpoint_path


absolute_model_dir = "/".join(input_checkpoint.split('/')[:-1])
output_graph = absolute_model_dir + "/frozen_model.pb"


clear_devices = True


with tf.Session(graph=tf.Graph()) as sess:
    
    saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices)

    
    saver.restore(sess, input_checkpoint)

    
    
    
    output_graph_def = tf.graph_util.convert_variables_to_constants(
        sess,
        tf.get_default_graph().as_graph_def(), 
        output_node_names.split(",")
    ) 
    
    
    with tf.gfile.GFile(output_graph, "wb") as f:
        f.write(output_graph_def.SerializeToString())
    print("%d ops in the final graph." % len(output_graph_def.node))

if name == 'main': parser = argparse.ArgumentParser() parser.add_argument("--model_dir", type=str, default="", help="Model folder to export") parser.add_argument("--output_node_names", type=str, default="", help="The name of the output nodes, comma separated.") args = parser.parse_args() freeze_graph(args.model_dir, args.output_node_names)`

gulingfengze avatar Jun 11 '19 10:06 gulingfengze

here is my script for saving pb file:

////

def save_PbModel(self):
    input_name = "Image"
    output_name = "segment/Sigmoid"
    output_node_names = [input_name,output_name]
    print("模型保存为pb格式,输入节点name:{}输出节点name: {}".format(input_name,output_name))
    output_graph_def = tf.graph_util.convert_variables_to_constants(
    self.__session,
    self.__session.graph_def,
    output_node_names,
    )
    with tf.gfile.GFile("zy.pb", "wb") as f:
        f.write(output_graph_def.SerializeToString())
        print("=>  ops written to it")
    

/////

------------------ 原始邮件 ------------------ 发件人: "gulingfengze"[email protected]; 发送时间: 2019年6月11日(星期二) 晚上6:45 收件人: "Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection"Deep-Learning-Approach-for-Surface-Defect-Detection@noreply.github.com; 抄送: "邹易"[email protected]; "Mention"[email protected]; 主题: Re: [Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection] howto save pbfile (#3)

@ZOUYIyi Thank you very much for your reply, but I still encountered some problems. I use the following script to go back to the pb file, and according to the output node name you provided, execute the script reminder: AssertionError: segment/Sigmoid is not in graph, the node name is not seen in the code, do I need to add it manually? If so, where do you add it? `import os import argparse import tensorflow as tf from tensorflow.python.framework import graph_util

def freeze_graph(model_dir, output_node_names): """Extract the sub graph defined by the output nodes and convert all its variables into constant Args: model_dir: the root folder containing the checkpoint state file output_node_names: a string, containing all the output node's names, comma separated """ if not tf.gfile.Exists(model_dir): raise AssertionError( "Export directory doesn't exists. Please specify an export " "directory: %s" % model_dir) if not output_node_names: print("You need to supply the name of a node to --output_node_names.") return -1 checkpoint = tf.train.get_checkpoint_state(model_dir) input_checkpoint = checkpoint.model_checkpoint_path absolute_model_dir = "/".join(input_checkpoint.split('/')[:-1]) output_graph = absolute_model_dir + "/frozen_model.pb" clear_devices = True with tf.Session(graph=tf.Graph()) as sess: saver = tf.train.import_meta_graph(input_checkpoint + '.meta', clear_devices=clear_devices) saver.restore(sess, input_checkpoint) output_graph_def = tf.graph_util.convert_variables_to_constants( sess, tf.get_default_graph().as_graph_def(), output_node_names.split(",") ) with tf.gfile.GFile(output_graph, "wb") as f: f.write(output_graph_def.SerializeToString()) print("%d ops in the final graph." % len(output_graph_def.node))
if name == 'main': parser = argparse.ArgumentParser() parser.add_argument("--model_dir", type=str, default="", help="Model folder to export") parser.add_argument("--output_node_names", type=str, default="", help="The name of the output nodes, comma separated.") args = parser.parse_args() freeze_graph(args.model_dir, args.output_node_names)`

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

ZOUYIyi avatar Jun 11 '19 10:06 ZOUYIyi

@ZOUYIyi Thank you very much for your help. I modified my model freezing script, successfully exported pb file, and tested well on my test script.

gulingfengze avatar Jun 24 '19 08:06 gulingfengze

@gulingfengze Could you please send me the freezing script and test script?I am new here. and I want to build an interface , the input is an image and out put is the predicted label

HuitMahoon avatar Jan 20 '20 03:01 HuitMahoon

@HuitMahoon I'm happy to do it all, freeze script test scrip

gulingfengze avatar Jan 21 '20 02:01 gulingfengze

@HuitMahoon I'm happy to do it all, freeze script test scrip

@gulingfengze @HuitMahoon 请问你们在自己数据上面跑过吗,能否分享你们对数据标注和结果预测的经验?感谢

bigbigxing823 avatar Mar 10 '20 04:03 bigbigxing823

input_name = "Image" output_name = "segment/Sigmoid" ------------------ 原始邮件 ------------------ 发件人: "gulingfengze"[email protected]; 发送时间: 2019年6月11日(星期二) 下午5:13 收件人: "Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection"Deep-Learning-Approach-for-Surface-Defect-Detection@noreply.github.com; 抄送: "邹易"[email protected]; "Mention"[email protected]; 主题: Re: [Wslsdx/Deep-Learning-Approach-for-Surface-Defect-Detection] howto save pbfile (#3) @ZOUYIyi What are the input and output node names? I'm not sure. — You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

input_name = 'Image', output_name = "segment/Sigmoid" , the globalvariables are freezed, but when the pb files is loaded with opencv, readNetFromTensorflow, error happens( something like "can not detetmine the number of batchnorm layers")

jinlongfabxlab avatar Jul 14 '20 07:07 jinlongfabxlab