def create_train_data():
    training_data = []
    for img in tqdm(os.listdir(TRAIN_DIR)):
        label = label_img(img)
        path = os.path.join(TRAIN_DIR, img)
        img = cv2.imread(path)
        img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
        training_data.append([np.array(img), np.array(label)])
    shuffle(training_data)
    np.save('train_data.npy', training_data)
    return training_data

train_data = create_train_data()

train = train_data[:-500]
test = train_data[-500:]

X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
Y = [i[1] for i in train]

test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,1)
test_y = [i[1] for i in test]

model.fit({'input': X}, {'targets': Y}, n_epoch=3, validation_set=({'input': test_x}, {'targets': test_y}),
    snapshot_step=500, show_metric=True, run_id=MODEL_NAME)

**Getting following error**


Traceback (most recent call last):
Training samples: 168132
Validation samples: 1500
--
  File "C:\Users\Anas\AppData\Local\Programs\Python\Python36\lib\threading.py", line 916, in _bootstrap_inner
    self.run()
  File "C:\Users\Anas\AppData\Local\Programs\Python\Python36\lib\threading.py", line 864, in run
    self._target(*self._args, **self._kwargs)
  File "C:\Users\Anas\PycharmProjects\Minorproject\venv\lib\site-packages\tflearn\data_flow.py", line 187, in fill_feed_dict_queue
    data = self.retrieve_data(batch_ids)
  File "C:\Users\Anas\PycharmProjects\Minorproject\venv\lib\site-packages\tflearn\data_flow.py", line 222, in retrieve_data
    utils.slice_array(self.feed_dict[key], batch_ids)
  File "C:\Users\Anas\PycharmProjects\Minorproject\venv\lib\site-packages\tflearn\utils.py", line 187, in slice_array
    return X[start]
IndexError: index 69804 is out of bounds for axis 0 with size 56044


我拥有32X32像素RGB图像(4波段)的所有图像。我收到上述错误。我不知道为什么它越界,不是因为图像太多吗?
有谁知道如何解决这个问题?

最佳答案

您正在错误地重塑它。您想修饰ARGB图像,但要将X重塑为reshape(-1,IMG_SIZE,IMG_SIZE,1),而应该对4通道图像进行reshape(-1,IMG_SIZE,IMG_SIZE,4),并且与test_x变量相同。

编辑部分代码:

X = np.array([i[0] for i in train]).reshape(-1,IMG_SIZE,IMG_SIZE,4)
test_x = np.array([i[0] for i in test]).reshape(-1,IMG_SIZE,IMG_SIZE,4)

关于python-3.x - IndexError:索引69791超出了轴0的范围,大小为56044,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/50843392/

10-12 22:15