InvalidArgumentError: slice index 5 of dimension 0 out of bounds. [Op:StridedSlice] name: strided_slice/
InvalidArgumentError: slice index 5 of dimension 0 out of bounds. [Op:StridedSlice] name: strided_slice/
我正在做一个关于图像分类的项目。这里我有 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")
问题是您正在使用 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")
我正在做一个关于图像分类的项目。这里我有 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")
问题是您正在使用 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")