project Refactor, CNN Classifier Basics
This commit is contained in:
+158
-18
@@ -1,6 +1,13 @@
|
||||
from datasets.paired_dataset import TrajPairData
|
||||
from lib.modules.blocks import ConvModule
|
||||
from lib.modules.utils import LightningBaseModule
|
||||
import torch
|
||||
from functools import reduce
|
||||
from operator import mul
|
||||
|
||||
from torch import nn
|
||||
from torch.optim import Adam
|
||||
|
||||
from datasets.trajectory_dataset import TrajData
|
||||
from lib.modules.blocks import ConvModule, ResidualModule, DeConvModule
|
||||
from lib.modules.utils import LightningBaseModule, Flatten
|
||||
|
||||
|
||||
class CNNRouteGeneratorModel(LightningBaseModule):
|
||||
@@ -8,36 +15,169 @@ class CNNRouteGeneratorModel(LightningBaseModule):
|
||||
name = 'CNNRouteGenerator'
|
||||
|
||||
def configure_optimizers(self):
|
||||
pass
|
||||
|
||||
def validation_step(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
def validation_end(self, outputs):
|
||||
pass
|
||||
return Adam(self.parameters(), lr=self.hparams.train_param.lr)
|
||||
|
||||
def training_step(self, batch_xy, batch_nb, *args, **kwargs):
|
||||
pass
|
||||
batch_x, label = batch_xy
|
||||
|
||||
generated_alternative, z, mu, logvar = self(batch_x + [label, ])
|
||||
map_array, trajectory = batch_x
|
||||
|
||||
map_stack = torch.cat((map_array, trajectory, generated_alternative), dim=1)
|
||||
pred_label = self.discriminator(map_stack)
|
||||
|
||||
discriminated_bce_loss = self.criterion(pred_label, label.float().unsqueeze(-1))
|
||||
|
||||
# see Appendix B from VAE paper:
|
||||
# Kingma and Welling. Auto-Encoding Variational Bayes. ICLR, 2014
|
||||
# https://arxiv.org/abs/1312.6114
|
||||
# 0.5 * sum(1 + log(sigma^2) - mu^2 - sigma^2)
|
||||
kld_loss = -0.5 * torch.sum(1 + logvar - mu.pow(2) - logvar.exp())
|
||||
loss = (kld_loss + discriminated_bce_loss) / 2
|
||||
return dict(loss=loss, log=dict(loss=loss,
|
||||
discriminated_bce_loss=discriminated_bce_loss,
|
||||
kld_loss=kld_loss)
|
||||
)
|
||||
|
||||
def test_step(self, *args, **kwargs):
|
||||
pass
|
||||
|
||||
@property
|
||||
def discriminator(self):
|
||||
if self._disc is None:
|
||||
raise RuntimeError('Set the Discriminator first; "set_discriminator(disc_model)')
|
||||
return self._disc
|
||||
|
||||
def set_discriminator(self, disc_model):
|
||||
if self._disc is not None:
|
||||
raise RuntimeError('Discriminator has already been set... What are trying to do?')
|
||||
self._disc = disc_model
|
||||
|
||||
def __init__(self, *params):
|
||||
super(CNNRouteGeneratorModel, self).__init__(*params)
|
||||
|
||||
# Dataset
|
||||
self.dataset = TrajPairData(self.hparams.data_param.data_root)
|
||||
self.dataset = TrajData(self.hparams.data_param.map_root, mode='just_route')
|
||||
|
||||
# Additional Attributes
|
||||
self.in_shape = self.dataset.map_shapes_max
|
||||
# Todo: Better naming and size in Parameters
|
||||
self.feature_dim = 10
|
||||
self._disc = None
|
||||
|
||||
# NN Nodes
|
||||
###################################################
|
||||
#
|
||||
# Utils
|
||||
self.relu = nn.ReLU()
|
||||
self.criterion = nn.MSELoss()
|
||||
|
||||
#
|
||||
# Map Encoder
|
||||
self.map_conv_0 = ConvModule(self.in_shape, conv_kernel=3, conv_stride=1, conv_padding=1,
|
||||
conv_filters=self.hparams.model_param.filters[0])
|
||||
|
||||
self.conv2 = ConvModule(self.conv1.shape, conv_kernel=3, conv_stride=1, conv_padding=0,
|
||||
conv_filters=self.hparams.model_param.filters[0])
|
||||
self.conv3 = ConvModule(self.conv2.shape, conv_kernel=3, conv_stride=1, conv_padding=0,
|
||||
conv_filters=self.hparams.model_param.filters[0])
|
||||
self.map_res_1 = ResidualModule(self.map_conv_0.shape, ConvModule, 2, conv_kernel=3, conv_stride=1,
|
||||
conv_padding=1, conv_filters=self.hparams.model_param.filters[0])
|
||||
self.map_conv_1 = ConvModule(self.map_res_1.shape, conv_kernel=3, conv_stride=1, conv_padding=0,
|
||||
conv_filters=self.hparams.model_param.filters[1])
|
||||
|
||||
def forward(self, x):
|
||||
pass
|
||||
self.map_res_2 = ResidualModule(self.map_conv_1.shape, ConvModule, 2, conv_kernel=3, conv_stride=1,
|
||||
conv_padding=1, conv_filters=self.hparams.model_param.filters[1])
|
||||
self.map_conv_2 = ConvModule(self.map_res_2.shape, conv_kernel=3, conv_stride=1, conv_padding=0,
|
||||
conv_filters=self.hparams.model_param.filters[2])
|
||||
|
||||
self.map_res_3 = ResidualModule(self.map_conv_2.shape, ConvModule, 2, conv_kernel=3, conv_stride=1,
|
||||
conv_padding=1, conv_filters=self.hparams.model_param.filters[2])
|
||||
self.map_conv_3 = ConvModule(self.map_res_3.shape, conv_kernel=5, conv_stride=1, conv_padding=0,
|
||||
conv_filters=self.hparams.model_param.filters[2]*2)
|
||||
|
||||
self.map_flat = Flatten(self.map_conv_3.shape)
|
||||
|
||||
self.map_lin = nn.Linear(reduce(mul, self.map_conv_3.shape), self.feature_dim)
|
||||
|
||||
#
|
||||
# Trajectory Encoder
|
||||
self.traj_conv_1 = ConvModule(self.in_shape, conv_kernel=3, conv_stride=1, conv_padding=0,
|
||||
conv_filters=self.hparams.model_param.filters[0])
|
||||
|
||||
self.traj_conv_2 = ConvModule(self.traj_conv_1.shape, conv_kernel=3, conv_stride=1, conv_padding=0,
|
||||
conv_filters=self.hparams.model_param.filters[0])
|
||||
|
||||
self.traj_conv_3 = ConvModule(self.traj_conv_2.shape, conv_kernel=3, conv_stride=1, conv_padding=0,
|
||||
conv_filters=self.hparams.model_param.filters[0])
|
||||
|
||||
self.traj_flat = Flatten(self.traj_conv_3.shape)
|
||||
|
||||
self.traj_lin = nn.Linear(reduce(mul, self.traj_conv_3.shape), self.feature_dim)
|
||||
|
||||
#
|
||||
# Variational Bottleneck
|
||||
self.mu = nn.Linear(self.feature_dim + self.feature_dim + 1, self.hparams.model_param.lat_dim)
|
||||
self.logvar = nn.Linear(self.feature_dim + self.feature_dim + 1, self.hparams.model_param.lat_dim)
|
||||
|
||||
#
|
||||
# Alternative Generator
|
||||
self.alt_lin_1 = nn.Linear(self.hparams.model_param.lat_dim, self.feature_dim)
|
||||
self.alt_lin_2 = nn.Linear(self.feature_dim, reduce(mul, self.traj_conv_3.shape))
|
||||
|
||||
self.reshape_to_map = Flatten(reduce(mul, self.traj_conv_3.shape), self.traj_conv_3.shape)
|
||||
|
||||
self.alt_deconv_1 = DeConvModule(self.traj_conv_3.shape, self.hparams.model_param.filters[2],
|
||||
conv_padding=0, conv_kernel=5, conv_stride=1)
|
||||
self.alt_deconv_2 = DeConvModule(self.alt_deconv_1.shape, self.hparams.model_param.filters[1],
|
||||
conv_padding=0, conv_kernel=3, conv_stride=1)
|
||||
self.alt_deconv_3 = DeConvModule(self.alt_deconv_2.shape, self.hparams.model_param.filters[0],
|
||||
conv_padding=1, conv_kernel=3, conv_stride=1)
|
||||
self.alt_deconv_out = DeConvModule(self.alt_deconv_3.shape, 1, activation=None,
|
||||
conv_padding=1, conv_kernel=3, conv_stride=1)
|
||||
|
||||
def forward(self, batch_x):
|
||||
#
|
||||
# Sorting the Input
|
||||
map_array, trajectory, label = batch_x
|
||||
|
||||
#
|
||||
# Encode
|
||||
map_tensor = self.map_conv_0(map_array)
|
||||
map_tensor = self.map_res_1(map_tensor)
|
||||
map_tensor = self.map_conv_1(map_tensor)
|
||||
map_tensor = self.map_res_2(map_tensor)
|
||||
map_tensor = self.map_conv_2(map_tensor)
|
||||
map_tensor = self.map_res_3(map_tensor)
|
||||
map_tensor = self.map_conv_3(map_tensor)
|
||||
map_tensor = self.map_flat(map_tensor)
|
||||
map_tensor = self.map_lin(map_tensor)
|
||||
|
||||
traj_tensor = self.traj_conv_1(trajectory)
|
||||
traj_tensor = self.traj_conv_2(traj_tensor)
|
||||
traj_tensor = self.traj_conv_3(traj_tensor)
|
||||
traj_tensor = self.traj_flat(traj_tensor)
|
||||
traj_tensor = self.traj_lin(traj_tensor)
|
||||
|
||||
mixed_tensor = torch.cat((map_tensor, traj_tensor, label.float().unsqueeze(-1)), dim=1)
|
||||
mixed_tensor = self.relu(mixed_tensor)
|
||||
|
||||
#
|
||||
# Parameter and Sampling
|
||||
mu = self.mu(mixed_tensor)
|
||||
logvar = self.logvar(mixed_tensor)
|
||||
z = self.reparameterize(mu, logvar)
|
||||
|
||||
#
|
||||
# Generate
|
||||
alt_tensor = self.alt_lin_1(z)
|
||||
alt_tensor = self.alt_lin_2(alt_tensor)
|
||||
alt_tensor = self.reshape_to_map(alt_tensor)
|
||||
alt_tensor = self.alt_deconv_1(alt_tensor)
|
||||
alt_tensor = self.alt_deconv_2(alt_tensor)
|
||||
alt_tensor = self.alt_deconv_3(alt_tensor)
|
||||
alt_tensor = self.alt_deconv_out(alt_tensor)
|
||||
|
||||
return alt_tensor, z, mu, logvar
|
||||
|
||||
@staticmethod
|
||||
def reparameterize(mu, logvar):
|
||||
std = torch.exp(0.5 * logvar)
|
||||
eps = torch.randn_like(std)
|
||||
return mu + eps * std
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
from lib.modules.blocks import LightningBaseModule
|
||||
from lib.modules.losses import BinaryHomotopicLoss
|
||||
from lib.modules.utils import LightningBaseModule
|
||||
from lib.objects.map import Map
|
||||
from lib.objects.trajectory import Trajectory
|
||||
|
||||
import torch.nn as nn
|
||||
|
||||
nn.MSELoss
|
||||
|
||||
class LinearRouteGeneratorModel(LightningBaseModule):
|
||||
|
||||
def test_epoch_end(self, outputs):
|
||||
pass
|
||||
|
||||
name = 'LinearRouteGenerator'
|
||||
|
||||
def configure_optimizers(self):
|
||||
@@ -33,6 +35,12 @@ class LinearRouteGeneratorModel(LightningBaseModule):
|
||||
pred_y = self(map_x, traj_x, label_x)
|
||||
|
||||
loss = self.loss(traj_x, pred_y)
|
||||
|
||||
def training_step(self, batch_xy, batch_nb, *args, **kwargs):
|
||||
batch_x, batch_y = batch_xy
|
||||
pred_y = self(batch_x)
|
||||
loss = self.criterion(pred_y, batch_y.unsqueeze(-1).float())
|
||||
|
||||
return dict(loss=loss, log=dict(loss=loss))
|
||||
|
||||
def test_step(self, *args, **kwargs):
|
||||
@@ -41,7 +49,7 @@ class LinearRouteGeneratorModel(LightningBaseModule):
|
||||
def __init__(self, *params):
|
||||
super(LinearRouteGeneratorModel, self).__init__(*params)
|
||||
|
||||
self.loss = BinaryHomotopicLoss(self.map_storage)
|
||||
self.criterion = BinaryHomotopicLoss(self.map_storage)
|
||||
|
||||
def forward(self, map_x, traj_x, label_x):
|
||||
pass
|
||||
|
||||
@@ -24,41 +24,44 @@ class ConvHomDetector(LightningBaseModule):
|
||||
def training_step(self, batch_xy, batch_nb, *args, **kwargs):
|
||||
batch_x, batch_y = batch_xy
|
||||
pred_y = self(batch_x)
|
||||
loss = F.binary_cross_entropy(pred_y, batch_y.float())
|
||||
loss = self.criterion(pred_y, batch_y.unsqueeze(-1).float())
|
||||
return {'loss': loss, 'log': dict(loss=loss)}
|
||||
|
||||
def test_step(self, batch_xy, **kwargs):
|
||||
def test_step(self, batch_xy, batch_nb, **kwargs):
|
||||
batch_x, batch_y = batch_xy
|
||||
pred_y = self(batch_x)
|
||||
return dict(prediction=pred_y, label=batch_y)
|
||||
return dict(prediction=pred_y, label=batch_y, batch_nb=batch_nb)
|
||||
|
||||
def test_end(self, outputs):
|
||||
evaluation = ROCEvaluation()
|
||||
predictions = torch.stack([x['prediction'] for x in outputs])
|
||||
labels = torch.stack([x['label'] for x in outputs])
|
||||
def test_epoch_end(self, outputs):
|
||||
evaluation = ROCEvaluation(plot_roc=True)
|
||||
predictions = torch.cat([x['prediction'] for x in outputs])
|
||||
labels = torch.cat([x['label'] for x in outputs]).unsqueeze(1)
|
||||
|
||||
scores = evaluation(predictions.numpy(), labels.numpy(), )
|
||||
self.logger.log_metrics({key:value for key, value in zip(['roc_auc', 'tpr', 'fpr'], scores)})
|
||||
# Sci-py call ROC eval call is eval(true_label, prediction)
|
||||
roc_auc, tpr, fpr = evaluation(labels.cpu().numpy(), predictions.cpu().numpy(), )
|
||||
score_dict = dict(roc_auc=roc_auc)
|
||||
# self.logger.log_metrics(score_dict)
|
||||
self.logger.log_image(f'{self.name}', plt.gcf())
|
||||
pass
|
||||
|
||||
def __init__(self, *params):
|
||||
super(ConvHomDetector, self).__init__(*params)
|
||||
return dict(log=score_dict)
|
||||
|
||||
def __init__(self, hparams):
|
||||
super(ConvHomDetector, self).__init__(hparams)
|
||||
|
||||
# Dataset
|
||||
self.dataset = TrajData(self.hparams.data_param.root)
|
||||
self.dataset = TrajData(self.hparams.data_param.map_root, mode='all_in_map')
|
||||
|
||||
# Additional Attributes
|
||||
self.map_shape = self.dataset.map_shapes_max
|
||||
|
||||
# Model Paramters
|
||||
# Model Parameters
|
||||
self.in_shape = self.dataset.map_shapes_max
|
||||
assert len(self.in_shape) == 3, f'Image or map shape has to have 3 dims, but had: {len(self.in_shape)}'
|
||||
self.criterion = nn.BCEWithLogitsLoss()
|
||||
|
||||
# NN Nodes
|
||||
# ============================
|
||||
# Convolutional Map Processing
|
||||
#
|
||||
self.map_conv_0 = ConvModule(self.in_shape, conv_kernel=3, conv_stride=1,
|
||||
conv_padding=0, conv_filters=self.hparams.model_param.filters[0])
|
||||
self.map_res_1 = ResidualModule(self.map_conv_0.shape, ConvModule, 3,
|
||||
@@ -86,7 +89,6 @@ class ConvHomDetector(LightningBaseModule):
|
||||
self.linear = nn.Linear(reduce(mul, self.flatten.shape), self.hparams.model_param.classes * 10)
|
||||
# Comments on Multi Class labels
|
||||
self.classifier = nn.Linear(self.hparams.model_param.classes * 10, 1) # self.hparams.model_param.classes)
|
||||
self.out_activation = nn.Sigmoid() # nn.Softmax
|
||||
|
||||
def forward(self, x):
|
||||
tensor = self.map_conv_0(x)
|
||||
@@ -98,25 +100,4 @@ class ConvHomDetector(LightningBaseModule):
|
||||
tensor = self.flatten(tensor)
|
||||
tensor = self.linear(tensor)
|
||||
tensor = self.classifier(tensor)
|
||||
tensor = self.out_activation(tensor)
|
||||
return tensor
|
||||
|
||||
# Dataloaders
|
||||
# ================================================================================
|
||||
# Train Dataloader
|
||||
def train_dataloader(self):
|
||||
return DataLoader(dataset=self.dataset.train_dataset, shuffle=True,
|
||||
batch_size=self.hparams.data_param.batchsize,
|
||||
num_workers=self.hparams.data_param.worker)
|
||||
|
||||
# Test Dataloader
|
||||
def test_dataloader(self):
|
||||
return DataLoader(dataset=self.dataset.test_dataset, shuffle=True,
|
||||
batch_size=self.hparams.data_param.batchsize,
|
||||
num_workers=self.hparams.data_param.worker)
|
||||
|
||||
# Validation Dataloader
|
||||
def val_dataloader(self):
|
||||
return DataLoader(dataset=self.dataset.val_dataset, shuffle=True,
|
||||
batch_size=self.hparams.data_param.batchsize,
|
||||
num_workers=self.hparams.data_param.worker)
|
||||
|
||||
Reference in New Issue
Block a user