-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathDataHandler.py
More file actions
64 lines (48 loc) · 2.03 KB
/
DataHandler.py
File metadata and controls
64 lines (48 loc) · 2.03 KB
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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
import torch
import os
from PIL import Image
import torchvision.transforms as transforms
import random
from matplotlib.image import imsave
class DataHandler():
def __init__(self, batch_size=32, device=None, split='convert'):
self.device = device
if device == None:
self.device = 'cuda' if torch.cuda.is_available() else 'cpu'
if split not in ('train', 'test', 'convert'):
raise ValueError("Please Specify a valid data split to use")
self.transform = transforms.Compose([
transforms.PILToTensor(),
transforms.Resize((512, 512)),
])
if split == 'convert':
return
self.files = os.listdir('grayscale')
self.files = self.files[ : int(len(self.files) * 0.8)] if split == 'train' else self.files[int(len(self.files) * 0.8) : ]
random.Random(29).shuffle(self.files)
self.batch_size = batch_size
self.used = 0
def all_done(self):
return self.used >= len(self.files)
def get_batch(self):
batch = random.sample(self.files, k=self.batch_size)
y = torch.stack([self.path_to_file_to_tensor(os.path.join('original', file)) for file in batch])
# y = None
x = torch.stack([self.path_to_file_to_tensor(os.path.join('grayscale', file)) for file in batch])
return x, y
def path_to_file_to_tensor(self, path):
img = Image.open(path)
img = self.transform(img).to(self.device).type(torch.float) / 127.5 - 1# [0, 255] -> [0, 1]
# img = transforms.Normalize(0, 1)(img)
return img
def save_tensor_as_image(self, img, dest):
if dest is None:
raise ValueError("Incorrect path to save image")
transform = transforms.ToPILImage()
img = transform((img + 1)/2)
img.save(dest)
if __name__ == '__main__':
handler = DataHandler()
img = handler.path_to_file_to_tensor(os.path.join('grayscale', 'COCO_train2014_000000000009.jpg'))
print(img.shape)
print(img)