2

I am trying impelement code in pytorch but I get bellow error. my python version is 3.6 and my os is linux ubuntu 16.04 lts. I installed my linux alongside of mac os. We will use torchvision and torch.utils.data packages for loading the data.There are 75 validation images for each class.

OSError                                   Traceback (most recent call    last)
<ipython-input-4-e0e3a841f698> in <module>()
     62 
 63 # Get a batch of training data
---> 64 inputs, classes = next(iter(dset_loaders['train']))
 65 
 66 # Make a grid from batch

/home/zeinab/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py in __next__(self)
172                 self.reorder_dict[idx] = batch
173                 continue
--> 174             return self._process_next_batch(batch)
175 
176     next = __next__  # Python 2 compatibility

/home/zeinab/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py in _process_next_batch(self, batch)
196         self._put_indices()
197         if isinstance(batch, ExceptionWrapper):
--> 198             raise batch.exc_type(batch.exc_msg)
199         return batch
200 

OSError: Traceback (most recent call last):
  File "/home/zeinab/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 32, in _worker_loop
samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/zeinab/anaconda3/lib/python3.6/site-packages/torch/utils/data/dataloader.py", line 32, in <listcomp>
samples = collate_fn([dataset[i] for i in batch_indices])
  File "/home/zeinab/anaconda3/lib/python3.6/site-packages/torchvision-0.1.7-py3.6.egg/torchvision/datasets/folder.py", line 57, in __getitem__
img = self.loader(os.path.join(self.root, path))
  File "/home/zeinab/anaconda3/lib/python3.6/site-packages/torchvision-0.1.7-py3.6.egg/torchvision/datasets/folder.py", line 38, in default_loader
return Image.open(path).convert('RGB')
  File "/home/zeinab/anaconda3/lib/python3.6/site-packages/PIL/Image.py", line 2349, in open
% (filename if filename else fp))
OSError: cannot identify image file 'hymenoptera_data/train/ants/._154124431_65460430f2.jpg'

my code is bellow:

%matplotlib inline
# License: BSD
# Author: Sasank Chilamkurthy

from __future__ import print_function, division

import torch
import torch.nn as nn
import torch.optim as optim
from torch.autograd import Variable
import numpy as np
import torchvision
from torchvision import datasets, models, transforms
import matplotlib.pyplot as plt
import time
import copy
import os
from PIL import Image
import os, sys

plt.ion()   # interactive mode
# Data augmentation and normalization for training 
# Just normalization for validation
data_transforms = {
    'train': transforms.Compose([
        transforms.RandomSizedCrop(224),
        transforms.RandomHorizontalFlip(),
        transforms.ToTensor(),
        transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
    'val': transforms.Compose([
    transforms.Scale(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225])
]),
}

 data_dir = 'hymenoptera_data'
dsets = {x: datasets.ImageFolder(os.path.join(data_dir, x),         data_transforms[x])
     for x in ['train', 'val']}
dset_loaders = {x: torch.utils.data.DataLoader(dsets[x], batch_size=4,
                                           shuffle=True, num_workers=4)
            for x in ['train', 'val']}
dset_sizes = {x: len(dsets[x]) for x in ['train', 'val']}
dset_classes = dsets['train'].classes

use_gpu = torch.cuda.is_available()
def imshow(inp, title=None):
"""Imshow for Tensor."""
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
plt.imshow(inp)
if title is not None:
    plt.title(title)
plt.pause(0.001)  # pause a bit so that plots are updated


# Get a batch of training data
inputs, classes = next(iter(dset_loaders['train']))

# Make a grid from batch
out = torchvision.utils.make_grid(inputs)

imshow(out, title=[dset_classes[x] for x in classes])
MBT
  • 21,733
  • 19
  • 84
  • 102
mary
  • 31
  • 2
  • Does `hymenoptera_data/train/ants/._154124431_65460430f2.jpg` exist? Is it a valid image? – Martin Thoma Apr 15 '17 at 08:19
  • Yes,It exist. I am sure and I opened those – mary Apr 15 '17 at 13:51
  • Try `from PIL import Image;Image.open(open("path/to/file", 'rb'))`. What happens? – Martin Thoma Apr 15 '17 at 13:59
  • should I install another library? such as pip install image . Does it cause from root or Hidden folders? why OSError?? – mary Apr 15 '17 at 13:59
  • As you suggested I try Image.open(open("path/to/file", 'rb')) and I received the image . What should I do now? I tried to solve it with different solutions But It didn't work . I read this line of error OSError: cannot identify image file 'hymenoptera_data/train/ants/._154124431_65460430f2.jpg' and I see my image is 154124431_65460430f2.jpg but here is ._154124431_65460430f2.jpg !!! – mary Apr 15 '17 at 14:23
  • I saw my image is 154124431_65460430f2.jpg but here is ._154124431_65460430f2.jpg !!! that is mean it show and read my image's name in wrong so cannot identify that!! How can I solve that and code read names correct?? – mary Apr 15 '17 at 14:32

1 Answers1

0

I guess the pictures or folder containing the pictures are created in MacOS while you are doing with pillow in Windows environment. Manually create folders (like 'train/ants') and copy the images to the new folders would solve it.