|
在用Pytorch做图像分类的时候,遇到了这个BUG,因为这段代码和网上例子一样仍报错,所以很奇怪,求解答
先贴上我报错的信息和代码
- TypeError: 'builtin_function_or_method' object is not iterable
复制代码- import torch
- import torch.utils.data as Data
- import torchvision
- from torchvision import transforms, datasets
-
- # hyper parameters
- BATCH_SIZE = 8
-
- path = "DogsVSCats"
- # define the data transform
- data_transform = transforms.Compose([
- transforms.ToTensor(), # change the pixel to [0, 1.0]
- transforms.CenterCrop(224),
- transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
- ])
-
- # define the data set in dictionary form
- image_data = {x:datasets.ImageFolder(root=os.path.join(path, x), transform=data_transform)
- for x in ['train', 'valid']}
- # define the data loader
- data_loader = {x:Data.DataLoader(dataset=image_data[x], batch_size=BATCH_SIZE, shuffle=True)
- for x in ['train', 'valid']}
-
- # preview a batch of data in data set
- # x_example, y_example = next(iter(data_loader['train']))
- x_train, y_train = next(iter(data_loader["train"]))
- print("x_example个数:", len(x_train))
- print("y_example个数:", len(y_train))
复制代码
报错的是第三行
- x_train, y_train = next(iter(data_loader["train"]))
复制代码 求大神告知
|
|