1. 程式人生 > >InvalidArgumentError: slice index xxx of dimension xxx out of bounds

InvalidArgumentError: slice index xxx of dimension xxx out of bounds

InvalidArgumentError: slice index xxx of dimension xxx out of bounds

發生原因

這是在呼叫Mask_RCNN中的run_graph時所發生的錯誤。此處有用到的run_graph是經筆者修改過後,可以在"training"模式下執行的版本,詳見run_graph at training mode.
在config中宣告BATCH_SIZE為4,因此在呼叫模型的時候,必須確保輸入的資料符合這個條件。

model =
modellib.MaskRCNN(mode="training", config=config, model_dir=MODEL_DIR) #此處config中的BATCH_SIZE為4

一開始筆者疏忽,使用第0維全為1的輸入:

print(image.shape, image_meta.shape, rpn_match.shape, rpn_bbox.shape, gt_class_ids.shape, gt_boxes.shape, gt_masks.shape)
# (1, 576, 576, 3) (1, 18) (1, 82863, 1) (1, 256, 4) (1, 0) (1, 0, 4) (1, 576, 576, 0)

這導致文末InvalidArgumentError: slice index xxx of dimension xxx out of bounds的錯誤發生。

解決辦法

後來將輸入的第0維全部修改成4:

print(image.shape, image_meta.shape, rpn_match.shape, rpn_bbox.shape, gt_class_ids.shape, gt_boxes.shape, gt_masks.shape)
# (4, 576, 576, 3) (4, 18) (4, 82863, 1) (4, 256, 4) (4, 0) (4, 0, 4) (4, 576, 576, 0)

run_graph()便可成功執行:

model = modellib.MaskRCNN(mode="training", config=config,
                          model_dir=MODEL_DIR) #此處config中的BATCH_SIZE為4
pillar = model.keras_model.get_layer("ROI").output  # node to start searching from

graph = model.run_graph([image, image_meta, rpn_match, rpn_bbox, gt_class_ids, gt_boxes, gt_masks], [
    ("rpn_class", model.keras_model.get_layer("rpn_class").output),
    ("rpn_bbox", model.keras_model.get_layer("rpn_bbox").output),
    ("pillar", pillar),
    ("pre_nms_anchors", model.ancestor(pillar, "ROI/pre_nms_anchors:0")),
    ("refined_anchors", model.ancestor(pillar, "ROI/refined_anchors:0")),
    ("refined_anchors_clipped", model.ancestor(pillar, "ROI/refined_anchors_clipped:0")),
    ("rois", tf.get_default_graph().get_tensor_by_name("proposal_targets/rois:0")),
    ("target_class_ids", tf.get_default_graph().get_tensor_by_name("proposal_targets/target_class_ids:0")),
    ("target_bbox", tf.get_default_graph().get_tensor_by_name("proposal_targets/target_bbox:0")),
    ("target_mask", tf.get_default_graph().get_tensor_by_name("proposal_targets/target_mask:0")), 
    ("top_anchors", model.ancestor(pillar, "ROI/top_anchors:1")),
    ("proposals", model.keras_model.get_layer("ROI").output),
    ("mrcnn_class_logits", model.keras_model.get_layer("mrcnn_class_logits").output),
    ("mrcnn_class", model.keras_model.get_layer("mrcnn_class").output),
    ("mrcnn_bbox", model.keras_model.get_layer("mrcnn_bbox").output),
    ("rpn_class_loss", model.keras_model.get_layer("rpn_class_loss").output),
    ("rpn_bbox_loss", model.keras_model.get_layer("rpn_bbox_loss").output),
    ("mrcnn_class_loss", model.keras_model.get_layer("mrcnn_class_loss").output),
    ("mrcnn_bbox_loss", model.keras_model.get_layer("mrcnn_bbox_loss").output),
    ("mrcnn_mask_loss", model.keras_model.get_layer("mrcnn_mask_loss").output),
    ("target_class_ids", model.keras_model.get_layer("proposal_targets").output[1]),
    ("target_bbox", model.keras_model.get_layer("proposal_targets").output[2]),
    ("target_mask", model.keras_model.get_layer("proposal_targets").output[3])
])
"""
result:
rpn_class                shape: (4, 82863, 2)         min:    0.00000  max:    1.00000  float32
rpn_bbox                 shape: (4, 82863, 4)         min:  -36.51252  max:   63.45781  float32
pillar                   shape: (4, 2000, 4)          min:    0.00000  max:    1.00000  float32
pre_nms_anchors          shape: (4, 6000, 4)          min:   -0.00123  max:    0.91898  float32
refined_anchors          shape: (4, 6000, 4)          min:  -24.39013  max:   24.44984  float32
refined_anchors_clipped  shape: (4, 6000, 4)          min:    0.00000  max:    1.00000  float32
rois                     shape: (4, 32, 4)            min:    0.00000  max:    0.00000  float32
target_class_ids         shape: (4, 32)               min:    0.00000  max:    0.00000  int32
target_bbox              shape: (4, 32, 4)            min:    0.00000  max:    0.00000  float32
target_mask              shape: (4, 32, 28, 28)       min:    0.00000  max:    0.00000  float32
top_anchors              shape: (4, 6000)             min:   11.00000  max: 20525.00000  int32
proposals                shape: (4, 2000, 4)          min:    0.00000  max:    1.00000  float32
mrcnn_class_logits       shape: (4, 32, 6)            min:   -2.87763  max:    3.33817  float32
mrcnn_class              shape: (4, 32, 6)            min:    0.00174  max:    0.86989  float32
mrcnn_bbox               shape: (4, 32, 6, 4)         min:   -6.20270  max:    6.32315  float32
rpn_class_loss           shape: ()                    min:    8.17014  max:    8.17014  float32
rpn_bbox_loss            shape: ()                    min:    0.00000  max:    0.00000  float32
mrcnn_class_loss         shape: ()                    min:    0.00000  max:    0.00000  float32
mrcnn_bbox_loss          shape: ()                    min:    0.00000  max:    0.00000  float32
mrcnn_mask_loss          shape: ()                    min:    0.00000  max:    0.00000  float32
"""

完整Traceback

InvalidArgumentErrorTraceback (most recent call last)
/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1321     try:
-> 1322       return fn(*args)
   1323     except errors.OpError as e:

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _run_fn(feed_dict, fetch_list, target_list, options, run_metadata)
   1306       return self._call_tf_sessionrun(
-> 1307           options, feed_dict, fetch_list, target_list, run_metadata)
   1308 

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _call_tf_sessionrun(self, options, feed_dict, fetch_list, target_list, run_metadata)
   1408           self._session, options, feed_dict, fetch_list, target_list,
-> 1409           run_metadata)
   1410     else:

InvalidArgumentError: slice index 2 of dimension 0 out of bounds.
	 [[Node: proposal_targets/strided_slice_77 = StridedSlice[Index=DT_INT32, T=DT_BOOL, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_input_gt_masks_0_2, proposal_targets/strided_slice_40/stack_1, proposal_targets/strided_slice_77/stack_1, proposal_targets/strided_slice_3/stack_1)]]

During handling of the above exception, another exception occurred:

InvalidArgumentErrorTraceback (most recent call last)
<ipython-input-33-9364c9c7da1e> in <module>
     74     ("target_class_ids", model.keras_model.get_layer("proposal_targets").output[1]),
     75     ("target_bbox", model.keras_model.get_layer("proposal_targets").output[2]),
---> 76     ("target_mask", model.keras_model.get_layer("proposal_targets").output[3])
     77 ])

/notebooks/Lorenzo/Mask_RCNN/mrcnn/model_allow_bg.py in run_graph(self, inputs, outputs)
   2923 #         outputs_np = samplewise_function(lcn, inputs, batch_size=4)
   2924 #         outputs_np = batchwise_function(lcn, inputs, batch_size=4)
-> 2925         outputs_np = kf(model_in)
   2926 
   2927         # Pack the generated Numpy arrays into a a dict and log the results.

/usr/local/lib/python3.5/dist-packages/keras/backend/tensorflow_backend.py in __call__(self, inputs)
   2473         session = get_session()
   2474         updated = session.run(fetches=fetches, feed_dict=feed_dict,
-> 2475                               **self.session_kwargs)
   2476         return updated[:len(self.outputs)]
   2477 

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in run(self, fetches, feed_dict, options, run_metadata)
    898     try:
    899       result = self._run(None, fetches, feed_dict, options_ptr,
--> 900                          run_metadata_ptr)
    901       if run_metadata:
    902         proto_data = tf_session.TF_GetBuffer(run_metadata_ptr)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _run(self, handle, fetches, feed_dict, options, run_metadata)
   1133     if final_fetches or final_targets or (handle and feed_dict_tensor):
   1134       results = self._do_run(handle, final_targets, final_fetches,
-> 1135                              feed_dict_tensor, options, run_metadata)
   1136     else:
   1137       results = []

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _do_run(self, handle, target_list, fetch_list, feed_dict, options, run_metadata)
   1314     if handle is None:
   1315       return self._do_call(_run_fn, feeds, fetches, targets, options,
-> 1316                            run_metadata)
   1317     else:
   1318       return self._do_call(_prun_fn, handle, feeds, fetches)

/usr/local/lib/python3.5/dist-packages/tensorflow/python/client/session.py in _do_call(self, fn, *args)
   1333         except KeyError:
   1334           pass
-> 1335       raise type(e)(node_def, op, message)
   1336 
   1337   def _extend_graph(self):

InvalidArgumentError: slice index 2 of dimension 0 out of bounds.
	 [[Node: proposal_targets/strided_slice_77 = StridedSlice[Index=DT_INT32, T=DT_BOOL, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_input_gt_masks_0_2, proposal_targets/strided_slice_40/stack_1, proposal_targets/strided_slice_77/stack_1, proposal_targets/strided_slice_3/stack_1)]]

Caused by op 'proposal_targets/strided_slice_77', defined at:
  File "/usr/lib/python3.5/runpy.py", line 184, in _run_module_as_main
    "__main__", mod_spec)
  File "/usr/lib/python3.5/runpy.py", line 85, in _run_code
    exec(code, run_globals)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel_launcher.py", line 16, in <module>
    app.launch_new_instance()
  File "/usr/local/lib/python3.5/dist-packages/traitlets/config/application.py", line 658, in launch_instance
    app.start()
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelapp.py", line 505, in start
    self.io_loop.start()
  File "/usr/local/lib/python3.5/dist-packages/tornado/platform/asyncio.py", line 132, in start
    self.asyncio_loop.run_forever()
  File "/usr/lib/python3.5/asyncio/base_events.py", line 345, in run_forever
    self._run_once()
  File "/usr/lib/python3.5/asyncio/base_events.py", line 1312, in _run_once
    handle._run()
  File "/usr/lib/python3.5/asyncio/events.py", line 125, in _run
    self._callback(*self._args)
  File "/usr/local/lib/python3.5/dist-packages/tornado/ioloop.py", line 758, in _run_callback
    ret = callback()
  File "/usr/local/lib/python3.5/dist-packages/tornado/stack_context.py", line 300, in null_wrapper
    return fn(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/tornado/gen.py", line 1233, in inner
    self.run()
  File "/usr/local/lib/python3.5/dist-packages/tornado/gen.py", line 1147, in run
    yielded = self.gen.send(value)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 370, in dispatch_queue
    yield self.process_one()
  File "/usr/local/lib/python3.5/dist-packages/tornado/gen.py", line 346, in wrapper
    runner = Runner(result, future, yielded)
  File "/usr/local/lib/python3.5/dist-packages/tornado/gen.py", line 1080, in __init__
    self.run()
  File "/usr/local/lib/python3.5/dist-packages/tornado/gen.py", line 1147, in run
    yielded = self.gen.send(value)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 357, in process_one
    yield gen.maybe_future(dispatch(*args))
  File "/usr/local/lib/python3.5/dist-packages/tornado/gen.py", line 326, in wrapper
    yielded = next(result)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 267, in dispatch_shell
    yield gen.maybe_future(handler(stream, idents, msg))
  File "/usr/local/lib/python3.5/dist-packages/tornado/gen.py", line 326, in wrapper
    yielded = next(result)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/kernelbase.py", line 534, in execute_request
    user_expressions, allow_stdin,
  File "/usr/local/lib/python3.5/dist-packages/tornado/gen.py", line 326, in wrapper
    yielded = next(result)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/ipkernel.py", line 294, in do_execute
    res = shell.run_cell(code, store_history=store_history, silent=silent)
  File "/usr/local/lib/python3.5/dist-packages/ipykernel/zmqshell.py", line 536, in run_cell
    return super(ZMQInteractiveShell, self).run_cell(*args, **kwargs)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2817, in run_cell
    raw_cell, store_history, silent, shell_futures)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 2843, in _run_cell
    return runner(coro)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/async_helpers.py", line 67, in _pseudo_sync_runner
    coro.send(None)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 3018, in run_cell_async
    interactivity=interactivity, compiler=compiler, result=result)
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 3183, in run_ast_nodes
    if (yield from self.run_code(code, result)):
  File "/usr/local/lib/python3.5/dist-packages/IPython/core/interactiveshell.py", line 3265, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "<ipython-input-16-7928c4edfc77>", line 3, in <module>
    model_dir=MODEL_DIR)
  File "/notebooks/Lorenzo/Mask_RCNN/mrcnn/model_allow_bg.py", line 2007, in __init__
    self.keras_model = self.build(mode=mode, config=config)
  File "/notebooks/Lorenzo/Mask_RCNN/mrcnn/model_allow_bg.py", line 2160, in build
    target_rois, input_gt_class_ids, gt_boxes, input_gt_masks])
  File "/usr/local/lib/python3.5/dist-packages/keras/engine/topology.py", line 617, in __call__
    output = self.call(inputs, **kwargs)
  File "/notebooks/Lorenzo/Mask_RCNN/mrcnn/model_allow_bg.py", line 697, in call
    self.config.IMAGES_PER_GPU, names=names)
  File "/notebooks/Lorenzo/Mask_RCNN/mrcnn/utils.py", line 829, in batch_slice
    inputs_slice = [x[i] for x in inputs]
  File "/notebooks/Lorenzo/Mask_RCNN/mrcnn/utils.py", line 829, in <listcomp>
    inputs_slice = [x[i] for x in inputs]
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 523, in _slice_helper
    name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/array_ops.py", line 689, in strided_slice
    shrink_axis_mask=shrink_axis_mask)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gen_array_ops.py", line 8232, in strided_slice
    name=name)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/op_def_library.py", line 787, in _apply_op_helper
    op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 3414, in create_op
    op_def=op_def)
  File "/usr/local/lib/python3.5/dist-packages/tensorflow/python/framework/ops.py", line 1740, in __init__
    self._traceback = self._graph._extract_stack()  # pylint: disable=protected-access

InvalidArgumentError (see above for traceback): slice index 2 of dimension 0 out of bounds.
	 [[Node: proposal_targets/strided_slice_77 = StridedSlice[Index=DT_INT32, T=DT_BOOL, begin_mask=0, ellipsis_mask=0, end_mask=0, new_axis_mask=0, shrink_axis_mask=1, _device="/job:localhost/replica:0/task:0/device:CPU:0"](_arg_input_gt_masks_0_2, proposal_targets/strided_slice_40/stack_1, proposal_targets/strided_slice_77/stack_1, proposal_targets/strided_slice_3/stack_1)]]

參考連結

解決問題tensorflow.python.framework.errors_impl.InvalidArgumentError: slice index 1 of dimension 0 out o