-1

我正在研究图像中的衣服检测。为此,我在 python 上使用包 TorchVision,我想从一个预训练模型开始,并为我的课程(不同种类的衣服)微调它。我的图像在一个 zip 文件中。当我运行函数 train_one_epoch 时,我收到此错误:

IOError: Traceback (most recent call last):
  File "/usr/local/lib/python2.7/dist-packages/torch/utils/data/_utils/worker.py", line 99, in _worker_loop
    samples = collate_fn([dataset[i] for i in batch_indices])
  File "<ipython-input-16-4557e2f6ca8b>", line 51, in __getitem__
    img = Image.open(img_path).convert("RGB")
  File "/usr/local/lib/python2.7/dist-packages/PIL/Image.py", line 2530, in open
    fp = builtins.open(filename, "rb")
IOError: [Errno 20] Not a directory: 'gdrive/My Drive/donnees_kaggle_iMaterialist/train.zip/062fc2042f49d10fa625151cb652c3da.jpg'

这是我的代码的一部分:

python

num_classes = 46 + 1
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')

dataset_train = FashionDataset('gdrive/My Drive/donnees_kaggle_iMaterialist/train.zip/', # "../input/imaterialist-fashion-2019-FGVC6/train/",
                               'gdrive/My Drive/donnees_kaggle_iMaterialist/train.csv.zip', #"../input/imaterialist-fashion-2019-FGVC6/train.csv",
                               256,
                               256,
                               transforms=get_transform(train=True))


model_ft = get_instance_segmentation_model(num_classes)
model_ft.to(device)

data_loader = torch.utils.data.DataLoader(
    dataset_train, batch_size=4, shuffle=True, num_workers=8,
    collate_fn=lambda x: tuple(zip(*x)))

params = [p for p in model_ft.parameters() if p.requires_grad]
optimizer = torch.optim.SGD(params, lr=0.001,
                            momentum=0.9, weight_decay=0.0005)

lr_scheduler = torch.optim.lr_scheduler.StepLR(optimizer,
                                               step_size=5,
                                               gamma=0.1)
num_epochs = 10

for epoch in range(num_epochs):
    train_one_epoch(model_ft, optimizer, data_loader, device, epoch, print_freq=10)
    lr_scheduler.step()

torch.save(model_ft.state_dict(), "model.bin")
4

1 回答 1

1

您收到上述错误是因为train.zip是 zip 文件,而不是文件夹。因此,首先train.zip使用以下代码解压缩文件:

import zipfile
zr1 = zipfile.ZipFile("/content/gdrive/My Drive/donnees_kaggle_iMaterialist/train.zip", 'r')
zr1.extractall("/content/gdrive/My Drive/donnees_kaggle_iMaterialist/train/")
zr1.close()

然后更改'gdrive/My Drive/donnees_kaggle_iMaterialist/train.zip/''gdrive/My Drive/donnees_kaggle_iMaterialist/train/'

此外,train.csv.zip它是一个 zip 文件,在实际使用之前先将其解压缩。使用以下代码:

zr2 = zipfile.ZipFile("/content/gdrive/My Drive/donnees_kaggle_iMaterialist/train.csv.zip", 'r')
zr2.extractall("/content/gdrive/My Drive/donnees_kaggle_iMaterialist/")
zr2.close()

然后,更改'gdrive/My Drive/donnees_kaggle_iMaterialist/train.csv.zip''gdrive/My Drive/donnees_kaggle_iMaterialist/train.csv.csv'.

请注意这里提取的csv文件名将train.csv.csv(带扩展名)作为 zipfile name 的一件事train.csv.zip

于 2019-07-18T15:56:28.687 回答