【问题标题】:InvalidArgumentError: slice index 5 of dimension 0 out of bounds. [Op:StridedSlice] name: strided_slice/InvalidArgumentError:维度 0 的切片索引 5 超出范围。 [Op:StridedSlice] 名称:strided_slice/
【发布时间】:2021-12-08 01:47:21
【问题描述】:

我正在做一个图像分类项目。这里我有 30 张图片,当我尝试绘制这些图片时,会出现以下错误,

InvalidArgumentError: slice index 5 of dimension 0 out of bounds.
[Op:StridedSlice] name: strided_slice/

下面是我的代码:

BATCH_SIZE = 5
IMAGE_SIZE = 256
CHANNELS=3  
EPOCHS=10

train_ds = tf.keras.utils.image_dataset_from_directory(
path_to_data,
validation_split=0.2,
subset="training",
seed=123,
image_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE)

val_ds = tf.keras.utils.image_dataset_from_directory(
path_to_data,
validation_split=0.2,
subset="validation",
seed=123,
image_size=(IMAGE_SIZE, IMAGE_SIZE),
batch_size=BATCH_SIZE)

for image_batch, labels_batch in train_ds:
print(image_batch.shape)
print(labels_batch.shape)
break

plt.figure(figsize=(10, 10))
for images, labels in train_ds.take(1):
   for i in range(9):
       ax = plt.subplot(3, 3, i + 1)
       plt.imshow(images[i].numpy().astype('uint8'))
       plt.title(class_names[labels[i]])
       plt.axis("off")

错误:

InvalidArgumentError: slice index 5 of dimension 0 out of bounds. [Op:StridedSlice] 
name: strided_slice/

追溯:

InvalidArgumentError                      Traceback (most recent call last)
<ipython-input-74-385157730873> in <module>()
  5   for i in range(9):
  6     ax = plt.subplot(3, 3, i + 1)
----> 7     plt.imshow(images[i].numpy().astype('uint8'))
  8     plt.title(class_names[labels[i]])
  9     plt.axis("off")

【问题讨论】:

    标签: python-3.x tensorflow keras tensorflow2.0 image-classification


    【解决方案1】:

    问题是您正在使用train_ds.take(1) 从您的数据集中获取一个批次,其中包含BATCH_SIZE = 5。如果您想在 3x3 图中显示 9 张图像,只需将您的 BATCH_SIZE 更改为 9。或者,您可以调整您想要创建的 subplots 的数量,如下所示:

    BATCH_SIZE = 5
    plt.figure(figsize=(10, 10))
    for images, labels in train_ds.take(1):
       for i in range(BATCH_SIZE):
           ax = plt.subplot(1, 5, i + 1)
           plt.imshow(images[i].numpy().astype('uint8'))
           plt.title(class_names[labels[i]])
           plt.axis("off")
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2022-06-28
      • 2019-09-21
      • 2015-02-01
      • 1970-01-01
      • 1970-01-01
      • 2016-12-16
      • 2012-04-07
      相关资源
      最近更新 更多