forked from dailing/DeepDRPlus
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain_eval_fund.py
210 lines (182 loc) · 6.49 KB
/
train_eval_fund.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
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
import math
from model import ModelProgression
from torch import nn
import torch
import numpy as np
from functools import cached_property
from trainer import Trainer
from torch.utils.data import Dataset
import pandas as pd
import cv2
import albumentations as aug
import albumentations.pytorch as aug_torch
class DeepSurModel(nn.Module):
def __init__(self, K=512) -> None:
super().__init__()
self.K = K
# sample parameters for the mixture model
rnd = np.random.RandomState(12345)
b = torch.FloatTensor(abs(rnd.normal(0, 10, (1, 1, self.K))+5.0))
k = torch.FloatTensor(abs(rnd.normal(0, 10, (1, 1, self.K))+5.0))
self.register_buffer('b', b)
self.register_buffer('k', k)
self.cnn = ModelProgression(backbone='resnet50', output_size=512)
def _cdf_at(self, t):
# pdf: nBatch * n * K
pdf = 1 - torch.exp(-(1/self.b * (t)) ** self.k)
return pdf
def _pdf_at(self, t):
# pdf: nBatch * n * K
pdf = self._cdf_at(t)
pdf = (1-pdf) * self.k * (1/self.b)*(t/self.b)**(self.k-1)
return pdf
def calculate_cdf(self, w, t):
"""
Calculates the cumulative probability distribution function (CDF)
for the given data.
param w: nBatch * K: weights for mixture model
param t: nBatch * n: target time to calculate pdf at
return: nBatch * n: pdf values
"""
t = t.unsqueeze(dim=2)
w = nn.functional.softmax(w, dim=1)
w = w.unsqueeze(dim=1)
pdf = self._cdf_at(t)
pdf = pdf * w
pdf = pdf.sum(dim=2)
return pdf
def calculate_pdf(self, w, t):
"""
Calculates the probability distribution function (pdf) for the given
data.
param w: nBatch * K: weights for mixture model
param t: nBatch * n: target time to calculate pdf at
return: nBatch * n: pdf values
"""
t = t.unsqueeze(dim=2)
w = nn.functional.softmax(w, dim=1)
w = w.unsqueeze(dim=1)
pdf = self._pdf_at(t)
pdf = pdf * w
pdf = pdf.sum(dim=2)
return pdf
def calculate_survial_time(self, w, t_max=10, resolution=20):
"""
Calculates the survival time for the given data.
"""
t = torch.linspace(
1/resolution,
t_max,
math.ceil(resolution*t_max)-1,
dtype=torch.float32,
device=w.device).view(1, -1)
pdf = self.calculate_pdf(w, t)
# print(pdf[])
est = t.view(-1)[torch.argmax(pdf, dim=1)]
# print(torch.argmax(pdf, dim=1).shape())
return est
def forward(self, x, t=None):
x = self.cnn(x)
if t is None:
return x
return x, self.calculate_cdf(x, t)
class ProgressionData(Dataset):
def __init__(self, datasheet, transform):
super().__init__()
self.df = pd.read_csv(datasheet)
self.transform = transform
def __len__(self):
return len(self.df)
def __getitem__(self, idx):
img_file = self.df.iloc[idx]['image']
image = cv2.imread(img_file, cv2.IMREAD_COLOR)
image = self.transform(image=image)['image']
return dict(
image=image,
t1=self.df.iloc[idx]['t1'],
t2=self.df.iloc[idx]['t2'],
e=self.df.iloc[idx]['e'],
# simulation only
gt=self.df.iloc[idx]['gt'] if 'gt' in self.df.columns else 0,
)
class TrainerDR(Trainer):
@cached_property
def model(self):
model = DeepSurModel().to(self.device)
if self.cfg.load_pretrain is not None:
print('loading ', self.cfg.load_pretrain)
print(model.cnn.backbone.load_state_dict(
torch.load(self.cfg.load_pretrain, map_location=self.device)
))
return model
@cached_property
def beta(self):
return 1
@cached_property
def train_dataset(self):
transform = aug.Compose([
aug.SmallestMaxSize(
max_size=self.cfg.image_size, always_apply=True),
aug.CenterCrop(self.cfg.image_size, self.cfg.image_size,
always_apply=True),
aug.Flip(p=0.5),
aug.ImageCompression(quality_lower=10, quality_upper=80, p=0.2),
aug.MedianBlur(p=0.3),
aug.RandomBrightnessContrast(p=0.5),
aug.RandomGamma(p=0.2),
aug.GaussNoise(p=0.2),
aug.Rotate(border_mode=cv2.BORDER_CONSTANT,
value=0, p=0.7, limit=45),
aug.ToFloat(always_apply=True),
aug_torch.ToTensorV2(),
])
return ProgressionData('data_fund/train.csv', transform)
@cached_property
def test_dataset(self):
transform = aug.Compose([
aug.SmallestMaxSize(
max_size=self.cfg.image_size, always_apply=True),
aug.CenterCrop(self.cfg.image_size, self.cfg.image_size,
always_apply=True),
aug.ToFloat(always_apply=True),
aug_torch.ToTensorV2(),
])
return ProgressionData('data_fund/test.csv', transform)
@cached_property
def optimizer(self):
optimizer = torch.optim.Adam(
self.model.parameters(), lr=self.cfg.lr, weight_decay=1e-5)
return optimizer
def batch(self, epoch, i_batch, data) -> dict:
# get and prepare data elements
imgs = data['image'].to(self.device)
t1 = data['t1'].to(self.device)
t2 = data['t2'].to(self.device)
e = data['e'].to(self.device)
w, P = self.model(imgs, torch.stack([t1, t2], dim=1))
P1 = P[:, 0]
P2 = P[:, 1]
loss = -torch.log(1-P1 + 0.000001) - torch.log(P2 +
0.000001) * self.beta * (e)
loss += torch.abs(w).mean() * 0.00000001
time_to_cal = torch.linspace(0, 20, 240).to(
self.cfg.device).view(1, -1)
cdf = self.model.calculate_cdf(w, time_to_cal)
pdf = self.model.calculate_pdf(w, time_to_cal)
survival_time = self.model.calculate_survial_time(w)
return dict(
loss=loss.mean(),
pdf=pdf,
cdf=cdf,
t1=t1,
t2=t2,
survival_time=survival_time,
gt=data['gt'],
)
def matrix(self, epoch, data) -> dict:
return dict(
loss=float(data['loss'].mean())
)
if __name__ == '__main__':
trainer = TrainerDR()
trainer.train()