【问题标题】:IndexError:Index 10 is out of bounds for axis 1 with size 10IndexError:索引 10 超出轴 1 的范围,大小为 10
【发布时间】:2018-06-05 07:42:58
【问题描述】:

我正在使用 CNN 将图像分类为 30 个不同的类别,并且在运行 CNN 时,我得到以下回溯-

Traceback (most recent call last):
File "thapar.py", line 165, in <module>
Y = np_utils.to_categorical(labels, num_classes)
File "/home/rishav/anaconda3/envs/tensorflow/lib/python3.6/site-packages/keras/utils/np_utils.py", line 31, in to_categorical
categorical[np.arange(n), y] = 1
IndexError: index 10 is out of bounds for axis 1 with size 10

Python 3.6 Keras 2.1.5 使用 tensorflow 后端 Ubuntu 16.04

我探索了其他类似的问题,但无法找到问题的相似之处。

如何解决这个问题?

编辑:

num_classes = 30
num_of_samples = img_data.shape[0]
labels = np.ones((num_of_samples,),dtype='int64')
labels[0:20]=0
labels[20:40]=1
labels[40:60]=2
labels[60:80]=3
labels[80:100]=4
labels[100:120]=5
labels[120:140]=6
labels[140:160]=7
labels[160:180]=8
labels[180:200]=9
labels[200:220]=10
labels[220:240]=11
labels[240:260]=12
labels[260:280]=13
labels[280:300]=14
labels[300:320]=15
labels[320:340]=16
labels[340:360]=17
labels[360:380]=18
labels[380:400]=19
labels[400:420]=20
labels[420:440]=21
labels[440:460]=22
labels[460:480]=23
labels[480:500]=24
labels[500:520]=25
labels[520:540]=26
labels[540:560]=27
labels[560:580]=28
labels[580:600]=29

names = 

['bent','bowl','claw','fist','good','thumbs_up','A','B','C','D','E','F','G','H','I','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y']

Y = np_utils.to_categorical(labels, num_classes)

【问题讨论】:

  • 请分享与您的问题相关的隔离代码。 (labelsnum_classes 包含什么?)
  • 已编辑,请检查
  • 您的代码正在为我工​​作,按预期生成形状为(600, 30)Y。但是,根据您的错误跟踪,在我看来,您使用的 num_classes = 10labels 包含高于 9 的值。

标签: python-3.x keras convolutional-neural-network


【解决方案1】:

最近,我遇到了同样的问题,然后我知道它只对一些图像给出错误,我删除了这些图像,然后它对我来说非常有用。

【讨论】:

猜你喜欢
  • 2016-07-29
  • 2019-07-31
  • 2015-08-06
  • 2021-05-16
  • 1970-01-01
  • 1970-01-01
  • 2018-04-10
  • 2018-05-09
  • 2020-01-16
相关资源
最近更新 更多