Open kongdaniel opened 4 years ago
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)
我用你的代码依然不行,和版本有关系吗。我是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 https://github.com/tensorflow/tensorflow/issues/28485.
Fatal Python error: Aborted`
还是保错,这是因为什么呢,请问你使用的是他原来的pb吗,假如不是,你是否是更改了他的代码,然后使得可以转换呢。似乎他原来的代码中生成的模型有程序控制流 2020-04-26 14:25:02.322529: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before Removing unused ops: 2421 operators, 4174 arrays (0 quantized) 2020-04-26 14:25:02.711969: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] After Removing unused ops pass 1: 1484 operators, 2717 arrays (0 quantized) 2020-04-26 14:25:03.052979: I tensorflow/lite/toco/graph_transformations/graph_transformations.cc:39] Before general graph transformations: 1484 operators, 2717 arrays (0 quantized) 2020-04-26 14:25:03.066037: 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 https://github.com/tensorflow/tensorflow/issues/28485. Fatal Python error: Aborted
------------------ 原始邮件 ------------------ 发件人: "syaringan357"<notifications@github.com>; 发送时间: 2020年4月26日(星期天) 中午1:59 收件人: "syaringan357/Android-MobileFaceNet-MTCNN-FaceAntiSpoofing"<Android-MobileFaceNet-MTCNN-FaceAntiSpoofing@noreply.github.com>; 抄送: "2459431805"<2459431805@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [syaringan357/Android-MobileFaceNet-MTCNN-FaceAntiSpoofing] 关于moilefacenet的pb模型转换成tf.lite (#14)
-- coding:utf-8 --
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)
converter.allow_custom_ops = True
tflite_model = converter.convert()
with open(out_path, "wb") as f: f.write(tflite_model)
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.
哦,我不是用他原来那个pb转的,用他的ckpt重新固化一个pb,再转。
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):
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()
凑合看把 上面都是代码
凑合看把 上面都是代码
谢谢大佬,跪谢。
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"<notifications@github.com>; 发送时间: 2020年5月6日(星期三) 上午9:42 收件人: "syaringan357/Android-MobileFaceNet-MTCNN-FaceAntiSpoofing"<Android-MobileFaceNet-MTCNN-FaceAntiSpoofing@noreply.github.com>; 抄送: "2459431805"<2459431805@qq.com>;"Author"<author@noreply.github.com>; 主题: Re: [syaringan357/Android-MobileFaceNet-MTCNN-FaceAntiSpoofing] 关于moilefacenet的pb模型转换成tf.lite (#14)
我也不太清楚,我也是按报错提示改的。可能是他一些代码经过长时间修改之后对不上了。
— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or unsubscribe.
你好,请问这个问题解决了吗?方便说一下解决方法嘛?多谢
你好,请问这个问题解决了吗?方便说一下解决方法嘛?多谢
你好,你可以看作者发的代码,可以解决,当时,我就是用他的代码,现在我代码找不到了。换了块硬盘,找起来费劲得很。你如果用他的代码无语,那就正好,如果有误,我就再找找
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
sorry,I don't learn it so long,I forgeted it.
---原始邮件--- 发件人: @.> 发送时间: 2021年12月31日(周五) 下午4:58 收件人: @.>; 抄送: @.**@.>; 主题: Re: [syaringan357/Android-MobileFaceNet-MTCNN-FaceAntiSpoofing] 关于moilefacenet的pb模型转换成tf.lite (#14)
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
— Reply to this email directly, view it on GitHub, or unsubscribe. Triage notifications on the go with GitHub Mobile for iOS or Android. You are receiving this because you authored the thread.Message ID: @.***>
But how can you export a tflite version from the model? :o
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吗?
你好请问你的pb模型是这么转换到lite,模型的,我现在在tf的1.80,1.12-1.15都试过,但总是转不了,请问你当时是怎么转换的呢。