错误列表索引超出与标签文件有关的范围

我正在尝试自己学习迁移学习(区分猫和狗)。 而且我不明白是什么引起了与文件标签相关的索引问题。 下面是我的代码。

IMG_DIM = (150,150)
test_files = glob.glob('test_data/*')
test_imgs = [img_to_array(load_img(img,target_size=IMG_DIM)) for img in test_files]
test_imgs = np.array(test_imgs)
test_labels = [fn.split('/')[1].split('.')[0].strip() for fn in test_files]
test_imgs_scaled = test_imgs.astype('float32')
test_imgs_scaled /= 255
test_labels_enc = class2num_label_transformer(test_labels)
print('Test dataset shape:',test_imgs.shape)
print(test_labels[0:4],test_labels_enc[0:4])

这是错误消息。

    ---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-166-d351a31bcadc> in <module>
      4 test_imgs = [img_to_array(load_img(img,target_size=IMG_DIM)) for img in test_files]
      5 test_imgs = np.array(test_imgs)
----> 6 test_labels = [fn.split('/')[1].split('.')[0].strip() for fn in test_files]
      7 
      8 test_imgs_scaled = test_imgs.astype('float32')

<ipython-input-166-d351a31bcadc> in <listcomp>(.0)
      4 test_imgs = [img_to_array(load_img(img,target_size=IMG_DIM)) for img in test_files]
      5 test_imgs = np.array(test_imgs)
----> 6 test_labels = [fn.split('/')[1].split('.')[0].strip() for fn in test_files]
      7 
      8 test_imgs_scaled = test_imgs.astype('float32')

IndexError:列表索引超出范围

我还附加了包含此代码中使用的文件的文件夹。

错误列表索引超出与标签文件有关的范围

baizhongzheng 回答:错误列表索引超出与标签文件有关的范围

暂时没有好的解决方案,如果你有好的解决方案,请发邮件至:iooj@foxmail.com
本文链接:https://www.f2er.com/3156890.html

大家都在问