指定TensorFlow版本下EfficientPose调用layers.Input报类型错误求助
解决TensorFlow 1.15中layers.Input触发TypeError的问题
错误核心原因
在nvidia-tensorflow==1.15.4+nv20.12版本中,keras.layers.Input的shape参数仅接受纯整数组成的元组/列表,若传入的input_shape是带字符串索引的字典、自定义类实例等结构,就会触发TypeError: list indices must be integers or slices, not str错误——因为TF1.15的shape解析逻辑无法处理字符串索引。
具体解决步骤
1. 排查input_shape的实际结构
在调用layers.Input前添加调试代码,确认input_shape的类型和内容:
print("input_shape类型:", type(input_shape)) print("input_shape内容:", input_shape)
如果输出是字典(比如{'height': 480, 'width': 640, 'channels': 3})或其他带字符串键的结构,直接进入下一步修正。
2. 将input_shape转换为TF1.15兼容格式
根据input_shape的实际结构,提取纯整数值组成元组/列表:
- 若为字典:
# 示例:从字典中提取高、宽、通道数,顺序根据模型需求调整 input_shape = (input_shape['height'], input_shape['width'], input_shape['channels']) - 若为自定义对象:
# 示例:假设对象有h/w/c属性 input_shape = (input_shape.h, input_shape.w, input_shape.c)
3. 修正Input层调用代码
TF1.15中建议显式指定dtype,避免隐式转换异常,修改后的调用:
import tensorflow as tf from tensorflow.keras import layers # 确保input_shape是纯整数元组/列表 input_tensor = layers.Input(shape=input_shape, dtype=tf.float32)
4. 排查protobuf版本兼容问题
你当前使用的protobuf==4.25.1与TF1.15存在版本兼容风险(TF1.15原生适配protobuf3.x),可临时降级验证:
pip install protobuf==3.20.3 --force-reinstall
若降级后错误消失,说明是protobuf版本导致的shape解析异常,需保留该兼容版本。
代码修正示例
假设原错误代码片段:
# config是包含字符串键的字典 input_shape = config['input_size'] input_layer = layers.Input(shape=input_shape)
修正后:
input_shape = (config['input_size']['h'], config['input_size']['w'], config['input_size']['c']) input_layer = layers.Input(shape=input_shape, dtype=tf.float32)
内容的提问来源于stack exchange,提问作者Mona Jalal
相关产品推荐
相关产品推荐

