Skip to main content
 首页 » 编程设计

python之类型错误 : cannot unpack non-iterable int object

2024年12月31日8JustinYoung

我正在尝试使用 pyTorch 制作我的第一个 CNN,并且正在关注在线帮助和人们已经编写的代码。我正在尝试重现他们的结果。我为此使用了 Kaggle Dogs Breed 数据集,下面是我得到的错误。 trainloader 不返回我的图像和标签,任何获取它们的尝试都会导致错误:

Traceback (most recent call last): 
  File "E:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\helpers\pydev\pydevd.py", line 1664, in <module> 
    main() 
  File "E:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\helpers\pydev\pydevd.py", line 1658, in main 
    globals = debugger.run(setup['file'], None, None, is_module) 
  File "E:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\helpers\pydev\pydevd.py", line 1068, in run 
    pydev_imports.execfile(file, globals, locals)  # execute the script 
  File "E:\Program Files\JetBrains\PyCharm Community Edition 2018.2.4\helpers\pydev\_pydev_imps\_pydev_execfile.py", line 18, in execfile 
    exec(compile(contents+"\n", file, 'exec'), glob, loc) 
  File "C:/Users/sbzfk/PycharmProjects/my_FCN_attempt/Kaggle_Dogs_Competition.py", line 85, in <module> 
    img, label = next(iter(train_loader)) 
  File "C:\Users\sbzfk\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py", line 314, in __next__ 
    batch = self.collate_fn([self.dataset[i] for i in indices]) 
  File "C:\Users\sbzfk\AppData\Local\Programs\Python\Python37\lib\site-packages\torch\utils\data\dataloader.py", line 314, in <listcomp> 
    batch = self.collate_fn([self.dataset[i] for i in indices]) 
  File "C:/Users/sbzfk/PycharmProjects/my_FCN_attempt/Kaggle_Dogs_Competition.py", line 42, in __getitem__ 
    img = self.transform(img) 
  File "C:\Users\sbzfk\AppData\Local\Programs\Python\Python37\lib\site-packages\torchvision\transforms.py", line 34, in __call__ 
    img = t(img) 
  File "C:\Users\sbzfk\AppData\Local\Programs\Python\Python37\lib\site-packages\torchvision\transforms.py", line 187, in __call__ 
    w, h = img.size 
TypeError: cannot unpack non-iterable int object 

下面是我的代码:

class DogsDataset(Dataset): 
 
    def __init__(self, filenames, labels, root_dir, transform=None): 
        assert len(filenames) == len(labels)        # if the two are not of equal length throw an error 
        self.filenames = filenames 
        self.labels = labels 
        self.root_dir = root_dir 
        self.transform = transform 
 
    def __len__(self): 
        return len(self.filenames) 
 
    def __getitem__(self, idx): 
        this_img = join(self.root_dir, 'train', self.filenames[idx]+'.jpg') 
        print(this_img) 
        img = io.imread(this_img) 
        label = self.labels[idx] 
        print(label) 
 
        if self.transform: 
            img = self.transform(img) 
 
        return [img, label] 
 
 
 
batch_size = 64 
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") 
 
dataset_root = expanduser(join('~', 'Documents', 'kaggle_dogs_dataset')) 
# join will intelligently join directories irrespective of OS, and expanduser will 
# replace with /home/ in linux or the username in Windows 
 
csv_file = pd.read_csv(join(dataset_root, 'labels.csv'))        # csv file has two columns, id which are filenames and breed which are labels 
filenames = csv_file.id.values              # convert that column to an array, id is the column name and values converty to numpy array 
# le = LabelEncoder() 
# labels = le.fit_transform(csv_file.breed)           # this will just encode the names between 0 to models-1 , basically changing strings to integers 
labels = csv_file.breed.values 
 
filenames_train, filenames_eval, labels_train, labels_eval = train_test_split(filenames, labels, 
                                                                              test_size=0.1, stratify=labels)       # this is an import from sklearn as the name implies, it randomly splits data into train and eval, 10% of it to test and rest train 
 
data_transform = transforms.Compose([transforms.Scale(224), 
                                     transforms.CenterCrop(224), 
                                     transforms.ToTensor(), 
                                     transforms.Normalize((0.485, 0.456, 0.406), (0.229, 0.224, 0.225))]) 
 
dog_train = DogsDataset(filenames_train, labels_train, dataset_root, transform=data_transform) 
train_loader = DataLoader(dog_train, batch_size, shuffle=True) 
 
dog_eval = DogsDataset(filenames_eval, labels_eval, dataset_root, transform=data_transform) 
eval_loader = DataLoader(dog_eval, batch_size, shuffle=True) 
 
 
def im_show(axis, inp): 
    """Denormalize and show""" 
    inp = inp.numpy().transpose((1, 2, 0)) 
    mean = np.array([0.485, 0.456, 0.406]) 
    std = np.array([0.229, 0.224, 0.225]) 
    inp = std * inp + mean 
    axis.imshow(inp) 
 
 
img, label = next(iter(train_loader)) 
print(img.size(), label.size()) 
fig = plt.figure(1, figsize=(16, 4)) 
grid = ImageGrid(fig, 111, nrows_ncols=(1, 4), axes_pad=0.05) 
for i in range(img.size()[0]): 
    ax = grid[i] 
    im_show(ax, img[i]) 

我尝试逐行调试它并使用 transform=none 我似乎读取了所有图像,只有 transform=data_transform 我似乎得到了这个错误。

请您参考如下方法:

您似乎正在使用 torchvision 的图像转换。其中一些转换期望作为输入 PIL.Image对象,而不是张量或 numpy 数组。
您正在使用 io.imread 读取图像文件,我怀疑此 io 不是 PIL.Image 导致的 numpy 数组。< br/> 确保将 PIL.Image 对象传递给 transforms 并且您的 DogsDataset 返回图像的 3D 张量(C-H-W 形)。