简体   繁体   English

如何获取TOCO tf_convert的冻结Tensorflow模型的input_shape

[英]How do i get the input_shape of a frozen Tensorflow-Model for TOCO tf_convert

I'm trying to convert a frozen model i got from davidsandberg/facenet to .tflite on Ubuntu 18.04.1 LTS (VirtualBox) using the TF Lite Converter (this is the specific model i am using) . 我正在尝试使用TF Lite转换器 (这是我正在使用的特定模型)将我从davidsandberg / facenet获得的冻结模型转换为.tflite在Ubuntu 18.04.1 LTS(VirtualBox )上 When i try to run the command: 当我尝试运行命令时:

/home/nils/.local/bin/tflite_convert 
--output_file=/home/nils/Documents/frozen.tflite 
--graph_def_file=/home/nils/Documents/20180402-114759/20180402-114759.pb 
--input_arrays=input --output_array=embeddings

i get the following error: 我收到以下错误:

2018-11-29 16:36:21.774098: I 
tensorflow/core/platform/cpu_feature_guard.cc:141] Your CPU supports 
instructions that this TensorFlow binary was not compiled to use: AVX2
Traceback (most recent call last):
File "/home/nils/.local/bin/tflite_convert", line 11, in <module>
 sys.exit(main())
File 
"/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib   /lite/python/tflite_convert.py", 
line 412, in main
 app.run(main=run_main, argv=sys.argv[:1])
   File 
"/home/nils/.local/lib/python3.6/site-packages/tensorflow/python/platform/app.py", 
line 125, in run
 _sys.exit(main(argv))
File 
"/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", 
line 408, in run_main
 _convert_model(tflite_flags)
File 
"/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/tflite_convert.py", 
line 162, in _convert_model
 output_data = converter.convert()
File 
"/home/nils/.local/lib/python3.6/site-packages/tensorflow/contrib/lite/python/lite.py", 
line 404, in convert
 "'{0}'.".format(_tensor_name(tensor)))
ValueError: Provide an input shape for input array 'input'.

Since i havn't trained the model myself, i don't know what exact shape the input had. 由于我自己没有训练模型,我不知道输入的确切形状。 Probably one can extract it from the classifier.py and the facenet.py found in David Sandberg's GitHubRep., at facenet/src but i don't understand the code enough to do this myself. 可能有人可以从大卫桑德伯格的GitHubRep中的classifier.py和facenet.py中提取它,在facenet / src,但是我自己也不明白这个代码。 I even tried to analyse the graph via tensorboard. 我甚至尝试通过tensorboard分析图表。 I couldn't figure it out anyway, but maybe u can: Tensorboard-Screenshot As you might have already noticed, I'm quite new to Ubuntu, Tensorflow and everything related, so I am happy to take any kind of advice on this issue. 无论如何我无法弄明白,但也许你可以: Tensorboard-Screenshot你可能已经注意到了,我对Ubuntu,Tensorflow以及所有相关内容都很陌生,所以我很乐意就这个问题提出任何建议。 Thank you in advance! 先感谢您!

This is the relevant part of classifier.py, where the model gets loaded and set up: 这是classifier.py的相关部分,其中模型被加载和设置:

 # Load the model
        print('Loading feature extraction model')
        facenet.load_model(args.model)

        # Get input and output tensors
        images_placeholder = tf.get_default_graph().get_tensor_by_name("input:0")
        embeddings = tf.get_default_graph().get_tensor_by_name("embeddings:0")
        phase_train_placeholder = tf.get_default_graph().get_tensor_by_name("phase_train:0")
        embedding_size = embeddings.get_shape()[1]

        # Run forward pass to calculate embeddings
        print('Calculating features for images')
        nrof_images = len(paths)
        nrof_batches_per_epoch = int(math.ceil(1.0*nrof_images / args.batch_size))
        emb_array = np.zeros((nrof_images, embedding_size))
        for i in range(nrof_batches_per_epoch):
            start_index = i*args.batch_size
            end_index = min((i+1)*args.batch_size, nrof_images)
            paths_batch = paths[start_index:end_index]
            images = facenet.load_data(paths_batch, False, False, args.image_size)
            feed_dict = { images_placeholder:images, phase_train_placeholder:False }
            emb_array[start_index:end_index,:] = sess.run(embeddings, feed_dict=feed_dict)

        classifier_filename_exp = os.path.expanduser(args.classifier_filename)

Thank you for your help, i did like Alan Chiao said and followed load_data() to the facenet.py, where i eventually found the shape [1,160, 160, 3]. 谢谢你的帮助,我确实像Alan Chiao说的那样跟着load_data()到facenet.py,在那里我最终找到了形状[1,160,160,3]。 Also, Tensorflow's command line reference for the tf lite converter showed me what i had to look out for: 此外, Tensorflow的tf lite转换器的命令行参考向我展示了我必须注意的事项:

--input_shapes. --input_shapes。 Type: colon-separated list of comma-separated lists of integers. 键入:以逗号分隔的整数列表的以冒号分隔的列表。 Each comma-separated list of integers gives the shape of one of the input arrays specified in TensorFlow convention. 每个以逗号分隔的整数列表都给出了TensorFlow约定中指定的输入数组之一的形状。

Example: --input_shapes=1,60,80,3 for a typical vision model means a batch size of 1, an input image height of 60, an input image widthof 80, and an input image depth of 3 (representing RGB channels). 示例:典型视觉模型的--input_shapes = 1,60,80,3表示批量大小为1,输入图像高度为60,输入图像宽度为80,输入图像深度为3(表示RGB通道) 。

I went through the code for tflite converter. 我查看了tflite转换器的代码。 I found out that you need to give the input shape as a dictionary in the format {"input_tensor_name": [input shape]} . 我发现你需要以{"input_tensor_name": [input shape]}格式将输入形状作为字典。

Here's an example that resolves the issue: 这是解决问题的示例:

`graph_def_file = "20180402-114759/20180402-114759.pb"
input_arrays = ["input"]
output_arrays = ["embeddings"]

converter = tf.lite.TFLiteConverter.from_frozen_graph(
  graph_def_file, input_arrays, output_arrays,input_shapes={"input":[1,160,160,3]})

tflite_model = converter.convert()
open("model.tflite", "wb").write(tflite_model)
`

If you start Tensorboard again, go back to the graph that you've seen, there should be a search icon (I think at the upperleft corner) where you can type in "input" and find the input tensor. 如果再次启动Tensorboard,请返回到您看过的图表,应该有一个搜索图标(我认为在左上角),您可以在其中键入“输入”并找到输入张量。 It'll give you the desired shape. 它会给你想要的形状。 I'm guessing it'll be something in the form of '[1,image_size, image_size, 3]'. 我猜这将是'[1,image_size,image_size,3]'形式的东西。

Alternatively, you can examine the code 或者,您可以检查代码

feed_dict = { images_placeholder:images, phase_train_placeholder:False }

Note that we're feeding in the 'images' object into the images_placeholder, which maps to the "input:0" tensor. 请注意,我们将'images'对象输入images_placeholder,映射到“input:0”张量。 You essentially then want the shape of the images object. 你基本上想要图像对象的形状。

images comes from the call to facenet.load_data(). 图像来自对facenet.load_data()的调用。 If you go into facenet.py and examine the load_data function, you can observe that the shape is something like what I suggested above. 如果你进入facenet.py并检查load_data函数,你可以观察到形状就像我上面提到的那样。 If you print the image_size value, it should match what you see in Tensorboard. 如果您打印image_size值,它应该与您在Tensorboard中看到的值相匹配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我将如何 go 关于更改 Alex 网络的 input_shape(使用 tf-keras)? - How would I go about changing the input_shape for Alex net (with tf-keras)? 如何将在 tensorflow 2 中训练的模型转换为 tensorflow 1 冻结图 - How can I convert a model trained in tensorflow 2 to a tensorflow 1 frozen graph Tensorflow:如何将冻结模型转换为已保存模型 - Tensorflow: how to convert a frozen model to saved model 我如何在Tensorflow中调整未知尺寸的图像的大小(tf.shape(input)方法不起作用) - How do I resize image with unknown size in Tensorflow(tf.shape(input) method doesn't work) 如何在Keras的转移学习中使用input_shape和input_tensor? - How do I use input_shape and input_tensor in Transfer Learning in Keras? 如何为Keras顺序模型指定input_shape - How to specify input_shape for Keras Sequential model 双向/LSTM 深度学习模型,我应该如何定义 input_shape? - Bidirectional/LSTM Deep Learning Model, how should I be defining input_shape? Keras/Tensorflow 中的 input_shape 参数 - input_shape parameter in Keras/Tensorflow 为什么我会收到Keras LSTM RNN input_shape错误? - Why do I get a Keras LSTM RNN input_shape error? 如何在TensorFlow中清除tf.Tensor的形状信息? - How do I clear the shape information of tf.Tensor in TensorFlow?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM