-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathCNNModelFinetune.py
More file actions
134 lines (110 loc) · 4.79 KB
/
CNNModelFinetune.py
File metadata and controls
134 lines (110 loc) · 4.79 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
from pytorch_lightning import LightningModule, Trainer
import torch
import numpy as np
from torch import nn
from torch.nn import functional as F
from torchmetrics import Accuracy, AUROC, Precision, Recall, ROC
from torchvision import transforms
import random
from sklearn import metrics
from torch.optim.lr_scheduler import StepLR, MultiStepLR, ReduceLROnPlateau
import torchvision.models as models
def create_model(feature_extract, train_layers):
model = models.resnet.resnet50(weights="DEFAULT")
# model.load_state_dict(torch.load(f"/home/lbrocki/BYOL/weights/old/epoch_19.pt"))
# freeze layers, except for last train_layers
if feature_extract:
num_param = len(list(model.parameters()))
for i, param in enumerate(model.parameters()):
if(i < num_param - train_layers):
param.requires_grad = False
model.fc = nn.Linear(2048, 1)
return model
class CNNModelFinetune(LightningModule):
def __init__(self,
learning_rate=1e-3,
momentum=0.0,
feature_extract=False,
train_layers = 10):
super().__init__()
# Set our init args as class attributes
self.learning_rate = learning_rate
self.momentum = momentum
self.feature_extract = feature_extract
self.model = create_model(feature_extract, train_layers)
self.accuracy = Accuracy(task="binary")
self.auroc = AUROC(task="binary")
self.precision_ = Precision(task="binary")
self.recall = Recall(task="binary")
self.roc = ROC(task="binary")
#self.criterion = nn.CrossEntropyLoss()
self.criterion = nn.BCEWithLogitsLoss()
def forward(self, x):
return self.model(x)
def training_step(self, batch, batch_idx):
x, y, = batch
logits = self(x)
# logits_views = torch.zeros((3, y.shape[0], 2), device=self.device)
# for i in range(3):
# logits_views[i] = self(x[i])
# logits = torch.mean(logits_views, axis=0)
#preds = torch.argmax(logits, dim=1)
preds = logits.squeeze()
acc = self.accuracy(preds, y)
loss = self.criterion(preds, y.float())
#loss = self.criterion(preds, y)
self.log("train_loss", loss, prog_bar=True, on_epoch=True)
self.log("train_acc", acc, prog_bar=True, on_epoch=True)
return loss
def validation_step(self, batch, batch_idx):
# on validation we return a list with all three views
# final output is averaged over these views
x, y = batch
logits_views = torch.zeros((3, y.shape[0], 1), device=self.device)
for i in range(3):
logits_views[i] = self(x[i])
logits = torch.mean(logits_views, axis=0).squeeze()
#preds = torch.argmax(logits, dim=1)
preds=logits
m = nn.Sigmoid()
preds_sig = m(preds)
acc = self.accuracy(preds_sig, y)
auroc = self.auroc(preds_sig, y)
precision = self.precision_(preds_sig,y)
recall = self.recall(preds_sig, y)
roc = self.roc(preds_sig, y)
loss = self.criterion(logits, y.float())
# print(metrics.roc_auc_score(y.cpu().numpy(), preds_sig.cpu().numpy()))
# print(auroc)
# Calling self.log will surface up scalars for you in TensorBoard
self.log("val_loss", loss, prog_bar=True)
self.log("val_acc", acc, prog_bar=True)
self.log("val_auroc", auroc, prog_bar=True)
self.log("val_recall", recall, prog_bar=True)
self.log("val_precision", precision, prog_bar=True)
return loss
def test_step(self, batch, batch_idx):
# Here we just reuse the validation_step for testing
return self.validation_step(batch, batch_idx)
def configure_optimizers(self):
params_to_update = self.model.parameters()
print("Params to learn:")
if self.feature_extract:
params_to_update = []
for name,param in self.model.named_parameters():
if param.requires_grad == True:
params_to_update.append(param)
print("\t",name)
# else:
# for name,param in self.model.named_parameters():
# if param.requires_grad == True:
# print("\t",name)
# optimizer = torch.optim.Adam(self.parameters(), self.learning_rate)
optimizer = torch.optim.Adam(params_to_update, self.learning_rate)
lr_scheduler = {
'scheduler': MultiStepLR(optimizer, milestones=[20, 40], gamma=0.1),
#'scheduler': ReduceLROnPlateau(optimizer),
'monitor': 'val_loss',
'name': 'log_lr'
}
return [optimizer], [lr_scheduler]