ResidualModule and New Parameters, Speed Manipulation
This commit is contained in:
@@ -1,11 +1,10 @@
|
||||
from argparse import Namespace
|
||||
|
||||
from torch import nn
|
||||
from torch.nn import ModuleDict, ModuleList
|
||||
from torch.nn import ModuleList
|
||||
|
||||
from ml_lib.modules.blocks import ConvModule, LinearModule
|
||||
from ml_lib.modules.utils import (LightningBaseModule, HorizontalSplitter,
|
||||
HorizontalMerger, F_x)
|
||||
from ml_lib.modules.utils import (LightningBaseModule, HorizontalSplitter, HorizontalMerger)
|
||||
from util.module_mixins import (BaseOptimizerMixin, BaseTrainMixin, BaseValMixin, BinaryMaskDatasetFunction,
|
||||
BaseDataloadersMixin)
|
||||
|
||||
@@ -30,44 +29,39 @@ class BandwiseConvClassifier(BinaryMaskDatasetFunction,
|
||||
# Additional parameters
|
||||
self.in_shape = self.dataset.train_dataset.sample_shape
|
||||
self.conv_filters = self.params.filters
|
||||
self.criterion = nn.BCELoss()
|
||||
self.n_band_sections = 4
|
||||
|
||||
# Modules
|
||||
# =============================================================================
|
||||
self.split = HorizontalSplitter(self.in_shape, self.n_band_sections)
|
||||
self.conv_dict = ModuleDict()
|
||||
|
||||
self.conv_dict.update({f"conv_1_{band_section}":
|
||||
ConvModule(self.split.shape, self.conv_filters[0], 3, conv_stride=1, **self.params.module_kwargs)
|
||||
for band_section in range(self.n_band_sections)}
|
||||
)
|
||||
self.conv_dict.update({f"conv_2_{band_section}":
|
||||
ConvModule(self.conv_dict['conv_1_1'].shape, self.conv_filters[1], 3, conv_stride=1,
|
||||
**self.params.module_kwargs) for band_section in range(self.n_band_sections)}
|
||||
)
|
||||
self.conv_dict.update({f"conv_3_{band_section}":
|
||||
ConvModule(self.conv_dict['conv_2_1'].shape, self.conv_filters[2], 3, conv_stride=1,
|
||||
**self.params.module_kwargs)
|
||||
for band_section in range(self.n_band_sections)}
|
||||
)
|
||||
k = 3
|
||||
self.band_list = ModuleList()
|
||||
for band in range(self.n_band_sections):
|
||||
last_shape = self.split.shape
|
||||
conv_list = ModuleList()
|
||||
for filters in self.conv_filters:
|
||||
conv_list.append(ConvModule(last_shape, filters, (k,k), conv_stride=(2, 2), conv_padding=2,
|
||||
**self.params.module_kwargs))
|
||||
last_shape = conv_list[-1].shape
|
||||
# self.conv_list.append(ConvModule(last_shape, 1, 1, conv_stride=1, **self.params.module_kwargs))
|
||||
# last_shape = self.conv_list[-1].shape
|
||||
self.band_list.append(conv_list)
|
||||
|
||||
self.merge = HorizontalMerger(self.conv_dict['conv_3_1'].shape, self.n_band_sections)
|
||||
self.merge = HorizontalMerger(self.band_list[-1][-1].shape, self.n_band_sections)
|
||||
|
||||
self.full_1 = LinearModule(self.flat.shape, self.params.lat_dim, **self.params.module_kwargs)
|
||||
self.full_1 = LinearModule(self.merge.shape, self.params.lat_dim, **self.params.module_kwargs)
|
||||
self.full_2 = LinearModule(self.full_1.shape, self.full_1.shape * 2, **self.params.module_kwargs)
|
||||
self.full_3 = LinearModule(self.full_2.shape, self.full_2.out_features // 2, **self.params.module_kwargs)
|
||||
self.full_3 = LinearModule(self.full_2.shape, self.full_2.shape // 2, **self.params.module_kwargs)
|
||||
|
||||
self.full_out = LinearModule(self.full_3.shape, 1, bias=self.params.bias, activation=nn.Sigmoid)
|
||||
|
||||
def forward(self, batch, **kwargs):
|
||||
tensors = self.split(batch)
|
||||
for idx, tensor in enumerate(tensors):
|
||||
tensors[idx] = self.conv_dict[f"conv_1_{idx}"](tensor)
|
||||
for idx, tensor in enumerate(tensors):
|
||||
tensors[idx] = self.conv_dict[f"conv_2_{idx}"](tensor)
|
||||
for idx, tensor in enumerate(tensors):
|
||||
tensors[idx] = self.conv_dict[f"conv_3_{idx}"](tensor)
|
||||
for idx, (tensor, convs) in enumerate(zip(tensors, self.band_list)):
|
||||
for conv in convs:
|
||||
tensor = conv(tensor)
|
||||
tensors[idx] = tensor
|
||||
|
||||
tensor = self.merge(tensors)
|
||||
tensor = self.full_1(tensor)
|
||||
|
@@ -22,24 +22,32 @@ class BandwiseConvMultiheadClassifier(BinaryMaskDatasetFunction,
|
||||
batch_x, batch_y = batch_xy
|
||||
y = self(batch_x)
|
||||
y, bands_y = y.main_out, y.bands
|
||||
bands_y_losses = [self.criterion(band_y, batch_y) for band_y in bands_y]
|
||||
bands_y_losses = [self.bce_loss(band_y, batch_y) for band_y in bands_y]
|
||||
return_dict = {f'band_{band_idx}_loss': band_y for band_idx, band_y in enumerate(bands_y_losses)}
|
||||
overall_loss = self.criterion(y, batch_y)
|
||||
combined_loss = overall_loss + torch.stack(bands_y_losses).sum()
|
||||
return_dict.update(loss=combined_loss, overall_loss=overall_loss)
|
||||
|
||||
last_bce_loss = self.bce_loss(y, batch_y)
|
||||
return_dict.update(last_bce_loss=last_bce_loss)
|
||||
|
||||
bands_y_losses.append(last_bce_loss)
|
||||
combined_loss = torch.stack(bands_y_losses).mean()
|
||||
|
||||
return_dict.update(loss=combined_loss)
|
||||
return return_dict
|
||||
|
||||
def validation_step(self, batch_xy, batch_idx, *args, **kwargs):
|
||||
batch_x, batch_y = batch_xy
|
||||
y = self(batch_x)
|
||||
y, bands_y = y.main_out, y.bands
|
||||
bands_y_losses = [self.criterion(band_y, batch_y) for band_y in bands_y]
|
||||
bands_y_losses = [self.bce_loss(band_y, batch_y) for band_y in bands_y]
|
||||
return_dict = {f'band_{band_idx}_val_loss': band_y for band_idx, band_y in enumerate(bands_y_losses)}
|
||||
overall_loss = self.criterion(y, batch_y)
|
||||
combined_loss = overall_loss + torch.stack(bands_y_losses).sum()
|
||||
|
||||
val_abs_loss = self.absolute_loss(y, batch_y)
|
||||
return_dict.update(val_bce_loss=combined_loss, val_abs_loss=val_abs_loss,
|
||||
last_bce_loss = self.bce_loss(y, batch_y)
|
||||
return_dict.update(last_bce_loss=last_bce_loss)
|
||||
|
||||
bands_y_losses.append(last_bce_loss)
|
||||
combined_loss = torch.stack(bands_y_losses).mean()
|
||||
|
||||
return_dict.update(val_bce_loss=combined_loss,
|
||||
batch_idx=batch_idx, y=y, batch_y=batch_y
|
||||
)
|
||||
return return_dict
|
||||
@@ -56,7 +64,6 @@ class BandwiseConvMultiheadClassifier(BinaryMaskDatasetFunction,
|
||||
# Additional parameters
|
||||
self.in_shape = self.dataset.train_dataset.sample_shape
|
||||
self.conv_filters = self.params.filters
|
||||
self.criterion = nn.BCELoss()
|
||||
self.n_band_sections = 4
|
||||
k = 3 # Base Kernel Value
|
||||
|
||||
@@ -69,7 +76,7 @@ class BandwiseConvMultiheadClassifier(BinaryMaskDatasetFunction,
|
||||
last_shape = self.split.shape
|
||||
conv_list = ModuleList()
|
||||
for filters in self.conv_filters:
|
||||
conv_list.append(ConvModule(last_shape, filters, (k, k*4), conv_stride=(1, 2),
|
||||
conv_list.append(ConvModule(last_shape, filters, (k,k), conv_stride=(1, 1),
|
||||
**self.params.module_kwargs))
|
||||
last_shape = conv_list[-1].shape
|
||||
# self.conv_list.append(ConvModule(last_shape, 1, 1, conv_stride=1, **self.params.module_kwargs))
|
||||
|
@@ -29,23 +29,23 @@ class ConvClassifier(BinaryMaskDatasetFunction,
|
||||
# Additional parameters
|
||||
self.in_shape = self.dataset.train_dataset.sample_shape
|
||||
self.conv_filters = self.params.filters
|
||||
self.criterion = nn.BCELoss()
|
||||
|
||||
# Modules with Parameters
|
||||
self.conv_list = ModuleList()
|
||||
last_shape = self.in_shape
|
||||
k = 3 # Base Kernel Value
|
||||
for filters in self.conv_filters:
|
||||
self.conv_list.append(ConvModule(last_shape, filters, (k, k*2), conv_stride=2, **self.params.module_kwargs))
|
||||
self.conv_list.append(ConvModule(last_shape, filters, (k,k), conv_stride=(2, 2), conv_padding=2,
|
||||
**self.params.module_kwargs))
|
||||
last_shape = self.conv_list[-1].shape
|
||||
# self.conv_list.append(ConvModule(last_shape, 1, 1, conv_stride=1, **self.params.module_kwargs))
|
||||
# last_shape = self.conv_list[-1].shape
|
||||
|
||||
self.full_1 = LinearModule(self.flat.shape, self.params.lat_dim, **self.params.module_kwargs)
|
||||
self.full_2 = LinearModule(self.full_1.out_features, self.full_1.out_features * 2, self.params.bias)
|
||||
self.full_3 = LinearModule(self.full_2.out_features, self.full_2.out_features // 2, self.params.bias)
|
||||
self.full_1 = LinearModule(self.conv_list[-1].shape, self.params.lat_dim, **self.params.module_kwargs)
|
||||
self.full_2 = LinearModule(self.full_1.shape, self.full_1.shape * 2, **self.params.module_kwargs)
|
||||
self.full_3 = LinearModule(self.full_2.shape, self.full_2.shape // 2, **self.params.module_kwargs)
|
||||
|
||||
self.full_out = LinearModule(self.full_3.out_features, 1, bias=self.params.bias, activation=nn.Sigmoid)
|
||||
self.full_out = LinearModule(self.full_3.shape, 1, bias=self.params.bias, activation=nn.Sigmoid)
|
||||
|
||||
def forward(self, batch, **kwargs):
|
||||
tensor = batch
|
||||
|
64
models/residual_conv_classifier.py
Normal file
64
models/residual_conv_classifier.py
Normal file
@@ -0,0 +1,64 @@
|
||||
from argparse import Namespace
|
||||
|
||||
from torch import nn
|
||||
from torch.nn import ModuleList
|
||||
|
||||
from ml_lib.modules.blocks import ConvModule, LinearModule, ResidualModule
|
||||
from ml_lib.modules.utils import LightningBaseModule
|
||||
from util.module_mixins import (BaseOptimizerMixin, BaseTrainMixin, BaseValMixin, BinaryMaskDatasetFunction,
|
||||
BaseDataloadersMixin)
|
||||
|
||||
|
||||
class ResidualConvClassifier(BinaryMaskDatasetFunction,
|
||||
BaseDataloadersMixin,
|
||||
BaseTrainMixin,
|
||||
BaseValMixin,
|
||||
BaseOptimizerMixin,
|
||||
LightningBaseModule
|
||||
):
|
||||
|
||||
def __init__(self, hparams):
|
||||
super(ResidualConvClassifier, self).__init__(hparams)
|
||||
|
||||
# Dataset
|
||||
# =============================================================================
|
||||
self.dataset = self.build_dataset()
|
||||
|
||||
# Model Paramters
|
||||
# =============================================================================
|
||||
# Additional parameters
|
||||
self.in_shape = self.dataset.train_dataset.sample_shape
|
||||
self.conv_filters = self.params.filters
|
||||
|
||||
# Modules with Parameters
|
||||
self.conv_list = ModuleList()
|
||||
last_shape = self.in_shape
|
||||
k = 3 # Base Kernel Value
|
||||
conv_module_params = self.params.module_kwargs
|
||||
conv_module_params.update(conv_kernel=(k, k), conv_stride=(1, 1), conv_padding=1)
|
||||
self.conv_list.append(ConvModule(last_shape, self.conv_filters[0], (k, k), conv_stride=(2, 2), conv_padding=1,
|
||||
**self.params.module_kwargs))
|
||||
last_shape = self.conv_list[-1].shape
|
||||
for filters in self.conv_filters:
|
||||
conv_module_params.update(conv_filters=filters)
|
||||
self.conv_list.append(ResidualModule(last_shape, ConvModule, 3, **conv_module_params))
|
||||
last_shape = self.conv_list[-1].shape
|
||||
self.conv_list.append(ConvModule(last_shape, filters, (k, k), conv_stride=(2, 2), conv_padding=2,
|
||||
**self.params.module_kwargs))
|
||||
last_shape = self.conv_list[-1].shape
|
||||
|
||||
self.full_1 = LinearModule(self.conv_list[-1].shape, self.params.lat_dim, **self.params.module_kwargs)
|
||||
self.full_2 = LinearModule(self.full_1.shape, self.full_1.shape * 2, **self.params.module_kwargs)
|
||||
self.full_3 = LinearModule(self.full_2.shape, self.full_2.shape // 2, **self.params.module_kwargs)
|
||||
|
||||
self.full_out = LinearModule(self.full_3.shape, 1, bias=self.params.bias, activation=nn.Sigmoid)
|
||||
|
||||
def forward(self, batch, **kwargs):
|
||||
tensor = batch
|
||||
for conv in self.conv_list:
|
||||
tensor = conv(tensor)
|
||||
tensor = self.full_1(tensor)
|
||||
tensor = self.full_2(tensor)
|
||||
tensor = self.full_3(tensor)
|
||||
tensor = self.full_out(tensor)
|
||||
return Namespace(main_out=tensor)
|
Reference in New Issue
Block a user