-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtrain_lc-crf_embryos.py
238 lines (218 loc) · 10.8 KB
/
train_lc-crf_embryos.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
import torch
import torch.nn.functional as F
from torch import optim
from torch.utils.data import DataLoader
from torchvision import models
import json
import os
import argparse
import numpy as np
import our_models
import struct_utils
from crf_dataloader import CRFDataLoader
def main():
parser = argparse.ArgumentParser(description='Linear-Chain CRF Training')
parser.add_argument('--num_workers', type=int, default=8,
help='number of workers')
parser.add_argument('--epochs', type=int, default=300,
help='number of epochs')
parser.add_argument('--backbone', default='resnet50', type=str,
help='select model <resnet18/resnet34/resnet50>')
parser.add_argument('--lr', '--learning-rate', default=1e-4, type=float,
help='initial learning rate')
parser.add_argument('--out', default=None, type=str,
help='output directory')
parser.add_argument('--input_size', default=112, type=int,
help='image size')
parser.add_argument('--batch_size', default=4, type=int,
help='batch size')
parser.add_argument('--num_samples', default=50, type=int,
help='number of sampled frames per training video')
parser.add_argument('--lambda_p', default=1., type=float,
help='lambda temparature between unary/pairwise')
parser.add_argument('--trans_weight', default=1., type=float,
help='weight to the transition label in motion model')
parser.add_argument('--ce_weight', default=1., type=float,
help='weight to the cross entropy')
args = parser.parse_args()
print(dict(args._get_kwargs()))
if args.out:
out_dir = args.out
else:
out_dir = 'pipeline_results/{}-{}/'.format(
args.num_samples, args.trans_weight)
os.makedirs(out_dir, exist_ok=True)
with open(out_dir + 'params.json', 'w') as fp:
json.dump(dict(args._get_kwargs()), fp)
N_INPUT_FLOW = 2
N_OUTPUT_FLOW = 2
STAGE_TO_NUMBER = {'1-cell': 0,
'2-cell': 1,
'3-cell': 2,
'4-cell': 3,
'5-cell': 4,
'6-cell': 5,
'7-cell': 6,
'8-cell': 7,
'9+-cell': 8,
'morula': 9,
'blastocyst': 10,
}
embryo_datasets = dict()
embryo_datasets['training'] = CRFDataLoader(
STAGE_TO_NUMBER, input_size=args.input_size,
train=True, set_name='training', num_samples=args.num_samples)
embryo_datasets['validation'] = CRFDataLoader(
STAGE_TO_NUMBER, input_size=args.input_size,
train=False, set_name='validation')
train_loader = DataLoader(embryo_datasets['training'],
batch_size=args.batch_size,
shuffle=True,
pin_memory=True,
num_workers=args.num_workers,
drop_last=True)
val_loader = DataLoader(embryo_datasets['validation'],
batch_size=1,
shuffle=False,
pin_memory=True,
num_workers=args.num_workers)
# Initialize the model
use_pretrained = True
device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
if args.backbone == "resnet18":
backbone_model = models.resnet18(pretrained=use_pretrained)
elif args.backbone == "resnet34":
backbone_model = models.resnet34(pretrained=use_pretrained)
elif args.backbone == "resnet50":
backbone_model = models.resnet50(pretrained=use_pretrained)
n_output_classes = len(STAGE_TO_NUMBER)
model_im = our_models.model_building_rgb(
n_output_classes, backbone_model, device)
model_im = model_im.to(device)
if args.backbone == "resnet18":
backbone_model = models.resnet18(pretrained=use_pretrained)
elif args.backbone == "resnet34":
backbone_model = models.resnet34(pretrained=use_pretrained)
elif args.backbone == "resnet50":
backbone_model = models.resnet50(pretrained=use_pretrained)
model_flow = our_models.model_building_flow(
N_OUTPUT_FLOW, backbone_model, device, N_INPUT_FLOW)
model_flow = model_flow.to(device)
params = list(model_im.parameters()) + list(model_flow.parameters())
optimizer = optim.Adam(params, lr=args.lr, weight_decay=1e-5)
scheduler = optim.lr_scheduler.MultiStepLR(optimizer,
[250, 280],
gamma=0.1,
verbose=True)
num_warmup = 1000
warmup_optimizer = optim.Adam(params, lr=args.lr*.001, weight_decay=1e-5)
warmup_scheduler = optim.lr_scheduler.StepLR(warmup_optimizer,
step_size=1,
gamma=1.0069,
verbose=False)
train_hist = {'loss': [], 'acc': []}
val_hist = {'loss': [], 'acc': [], 'acc_raw': []}
best_acc = 0
flow_weights = torch.tensor([1., args.trans_weight]).to(device)
num_iters = 0
for epoch in range(args.epochs):
# TRAINNG
print(f'\n===============\nTRAINING - Epoch {epoch+1}/{args.epochs}:')
model_im.train()
model_flow.train()
running_correct, running_loss, total = 0, 0.0, 0
for i, (inputs_im, im_label, inputs_flow, flow_label) in enumerate(
train_loader):
inputs_im = inputs_im.view(-1, 1, 112, 112).to(device)
inputs_flow = inputs_flow.view(-1, 2, 112, 112).to(device)
im_label = im_label.view(-1, 1).to(device)
flow_label = flow_label.view(-1, 1).to(device)
# run models
if num_iters < num_warmup:
warmup_optimizer.zero_grad()
else:
optimizer.zero_grad()
im_pred = model_im(inputs_im)
flow_pred = model_flow(inputs_flow)
im_crfinput = im_pred.view(
args.batch_size, args.num_samples, -1)
flow_crfinput = flow_pred.view(
args.batch_size, args.num_samples - 1, -1)
argmax_C_by_C, argmax_labels, parts_C_by_C, parts_labels, dist = \
struct_utils.linear_chain_distribution(
im_crfinput, flow_crfinput, im_label, args.num_samples,
img=True, flow=True, verbose=False, use_weighting=False,
lambda_p=args.lambda_p)
loss = -dist.log_prob(parts_C_by_C).sum()
loss += args.ce_weight*F.cross_entropy(im_pred,
im_label.squeeze(),
reduction='sum')
loss += args.ce_weight*F.cross_entropy(flow_pred,
flow_label.squeeze(),
weight=flow_weights,
reduction='sum')
loss.backward()
if num_iters < num_warmup:
warmup_optimizer.step()
warmup_scheduler.step()
else:
optimizer.step()
running_loss += loss.item()
running_correct += np.sum(argmax_labels == parts_labels)
total += parts_labels.size
num_iters += 1
train_loss = running_loss / len(train_loader)
train_acc = running_correct / total
train_hist['loss'].append(train_loss)
train_hist['acc'].append(train_acc)
# VALIDATION
model_im.eval()
model_flow.eval()
running_correct, running_loss, total = 0, 0.0, 0
with torch.no_grad():
for i, (inputs_im, im_label, inputs_flow, flow_label) in enumerate(
val_loader):
inputs_im = inputs_im.view(-1, 1, 112, 112)
inputs_flow = inputs_flow.view(-1, 2, 112, 112)
inputs_im = inputs_im.to(device)
inputs_flow = inputs_flow.to(device)
im_label = torch.stack(im_label, 1).view(-1, 1).to(device)
flow_label = torch.stack(flow_label, 1).view(-1, 1).to(device)
# run models
im_pred = model_im(inputs_im)
flow_pred = model_flow(inputs_flow)
im_crfinput = im_pred.view(1, -1, n_output_classes)
flow_crfinput = flow_pred.view(1, -1, N_OUTPUT_FLOW)
argmax_C_by_C, argmax_labels, parts_C_by_C, parts_labels, \
dist = struct_utils.linear_chain_distribution(
im_crfinput, flow_crfinput, im_label,
im_label.shape[0], img=True, flow=True, verbose=False,
use_weighting=False, lambda_p=args.lambda_p)
loss = -dist.log_prob(parts_C_by_C).sum()
loss += args.ce_weight*F.cross_entropy(im_pred,
im_label.squeeze(),
reduction='sum')
loss += args.ce_weight*F.cross_entropy(flow_pred,
flow_label.squeeze(),
weight=flow_weights,
reduction='sum')
running_loss += loss.item()
running_correct += np.sum(argmax_labels == parts_labels)
total += parts_labels.size
val_loss = running_loss / len(val_loader)
val_acc = running_correct / total
val_hist['loss'].append(val_loss)
val_hist['acc'].append(val_acc)
scheduler.step()
print(f'Epoch {epoch+1} | train loss: \t{train_loss:.4f}, train acc: \t{train_acc:.4f}')
print(f'Epoch {epoch+1} | val loss: \t{val_loss:.4f}, val acc: \t{val_acc:.4f}')
state = {'epoch': epoch, 'im_state_dict': model_im.state_dict(
), 'flow_state_dict': model_flow.state_dict()}
if best_acc < val_acc:
best_acc = val_acc
print(os.path.join(out_dir, "best-checkpoint.pth.tar"))
torch.save(state, os.path.join(out_dir, "best-checkpoint.pth.tar"))
print(os.path.join(out_dir, "latest-checkpoint.pth.tar"))
torch.save(state, os.path.join(out_dir, "latest-checkpoint.pth.tar"))
if __name__ == '__main__':
main()