【问题标题】:Tensorflow 2.3.1 IndexError: list index out of rangeTensorflow 2.3.1 IndexError:列表索引超出范围
【发布时间】:2021-02-11 19:47:37
【问题描述】:

出现错误,IndexError: list index out of range.

它在另一台机器上工作,但在我将它转移到另一台机器后它不再工作了。

Python:3.8.5

张量流:2.3.1

回溯说:

tensorflow.python.autograph.impl.api.StagingError: in user code:

    Load_Model.py:40 detect_fn  *
        image, shapes = detection_model.preprocess(image)
    C:\Users\Tensorflow\tensorflow 2.x\models\research\object_detection\meta_architectures\ssd_meta_arch.py:482 preprocess  *
        normalized_inputs = self._feature_extractor.preprocess(inputs)
    C:\Users\Tensorflow\tensorflow 2.x\models\research\object_detection\models\ssd_resnet_v1_fpn_keras_feature_extractor.py:204 preprocess  *
        if resized_inputs.shape.as_list()[3] == 3:

    IndexError: list index out of range

我的代码:

import tensorflow as tf
import os
import cv2
from object_detection.utils import label_map_util
from object_detection.utils import config_util
from object_detection.utils import visualization_utils as viz_utils
from object_detection.builders import model_builder

model_name = 'ssd_resnet101_v1_fpn_640x640_coco17_tpu-8'
data_dir = os.path.join(os.getcwd(), 'data')
models_dir = os.path.join(data_dir, 'models')
path_to_ckg = os.path.join(models_dir, os.path.join(model_name, 'pipeline.config'))
PATH_TO_CFG = os.path.join(models_dir)
path_to_cktp = os.path.join(models_dir, os.path.join(model_name, 'checkpoint/'))
label_filename = 'mscoco_label_map.pbtxt'
path_to_labels = os.path.join(models_dir, os.path.join(model_name, label_filename))


tf.get_logger().setLevel('ERROR')           # Suppress TensorFlow logging (2)

#Enable GPU dynamic memory allocation
gpus = tf.config.experimental.list_physical_devices('GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)

#Load pipeline config and build a detection model'
configs = config_util.get_configs_from_pipeline_file(path_to_ckg)
model_config = configs['model']
detection_model = model_builder.build(model_config=model_config, is_training=False)

#Restore checkpoint
ckpt = tf.compat.v2.train.Checkpoint(model=detection_model)
ckpt.restore(os.path.join(path_to_cktp, 'ckpt-0')).expect_partial()

@tf.function
def detect_fn(image):
    """Detect objects in image."""

    image, shapes = detection_model.preprocess(image)
    prediction_dict = detection_model.predict(image, shapes)
    detections = detection_model.postprocess(prediction_dict, shapes)

    return detections, prediction_dict, tf.reshape(shapes, [-1])

category_index = label_map_util.create_category_index_from_labelmap(path_to_labels,
                                                                    use_display_name=True)

cap = cv2.VideoCapture('rtsp://username:pass@192.168.1.103:8000/tcp/av0_1')

import numpy as np

while True:
    #Read frame from camera
    ret, image_np = cap.read()

    #Expand dimensions since the model expects images to have shape: [1, None, None, 3]
    image_np_expanded = np.expand_dims(image_np, axis=0)

    #Things to try:
    #Flip horizontally
    #image_np = np.fliplr(image_np).copy()

    #Convert image to grayscale
    #image_np = np.tile(
    #np.mean(image_np, 2, keepdims=True), (1, 1, 3)).astype(np.uint8)

    input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32)
    detections, predictions_dict, shapes = detect_fn(input_tensor)

    label_id_offset = 1
    image_np_with_detections = image_np.copy()

    viz_utils.visualize_boxes_and_labels_on_image_array(
          image_np_with_detections,
          detections['detection_boxes'][0].numpy(),
          (detections['detection_classes'][0].numpy() + label_id_offset).astype(int),
          detections['detection_scores'][0].numpy(),
          category_index,
          use_normalized_coordinates=True,
          max_boxes_to_draw=200,
          min_score_thresh=.30,
          agnostic_mode=False)

    #Display output
    cv2.imshow('object detection', cv2.resize(image_np_with_detections, (800, 600)))

    if cv2.waitKey(25) & 0xFF == ord('q'):
        break

cap.release()
cv2.destroyAllWindows()

我真的不明白为什么会发生这样的错误。

我的代码有什么问题?我应该如何解决这个问题?

【问题讨论】:

  • 你能告诉我使用 cap.read() 后 image_np 的形状吗?
  • # 从相机读取帧 ret, image_np = cap.read() # 扩展维度,因为模型期望图像具有形状:[1, None, None, 3] image_np_expanded = np.expand_dims(image_np , axis=0) # 尝试:# 水平翻转 # image_np = np.fliplr(image_np).copy() # 将图像转换为灰度 # image_np = np.tile( # np.mean(image_np, 2, keepdims=True ), (1, 1, 3)).astype(np.uint8) input_tensor = tf.convert_to_tensor(np.expand_dims(image_np, 0), dtype=tf.float32) 检测,predictions_dict,shapes = detect_fn(input_tensor)跨度>

标签: python tensorflow tensorflow2.0 index-error tensorflow2.x


【解决方案1】:

在get_model_detection_function函数中定义detect_fn, 像这样:

def get_model_detection_function(model):
"""Get a tf.function for detection."""

    @tf.function
    def detect_fn(image):
        """Detect objects in image."""

        image, shapes = model.preprocess(image)
        prediction_dict = model.predict(image, shapes)
        detections = model.postprocess(prediction_dict, shapes)

        return detections, prediction_dict, tf.reshape(shapes, [-1])

    return detect_fn

detect_fn = get_model_detection_function(detection_model)

看看有没有帮助?

【讨论】:

  • 我遇到了同样的问题,我就这样解决了,谢谢!只是出于好奇,您能否详细说明为什么应该以这种方式从另一个函数中返回detect_fn?我尝试只使用 def detect_fn(detection_model, image),并在帧的循环中调用它,但我得到了与 OP 中相同的错误
  • 是的,当然! tf.function 构造了一个可调用对象,该可调用对象执行通过跟踪编译 func 中的 TensorFlow 操作而创建的 TensorFlow 图 (tf.Graph),从而有效地将 func 作为 TensorFlow 图执行。因此有必要将其作为装饰器添加到 detect_fn 函数中。
【解决方案2】:

您得到的错误是“如果 resized_inputs.shape.as_list()[3] == 3:”它表示三个输入的形状,即高度、宽度和 RGB 颜色变化

from PIL import Image

def load_image_into_numpy_array(path):
    """Load an image from file into a numpy array.

    Puts image into numpy array to feed into tensorflow graph.
    Note that by convention we put it into a numpy array with shape
    (height, width, channels), where channels=3 for RGB.

    Args:
      path: the file path to the image

    Returns:
      uint8 numpy array with shape (img_height, img_width, 3)
    """
    return np.array(Image.open(path))


image_np = load_image_into_numpy_array(IMAGE_PATH)

我认为这可行

【讨论】:

    猜你喜欢
    • 2019-01-22
    • 1970-01-01
    • 2011-10-31
    • 2015-06-26
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多