Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix/equiv padding weightedmse #346

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion cyto_dl/models/vae/base_vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,10 @@ def __init__(
self.latent_dim = latent_dim

if decoder_latent_parts is None:
self.decoder_latent_parts = {key: self.prior.keys() for key in self.decoder.keys()}
pass
# self.decoder_latent_parts = {
# key: self.prior.keys() for key in self.decoder.keys()
# }
else:
self.decoder_latent_parts = decoder_latent_parts
for key in self.decoder.keys():
Expand All @@ -157,9 +160,11 @@ def __init__(
def calculate_rcl(self, x, xhat, input_key, target_key=None):
if not target_key:
target_key = input_key

rcl_per_input_dimension = self.reconstruction_loss[input_key](
x[target_key], xhat[input_key]
)

return rcl_per_input_dimension

def calculate_rcl_dict(self, x, xhat, z):
Expand Down Expand Up @@ -190,6 +195,10 @@ def calculate_elbo(self, x, xhat, z):

total_kld = sum(kld_per_part_summed.values())
total_recon = sum(rcl_reduced.values())
if len(total_recon.shape) > 0:
total_recon = total_recon.mean()
for key in rcl_reduced.keys():
rcl_reduced[key] = rcl_reduced[key].mean()

return (
total_recon + self.beta * total_kld,
Expand Down
2 changes: 2 additions & 0 deletions cyto_dl/models/vae/image_encoder.py
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ def make_block(
subunits=self.num_res_units,
bias=bias,
)
if padding is None:
padding = same_padding(kernel_size)

return Convolution(
spatial_dims=self.spatial_dims,
Expand Down
8 changes: 6 additions & 2 deletions cyto_dl/models/vae/image_vae.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def forward(self, x):
class ImageVAE(BaseVAE):
def __init__(
self,
x_label: str,
latent_dim: int,
spatial_dims: int,
in_shape: Sequence[int],
Expand Down Expand Up @@ -61,14 +62,15 @@ def __init__(
num_res_units: int = 2,
up_kernel_size: int = 3,
first_conv_padding_mode: str = "replicate",
encoder_padding: Optional[Union[int, Sequence[int]]] = None,
eps: float = 1e-8,
encoder_padding: Optional[Union[int, Sequence[int]]] = None,
metric_keys: Optional[list] = None,
**base_kwargs,
):
in_channels, *in_shape = in_shape

self.out_channels = out_channels if out_channels is not None else in_channels

self.x_label = x_label
self.spatial_dims = spatial_dims
self.final_size = np.asarray(in_shape, dtype=int)
self.up_kernel_size = up_kernel_size
Expand Down Expand Up @@ -207,6 +209,8 @@ def __init__(
decoder=decoder,
latent_dim=latent_dim,
prior=prior,
x_label=x_label,
metric_keys=metric_keys,
**base_kwargs,
)

Expand Down
Loading
Loading