Object detection torchvision : IOError: [Errno 2] No such file or directory:
Object detection torchvision : IOError: [Errno 2] No such file or directory:
我正在研究图像中的衣服检测。为此,我在 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")
您遇到上述错误是因为 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
(带扩展名),因为 zip 文件名是 train.csv.zip
。
我正在研究图像中的衣服检测。为此,我在 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")
您遇到上述错误是因为 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
(带扩展名),因为 zip 文件名是 train.csv.zip
。