You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

85 lines
2.5 KiB

import lightning as L
11 months ago
import torch
import torch.nn as nn
11 months ago
from torch.optim.lr_scheduler import ReduceLROnPlateau
11 months ago
10 months ago
from losses import calculate_separation_loss, preservation_loss # noqa: F401
11 months ago
class ColorTransformerModel(L.LightningModule):
def __init__(
self,
transform: str = "relu",
width: int = 128,
depth: int = 1,
bias: bool = False,
alpha: float = 0,
):
11 months ago
super().__init__()
self.save_hyperparameters()
if self.hparams.transform.lower() == "tanh":
t = nn.Tanh
elif self.hparams.transform.lower() == "relu":
t = nn.ReLU
w = self.hparams.width
d = self.hparams.depth
bias = self.hparams.bias
midlayers = [nn.Linear(w, w, bias=bias), t()] * d
self.network = nn.Sequential(
nn.Linear(3, w, bias=bias),
t(),
*midlayers,
nn.Linear(w, 3, bias=bias),
t(),
nn.Linear(3, 1, bias=bias),
11 months ago
)
11 months ago
11 months ago
def forward(self, x):
x = self.network(x)
# Circular mapping
# x = (torch.sin(x) + 1) / 2
x = torch.sigmoid(x)
return x
11 months ago
11 months ago
def training_step(self, batch, batch_idx):
inputs, labels = batch # x are the RGB inputs, labels are the strings
outputs = self.forward(inputs)
11 months ago
p_loss = preservation_loss(
11 months ago
inputs,
outputs,
)
# alpha = self.hparams.alpha # TODO: decide what to do with this...
loss = p_loss
self.log("hp_metric", loss)
11 months ago
self.log("p_loss", p_loss)
11 months ago
return loss
def validation_step(self, batch):
10 months ago
inputs, labels = batch # these are true HSV labels - no learning allowed.
outputs = self.forward(inputs)
distance = torch.minimum(
torch.abs(outputs - labels), torch.abs(1 + outputs - labels)
)
mean_loss = torch.mean(distance)
max_loss = torch.max(distance)
self.log("val_mean_loss", mean_loss)
self.log("val_max_loss", max_loss)
return mean_loss
11 months ago
def configure_optimizers(self):
optimizer = torch.optim.SGD(
self.parameters(),
lr=0.1,
)
lr_scheduler = ReduceLROnPlateau(
optimizer, mode="min", factor=0.05, patience=5, cooldown=10, verbose=True
)
11 months ago
return {
"optimizer": optimizer,
"lr_scheduler": {
"scheduler": lr_scheduler,
11 months ago
"monitor": "hp_metric", # Specify the metric to monitor
},
}