一、前言

在用faster-rcnn-tf2-main 训练自己的数据集时,在第三行代码处报错。

顺带提一下,我找了很久tensorflow2.x实现的faster_rcnn,发现这个写的不错:

https://github.com/bubbliiiing/faster-rcnn-tf2

二、错误提示:

TypeError: Only integers, slices (`:`), ellipsis (`...`), tf.newaxis (`None`) and scalar tf.int32/tf.int64 tensors are valid indices, got array([1076,  806,  797, ..., 3534, 4776, 3912], dtype=int64)

三、错误原因

错误原因,数据类型不对。

四、解决方法

1、首先看错误处的数据类型(代码第二段)。

         c_confs = mbox_conf[i, :, 0]
         print(type(c_confs))

        argsort_index = np.argsort(c_confs)[::-1]
        # c_confs = np.array(c_confs)
        c_confs = c_confs[argsort_index[:self.rpn_pre_boxes]]
<class 'tensorflow.python.framework.ops.EagerTensor'>

2、容易知道,数据类型为EagerTensor类型,我们进行操作是对数组进行操作,因此在数组操作之前,将数据类型转换为array即可(代码第三段)。

            c_confs = mbox_conf[i, :, 0]

            argsort_index = np.argsort(c_confs)[::-1]
            c_confs=np.array(c_confs)

            c_confs = c_confs[argsort_index[:self.rpn_pre_boxes]]

问题即可解决