add UnFreezeModelCallback
This commit is contained in:
1 parent
320cd8b3ad
commit
0b1723d0f8
3 files changed
+36
-17
No files matched your search
@@ -17,7 +17,6 @@ class ExampleCallback(Callback):
|
|||||||
|
|
||||||
|
|
||||||
class SaveOnnxToWandbCallback(Callback):
|
class SaveOnnxToWandbCallback(Callback):
|
||||||
|
|
||||||
def __init__(self, dataloader, wandb_save_dir):
|
def __init__(self, dataloader, wandb_save_dir):
|
||||||
first_batch = next(iter(dataloader))
|
first_batch = next(iter(dataloader))
|
||||||
x, y = first_batch
|
x, y = first_batch
|
||||||
@@ -50,5 +49,23 @@ class ImagePredictionLoggerCallback(Callback):
|
|||||||
trainer.logger.experiment.log({
|
trainer.logger.experiment.log({
|
||||||
"examples": [wandb.Image(x, caption=f"Pred:{pred}, Label:{y}")
|
"examples": [wandb.Image(x, caption=f"Pred:{pred}, Label:{y}")
|
||||||
for x, pred, y in zip(val_imgs, preds, self.val_labels)],
|
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)
|
||||||
|
|
||||||
@@ -12,25 +12,24 @@ class SimpleLinearMNIST(nn.Module):
|
|||||||
super().__init__()
|
super().__init__()
|
||||||
|
|
||||||
# mnist images are (1, 28, 28) (channels, width, height)
|
# mnist images are (1, 28, 28) (channels, width, height)
|
||||||
self.layer_1 = nn.Linear(28 * 28, config["lin1_size"])
|
self.model = nn.Sequential(
|
||||||
self.layer_2 = nn.Linear(config["lin1_size"], config["lin2_size"])
|
nn.Linear(28 * 28, config["lin1_size"]),
|
||||||
self.layer_3 = nn.Linear(config["lin2_size"], config["lin3_size"])
|
nn.ReLU(),
|
||||||
self.layer_4 = nn.Linear(config["lin3_size"], config["output_size"])
|
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):
|
def forward(self, x):
|
||||||
batch_size, channels, width, height = x.size()
|
batch_size, channels, width, height = x.size()
|
||||||
|
|
||||||
# (b, 1, 28, 28) -> (b, 1*28*28)
|
# (b, 1, 28, 28) -> (b, 1*28*28)
|
||||||
x = x.view(batch_size, -1)
|
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):
|
class EfficientNetPretrained(nn.Module):
|
||||||
@@ -42,7 +41,7 @@ class EfficientNetPretrained(nn.Module):
|
|||||||
# self.model = EfficientNet.from_pretrained('efficientnet-b7')
|
# self.model = EfficientNet.from_pretrained('efficientnet-b7')
|
||||||
|
|
||||||
for param in self.model.parameters():
|
for param in self.model.parameters():
|
||||||
param.requires_grad = True
|
param.requires_grad = False
|
||||||
|
|
||||||
self.model._fc = nn.Sequential(
|
self.model._fc = nn.Sequential(
|
||||||
nn.Linear(self.model._fc.in_features, config["lin1_size"]),
|
nn.Linear(self.model._fc.in_features, config["lin1_size"]),
|
||||||
@@ -100,7 +99,7 @@ class ResnetPretrained(nn.Module):
|
|||||||
# nn.ReLU(),
|
# nn.ReLU(),
|
||||||
# nn.Dropout(p=0.15),
|
# nn.Dropout(p=0.15),
|
||||||
# nn.Linear(config["lin1_size"], config["output_size"]),
|
# nn.Linear(config["lin1_size"], config["output_size"]),
|
||||||
# nn.LogSoftmax()
|
# nn.LogSoftmax(dim=1)
|
||||||
# )
|
# )
|
||||||
#
|
#
|
||||||
# def forward(self, x):
|
# def forward(self, x):
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import yaml
|
|||||||
# custom
|
# custom
|
||||||
from pipeline_modules.lightning_wrapper import LitModel
|
from pipeline_modules.lightning_wrapper import LitModel
|
||||||
from project.pipeline_modules.data_modules import *
|
from project.pipeline_modules.data_modules import *
|
||||||
|
from project.pipeline_modules.callbacks import *
|
||||||
|
|
||||||
|
|
||||||
def train(config):
|
def train(config):
|
||||||
@@ -39,6 +40,8 @@ def train(config):
|
|||||||
),
|
),
|
||||||
# ExampleCallback(),
|
# ExampleCallback(),
|
||||||
# LearningRateMonitor(),
|
# LearningRateMonitor(),
|
||||||
|
# ConfusionMatrixLoggerCallback(),
|
||||||
|
# UnFreezeModelCallback(wait_epochs=5),
|
||||||
# SaveOnnxToWandbCallback(dataloader=datamodule.train_dataloader(), wandb_save_dir=wandb_logger.save_dir)
|
# SaveOnnxToWandbCallback(dataloader=datamodule.train_dataloader(), wandb_save_dir=wandb_logger.save_dir)
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user