-
Notifications
You must be signed in to change notification settings - Fork 2
/
dataset.py
56 lines (46 loc) · 2.02 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
import os
import random
import torch
import numpy as np
import pandas as pd
from torch.utils.data import Dataset
from PIL import Image
import torchvision.transforms as transforms
class ContourDiffDataset(Dataset):
def __init__(self, df_meta, image_directory, contour_directory, transform_img=None, transform_contour=None, generator_seed=None, config=None):
self.df_meta = df_meta
self.image_directory = image_directory
self.contour_directory = contour_directory
self.transform_img = transform_img
self.transform_contour = transform_contour
self.generator_seed = generator_seed
if self.generator_seed is not None:
self.seed_generator = torch.Generator().manual_seed(self.generator_seed)
self.length = self.df_meta.shape[0]
self.config = config
def __len__(self):
return self.length
def __getitem__(self, index):
img_name = self.df_meta.iloc[index, :]["image_name"]
if self.config is None or self.config.in_channels == 1:
img = Image.open(os.path.join(self.image_directory, img_name)).convert("L")
elif self.config.in_channels == 3:
img = Image.open(os.path.join(self.image_directory, img_name)).convert("RGB")
contour_name = self.df_meta.iloc[index, :]["contour_name"]
contour = Image.open(os.path.join(self.contour_directory, contour_name))
if self.generator_seed is not None:
seed = self.seed_generator.seed()
if self.transform_img is not None:
if self.generator_seed is not None:
torch.manual_seed(seed)
img = self.transform_img(img)
if self.transform_contour is not None:
if self.generator_seed is not None:
torch.manual_seed(seed)
contour = self.transform_contour(contour)
return {
"images": img,
"contours": contour,
"image_name": img_name,
"contour_name": contour_name
}