-
Notifications
You must be signed in to change notification settings - Fork 69
/
multi_read_data.py
61 lines (47 loc) · 1.81 KB
/
multi_read_data.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
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
import numpy as np
import torch
import torch.utils.data
import random
from PIL import Image
from glob import glob
import torchvision.transforms as transforms
import os
batch_w = 600
batch_h = 400
class MemoryFriendlyLoader(torch.utils.data.Dataset):
def __init__(self, img_dir, task):
self.low_img_dir = img_dir
self.task = task
self.train_low_data_names = []
for root, dirs, names in os.walk(self.low_img_dir):
for name in names:
self.train_low_data_names.append(os.path.join(root, name))
self.train_low_data_names.sort()
self.count = len(self.train_low_data_names)
transform_list = []
transform_list += [transforms.ToTensor()]
self.transform = transforms.Compose(transform_list)
def load_images_transform(self, file):
im = Image.open(file).convert('RGB')
img_norm = self.transform(im).numpy()
img_norm = np.transpose(img_norm, (1, 2, 0))
return img_norm
def __getitem__(self, index):
low = self.load_images_transform(self.train_low_data_names[index])
h = low.shape[0]
w = low.shape[1]
#
h_offset = random.randint(0, max(0, h - batch_h - 1))
w_offset = random.randint(0, max(0, w - batch_w - 1))
#
# if self.task != 'test':
# low = low[h_offset:h_offset + batch_h, w_offset:w_offset + batch_w]
low = np.asarray(low, dtype=np.float32)
low = np.transpose(low[:, :, :], (2, 0, 1))
img_name = self.train_low_data_names[index].split('\\')[-1]
# if self.task == 'test':
# # img_name = self.train_low_data_names[index].split('\\')[-1]
# return torch.from_numpy(low), img_name
return torch.from_numpy(low), img_name
def __len__(self):
return self.count