|
@ -5,48 +5,54 @@ from torch.optim.lr_scheduler import ReduceLROnPlateau |
|
|
|
|
|
|
|
|
from losses import calculate_separation_loss, preservation_loss |
|
|
from losses import calculate_separation_loss, preservation_loss |
|
|
|
|
|
|
|
|
|
|
|
# class ColorTransformerModel(pl.LightningModule): |
|
|
|
|
|
# def __init__(self, params): |
|
|
|
|
|
# super().__init__() |
|
|
|
|
|
# self.save_hyperparameters(params) |
|
|
|
|
|
|
|
|
|
|
|
# # Model layers |
|
|
|
|
|
# self.layers = nn.Sequential( |
|
|
|
|
|
# nn.Linear(5, 128, bias=False), |
|
|
|
|
|
# nn.Linear(128, 3, bias=False), |
|
|
|
|
|
# nn.ReLU(), |
|
|
|
|
|
# nn.Linear(3, 64, bias=False), |
|
|
|
|
|
# nn.Linear(64, 128, bias=False), |
|
|
|
|
|
# nn.Linear(128, 256, bias=False), |
|
|
|
|
|
# nn.Linear(256, 128, bias=False), |
|
|
|
|
|
# nn.ReLU(), |
|
|
|
|
|
# nn.Linear(128, 1, bias=False), |
|
|
|
|
|
# ) |
|
|
|
|
|
|
|
|
class ColorTransformerModel(pl.LightningModule): |
|
|
# def forward(self, x): |
|
|
def __init__(self, params): |
|
|
# x = self.layers(x) |
|
|
super().__init__() |
|
|
# x = (torch.sin(x) + 1) / 2 |
|
|
self.save_hyperparameters(params) |
|
|
# return x |
|
|
|
|
|
|
|
|
# Model layers |
|
|
|
|
|
self.layers = nn.Sequential( |
|
|
|
|
|
nn.Linear(5, 128, bias=False), |
|
|
|
|
|
nn.Linear(128, 3, bias=False), |
|
|
|
|
|
nn.ReLU(), |
|
|
|
|
|
nn.Linear(3, 64, bias=False), |
|
|
|
|
|
nn.Linear(64, 128, bias=False), |
|
|
|
|
|
nn.Linear(128, 256, bias=False), |
|
|
|
|
|
nn.Linear(256, 128, bias=False), |
|
|
|
|
|
nn.ReLU(), |
|
|
|
|
|
nn.Linear(128, 1, bias=False), |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x): |
|
|
|
|
|
x = self.layers(x) |
|
|
|
|
|
x = (torch.sin(x) + 1) / 2 |
|
|
|
|
|
return x |
|
|
|
|
|
|
|
|
|
|
|
# class ColorTransformerModel(pl.LightningModule): |
|
|
# class ColorTransformerModel(pl.LightningModule): |
|
|
# def __init__(self, alpha, learning_rate): |
|
|
# def __init__(self, params): |
|
|
# super().__init__() |
|
|
# super().__init__() |
|
|
# self.save_hyperparameters() |
|
|
# self.save_hyperparameters(params) |
|
|
|
|
|
|
|
|
# # Embedding layer to expand the input dimensions |
|
|
# # Embedding layer to expand the input dimensions |
|
|
# self.embedding = nn.Linear(3, 128) |
|
|
# self.embedding = nn.Linear(3, 128, bias=False) |
|
|
|
|
|
|
|
|
# # Transformer block |
|
|
# # Transformer encoder-decoder |
|
|
# transformer_layer = nn.TransformerEncoderLayer( |
|
|
# encoder = nn.TransformerEncoderLayer( |
|
|
# d_model=128, nhead=4, dim_feedforward=512, dropout=0.1 |
|
|
# d_model=128, nhead=4, dim_feedforward=512, dropout=0.3 |
|
|
# ) |
|
|
# ) |
|
|
# self.transformer_encoder = nn.TransformerEncoder( |
|
|
# self.transformer_encoder = nn.TransformerEncoder( |
|
|
# transformer_layer, num_layers=3 |
|
|
# encoder, num_layers=3 |
|
|
|
|
|
# ) |
|
|
|
|
|
# # lower dimensionality decoder |
|
|
|
|
|
# decoder = nn.TransformerDecoderLayer( |
|
|
|
|
|
# d_model=128, nhead=4, dim_feedforward=512, dropout=0.3 |
|
|
|
|
|
# ) |
|
|
|
|
|
# self.transformer_decoder = nn.TransformerDecoder( |
|
|
|
|
|
# decoder, num_layers=3 |
|
|
# ) |
|
|
# ) |
|
|
|
|
|
|
|
|
# # Final linear layer to map back to 1D space |
|
|
# # Final linear layer to map back to 1D space |
|
|
# self.final_layer = nn.Linear(128, 1) |
|
|
# self.final_layer = nn.Linear(128, 1, bias=False) |
|
|
|
|
|
|
|
|
# def forward(self, x): |
|
|
# def forward(self, x): |
|
|
# # Embedding the input |
|
|
# # Embedding the input |
|
@ -58,6 +64,9 @@ class ColorTransformerModel(pl.LightningModule): |
|
|
# # Passing through the transformer |
|
|
# # Passing through the transformer |
|
|
# x = self.transformer_encoder(x) |
|
|
# x = self.transformer_encoder(x) |
|
|
|
|
|
|
|
|
|
|
|
# # Passing through the decoder |
|
|
|
|
|
# x = self.transformer_decoder(x, memory=x) |
|
|
|
|
|
|
|
|
# # Reshape back to original shape |
|
|
# # Reshape back to original shape |
|
|
# x = x.squeeze(1) |
|
|
# x = x.squeeze(1) |
|
|
|
|
|
|
|
@ -65,10 +74,37 @@ class ColorTransformerModel(pl.LightningModule): |
|
|
# x = self.final_layer(x) |
|
|
# x = self.final_layer(x) |
|
|
|
|
|
|
|
|
# # Apply sigmoid activation to ensure output is in (0, 1) |
|
|
# # Apply sigmoid activation to ensure output is in (0, 1) |
|
|
# x = torch.sigmoid(x) |
|
|
# # x = torch.sigmoid(x) |
|
|
|
|
|
# x = (torch.sin(x) + 1) / 2 |
|
|
# return x |
|
|
# return x |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class ColorTransformerModel(pl.LightningModule): |
|
|
|
|
|
def __init__(self, params): |
|
|
|
|
|
super().__init__() |
|
|
|
|
|
self.save_hyperparameters(params) |
|
|
|
|
|
|
|
|
|
|
|
# Neural network layers |
|
|
|
|
|
self.network = nn.Sequential( |
|
|
|
|
|
nn.Linear(5, 64), # Input layer |
|
|
|
|
|
nn.Tanh(), |
|
|
|
|
|
nn.Linear(64, 128), |
|
|
|
|
|
nn.Tanh(), |
|
|
|
|
|
nn.Linear(128, 128), |
|
|
|
|
|
nn.Tanh(), |
|
|
|
|
|
nn.Linear(128, 64), |
|
|
|
|
|
nn.Tanh(), |
|
|
|
|
|
nn.Linear(64, 1), # Output layer |
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
def forward(self, x): |
|
|
|
|
|
# Pass the input through the network |
|
|
|
|
|
x = self.network(x) |
|
|
|
|
|
# Circular mapping |
|
|
|
|
|
# x = (torch.sin(x) + 1) / 2 |
|
|
|
|
|
x = torch.sigmoid(x) |
|
|
|
|
|
return x |
|
|
|
|
|
|
|
|
def training_step(self, batch, batch_idx): |
|
|
def training_step(self, batch, batch_idx): |
|
|
inputs, labels = batch # x are the RGB inputs, labels are the strings |
|
|
inputs, labels = batch # x are the RGB inputs, labels are the strings |
|
|
outputs = self.forward(inputs) |
|
|
outputs = self.forward(inputs) |
|
@ -84,11 +120,12 @@ class ColorTransformerModel(pl.LightningModule): |
|
|
return loss |
|
|
return loss |
|
|
|
|
|
|
|
|
def configure_optimizers(self): |
|
|
def configure_optimizers(self): |
|
|
optimizer = torch.optim.AdamW( |
|
|
optimizer = torch.optim.SGD( |
|
|
self.parameters(), lr=self.hparams.learning_rate, |
|
|
self.parameters(), |
|
|
|
|
|
lr=self.hparams.learning_rate, |
|
|
) |
|
|
) |
|
|
lr_scheduler = ReduceLROnPlateau( |
|
|
lr_scheduler = ReduceLROnPlateau( |
|
|
optimizer, mode="min", factor=0.1, patience=10, verbose=True |
|
|
optimizer, mode="min", factor=0.05, patience=10, cooldown=20, verbose=True |
|
|
) |
|
|
) |
|
|
return { |
|
|
return { |
|
|
"optimizer": optimizer, |
|
|
"optimizer": optimizer, |
|
|