|
| 1 | +import torch.utils.data as data |
| 2 | +from PIL import Image |
| 3 | +import os |
| 4 | +import os.path |
| 5 | +import StringIO |
| 6 | +import string |
| 7 | +import sys |
| 8 | +if sys.version_info[0] == 2: |
| 9 | + import cPickle as pickle |
| 10 | +else: |
| 11 | + import pickle |
| 12 | + |
| 13 | +class LSUNClassDataset(data.Dataset): |
| 14 | + def __init__(self, db_path, transform=None, target_transform=None): |
| 15 | + import lmdb |
| 16 | + self.db_path = db_path |
| 17 | + self.env = lmdb.open(db_path, map_size=1099511627776, |
| 18 | + max_readers=100, readonly=True) |
| 19 | + with self.env.begin(write=False) as txn: |
| 20 | + self.length = txn.stat()['entries'] |
| 21 | + cache_file = '_cache_' + db_path.replace('/', '_') |
| 22 | + if os.path.isfile(cache_file): |
| 23 | + self.keys = pickle.load( open( cache_file, "rb" ) ) |
| 24 | + else: |
| 25 | + with self.env.begin(write=False) as txn: |
| 26 | + self.keys = [ key for key, _ in txn.cursor() ] |
| 27 | + pickle.dump( self.keys, open( cache_file, "wb" ) ) |
| 28 | + self.transform = transform |
| 29 | + self.target_transform = target_transform |
| 30 | + |
| 31 | + def __getitem__(self, index): |
| 32 | + img, target = None, None |
| 33 | + env = self.env |
| 34 | + with env.begin(write=False) as txn: |
| 35 | + imgbuf = txn.get(self.keys[index]) |
| 36 | + |
| 37 | + buf = StringIO.StringIO() |
| 38 | + buf.write(imgbuf) |
| 39 | + buf.seek(0) |
| 40 | + img = Image.open(buf).convert('RGB') |
| 41 | + |
| 42 | + if self.transform is not None: |
| 43 | + img = self.transform(img) |
| 44 | + |
| 45 | + if self.target_transform is not None: |
| 46 | + target = self.target_transform(target) |
| 47 | + |
| 48 | + return img, target |
| 49 | + |
| 50 | + def __len__(self): |
| 51 | + return self.length |
| 52 | + |
| 53 | + def __repr__(self): |
| 54 | + return self.__class__.__name__ + ' (' + self.db_path + ')' |
| 55 | + |
| 56 | +class LSUNDataset(data.Dataset): |
| 57 | + """ |
| 58 | + db_path = root directory for the database files |
| 59 | + classes = 'train' | 'val' | 'test' | ['bedroom_train', 'church_train', ...] |
| 60 | + """ |
| 61 | + def __init__(self, db_path, classes='train', |
| 62 | + transform=None, target_transform=None): |
| 63 | + categories = ['bedroom', 'bridge', 'church_outdoor', 'classroom', |
| 64 | + 'conference_room', 'dining_room', 'kitchen', |
| 65 | + 'living_room', 'restaurant', 'tower'] |
| 66 | + dset_opts = ['train', 'val', 'test'] |
| 67 | + self.db_path = db_path |
| 68 | + if type(classes) == str and classes in dset_opts: |
| 69 | + classes = [c + '_' + classes for c in categories] |
| 70 | + if type(classes) == list: |
| 71 | + for c in classes: |
| 72 | + c_short = c.split('_') |
| 73 | + c_short.pop(len(c_short) - 1) |
| 74 | + c_short = string.join(c_short, '_') |
| 75 | + if c_short not in categories: |
| 76 | + raise(ValueError('Unknown LSUN class: ' + c_short + '.'\ |
| 77 | + 'Options are: ' + str(categories))) |
| 78 | + c_short = c.split('_') |
| 79 | + c_short = c_short.pop(len(c_short) - 1) |
| 80 | + if c_short not in dset_opts: |
| 81 | + raise(ValueError('Unknown postfix: ' + c_short + '.'\ |
| 82 | + 'Options are: ' + str(dset_opts))) |
| 83 | + else: |
| 84 | + raise(ValueError('Unknown option for classes')) |
| 85 | + self.classes = classes |
| 86 | + |
| 87 | + # for each class, create an LSUNClassDataset |
| 88 | + self.dbs = [] |
| 89 | + for c in self.classes: |
| 90 | + self.dbs.append(LSUNClassDataset( |
| 91 | + db_path = db_path + '/' + c + '_lmdb', |
| 92 | + transform = transform)) |
| 93 | + |
| 94 | + self.indices = [] |
| 95 | + count = 0 |
| 96 | + for db in self.dbs: |
| 97 | + count += len(db) |
| 98 | + self.indices.append(count) |
| 99 | + |
| 100 | + self.length = count |
| 101 | + self.target_transform = target_transform |
| 102 | + |
| 103 | + def __getitem__(self, index): |
| 104 | + target = 0 |
| 105 | + sub = 0 |
| 106 | + for ind in self.indices: |
| 107 | + if index < ind: |
| 108 | + break |
| 109 | + target += 1 |
| 110 | + sub += ind |
| 111 | + |
| 112 | + db = self.dbs[target] |
| 113 | + index = index - sub |
| 114 | + |
| 115 | + if self.target_transform is not None: |
| 116 | + target = self.target_transform(target) |
| 117 | + |
| 118 | + return db[index], target |
| 119 | + |
| 120 | + def __len__(self): |
| 121 | + return self.length |
| 122 | + |
| 123 | + def __repr__(self): |
| 124 | + return self.__class__.__name__ + ' (' + self.db_path + ')' |
| 125 | + |
| 126 | +if __name__ == '__main__': |
| 127 | + #lsun = LSUNClassDataset(db_path='/home/soumith/local/lsun/train/bedroom_train_lmdb') |
| 128 | + #a = lsun[0] |
| 129 | + lsun = LSUNDataset(db_path='/home/soumith/local/lsun/train', |
| 130 | + classes=['bedroom_train', 'church_outdoor_train']) |
| 131 | + print(lsun.classes) |
| 132 | + print(lsun.dbs) |
| 133 | + a, t = lsun[len(lsun)-1] |
| 134 | + print(a) |
| 135 | + print(t) |
0 commit comments