我如何获得 TOCO tf_convert 的冻结 Tensorflow 模型的 input_shape
How do i get the input_shape of a frozen Tensorflow-Model for TOCO tf_convert
我正在尝试转换从 davidsandberg/facenet to .tflite on Ubuntu 18.04.1 LTS (VirtualBox) using the TF Lite Converter (this is the specific model i am using) 获得的冻结模型。
当我尝试 运行 命令时:
/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
我收到以下错误:
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'.
由于我自己没有训练过模型,所以我不知道输入的确切形状。也许可以从 David Sandberg 的 GitHubRep. 中的 classifier.py 和 facenet.py 中提取它,地址为 facenet/src,但我对代码的理解程度不足以自己执行此操作。
我什至尝试通过张量板分析图表。反正我想不通,但也许你可以:Tensorboard-Screenshot
正如您可能已经注意到的那样,我对 Ubuntu、Tensorflow 和所有相关的东西都很陌生,所以我很乐意就此问题提出任何建议。
提前致谢!
这是 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)
如果你再次启动 Tensorboard,回到你看到的图表,应该有一个搜索图标(我认为在左上角),你可以在其中输入 "input" 并找到输入张量。它会给你想要的形状。我猜它会是“[1,image_size, image_size, 3]”形式的东西。
或者,您可以检查代码
feed_dict = { images_placeholder:images, phase_train_placeholder:False }
请注意,我们将 'images' 对象输入 images_placeholder,它映射到 "input:0" 张量。然后,您基本上需要图像对象的形状。
图像来自对 facenet.load_data() 的调用。如果您进入 facenet.py 并检查 load_data 函数,您会发现形状与我上面建议的类似。如果您打印 image_size 值,它应该与您在 Tensorboard 中看到的相匹配。
谢谢你的帮助,我确实像 Alan Chiao 说的那样跟着 load_data() 到了 facenet.py,在那里我最终找到了形状 [1,160, 160, 3]。此外,Tensorflow's command line reference for the tf lite converter 向我展示了我必须注意的事项:
--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.
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).
我查看了 tflite 转换器的代码。我发现您需要以 {"input_tensor_name": [input shape]}
.
格式的字典形式提供输入形状
这是解决问题的示例:
`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)
`
我正在尝试转换从 davidsandberg/facenet to .tflite on Ubuntu 18.04.1 LTS (VirtualBox) using the TF Lite Converter (this is the specific model i am using) 获得的冻结模型。 当我尝试 运行 命令时:
/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
我收到以下错误:
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'.
由于我自己没有训练过模型,所以我不知道输入的确切形状。也许可以从 David Sandberg 的 GitHubRep. 中的 classifier.py 和 facenet.py 中提取它,地址为 facenet/src,但我对代码的理解程度不足以自己执行此操作。 我什至尝试通过张量板分析图表。反正我想不通,但也许你可以:Tensorboard-Screenshot 正如您可能已经注意到的那样,我对 Ubuntu、Tensorflow 和所有相关的东西都很陌生,所以我很乐意就此问题提出任何建议。 提前致谢!
这是 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)
如果你再次启动 Tensorboard,回到你看到的图表,应该有一个搜索图标(我认为在左上角),你可以在其中输入 "input" 并找到输入张量。它会给你想要的形状。我猜它会是“[1,image_size, image_size, 3]”形式的东西。
或者,您可以检查代码
feed_dict = { images_placeholder:images, phase_train_placeholder:False }
请注意,我们将 'images' 对象输入 images_placeholder,它映射到 "input:0" 张量。然后,您基本上需要图像对象的形状。
图像来自对 facenet.load_data() 的调用。如果您进入 facenet.py 并检查 load_data 函数,您会发现形状与我上面建议的类似。如果您打印 image_size 值,它应该与您在 Tensorboard 中看到的相匹配。
谢谢你的帮助,我确实像 Alan Chiao 说的那样跟着 load_data() 到了 facenet.py,在那里我最终找到了形状 [1,160, 160, 3]。此外,Tensorflow's command line reference for the tf lite converter 向我展示了我必须注意的事项:
--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.
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).
我查看了 tflite 转换器的代码。我发现您需要以 {"input_tensor_name": [input shape]}
.
这是解决问题的示例:
`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)
`