add UnFreezeModelCallback

This commit is contained in:
hobogalaxy committed 2020-11-16 19:06:10 +01:00
1 parent 320cd8b3ad
commit 0b1723d0f8
3 files changed
+35 -16

No files matched your search

+19 -2
View File
@@ -17,7 +17,6 @@ class ExampleCallback(Callback):
class SaveOnnxToWandbCallback(Callback):
def __init__(self, dataloader, wandb_save_dir):
first_batch = next(iter(dataloader))
x, y = first_batch
@@ -50,5 +49,23 @@ class ImagePredictionLoggerCallback(Callback):
trainer.logger.experiment.log({
"examples": [wandb.Image(x, caption=f"Pred:{pred}, Label:{y}")
for x, pred, y in zip(val_imgs, preds, self.val_labels)],
"global_step": trainer.global_step
})
class UnFreezeModelCallback(Callback):
def __init__(self, wait_epochs=5):
self.wait_epochs = wait_epochs
def on_epoch_end(self, trainer, pl_module):
if trainer.current_epoch == self.wait_epochs:
for param in pl_module.model.model.parameters():
param.requires_grad = True
class ConfusionMatrixLoggerCallback(Callback):
def __init__(self):
pass
def on_validation_epoch_end(self, trainer, pl_module):
print(trainer.callback_metrics)
+13 -14
View File
@@ -12,25 +12,24 @@ class SimpleLinearMNIST(nn.Module):
super().__init__()
# mnist images are (1, 28, 28) (channels, width, height)
self.layer_1 = nn.Linear(28 * 28, config["lin1_size"])
self.layer_2 = nn.Linear(config["lin1_size"], config["lin2_size"])
self.layer_3 = nn.Linear(config["lin2_size"], config["lin3_size"])
self.layer_4 = nn.Linear(config["lin3_size"], config["output_size"])
self.model = nn.Sequential(
nn.Linear(28 * 28, config["lin1_size"]),
nn.ReLU(),
nn.Linear(config["lin1_size"], config["lin2_size"]),
nn.ReLU(),
nn.Linear(config["lin2_size"], config["lin3_size"]),
nn.ReLU(),
nn.Linear(config["lin3_size"], config["output_size"]),
nn.LogSoftmax(dim=1),
)
def forward(self, x):
batch_size, channels, width, height = x.size()
# (b, 1, 28, 28) -> (b, 1*28*28)
x = x.view(batch_size, -1)
x = self.layer_1(x)
x = F.relu(x)
x = self.layer_2(x)
x = F.relu(x)
x = self.layer_3(x)
x = F.relu(x)
x = self.layer_4(x)
return F.log_softmax(x, dim=1)
return self.model(x)
class EfficientNetPretrained(nn.Module):
@@ -42,7 +41,7 @@ class EfficientNetPretrained(nn.Module):
# self.model = EfficientNet.from_pretrained('efficientnet-b7')
for param in self.model.parameters():
param.requires_grad = True
param.requires_grad = False
self.model._fc = nn.Sequential(
nn.Linear(self.model._fc.in_features, config["lin1_size"]),
@@ -100,7 +99,7 @@ class ResnetPretrained(nn.Module):
# nn.ReLU(),
# nn.Dropout(p=0.15),
# nn.Linear(config["lin1_size"], config["output_size"]),
# nn.LogSoftmax()
# nn.LogSoftmax(dim=1)
# )
#
# def forward(self, x):
+3
View File
@@ -6,6 +6,7 @@ import yaml
# custom
from pipeline_modules.lightning_wrapper import LitModel
from project.pipeline_modules.data_modules import *
from project.pipeline_modules.callbacks import *
def train(config):
@@ -39,6 +40,8 @@ def train(config):
),
# ExampleCallback(),
# LearningRateMonitor(),
# ConfusionMatrixLoggerCallback(),
# UnFreezeModelCallback(wait_epochs=5),
# SaveOnnxToWandbCallback(dataloader=datamodule.train_dataloader(), wandb_save_dir=wandb_logger.save_dir)
]