Spaces:
Runtime error
Runtime error
Commit ·
412b4f2
1
Parent(s): e628a3b
Create stylegan_dataset.pu
Browse files- stylegan_dataset.pu +40 -0
stylegan_dataset.pu
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
from io import BytesIO
|
| 2 |
+
|
| 3 |
+
import lmdb
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from torch.utils.data import Dataset
|
| 6 |
+
|
| 7 |
+
|
| 8 |
+
class MultiResolutionDataset(Dataset):
|
| 9 |
+
def __init__(self, path, transform, resolution=256):
|
| 10 |
+
self.env = lmdb.open(
|
| 11 |
+
path,
|
| 12 |
+
max_readers=32,
|
| 13 |
+
readonly=True,
|
| 14 |
+
lock=False,
|
| 15 |
+
readahead=False,
|
| 16 |
+
meminit=False,
|
| 17 |
+
)
|
| 18 |
+
|
| 19 |
+
if not self.env:
|
| 20 |
+
raise IOError('Cannot open lmdb dataset', path)
|
| 21 |
+
|
| 22 |
+
with self.env.begin(write=False) as txn:
|
| 23 |
+
self.length = int(txn.get('length'.encode('utf-8')).decode('utf-8'))
|
| 24 |
+
|
| 25 |
+
self.resolution = resolution
|
| 26 |
+
self.transform = transform
|
| 27 |
+
|
| 28 |
+
def __len__(self):
|
| 29 |
+
return self.length
|
| 30 |
+
|
| 31 |
+
def __getitem__(self, index):
|
| 32 |
+
with self.env.begin(write=False) as txn:
|
| 33 |
+
key = f'{self.resolution}-{str(index).zfill(5)}'.encode('utf-8')
|
| 34 |
+
img_bytes = txn.get(key)
|
| 35 |
+
|
| 36 |
+
buffer = BytesIO(img_bytes)
|
| 37 |
+
img = Image.open(buffer)
|
| 38 |
+
img = self.transform(img)
|
| 39 |
+
|
| 40 |
+
return img
|