-
Notifications
You must be signed in to change notification settings - Fork 4
/
data_loader.py
130 lines (104 loc) · 5.54 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
import numpy as np
import os
import torch
import torch.utils.data as data
import torchvision.transforms as transforms
from PIL import Image
import math
class Cephalometric(data.Dataset):
def __init__(self, pathDataset, mode, R_ratio=0.05, num_landmark=19, size=[800, 640]):
self.num_landmark = num_landmark
self.Radius = int(max(size) * R_ratio)
self.size = size
self.original_size = [2400, 1935]
# gen mask
mask = torch.zeros(2*self.Radius, 2*self.Radius, dtype=torch.float)
guassian_mask = torch.zeros(2*self.Radius, 2*self.Radius, dtype=torch.float)
for i in range(2*self.Radius):
for j in range(2*self.Radius):
distance = np.linalg.norm([i+1 - self.Radius, j+1 - self.Radius])
if distance < self.Radius:
mask[i][j] = 1
# for guassian mask
guassian_mask[i][j] = math.exp(-0.5 * math.pow(distance, 2) /\
math.pow(self.Radius, 2))
self.mask = mask
self.guassian_mask = guassian_mask
# gen offset
self.offset_x = torch.zeros(2*self.Radius, 2*self.Radius, dtype=torch.float)
self.offset_y = torch.zeros(2*self.Radius, 2*self.Radius, dtype=torch.float)
for i in range(2*self.Radius):
self.offset_x[:, i] = self.Radius - i
self.offset_y[i, :] = self.Radius - i
self.offset_x = self.offset_x * self.mask / self.Radius
self.offset_y = self.offset_y * self.mask / self.Radius
self.pth_Image = os.path.join(pathDataset, 'RawImage')
self.pth_label_junior = os.path.join(pathDataset, '400_junior')
self.pth_label_senior = os.path.join(pathDataset, '400_senior')
self.list = list()
if mode == 'Train':
self.pth_Image = os.path.join(self.pth_Image, 'TrainingData')
start = 1
end = 150
elif mode == 'Test1':
self.pth_Image = os.path.join(self.pth_Image, 'Test1Data')
start = 151
end = 400
else:
self.pth_Image = os.path.join(self.pth_Image, 'Test2Data')
start = 301
end = 400
normalize = transforms.Normalize([0.5], [0.5])
transformList = []
transformList.append(transforms.Resize(self.size))
transformList.append(transforms.ToTensor())
transformList.append(normalize)
self.transform = transforms.Compose(transformList)
for i in range(start, end + 1):
self.list.append({'ID': "{0:03d}".format(i)})
def resize_landmark(self, landmark):
for i in range(len(landmark)):
landmark[i] = int(landmark[i] * self.size[i] / self.original_size[i])
return landmark
def __getitem__(self, index):
item = self.list[index]
if self.transform != None:
pth_img = os.path.join(self.pth_Image, item['ID']+'.bmp')
item['image'] = self.transform(Image.open(pth_img).convert('RGB'))
landmark_list = list()
with open(os.path.join(self.pth_label_junior, item['ID']+'.txt')) as f1:
with open(os.path.join(self.pth_label_senior, item['ID']+'.txt')) as f2:
for i in range(self.num_landmark):
landmark1 = f1.readline().split()[0].split(',')
landmark2 = f2.readline().split()[0].split(',')
landmark = [int(0.5*(int(landmark1[i]) + int(landmark2[i]))) for i in range(len(landmark1))]
landmark_list.append(self.resize_landmark(landmark))
# GT, mask, offset
y, x = item['image'].shape[-2], item['image'].shape[-1]
gt = torch.zeros((self.num_landmark, y, x), dtype=torch.float)
mask = torch.zeros((self.num_landmark, y, x), dtype=torch.float)
guassian_mask = torch.zeros((self.num_landmark, y, x), dtype=torch.float)
offset_x = torch.zeros((self.num_landmark, y, x), dtype=torch.float)
offset_y = torch.zeros((self.num_landmark, y, x), dtype=torch.float)
for i, landmark in enumerate(landmark_list):
gt[i][landmark[1]][landmark[0]] = 1
margin_x_left = max(0, landmark[0] - self.Radius)
margin_x_right = min(x, landmark[0] + self.Radius)
margin_y_bottom = max(0, landmark[1] - self.Radius)
margin_y_top = min(y, landmark[1] + self.Radius)
mask[i][margin_y_bottom:margin_y_top, margin_x_left:margin_x_right] = \
self.mask[0:margin_y_top-margin_y_bottom, 0:margin_x_right-margin_x_left]
guassian_mask[i][margin_y_bottom:margin_y_top, margin_x_left:margin_x_right] = \
self.guassian_mask[0:margin_y_top-margin_y_bottom, 0:margin_x_right-margin_x_left]
offset_x[i][margin_y_bottom:margin_y_top, margin_x_left:margin_x_right] = \
self.offset_x[0:margin_y_top-margin_y_bottom, 0:margin_x_right-margin_x_left]
offset_y[i][margin_y_bottom:margin_y_top, margin_x_left:margin_x_right] = \
self.offset_y[0:margin_y_top-margin_y_bottom, 0:margin_x_right-margin_x_left]
return item['image'], mask, guassian_mask, offset_y, offset_x, landmark_list
def __len__(self):
return len(self.list)
if __name__ == '__main__':
test = Cephalometric('dataset/Cephalometric', 'Test2')
for i in range(100):
test.__getitem__(i)
print("pass")