-
Notifications
You must be signed in to change notification settings - Fork 2
/
data_loader.py
146 lines (103 loc) · 4.52 KB
/
data_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
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
import torch
from torch import nn,optim
from torch.utils.data import DataLoader,Dataset
from torchsummary import summary
from torch.autograd import Function
from torch.optim.lr_scheduler import StepLR
import torchvision.transforms.functional as TF
from torchvision import transforms
from PIL import Image
import pickle
from tqdm.notebook import tqdm
import random
from sklearn import metrics
from skimage import io, filters
import joblib
import json
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import glob
def get_data_from_pickle_dict(pkl_path):
with open(pkl_path,'rb') as fp:
d = pickle.load(fp)
return d
def get_keys_from_pickle_dict(pkl_path):
with open(pkl_path,'rb') as fp:
d = pickle.load(fp)
return list(d.keys())
class DataFromFolder(Dataset):
def __init__(self,source_data_path,source_data_pkl_lbl,target_data_path,transform=None):
self.source_data_path = source_data_path
self.source_data_pkl_lbl = source_data_pkl_lbl
self.target_data_path = target_data_path
self.transform = transform
self.source_data_lbl = get_data_from_pickle_dict(self.source_data_pkl_lbl)
source_label = list(np.ones((len(self.source_data_path))))
target_label = list(np.zeros((len(self.target_data_path))))
source_ds_lbl = list(zip(self.source_data_path,source_label))
target_ds_lbl = list(zip(self.target_data_path,target_label))
self.merged_data = source_ds_lbl + target_ds_lbl
random.shuffle(self.merged_data)
def __len__(self):
return len(self.merged_data)
def __getitem__(self,index):
cur_data_id,is_source = self.merged_data[index]
cur_key = cur_data_id.split('/')[-1][:-4]
if is_source:
x,y = Image.open(cur_data_id),self.source_data_lbl[cur_key]
else:
x,y = Image.open(cur_data_id), np.zeros((256,256))
if self.transform != None:
x = self.transform(x)
return x,torch.Tensor(y).unsqueeze(0),is_source
class DataFromPickle(Dataset):
def __init__(self,source_data_pkl,target_data_pkl,transform=None,source_ds=None,target_ds=None):
self.source_data_pkl = source_data_pkl
self.target_data_pkl = target_data_pkl
self.transform = transform
self.source_ds = source_ds
self.target_ds = target_ds
self.source_data = get_data_from_pickle_dict(self.source_data_pkl)
self.target_data = get_data_from_pickle_dict(self.target_data_pkl)
if self.source_ds == None:
self.source_ds = list(self.source_data.keys())
if self.target_ds == None:
self.target_ds = list(self.target_data.keys())
source_label = list(np.ones((len(self.source_ds))))
target_label = list(np.zeros((len(self.target_ds))))
source_ds_lbl = list(zip(self.source_ds,source_label))
target_ds_lbl = list(zip(self.target_ds,target_label))
self.merged_data = source_ds_lbl + target_ds_lbl
random.shuffle(self.merged_data)
def __len__(self):
return len(self.merged_data)
def __getitem__(self,index):
cur_data_id,is_source = self.merged_data[index]
if is_source:
x,y = self.source_data[cur_data_id]
else:
x,y = self.target_data[cur_data_id], torch.zeros((256,256))
if self.transform != None:
x = self.transform(Image.fromarray((x*255).astype(np.uint8)))
return x,torch.Tensor(y).unsqueeze(0),is_source.astype(np.float32)
class Data_baseline(Dataset):
def __init__(self,data_pkl,ds_order=None,transform=None,is_train=True):
self.data_pkl = data_pkl
self.transform = transform
self.is_train = is_train
with open(data_pkl,'rb') as fp:
self.ds = pickle.load(fp)
self.ds_order = ds_order if np.all(ds_order != None) else list(self.ds.keys())
def __len__(self):
return len(self.ds_order)
def __getitem__(self,index):
cur_data = self.ds_order[index]
if self.is_train:
x,y = self.ds[cur_data]
else:
x = self.ds[cur_data]
if self.transform != None:
x = self.transform(Image.fromarray((x*255).astype(np.uint8)))
if not self.is_train: return x
return x,torch.Tensor(y).unsqueeze(0)