fyu / lsun

LSUN Dataset Documentation and Demo Code
531 stars 175 forks source link

Cannot identify image file in LSUN object catetgories #24

Open JFAlexanderS opened 5 years ago

JFAlexanderS commented 5 years ago

I downloaded data from the 20 LSUN object categories listed here: http://tigress-web.princeton.edu/~fy/lsun/public/release/ but I get the following error when I try to open the images: OSError: cannot identify image file <_io.BytesIO object at 0x7f615045c3b8> This surprised me because my code works just fine with the LSUN scene recognition dataset. For reference, here's the rest of the code:

from torch.utils.data import Dataset
from PIL import Image
import os
import os.path
import io
import string
import sys
import lmdb
import pickle
class LSUN_object(Dataset):
    def __init__(self, root, target, transform=None, target_transform=None):
        super(LSUN_object, self).__init__()
        self.transform = transform
        self.target = target
        self.target_transform = target_transform
        self.env = lmdb.open(root, max_readers=1, readonly=True, lock=False, readahead=False, meminit=False, subdir=True)
        with self.env.begin(write=False) as txn: 
            self.length = txn.stat()['entries']
        cache_file = '_cache_' + ''.join(c for c in root if c in string.ascii_letters)
        if os.path.isfile(cache_file):
            self.keys = pickle.load(open(cache_file, "rb"))
        else:
            with self.env.begin(write=False) as txn:
                self.keys = [key for key, _ in txn.cursor()]
            pickle.dump(self.keys, open(cache_file, "wb"))
    def __len__(self):
        return self.length
    def __getitem__(self, index):
        img, target = None, None
        env = self.env
        with env.begin(write=False) as txn: 
            imgbuf = txn.get(self.keys[index])

        buf = io.BytesIO()
        buf.write(imgbuf)
        buf.seek(0)
        # Fails here, but I don't know why? Why would it not be an image in the object dataset? 
        img = Image.open(buf).convert('RGB')
        target = self.target
        if self.transform is not None:
            img = self.transform(img)

        if self.target_transform is not None:
            target = self.target_transform(target)
        return img, target

Is there any significant difference between scenes and objects that I'm not seeing?

penguinbing commented 4 years ago

@fyu Can you help with this problem?

fyu commented 4 years ago

This seems to be webp support problem. webp needs to be installed before Pillow is installed. For example, if you are on Mac, you can do brew install webp before pip install Pillow. A full installation guide is at https://pillow.readthedocs.io/en/3.0.x/installation.html. Make sure webp is enabled.

yunjey commented 4 years ago

@JFAlexanderS I got the same error as you. Did you solve this problem?

EDIT: Solved the problem by installing the latest version of Pillow (e.g. pip install pillow<7)