updates model file
Browse files- src/model.py +37 -16
src/model.py
CHANGED
|
@@ -16,15 +16,23 @@ class DRModel(L.LightningModule):
|
|
| 16 |
|
| 17 |
# Define the model
|
| 18 |
# self.model = models.densenet121(weights=models.DenseNet121_Weights.DEFAULT)
|
| 19 |
-
self.model = models.densenet169(weights=models.DenseNet169_Weights.DEFAULT)
|
| 20 |
-
# self.model = models.
|
|
|
|
|
|
|
|
|
|
| 21 |
# freeze the feature extractor
|
| 22 |
-
|
| 23 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
# Change the output layer to have the number of classes
|
| 25 |
-
in_features = self.model.classifier.in_features
|
| 26 |
-
|
| 27 |
-
self.model.
|
|
|
|
| 28 |
nn.Linear(in_features, in_features // 2),
|
| 29 |
nn.ReLU(),
|
| 30 |
nn.Dropout(0.5),
|
|
@@ -41,7 +49,7 @@ class DRModel(L.LightningModule):
|
|
| 41 |
x, y = batch
|
| 42 |
logits = self.model(x)
|
| 43 |
loss = self.criterion(logits, y)
|
| 44 |
-
self.log("train_loss", loss, prog_bar=True)
|
| 45 |
return loss
|
| 46 |
|
| 47 |
def validation_step(self, batch, batch_idx):
|
|
@@ -50,22 +58,35 @@ class DRModel(L.LightningModule):
|
|
| 50 |
loss = self.criterion(logits, y)
|
| 51 |
preds = torch.argmax(logits, dim=1)
|
| 52 |
acc = accuracy(preds, y, task="multiclass", num_classes=self.num_classes)
|
| 53 |
-
kappa = cohen_kappa(
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
|
| 58 |
def configure_optimizers(self):
|
| 59 |
# optimizer = torch.optim.Adam(
|
| 60 |
# self.parameters(), lr=self.learning_rate, weight_decay=1e-4
|
| 61 |
# )
|
| 62 |
-
# scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(optimizer, mode="min", factor=0.1, patience=3, verbose=True)
|
| 63 |
|
| 64 |
optimizer = torch.optim.AdamW(
|
| 65 |
-
self.parameters(), lr=self.learning_rate, weight_decay=
|
| 66 |
)
|
| 67 |
-
scheduler = torch.optim.lr_scheduler.
|
| 68 |
-
# scheduler = torch.optim.lr_scheduler.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 69 |
return {
|
| 70 |
"optimizer": optimizer,
|
| 71 |
"lr_scheduler": {
|
|
|
|
| 16 |
|
| 17 |
# Define the model
|
| 18 |
# self.model = models.densenet121(weights=models.DenseNet121_Weights.DEFAULT)
|
| 19 |
+
# self.model = models.densenet169(weights=models.DenseNet169_Weights.DEFAULT)
|
| 20 |
+
# self.model = models.densenet161(weights=models.DenseNet161_Weights.DEFAULT)
|
| 21 |
+
self.model = models.vit_b_16(weights=models.ViT_B_16_Weights.DEFAULT)
|
| 22 |
+
# self.model = models.vit_b_32(weights=models.ViT_B_32_Weights.DEFAULT)
|
| 23 |
+
|
| 24 |
# freeze the feature extractor
|
| 25 |
+
for param in self.model.parameters():
|
| 26 |
+
param.requires_grad = False
|
| 27 |
+
|
| 28 |
+
# self.model.head.weight.requires_grad = True
|
| 29 |
+
# self.model.head.bias.requires_grad = True
|
| 30 |
+
|
| 31 |
# Change the output layer to have the number of classes
|
| 32 |
+
# in_features = self.model.classifier.in_features
|
| 33 |
+
in_features = 768
|
| 34 |
+
self.model.heads = nn.Sequential(
|
| 35 |
+
# self.model.classifier = nn.Sequential(
|
| 36 |
nn.Linear(in_features, in_features // 2),
|
| 37 |
nn.ReLU(),
|
| 38 |
nn.Dropout(0.5),
|
|
|
|
| 49 |
x, y = batch
|
| 50 |
logits = self.model(x)
|
| 51 |
loss = self.criterion(logits, y)
|
| 52 |
+
self.log("train_loss", loss, on_step=True, on_epoch=True, prog_bar=True)
|
| 53 |
return loss
|
| 54 |
|
| 55 |
def validation_step(self, batch, batch_idx):
|
|
|
|
| 58 |
loss = self.criterion(logits, y)
|
| 59 |
preds = torch.argmax(logits, dim=1)
|
| 60 |
acc = accuracy(preds, y, task="multiclass", num_classes=self.num_classes)
|
| 61 |
+
kappa = cohen_kappa(
|
| 62 |
+
preds,
|
| 63 |
+
y,
|
| 64 |
+
task="multiclass",
|
| 65 |
+
num_classes=self.num_classes,
|
| 66 |
+
weights="quadratic",
|
| 67 |
+
)
|
| 68 |
+
self.log("val_loss", loss, on_step=True, on_epoch=True, prog_bar=True)
|
| 69 |
+
self.log("val_acc", acc, on_step=True, on_epoch=True, prog_bar=True)
|
| 70 |
+
self.log("val_kappa", kappa, on_step=True, on_epoch=True, prog_bar=True)
|
| 71 |
|
| 72 |
def configure_optimizers(self):
|
| 73 |
# optimizer = torch.optim.Adam(
|
| 74 |
# self.parameters(), lr=self.learning_rate, weight_decay=1e-4
|
| 75 |
# )
|
|
|
|
| 76 |
|
| 77 |
optimizer = torch.optim.AdamW(
|
| 78 |
+
self.parameters(), lr=self.learning_rate, weight_decay=0.05
|
| 79 |
)
|
| 80 |
+
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=5, gamma=0.1)
|
| 81 |
+
# scheduler = torch.optim.lr_scheduler.CosineAnnealingLR(optimizer, T_max=20)
|
| 82 |
+
scheduler = torch.optim.lr_scheduler.ReduceLROnPlateau(
|
| 83 |
+
optimizer,
|
| 84 |
+
mode="min", # or "max" if you're maximizing a metric
|
| 85 |
+
factor=0.1, # factor by which the learning rate will be reduced
|
| 86 |
+
patience=5, # number of epochs with no improvement after which learning rate will be reduced
|
| 87 |
+
verbose=True, # print a message when learning rate is reduced
|
| 88 |
+
threshold=0.001, # threshold for measuring the new optimum, to only focus on significant changes
|
| 89 |
+
)
|
| 90 |
return {
|
| 91 |
"optimizer": optimizer,
|
| 92 |
"lr_scheduler": {
|