Object detection torchvision : IOError: [Errno 2] No such file or directory:
I'm working on clothes detection in an image. For this, i'm using the package TorchVision on python, and i want to start from a pretrained model and to fintetune it for my classes (different kind of clothes). My images are in a zip file. When i run the function train_one_epoch, i obtain this error :


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 "&lt;ipython-input-16-4557e2f6ca8b&gt;", 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'



Here is a part of my code :

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")


You are getting above error because train.zip is a zip file, not a folder. So, first unzip train.zip file using below code:

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()


And, then change 'gdrive/My Drive/donnees_kaggle_iMaterialist/train.zip/' to 'gdrive/My Drive/donnees_kaggle_iMaterialist/train/'

Also, train.csv.zip is a zip file, extract it first before actually using it. Use below code:

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()


And, then change 'gdrive/My Drive/donnees_kaggle_iMaterialist/train.csv.zip' to something like 'gdrive/My Drive/donnees_kaggle_iMaterialist/train.csv.csv'. 

Note one thing here that extracted csv file name would be train.csv.csv(with extension) as zipfile name was train.csv.zip. 
