我正在尝试自己学习迁移学习(区分猫和狗)。 而且我不明白是什么引起了与文件标签相关的索引问题。 下面是我的代码。
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:列表索引超出范围