From 92050f593336e76ac05af6ed1a1e72def1d3e66f Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Sun, 1 May 2022 17:03:19 +0200 Subject: [PATCH] On/Off filter can be turned on/off --- Dataset.py | 115 +++++++++++++++++++++++++++++++++------------------ Parameter.py | 34 +++++++++++---- 2 files changed, 99 insertions(+), 50 deletions(-) diff --git a/Dataset.py b/Dataset.py index 11f9854..8808ddb 100644 --- a/Dataset.py +++ b/Dataset.py @@ -135,10 +135,13 @@ class DatasetMNIST(DatasetMaster): pattern = scripted_transforms(pattern) # => On/Off - my_on_off_filter: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) - gray: torch.Tensor = my_on_off_filter( - pattern[:, 0:1, :, :], - ) + if cfg.augmentation.use_on_off_filter is True: + my_on_off_filter: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) + gray: torch.Tensor = my_on_off_filter( + pattern[:, 0:1, :, :], + ) + else: + gray = pattern[:, 0:1, :, :] + torch.finfo(torch.float32).eps return gray @@ -166,10 +169,13 @@ class DatasetMNIST(DatasetMaster): pattern = scripted_transforms(pattern) # => On/Off - my_on_off_filter: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) - gray: torch.Tensor = my_on_off_filter( - pattern[:, 0:1, :, :], - ) + if cfg.augmentation.use_on_off_filter is True: + my_on_off_filter: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) + gray: torch.Tensor = my_on_off_filter( + pattern[:, 0:1, :, :], + ) + else: + gray = pattern[:, 0:1, :, :] + torch.finfo(torch.float32).eps return gray @@ -225,10 +231,13 @@ class DatasetFashionMNIST(DatasetMaster): pattern = scripted_transforms(pattern) # => On/Off - my_on_off_filter: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) - gray: torch.Tensor = my_on_off_filter( - pattern[:, 0:1, :, :], - ) + if cfg.augmentation.use_on_off_filter is True: + my_on_off_filter: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) + gray: torch.Tensor = my_on_off_filter( + pattern[:, 0:1, :, :], + ) + else: + gray = pattern[:, 0:1, :, :] + torch.finfo(torch.float32).eps return gray @@ -263,10 +272,13 @@ class DatasetFashionMNIST(DatasetMaster): pattern = scripted_transforms(pattern) # => On/Off - my_on_off_filter: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) - gray: torch.Tensor = my_on_off_filter( - pattern[:, 0:1, :, :], - ) + if cfg.augmentation.use_on_off_filter is True: + my_on_off_filter: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) + gray: torch.Tensor = my_on_off_filter( + pattern[:, 0:1, :, :], + ) + else: + gray = pattern[:, 0:1, :, :] + torch.finfo(torch.float32).eps return gray @@ -321,19 +333,29 @@ class DatasetCIFAR(DatasetMaster): pattern = scripted_transforms(pattern) # => On/Off - - my_on_off_filter_r: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) - my_on_off_filter_g: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[1]) - my_on_off_filter_b: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[2]) - r: torch.Tensor = my_on_off_filter_r( - pattern[:, 0:1, :, :], - ) - g: torch.Tensor = my_on_off_filter_g( - pattern[:, 1:2, :, :], - ) - b: torch.Tensor = my_on_off_filter_b( - pattern[:, 2:3, :, :], - ) + if cfg.augmentation.use_on_off_filter is True: + my_on_off_filter_r: OnOffFilter = OnOffFilter( + p=cfg.image_statistics.mean[0] + ) + my_on_off_filter_g: OnOffFilter = OnOffFilter( + p=cfg.image_statistics.mean[1] + ) + my_on_off_filter_b: OnOffFilter = OnOffFilter( + p=cfg.image_statistics.mean[2] + ) + r: torch.Tensor = my_on_off_filter_r( + pattern[:, 0:1, :, :], + ) + g: torch.Tensor = my_on_off_filter_g( + pattern[:, 1:2, :, :], + ) + b: torch.Tensor = my_on_off_filter_b( + pattern[:, 2:3, :, :], + ) + else: + r = pattern[:, 0:1, :, :] + torch.finfo(torch.float32).eps + g = pattern[:, 1:2, :, :] + torch.finfo(torch.float32).eps + b = pattern[:, 2:3, :, :] + torch.finfo(torch.float32).eps new_tensor: torch.Tensor = torch.cat((r, g, b), dim=1) return new_tensor @@ -370,18 +392,29 @@ class DatasetCIFAR(DatasetMaster): pattern = scripted_transforms(pattern) # => On/Off - my_on_off_filter_r: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[0]) - my_on_off_filter_g: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[1]) - my_on_off_filter_b: OnOffFilter = OnOffFilter(p=cfg.image_statistics.mean[2]) - r: torch.Tensor = my_on_off_filter_r( - pattern[:, 0:1, :, :], - ) - g: torch.Tensor = my_on_off_filter_g( - pattern[:, 1:2, :, :], - ) - b: torch.Tensor = my_on_off_filter_b( - pattern[:, 2:3, :, :], - ) + if cfg.augmentation.use_on_off_filter is True: + my_on_off_filter_r: OnOffFilter = OnOffFilter( + p=cfg.image_statistics.mean[0] + ) + my_on_off_filter_g: OnOffFilter = OnOffFilter( + p=cfg.image_statistics.mean[1] + ) + my_on_off_filter_b: OnOffFilter = OnOffFilter( + p=cfg.image_statistics.mean[2] + ) + r: torch.Tensor = my_on_off_filter_r( + pattern[:, 0:1, :, :], + ) + g: torch.Tensor = my_on_off_filter_g( + pattern[:, 1:2, :, :], + ) + b: torch.Tensor = my_on_off_filter_b( + pattern[:, 2:3, :, :], + ) + else: + r = pattern[:, 0:1, :, :] + torch.finfo(torch.float32).eps + g = pattern[:, 1:2, :, :] + torch.finfo(torch.float32).eps + b = pattern[:, 2:3, :, :] + torch.finfo(torch.float32).eps new_tensor: torch.Tensor = torch.cat((r, g, b), dim=1) return new_tensor diff --git a/Parameter.py b/Parameter.py index 92fe247..e8d63ea 100644 --- a/Parameter.py +++ b/Parameter.py @@ -42,12 +42,14 @@ class Network: its layers and the number of output neurons.""" number_of_output_neurons: int = field(default=0) - forward_kernel_size: list[list[int]] = field(default_factory=list) forward_neuron_numbers: list[list[int]] = field(default_factory=list) + is_pooling_layer: list[bool] = field(default_factory=list) + + forward_kernel_size: list[list[int]] = field(default_factory=list) strides: list[list[int]] = field(default_factory=list) dilation: list[list[int]] = field(default_factory=list) padding: list[list[int]] = field(default_factory=list) - is_pooling_layer: list[bool] = field(default_factory=list) + w_trainable: list[bool] = field(default_factory=list) eps_xy_trainable: list[bool] = field(default_factory=list) eps_xy_mean: list[bool] = field(default_factory=list) @@ -57,24 +59,34 @@ class Network: class LearningParameters: """Parameter required for training""" + learning_active: bool = field(default=True) + loss_coeffs_mse: float = field(default=0.5) loss_coeffs_kldiv: float = field(default=1.0) + + optimizer_name: str = field(default="Adam") learning_rate_gamma_w: float = field(default=-1.0) learning_rate_gamma_eps_xy: float = field(default=-1.0) learning_rate_threshold_w: float = field(default=0.00001) learning_rate_threshold_eps_xy: float = field(default=0.00001) - learning_active: bool = field(default=True) + + lr_schedule_name: str = field(default="ReduceLROnPlateau") + lr_scheduler_factor_w: float = field(default=0.75) + lr_scheduler_patience_w: int = field(default=-1) + + lr_scheduler_factor_eps_xy: float = field(default=0.75) + lr_scheduler_patience_eps_xy: int = field(default=-1) + + number_of_batches_for_one_update: int = field(default=1) + overload_path: str = field(default="./Previous") + weight_noise_amplitude: float = field(default=0.01) eps_xy_intitial: float = field(default=0.1) + test_every_x_learning_steps: int = field(default=50) test_during_learning: bool = field(default=True) - lr_scheduler_factor: float = field(default=0.75) - lr_scheduler_patience: int = field(default=10) - optimizer_name: str = field(default="Adam") - lr_schedule_name: str = field(default="ReduceLROnPlateau") - number_of_batches_for_one_update: int = field(default=1) + alpha_number_of_iterations: int = field(default=0) - overload_path: str = field(default="./Previous") @dataclass @@ -82,12 +94,16 @@ class Augmentation: """Parameters used for data augmentation.""" crop_width_in_pixel: int = field(default=2) + flip_p: float = field(default=0.5) + jitter_brightness: float = field(default=0.5) jitter_contrast: float = field(default=0.1) jitter_saturation: float = field(default=0.1) jitter_hue: float = field(default=0.15) + use_on_off_filter: bool = field(default=True) + @dataclass class ImageStatistics: