-
Notifications
You must be signed in to change notification settings - Fork 0
/
data.py
185 lines (117 loc) · 4.17 KB
/
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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
import torch.nn
import torch
import os
import numpy as np
from torch.utils.data import Dataset, DataLoader
import config
import random
import cv2
from torch.autograd import Variable
len_ed = 402
mean = (0.485,0.456,0.406)
std = (0.229,0.224,0.225)
x = 128
def crop(img,label,sal_edges,im_e,edges):
a = random.randint(1+x,300-x)
b = random.randint(1+x,300-x)
img = img[a-x:a+x,b-x:b+x]
sal_e = sal_edges[a-x:a+x,b-x:b+x]
sal = label[a-x:a+x,b-x:b+x]
im_e = im_e[a-x:a+x,b-x:b+x]
edges = edges[a-x:a+x,b-x:b+x]
return img,sal,sal_e,im_e,edges
def normalize(image):
image /= 255.
image -= image.mean(axis=(0,1))
s = image.std(axis=(0,1))
s[s == 0] = 1.0
image /= s
image = np.transpose(image,[2,0,1])
return image
def cal_weights(label):
s = np.sum(label)/np.prod((256,256))
weight = np.zeros_like(label)
weight[label==0]=1-s
weight[label==1] = s
return weight
class DataFolder(Dataset):
def __init__(self,imgs,sals,im_e,ed_label,trainable=True):
super(DataFolder,self).__init__()
self.img_paths = imgs
self.sal_paths = sals
self.im_e = im_e
self.ed_label = ed_label
self.trainable = trainable
def __len__(self):
return len(self.img_paths)
def __getitem__(self, idx1):
img_path = self.img_paths[idx1]
sal_p = self.sal_paths[idx1]
idx2 = idx1%len_ed
im_e = self.im_e[idx2]
ed_lp = self.ed_label[idx2]
s_p,s_ln = os.path.split(sal_p)
e_p,e_ln = os.path.split(ed_lp)
print(s_ln)
img = cv2.imread(img_path)
img_e = cv2.imread(im_e)
img = cv2.cvtColor(img,cv2.COLOR_BGR2RGB)
img_e = cv2.cvtColor(img_e, cv2.COLOR_BGR2RGB)
sal_l = cv2.imread(sal_p,0)
ed_l = cv2.imread(ed_lp,0)
sal_e = cv2.Canny(sal_l,50,50)
img = cv2.resize(img,config.SIZE)/ 255.
sal_l = cv2.resize(sal_l,config.SIZE)/ 255.
sal_e =cv2.resize(sal_e,config.SIZE)/ 255.
im_e = cv2.resize(img_e,config.SIZE)/ 255.
ed_l = cv2.resize(ed_l,config.SIZE)/ 255.
if self.trainable:
img, sal_l, sal_e, im_e, ed_l = crop( img,sal_l,sal_e,im_e,ed_l)
if random.random()<0.5:
img = cv2.flip(img,1)
sal_l = cv2.flip(sal_l, 1)
sal_e = cv2.flip(sal_e, 1)
im_e = cv2.flip(im_e,1)
ed_l = cv2.flip(ed_l,1)
w_s_m = cal_weights(sal_l)
w_s_e = cal_weights(sal_e)
w_e = cal_weights(ed_l)
img = normalize(img)
img_e = normalize(im_e)
img = torch.FloatTensor(img)
img_e = torch.FloatTensor(img_e)
sal_l = torch.FloatTensor(sal_l).unsqueeze(0)
sal_e = torch.FloatTensor(sal_e).unsqueeze(0)
ed_l = torch.FloatTensor(ed_l).unsqueeze(0)
return img,img_e,sal_l,sal_e,ed_l
def process_data_dir(data_dir):
files = os.listdir(data_dir)
files = map(lambda x:os.path.join(data_dir,x),files)
return sorted(files)
if __name__ =="__main__":
sal_dirs = [("/home/rabbit/Datasets/SED1/SED1-Image",
"/home/rabbit/Datasets/SED1/SED1-Mask")]
ed_dir =[("/home/rabbit/Desktop/edge_sal/images/test",
"/home/rabbit/Desktop/edge_sal/bon/test")]
bs = 2
DATA_DICT = {}
S_IMG_FILES = []
S_GT_FILES = []
E_IMG_FILES = []
E_GT_FILES = []
mean = (0.485, 0.456, 0.406)
std = (0.229, 0.224, 0.225)
for dir_pair in sal_dirs:
X, y = process_data_dir(dir_pair[0]), process_data_dir(dir_pair[1])
S_IMG_FILES.extend(X)
S_GT_FILES.extend(y)
for dir_pair in ed_dir:
X, y = process_data_dir(dir_pair[0]), process_data_dir(dir_pair[1])
E_IMG_FILES.extend(X)
E_GT_FILES.extend(y)
S_IMGS_train, S_GT_train = S_IMG_FILES, S_GT_FILES
E_IMGS_train, E_GT_train = E_IMG_FILES, E_GT_FILES
train_folder = DataFolder(S_IMGS_train, S_GT_train, E_IMGS_train, E_GT_train,True)
train_data = DataLoader(train_folder, batch_size=bs, num_workers=2, shuffle=True)
for iter ,(img,img_e,sal_l,sal_e,ed_l) in enumerate(train_data):
print(img.size())