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

Python 错误列表索引超出与标签文件相关的范围,python,arrays,numpy,Python,Arrays,Numpy,我正在尝试自己学习迁移学习(区分猫和狗)。 我不明白是什么导致了与文件标签相关的索引问题。 下面是我的代码 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.sp

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

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')
---------------------------------------------------------------------------
索引器回溯(最后一次最近调用)
在里面
4测试\u imgs=[img\u到\u数组(加载\u img(img,目标\u size=img\u DIM)),用于测试\u文件中的img]
5测试imgs=np.阵列(测试imgs)
---->6测试标签=[fn.split('/')[1]。split('.')[0]。测试文件中fn的strip()
7.
8 test\u imgs\u scaled=test\u imgs.astype('float32'))
英寸(.0)
4测试\u imgs=[img\u到\u数组(加载\u img(img,目标\u size=img\u DIM)),用于测试\u文件中的img]
5测试imgs=np.阵列(测试imgs)
---->6测试标签=[fn.split('/')[1]。split('.')[0]。测试文件中fn的strip()
7.
8 test\u imgs\u scaled=test\u imgs.astype('float32'))
索引器:列表索引超出范围

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

.split('/')[1]
一定是问题所在。您的一个
fn
中没有任何“/”。这与您的机器学习无关
fn.split('/')[1]。split
假设每个路径中都有一个
/
,这可能不是真的