Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于moilefacenet的pb模型转换成tf.lite #14

Open
kongdaniel opened this issue Apr 24, 2020 · 16 comments
Open

关于moilefacenet的pb模型转换成tf.lite #14

kongdaniel opened this issue Apr 24, 2020 · 16 comments

Comments

@kongdaniel
Copy link

kongdaniel commented Apr 24, 2020

你好请问你的pb模型是这么转换到lite,模型的,我现在在tf的1.80,1.12-1.15都试过,但总是转不了,请问你当时是怎么转换的呢。

@syaringan357
Copy link
Owner

syaringan357 commented Apr 26, 2020

import tensorflow as tf

in_path = "../output/mobilefacenet/frozen_graphs/MobileFaceNet.pb"
out_path = "../output/mobilefacenet/tflite/MobileFaceNet.tflite"

input_tensor_name = ["input"]
input_tensor_shape = {"input": [2, 112, 112, 3]}
classes_tensor_name = ["embeddings"]

converter = tf.lite.TFLiteConverter.from_frozen_graph(in_path, input_tensor_name,
classes_tensor_name, input_shapes=input_tensor_shape)
tflite_model = converter.convert()

with open(out_path, "wb") as f:
f.write(tflite_model)

@kongdaniel
Copy link
Author

我用你的代码依然不行,和版本有关系吗。我是1.15现在
2020-04-26 14:16:25.685949: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 1484 operators, 2717 arrays (0 quantized)
2020-04-26 14:16:25.696619: 他报这个错F .\tensorflow/lite/toco/toco_tooling.h:38] Check failed: s.ok() Found BatchNormalization as non-selected output from Switch, but only Merge supported. Control flow ops like Switch and Merge are not generally supported. We are working on fixing this, please see the Github issue at tensorflow/tensorflow#28485.
Fatal Python error: Aborted`

@kongdaniel
Copy link
Author

kongdaniel commented Apr 26, 2020 via email

@syaringan357
Copy link
Owner

哦,我不是用他原来那个pb转的,用他的ckpt重新固化一个pb,再转。

@syaringan357
Copy link
Owner

syaringan357 commented Apr 27, 2020

import os
import tensorflow as tf
from nets.MobileFaceNet import inference

training_checkpoint = "../output/mobilefacenet/MobileFaceNet_TF.ckpt"
OUTPUT_DIR = '../output/mobilefacenet/frozen_graphs'

def freeze_graph_def(sess, output_node_names):
# Replace all the variables in the graph with constants of the same values
output_graph_def = tf.compat.v1.graph_util.convert_variables_to_constants(
sess, sess.graph_def, output_node_names.split(","))
return output_graph_def

def save():
data_input = tf.placeholder(name='input', dtype=tf.float32, shape=[None, 112, 112, 3])

output, _ = inference(data_input, bottleneck_layer_size=192)
tf.identity(output, name='embeddings')
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)

    saver = tf.train.Saver()
    saver.restore(sess, training_checkpoint)

    # Freeze the graph def
    output_graph_def = freeze_graph_def(sess, 'embeddings')

    output_pnet = os.path.join(OUTPUT_DIR, 'MobileFaceNet.pb')
    # Serialize and dump the output graph to the filesystem
    with tf.gfile.GFile(output_pnet, 'wb') as f:
        f.write(output_graph_def.SerializeToString())

if name == 'main':
save()

@syaringan357
Copy link
Owner

凑合看把 上面都是代码

@kongdaniel
Copy link
Author

凑合看把 上面都是代码

谢谢大佬,跪谢。

@kongdaniel
Copy link
Author

import os
import tensorflow as tf
from nets.MobileFaceNet import inference

training_checkpoint = "../output/mobilefacenet/MobileFaceNet_TF.ckpt"
OUTPUT_DIR = '../output/mobilefacenet/frozen_graphs'

def freeze_graph_def(sess, output_node_names):

Replace all the variables in the graph with constants of the same values

output_graph_def = tf.compat.v1.graph_util.convert_variables_to_constants(
sess, sess.graph_def, output_node_names.split(","))
return output_graph_def

def save():
data_input = tf.placeholder(name='input', dtype=tf.float32, shape=[None, 112, 112, 3])

output, _ = inference(data_input, bottleneck_layer_size=192)
tf.identity(output, name='embeddings')
init = tf.global_variables_initializer()
with tf.Session() as sess:
    sess.run(init)

    saver = tf.train.Saver()
    saver.restore(sess, training_checkpoint)

    # Freeze the graph def
    output_graph_def = freeze_graph_def(sess, 'embeddings')

    output_pnet = os.path.join(OUTPUT_DIR, 'MobileFaceNet.pb')
    # Serialize and dump the output graph to the filesystem
    with tf.gfile.GFile(output_pnet, 'wb') as f:
        f.write(output_graph_def.SerializeToString())

if name == 'main':
save()

大哥,再问个问题,就是这一句output, _ = inference(data_input, bottleneck_layer_size=192)
为啥这里是192,我看他代码里写的都是128,所以说他默认的参数应该是128呀,但是,我用128转换他居然说要求192,这是啥原因。

@syaringan357
Copy link
Owner

我也不太清楚,我也是按报错提示改的。可能是他一些代码经过长时间修改之后对不上了。

@kongdaniel
Copy link
Author

kongdaniel commented May 6, 2020 via email

@yp19940913
Copy link

你好,请问这个问题解决了吗?方便说一下解决方法嘛?多谢

@kongdaniel
Copy link
Author

你好,请问这个问题解决了吗?方便说一下解决方法嘛?多谢

你好,你可以看作者发的代码,可以解决,当时,我就是用他的代码,现在我代码找不到了。换了块硬盘,找起来费劲得很。你如果用他的代码无语,那就正好,如果有误,我就再找找

@leduy99
Copy link

leduy99 commented Dec 31, 2021

Hi, I'm encouting the same problem and cannot solve this. My approach is to change 'Switch' op -> 'If' and 'Merge' -> 'While' but didn't work. Any suggestion? Tks :D

@kongdaniel
Copy link
Author

kongdaniel commented Dec 31, 2021 via email

@leduy99
Copy link

leduy99 commented Dec 31, 2021

But how can you export a tflite version from the model? :o

@redaihanyu
Copy link

import tensorflow as tf

in_path = "../output/mobilefacenet/frozen_graphs/MobileFaceNet.pb" out_path = "../output/mobilefacenet/tflite/MobileFaceNet.tflite"

input_tensor_name = ["input"] input_tensor_shape = {"input": [2, 112, 112, 3]} classes_tensor_name = ["embeddings"]

converter = tf.lite.TFLiteConverter.from_frozen_graph(in_path, input_tensor_name, classes_tensor_name, input_shapes=input_tensor_shape) tflite_model = converter.convert()

with open(out_path, "wb") as f: f.write(tflite_model)

您好,为什么您这里的batchsize 设置成 2呢,不应该是1吗?

Repository owner deleted a comment from marynavoitenko Feb 23, 2024
@github-staff github-staff deleted a comment from thangtq-pionero Apr 26, 2024
@github-staff github-staff deleted a comment from jguzej May 12, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

7 participants
@syaringan357 @redaihanyu @leduy99 @kongdaniel @yp19940913 and others