lsun.py 5.54 KB
Newer Older
1
import io
soumith's avatar
soumith committed
2
import os.path
3
import pickle
soumith's avatar
soumith committed
4
import string
5
from collections.abc import Iterable
6
from typing import Any, Callable, cast, List, Optional, Tuple, Union
7
8
9

from PIL import Image

10
from .utils import verify_str_arg, iterable_to_str
11
from .vision import VisionDataset
12

13

14
class LSUNClass(VisionDataset):
15
    def __init__(
16
        self, root: str, transform: Optional[Callable] = None, target_transform: Optional[Callable] = None
17
    ) -> None:
soumith's avatar
soumith committed
18
        import lmdb
Jason Park's avatar
Jason Park committed
19

20
        super().__init__(root, transform=transform, target_transform=target_transform)
21
22

        self.env = lmdb.open(root, max_readers=1, readonly=True, lock=False, readahead=False, meminit=False)
soumith's avatar
soumith committed
23
        with self.env.begin(write=False) as txn:
24
25
            self.length = txn.stat()["entries"]
        cache_file = "_cache_" + "".join(c for c in root if c in string.ascii_letters)
soumith's avatar
soumith committed
26
        if os.path.isfile(cache_file):
27
            self.keys = pickle.load(open(cache_file, "rb"))
soumith's avatar
soumith committed
28
29
        else:
            with self.env.begin(write=False) as txn:
30
                self.keys = [key for key in txn.cursor().iternext(keys=True, values=False)]
31
            pickle.dump(self.keys, open(cache_file, "wb"))
soumith's avatar
soumith committed
32

33
    def __getitem__(self, index: int) -> Tuple[Any, Any]:
soumith's avatar
soumith committed
34
35
36
37
38
        img, target = None, None
        env = self.env
        with env.begin(write=False) as txn:
            imgbuf = txn.get(self.keys[index])

Philip Meier's avatar
Philip Meier committed
39
        buf = io.BytesIO()
soumith's avatar
soumith committed
40
41
        buf.write(imgbuf)
        buf.seek(0)
42
        img = Image.open(buf).convert("RGB")
soumith's avatar
soumith committed
43
44
45
46
47
48
49
50
51

        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

52
    def __len__(self) -> int:
soumith's avatar
soumith committed
53
54
        return self.length

55

56
class LSUN(VisionDataset):
Nicolas Hug's avatar
Nicolas Hug committed
57
58
59
60
    """`LSUN <https://www.yf.io/p/lsun>`_ dataset.

    You will need to install the ``lmdb`` package to use this dataset: run
    ``pip install lmdb``
61
62

    Args:
Jason Park's avatar
Jason Park committed
63
        root (string): Root directory for the database files.
64
        classes (string or list): One of {'train', 'val', 'test'} or a list of
65
            categories to load. e,g. ['bedroom_train', 'church_outdoor_train'].
66
67
68
69
        transform (callable, optional): A function/transform that  takes in an PIL image
            and returns a transformed version. E.g, ``transforms.RandomCrop``
        target_transform (callable, optional): A function/transform that takes in the
            target and transforms it.
soumith's avatar
soumith committed
70
    """
71

72
    def __init__(
73
74
75
76
77
        self,
        root: str,
        classes: Union[str, List[str]] = "train",
        transform: Optional[Callable] = None,
        target_transform: Optional[Callable] = None,
78
    ) -> None:
79
        super().__init__(root, transform=transform, target_transform=target_transform)
80
81
82
83
84
        self.classes = self._verify_classes(classes)

        # for each class, create an LSUNClassDataset
        self.dbs = []
        for c in self.classes:
85
            self.dbs.append(LSUNClass(root=os.path.join(root, f"{c}_lmdb"), transform=transform))
86
87
88
89
90
91
92
93
94

        self.indices = []
        count = 0
        for db in self.dbs:
            count += len(db)
            self.indices.append(count)

        self.length = count

95
    def _verify_classes(self, classes: Union[str, List[str]]) -> List[str]:
96
97
98
99
100
101
102
103
104
105
106
107
108
        categories = [
            "bedroom",
            "bridge",
            "church_outdoor",
            "classroom",
            "conference_room",
            "dining_room",
            "kitchen",
            "living_room",
            "restaurant",
            "tower",
        ]
        dset_opts = ["train", "val", "test"]
Jason Park's avatar
Jason Park committed
109

110
        try:
111
            classes = cast(str, classes)
112
            verify_str_arg(classes, "classes", dset_opts)
113
            if classes == "test":
soumith's avatar
soumith committed
114
115
                classes = [classes]
            else:
116
                classes = [c + "_" + classes for c in categories]
117
        except ValueError:
118
            if not isinstance(classes, Iterable):
119
                msg = "Expected type str or Iterable for argument classes, but got type {}."
120
121
122
                raise ValueError(msg.format(type(classes)))

            classes = list(classes)
123
            msg_fmtstr_type = "Expected type str for elements in argument classes, but got type {}."
soumith's avatar
soumith committed
124
            for c in classes:
125
                verify_str_arg(c, custom_msg=msg_fmtstr_type.format(type(c)))
126
127
                c_short = c.split("_")
                category, dset_opt = "_".join(c_short[:-1]), c_short[-1]
128

129
                msg_fmtstr = "Unknown value '{}' for {}. Valid values are {{{}}}."
130
                msg = msg_fmtstr.format(category, "LSUN class", iterable_to_str(categories))
131
132
133
134
                verify_str_arg(category, valid_values=categories, custom_msg=msg)

                msg = msg_fmtstr.format(dset_opt, "postfix", iterable_to_str(dset_opts))
                verify_str_arg(dset_opt, valid_values=dset_opts, custom_msg=msg)
soumith's avatar
soumith committed
135

136
        return classes
soumith's avatar
soumith committed
137

138
    def __getitem__(self, index: int) -> Tuple[Any, Any]:
139
140
141
142
143
144
145
        """
        Args:
            index (int): Index

        Returns:
            tuple: Tuple (image, target) where target is the index of the target category.
        """
soumith's avatar
soumith committed
146
147
148
149
150
151
        target = 0
        sub = 0
        for ind in self.indices:
            if index < ind:
                break
            target += 1
Zhou Le's avatar
Zhou Le committed
152
            sub = ind
soumith's avatar
soumith committed
153
154
155
156
157
158
159

        db = self.dbs[target]
        index = index - sub

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

soumith's avatar
soumith committed
160
161
        img, _ = db[index]
        return img, target
soumith's avatar
soumith committed
162

163
    def __len__(self) -> int:
soumith's avatar
soumith committed
164
165
        return self.length

166
    def extra_repr(self) -> str:
167
        return "Classes: {classes}".format(**self.__dict__)