簡體   English   中英

如何獲取TOCO tf_convert的凍結Tensorflow模型的input_shape

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

我正在嘗試使用TF Lite轉換器 (這是我正在使用的特定模型)將我從davidsandberg / facenet獲得的凍結模型轉換為.tflite在Ubuntu 18.04.1 LTS(VirtualBox )上 當我嘗試運行命令時:

/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'.

由於我自己沒有訓練模型,我不知道輸入的確切形狀。 可能有人可以從大衛桑德伯格的GitHubRep中的classifier.py和facenet.py中提取它,在facenet / src,但是我自己也不明白這個代碼。 我甚至嘗試通過tensorboard分析圖表。 無論如何我無法弄明白,但也許你可以: 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)

謝謝你的幫助,我確實像Alan Chiao說的那樣跟着load_data()到facenet.py,在那里我最終找到了形狀[1,160,160,3]。 此外, Tensorflow的tf lite轉換器的命令行參考向我展示了我必須注意的事項:

--input_shapes。 鍵入:以逗號分隔的整數列表的以冒號分隔的列表。 每個以逗號分隔的整數列表都給出了TensorFlow約定中指定的輸入數組之一的形狀。

示例:典型視覺模型的--input_shapes = 1,60,80,3表示批量大小為1,輸入圖像高度為60,輸入圖像寬度為80,輸入圖像深度為3(表示RGB通道) 。

我查看了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)
`

如果再次啟動Tensorboard,請返回到您看過的圖表,應該有一個搜索圖標(我認為在左上角),您可以在其中鍵入“輸入”並找到輸入張量。 它會給你想要的形狀。 我猜這將是'[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中看到的值相匹配。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM