我是使用 tensorflow 學習物件檢測的新手,我正在參考這個網站https://www.mygreatlearning.com/blog/object-detection-using-tensorflow/來實作一個模型我已經遵循了所有步驟,但有兩個突出的一次又一次出現的錯誤,第一個是
運行這兩個命令時,“ INFO:tensorflow:Saver 未創建,因為圖中沒有要恢復的變數”
model_name = 'ssd_inception_v2_coco_2017_11_17'
detection_model = load_model(model_name)
另一個錯誤是 TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor' on running these
for image_path in TEST_IMAGE_PATHS:
print(image_path)
show_inference(detection_model, image_path)
這是呼叫堆疊:
models\research\object_detection\test_images\image1.jpg
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-20-e230477d58de> in <module>
1 for image_path in TEST_IMAGE_PATHS:
2 print(image_path)
----> 3 show_inference(detection_model, image_path)
<ipython-input-18-a9efdd893038> in show_inference(model, image_path)
7 # Actual detection.
8
----> 9 output_dict = run_inference_for_single_image(model, image_np)
10 # Visualization of the results of a detection.
11 vis_util.visualize_boxes_and_labels_on_image_array(
<ipython-input-17-7172365aecd9> in run_inference_for_single_image(model, image)
12 # Convert to numpy arrays, and take index [0] to remove the batch dimension.
13 # We're only interested in the first num_detections.
---> 14 num_detections = int(output_dict.pop('num_detections'))
15
16 #num_detections = int(tf.get_static_value(output_dict.pop('num_detections'))[0])
TypeError: int() argument must be a string, a bytes-like object or a number, not 'Tensor'
一些答案說要從 dict 中洗掉張量,但我不確切知道如果要從字典中洗掉它們,是否要從字典中洗掉它們,然后嘗試降級或升級 tensorflow。在注釋第 14 行并在取消注釋后使用第 16 行時,它會給出錯誤 None is not subscriptable。我還將 tf.gfile 更改為 tf.io.gfile 。降級后我使用的是 tensorflow 版本 1.15.0。
uj5u.com熱心網友回復:
關于第一個錯誤:您可能使用了您提到的博客文章中的以下代碼:
def load_model(model_name):
base_url = 'http://download.tensorflow.org/models/object_detection/'
model_file = model_name '.tar.gz'
model_dir = tf.keras.utils.get_file(
fname=model_name,
origin=base_url model_file,
untar=True)
model_dir = pathlib.Path(model_dir)/"saved_model"
model = tf.saved_model.load(str(model_dir))
return model
使用除錯器檢查模型是否從url.
關于第二個錯誤:
output_dict = run_inference_for_single_image(model, image_np)
似乎回傳一個以張量為值的字典。
嘗試:
num_detections = int(output_dict.pop('num_detections').numpy())
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/475726.html
上一篇:檢查JSON屬性是否存在
