Google Colab中的KeyError 0

时间:2020-08-01 19:40:27

标签: python python-3.x deep-learning google-colaboratory keyerror

即使我没有使用python字典,我也得到KeyError: 0

这是我在尝试训练CNN模型时遇到的错误。

ERROR

这是相关的代码段:

epochs = 5
batch_size = 64
num = len(x_train)

for epoch in range(epochs):
  for i in tqdm(range(int(num / batch_size))):
    train_x_ep, train_y_ep= TrainImageLoad(batch_size)
    train_x_ep = np.array(train_x_ep)
    train_x_ep = train_x_ep.astype(np.float16)
    train_y_ep = np.array(train_y_ep)
    train_y_ep = train_y_ep.astype(np.float16)
    train_y_ep.reshape(-1, 1)

    test_x_ep, test_y_ep = TestImageLoad(batch_size)  # THIS IS LINE 40
    test_x_ep = np.array(test_x_ep)
    test_x_ep = test_x_ep.astype(np.float16)
    test_y_ep = np.array(test_y_ep)
    test_y_ep = test_y_ep.astype(np.float16)
    test_y_ep.reshape(-1, 1)

    model.fit(train_x_ep, train_y_ep, validation_data = [test_x_ep, test_y_ep])

TestImageLoad函数的定义如下:

def TestImageLoad(batch_size):
  global Tester
  global x_test
  global y_test
  xt = []
  yt = []
  l = len(x_test)
  for i in range(batch_size):
    img1 = image.load_img(data_path + str(x_test[(i + Tester) % l]) +'.jpg', color_mode = 'rgb', target_size = [224, 224])
    img1 = image.image.img_to_array(img1) / 255.0
    img1 = img1.astype(np.float16)
    xt.append(img1)
    yt.append(y_test[(i + Tester) % l]) #*np.pi/180
    Tester += 1 

  return xt, yt

我尝试在Google Colab,GPU上运行它,并且每次它向我显示此错误时1 如何克服这个错误?

0 个答案:

没有答案