问题在于在构造测试集数据列表时,文件名是直接使用了文件夹下的文件名,没有加上文件夹的路径,导致在后面读取图片时出错。修改后的代码如下:

构造测试集数据列表

test_datas = [] test_dir = '/kaggle/input/shisuncat/cat_12_test/cat_12_test' for i in os.listdir(test_dir): test_datas.append(os.path.join(test_dir, i)) print('test_datas len:', len(test_datas))

写入test.txt

data_to_txt(test_datas, '/kaggle/working/test.txt')

构造数据集

class CatDataset(paddle.io.Dataset): def init(self, txtpath, mode='train', transform=None): super(CatDataset, self).init()

    assert mode in ['train', 'val', 'test'], "mode is one of ['train', 'val', 'test']"
    self.mode = mode
    self.transform = transform
    self.data = []

    with open(txtpath, 'r') as f:
        for line in f.readlines():
            line = line.strip()
            if mode != 'test':
                self.data.append([line.split('\t')[0], line.split('\t')[1]])
            else:
                self.data.append(os.path.join(test_dir, line))

def __getitem__(self, idx):
    if self.mode != 'test':
        img = Image.open(self.data[idx][0]).convert('RGB')
        label = self.data[idx][1]
        if self.transform:
            img = self.transform(img)
        return img.astype('float32'), np.array(label, dtype='int64')
    else:
        img = Image.open(self.data[idx]).convert('RGB')
        if self.transform:
            img = self.transform(img)
        return img.astype('float32')

def __len__(self):
    return len(self.data
以下源代码怎么报错了?请给出修改后的代码import osimport numpy as npimport pandas as pdimport paddleimport paddlevisiontransforms as Tfrom PIL import Image# 数据列表写入txt文件def data_to_txtdatas save_path with opensave_path

原文地址: https://www.cveoy.top/t/topic/hfbQ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录