forked from marian42/butterflies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
image_loader.py
28 lines (22 loc) · 855 Bytes
/
image_loader.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
import torch
from torch.utils.data import Dataset
import numpy as np
import glob
from skimage import io
class ImageDataset(Dataset):
def __init__(self, return_hashes=False):
file_names = glob.glob('data/images_rotated_128/**.jpg', recursive=True)
self.hashes = sorted([f.split('/')[-1][:-4] for f in file_names])
self.return_hashes = return_hashes
def __len__(self):
return len(self.hashes)
def __getitem__(self, index):
hash = self.hashes[index]
image_file_name = 'data/images_rotated_128/{:s}.jpg'.format(hash)
image = io.imread(image_file_name)
image = image.transpose((2, 0, 1)).astype(np.float32) / 255
image = torch.from_numpy(image)
if self.return_hashes:
return image, hash
else:
return image