索引器错误:索引917超出大小为11的轴0的界限,位于以十为单位的npargmax上

2024-04-25 23:09:40 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用示例here 对图像进行分类。我有2147张图片,属于11个班级。我正在对这批图像运行分类器。到目前为止,程序运行良好,但我无法分配标签,因为我在代码中遇到以下错误:labels_batch=image_labels[np.argmax(result_batch, axis=-1)]

Traceback (most recent call last): > > labels_batch=image_labels[np.argmax(result_batch, axis=-1)] IndexError: index 917 is out of bounds for axis 0 with size 11

更多信息: 我的标签是categories = ["Categorical_Boxplot", "Column_Charts", "Dendogram", "Heatmap", "Line_Chart", "Map","Node-Link_Diagram", "Ordination_Scatterplot", "Pie_Chart", "Scatterplot", "Stacked_Area_Chart"]

下面是我如何将这个标签分配给分类器的

image_labels = np.array(categories)

result_batch = classifier_model.predict(image_batch)
labels_batch = image_labels[np.argmax(result_batch, axis=-1)]
labels_batch

result_batch.shape

(32, 1001)

我的数据的形状

Image batch shape: (32, 224, 224, 3) Label batch shape: (32, 11)

我不知道我哪里出了问题,我该怎么解决?我已经尝试将图像标签附加到标签批处理,而不是用=来分配它。但没有起作用。你知道吗

分类器:

classifier_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/2" #@param {type:"string"}
IMAGE_SIZE = hub.get_expected_image_size(hub.Module(classifier_url))
classifier_layer = layers.Lambda(classifier, input_shape=IMAGE_SIZE + [3])
classifier_model = tf.keras.Sequential([classifier_layer])
classifier_model.summary()

Tags: 图像imagesizelabelsmodel分类器npbatch
1条回答
网友
1楼 · 发布于 2024-04-25 23:09:40

您的模型定义不完整。教程中的模型有1001个类,而您的模型只有11个类,所以您应该像这样在模型上附加一个新的classfiction头。你知道吗

classifier_url = "https://tfhub.dev/google/imagenet/mobilenet_v2_100_224/classification/2" #@param {type:"string"}
IMAGE_SIZE = hub.get_expected_image_size(hub.Module(classifier_url))
classifier_layer = layers.Lambda(classifier, input_shape=IMAGE_SIZE + [3])
classifier_model = tf.keras.Sequential([classifier_layer, layers.Dense(11, activation='softmax')])
classifier_model.summary()

相关问题 更多 >