From 0b1723d0f8a11c1429e1b83da7fd14d86ac17884 Mon Sep 17 00:00:00 2001 From: hobogalaxy Date: Mon, 16 Nov 2020 19:06:10 +0100 Subject: [PATCH] add UnFreezeModelCallback --- project/pipeline_modules/callbacks.py | 23 ++++++++++++++++++++--- project/pipeline_modules/models.py | 27 +++++++++++++-------------- project/train.py | 3 +++ 3 files changed, 36 insertions(+), 17 deletions(-) diff --git a/project/pipeline_modules/callbacks.py b/project/pipeline_modules/callbacks.py index 518671b..0e7299f 100644 --- a/project/pipeline_modules/callbacks.py +++ b/project/pipeline_modules/callbacks.py @@ -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 - }) \ No newline at end of file + }) + + +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) + diff --git a/project/pipeline_modules/models.py b/project/pipeline_modules/models.py index 8cfada3..def87f6 100644 --- a/project/pipeline_modules/models.py +++ b/project/pipeline_modules/models.py @@ -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): diff --git a/project/train.py b/project/train.py index 257f8bd..7bd9372 100644 --- a/project/train.py +++ b/project/train.py @@ -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) ]