-
Notifications
You must be signed in to change notification settings - Fork 1
/
dataset.py
63 lines (48 loc) · 2.1 KB
/
dataset.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
62
63
import torch
from torch import nn
from torch.utils.data import Dataset, DataLoader
from PIL import Image
from torchvision import transforms
import os
import numpy as np
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
torch.manual_seed(66)
LHR_TRAIN_DATA_PATH = '/content/Khoa_LHR_image/train'
LHR_VAL_DATA_PATH = '/content/Khoa_LHR_image/val'
BATCH_SIZE = 32
LOW_IMG_HEIGHT = 64
LOW_IMG_WIDTH = 64
class ImageDataset(Dataset):
def __init__(self, img_dir, is_train=True):
self.resize = transforms.Resize((LOW_IMG_WIDTH, LOW_IMG_HEIGHT), antialias=True)
self.is_train = is_train
self.img_dir = img_dir
self.images = os.listdir(img_dir)
def __len__(self):
return len(self.images)
def normalize(self, input_image, target_image):
input_image = input_image*2 - 1
target_image = target_image*2 - 1
return input_image, target_image
def random_jitter(self, input_image, target_image):
if torch.rand([]) < 0.5:
input_image = transforms.functional.hflip(input_image)
target_image = transforms.functional.hflip(target_image)
return input_image, target_image
def __getitem__(self, idx):
img_path = os.path.join(self.img_dir, self.images[idx])
image = np.array(Image.open(img_path).convert("RGB"))
image = transforms.functional.to_tensor(image)
input_image = self.resize(image).type(torch.float32)
target_image = image.type(torch.float32)
input_image, target_image = self.normalize(input_image, target_image)
if self.is_train:
input_image, target_image = self.random_jitter(input_image, target_image)
return input_image, target_image
train_dataset = ImageDataset(LHR_TRAIN_DATA_PATH, is_train=True)
train_loader = DataLoader(train_dataset, batch_size=BATCH_SIZE, shuffle=True)
test_dataset = ImageDataset(LHR_VAL_DATA_PATH, is_train=False)
test_loader = DataLoader(test_dataset, batch_size=BATCH_SIZE, shuffle=False)
in_batch, tar_batch = next(iter(train_loader))
in_batch = (in_batch + 1)/2
tar_batch = (tar_batch + 1)/2