簡體   English   中英

帶有 Keras 的 Mask-RCNN:嘗試將“形狀”轉換為張量但失敗了。 錯誤:不支持無值

[英]Mask-RCNN with Keras : Tried to convert 'shape' to a tensor and failed. Error: None values not supported

我正在嘗試在推理模式下運行Mask_RCNN 的 Keras 實現 基本上就是運行這段代碼demo.ipynb

但是當我運行它時,我在創建模型時收到以下錯誤:

ValueError: Tried to convert 'shape' to a tensor and failed. Error: None values not supported.

這是堆棧跟蹤:

Traceback (most recent call last):
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1758, in <module>
    main()
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1752, in main
    globals = debugger.run(setup['file'], None, None, is_module)
  File "/snap/pycharm-community/128/helpers/pydev/pydevd.py", line 1147, in run
    pydev_imports.execfile(file, globals, locals)  # execute the script
  File "/snap/pycharm-community/128/helpers/pydev/_pydev_imps/_pydev_execfile.py", line 18, in execfile
    exec(compile(contents+"\n", file, 'exec'), glob, loc)
  File "[PATH/TO/Mask_RCNN/]/Own_code/Test.py", line 44, in <module>
    model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)
  File "[PATH/TO/Mask_RCNN/]/Mask_RCNN/mrcnn/model.py", line 1833, in __init__
    self.keras_model = self.build(mode=mode, config=config)
  File "[PATH/TO/Mask_RCNN/]/Mask_RCNN/mrcnn/model.py", line 2035, in build
    fc_layers_size=config.FPN_CLASSIF_FC_LAYERS_SIZE)
  File "[PATH/TO/Mask_RCNN/]/Mask_RCNN/mrcnn/model.py", line 947, in fpn_classifier_graph
    mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)
  File "[PATH/TO/venv/]lib/python3.6/site-packages/tensorflow/python/keras/engine/base_layer.py", line 554, in __call__
    outputs = self.call(inputs, *args, **kwargs)
  File "[PATH/TO/venv/]lib/python3.6/site-packages/tensorflow/python/keras/layers/core.py", line 439, in call
    (array_ops.shape(inputs)[0],) + self.target_shape)
  File "[PATH/TO/venv/]lib/python3.6/site-packages/tensorflow/python/ops/gen_array_ops.py", line 7179, in reshape
    "Reshape", tensor=tensor, shape=shape, name=name)
  File "[PATH/TO/venv/]lib/python3.6/site-packages/tensorflow/python/framework/op_def_library.py", line 529, in _apply_op_helper
    (input_name, err))

看起來我的問題與此處描述的相同,但沒有真正的答案。

備注:

  • 我正在使用 Python 3.6 和 Tensorflow 1.13.1 運行它
  • 我已經編輯了 Keras 導入以使用嵌入在 Tensorflow 中的 Keras 版本
  • 我運行出現此錯誤的代碼對應於演示筆記本的前 3 個塊。

編輯

這是我正在運行的代碼:

# Root directory of the project
ROOT_DIR = os.path.abspath("../")

# Import Mask RCNN
sys.path.append(ROOT_DIR)  # To find local version of the library

# Import COCO config
sys.path.append(os.path.join(ROOT_DIR, "samples/coco/"))  # To find local version

# Directory to save logs and trained model
MODEL_DIR = os.path.join(ROOT_DIR, "logs")

# Local path to trained weights file
COCO_MODEL_PATH = os.path.join(ROOT_DIR, "mask_rcnn_coco.h5")
# Download COCO trained weights from Releases if needed
if not os.path.exists(COCO_MODEL_PATH):
    utils.download_trained_weights(COCO_MODEL_PATH)

# Directory of images to run detection on
IMAGE_DIR = os.path.join(ROOT_DIR, "images")

class InferenceConfig(coco.CocoConfig):
    # Set batch size to 1 since we'll be running inference on
    # one image at a time. Batch size = GPU_COUNT * IMAGES_PER_GPU
    GPU_COUNT = 1
    IMAGES_PER_GPU = 1

config = InferenceConfig()

# Create model object in inference mode.
model = modellib.MaskRCNN(mode="inference", model_dir=MODEL_DIR, config=config)

這實際上是來自 maskRCNN 存儲庫的demo.ipynb的復制/粘貼。 最后一行是發生錯誤的地方。

我得到了同樣的錯誤,直到:

  1. 使用keras.__version__ == 2.2.4
  2. Tensorflow-gpu version == 1.12.0
  3. Skimage.__version__ == 0.14.2

不確定其他版本的錯誤是什么,可能有些東西不適合pycocotools

optimizer = keras.optimizers.SGD(
            lr=learning_rate, momentum=momentum,
            clipnorm=self.config.GRADIENT_CLIP_NORM) #this portion.

我做了這個官方問題線程中推薦的所有更改。

這些基本上是 model.py 腳本中的更改,以使用 tf.keras 函數而不是純 keras 函數。

這是我的工作分支(針對 TF 2.4.1 更新 - 2021 年 5 月)。

您可以在其中或通過此鏈接找到我的 colab 示例

在 model.py 中替換此代碼

if s[1] is None:
    mrcnn_bbox = KL.Reshape((-1, num_classes, 4), name="mrcnn_bbox")(x)
else:
    mrcnn_bbox = KL.Reshape((s[1], num_classes, 4), name="mrcnn_bbox")(x)

暫無
暫無

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

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