Repository navigation
Expand file tree
/
Copy pathCLIPConditionedSegFormerModel.py
More file actions
137 lines (113 loc) · 5.07 KB
/
Copy pathCLIPConditionedSegFormerModel.py
File metadata and controls
137 lines (113 loc) · 5.07 KB
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
import torch
import torchvision
import pytorch_lightning as pl
import torchmetrics as tm
from params import *
from transformers import CLIPProcessor, CLIPModel
from ConditionedSegFormerPE import ConditionedSegFormer
from LossFunc import *
class CLIPConditionedSegFormer(pl.LightningModule):
def __init__(self):
super().__init__()
self.clip = CLIPModel.from_pretrained('openai/clip-vit-base-patch16')
self.processor = CLIPProcessor.from_pretrained('openai/clip-vit-base-patch16')
self.segformer = ConditionedSegFormer(
ModelParams.INCHANNELS,
ModelParams.WIDTHS,
ModelParams.DEPTHS,
512,
768,
ModelParams.PATCH_SIZES,
ModelParams.OVERLAP_SIZES,
ModelParams.NUM_HEADS,
ModelParams.EXPANSION_FACTORS,
ModelParams.DECODER_CHANNELS,
ModelParams.SCALE_FACTORS
)
self.plot_every = TrainParams.PLOT_EVERY
self.neloss = NELoss(LossParams.ALPHA, LossParams.BETA)
# self.neloss = FocalLoss()
self.acc = tm.Accuracy(task="binary", threshold=LossParams.THRESHOLD)
self.dice = DiceLoss()
self.iou = IoULoss(LossParams.THRESHOLD)
self.f1score = tm.F1Score(task="binary", threshold=LossParams.THRESHOLD)
# freeze CLIP
for param in self.clip.parameters():
param.requires_grad = False
def forward(self, x, x_c, condition):
condition = self.clip.text_model(condition).last_hidden_state
pe = self.clip.vision_model(x_c).last_hidden_state
out = self.segformer(x, pe, condition)
return out
def training_step(self, batch, batch_idx):
torch.cuda.empty_cache()
x, x_c, condition, y = batch
y_hat = self(x, x_c, condition)
loss = self.neloss(y_hat, y)
self.log("train_loss", loss, prog_bar=True)
t_y_hat = torch.where(y_hat > 0.5, 1, 0).long().view(-1)
t_y = torch.where(y > 0.5, 1, 0).long().view(-1)
acc = self.acc(t_y_hat, t_y)
dice = self.dice(y_hat, y)
iou = self.iou(y_hat, y)
f1 = self.f1score(t_y_hat, t_y)
self.log("train_acc", acc, prog_bar=True)
self.log("train_dice", dice, prog_bar=True)
self.log("train_iou", iou, prog_bar=True)
self.log("train_f1", f1, prog_bar=True)
if self.global_step % self.plot_every == 0:
y = y.repeat(1, 3, 1, 1)
y_hat = torch.sigmoid(y_hat)
y_hat = y_hat.repeat(1, 3, 1, 1)
# x_grid = torchvision.utils.make_grid(train_dataset.image_inverse_transform(x))
# self.logger.experiment.add_image('train_sample_image', x_grid, self.global_step)
grid = torchvision.utils.make_grid(torch.cat([y, y_hat], dim=0))
self.logger.experiment.add_image('train_sample_mask', grid, self.global_step)
return loss
def validation_step(self, batch, batch_idx):
torch.cuda.empty_cache()
x, x_c, condition, y = batch
y_hat = self(x, x_c, condition)
loss = self.neloss(y_hat, y)
t_y_hat = torch.where(y_hat > 0.5, 1, 0).long().view(-1)
t_y = torch.where(y > 0.5, 1, 0).long().view(-1)
acc = self.acc(t_y_hat, t_y)
dice = self.dice(y_hat, y)
iou = self.iou(y_hat, y)
f1 = self.f1score(t_y_hat, t_y)
self.log("val_loss", loss, prog_bar=True)
self.log("val_acc", acc, prog_bar=True)
self.log("val_dice", dice, prog_bar=True)
self.log("val_iou", iou, prog_bar=True)
self.log("val_f1", f1, prog_bar=True)
return loss
def test_step(self, batch, batch_idx):
torch.cuda.empty_cache()
x, x_c, condition, y = batch
y_hat = self(x, x_c, condition)
t_y_hat = torch.where(y_hat > 0.5, 1, 0).long().view(-1)
t_y = torch.where(y > 0.5, 1, 0).long().view(-1)
acc = self.acc(t_y_hat, t_y)
dice = self.dice(y_hat, y)
iou = self.iou(y_hat, y)
f1 = self.f1score(t_y_hat, t_y)
self.log("test_acc", acc, prog_bar=True)
self.log("test_dice", dice, prog_bar=True)
self.log("test_iou", iou, prog_bar=True)
self.log("test_f1", f1, prog_bar=True)
def calc_lr(self, step, dim_embed, warmup_steps):
return dim_embed**(-0.5) * min((step+1)**(-0.5), (step+1) * warmup_steps**(-1.5))
def configure_optimizers(self):
# optimizer = torch.optim.Adam(self.parameters(), lr=1e-4, betas=(0.9, 0.98), eps=1e-7)
# scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=2000, gamma=0.99)
optimizer = torch.optim.Adam(self.parameters(), lr=OptimParams.LR, betas=OptimParams.BETAS, eps=1e-7)
warmup_steps = OptimParams.WARMUP_STEPS
scheduler = torch.optim.lr_scheduler.LambdaLR(optimizer, lambda step: self.calc_lr(step, 512, warmup_steps))
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": scheduler,
"interval": "step",
"frequency": 1
}
}