我正在尝试使用pyTorch创建我的第一个CNN,并且正在关注人们已经编写的在线帮助和代码。我正在尝试重现他们的结果。我为此使用Kaggle狗品种数据集,以下是我得到的错误。培训人员不会返回我的图像和标签,任何试图获取它们的尝试都会导致错误:
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
时才能读取此错误。
答案 0 :(得分:1)
似乎您正在使用Torchvision的图像变换。其中一些转换期望将PIL.Image
对象而不是张量或numpy数组作为输入。
您正在使用var fb = d3.select('#foobar');
fb.select('text').remove();
fb.append('text')
.text(value)
.attr('x', xPosition)
.attr('y', yPosition);
来读取图像文件,但我怀疑此io.imread
不是io
导致了一个numpy数组。
确保将PIL.Image
个对象传递给PIL.Image
,并确保transforms
返回图像的3D张量(C-H-W形)。