likelihoods
Script containing modules for defining different likelihood functions (as nn.Module).
GaussianLikelihood
#
Bases: LikelihoodModule
A specialized LikelihoodModule
for Gaussian likelihood.
Specifically, in the LVAE model, the likelihood is defined as: p(x|z_1) = N(x|\mu_{p,1}, \sigma_{p,1}^2)
Source code in src/careamics/models/lvae/likelihoods.py
135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 |
|
__init__(predict_logvar=None, logvar_lowerbound=None)
#
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
predict_logvar | Union[Literal['pixelwise'], None] | If | None |
logvar_lowerbound | Union[float, None] | The lowerbound value for log-variance. Default is | None |
Source code in src/careamics/models/lvae/likelihoods.py
distr_params(x)
#
Get parameters (mean, log-var) of the Gaussian distribution defined by the likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | Tensor | The input tensor to the likelihood module, i.e., the output the LVAE 'output_layer'. Shape is: (B, 2 * C, [Z], Y, X) in case | required |
Source code in src/careamics/models/lvae/likelihoods.py
get_mean_lv(x)
#
Given the output of the top-down pass, compute the mean and log-variance of the Gaussian distribution defining the likelihood.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | Tensor | The input tensor to the likelihood module, i.e., the output of the top-down pass. | required |
Returns:
Type | Description |
---|---|
tuple of (torch.tensor, optional torch.tensor) | The first element of the tuple is the mean, the second element is the log-variance. If the attribute |
Source code in src/careamics/models/lvae/likelihoods.py
log_likelihood(x, params)
#
Compute Gaussian log-likelihood
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | Tensor | The target tensor. Shape is (B, C, [Z], Y, X). | required |
params | dict[str, Union[Tensor, None]] | The tensors obtained by chunking the output of the top-down pass, here used as parameters of the Gaussian distribution. | required |
Returns:
Type | Description |
---|---|
Tensor | The log-likelihood tensor. Shape is (B, C, [Z], Y, X). |
Source code in src/careamics/models/lvae/likelihoods.py
LikelihoodModule
#
Bases: Module
The base class for all likelihood modules. It defines the fundamental structure and methods for specialized likelihood models.
Source code in src/careamics/models/lvae/likelihoods.py
forward(input_, x)
#
Parameters:
Name | Type | Description | Default |
---|---|---|---|
input_ | Tensor | The output of the top-down pass (e.g., reconstructed image in HDN, or the unmixed images in 'Split' models). | required |
x | Union[Tensor, None] | The target tensor. If None, the log-likelihood is not computed. | required |
Source code in src/careamics/models/lvae/likelihoods.py
NoiseModelLikelihood
#
Bases: LikelihoodModule
Source code in src/careamics/models/lvae/likelihoods.py
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 |
|
__init__(data_mean, data_std, noise_model)
#
Constructor.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
data_mean | Union[ndarray, Tensor] | The mean of the data, used to unnormalize data for noise model evaluation. | required |
data_std | Union[ndarray, Tensor] | The standard deviation of the data, used to unnormalize data for noise model evaluation. | required |
noiseModel | The noise model instance used to compute the likelihood. | required |
Source code in src/careamics/models/lvae/likelihoods.py
log_likelihood(x, params)
#
Compute the log-likelihood given the parameters params
obtained from the reconstruction tensor and the target tensor x
.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | Tensor | The target tensor. Shape is (B, C, [Z], Y, X). | required |
params | dict[str, Tensor] | The tensors obtained from output of the top-down pass. Here, "mean" correspond to the whole output, while logvar is | required |
Returns:
Type | Description |
---|---|
Tensor | The log-likelihood tensor. Shape is (B, C, [Z], Y, X). |
Source code in src/careamics/models/lvae/likelihoods.py
likelihood_factory(config, noise_model=None)
#
Factory function for creating likelihood modules.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
config | Optional[Union[GaussianLikelihoodConfig, NMLikelihoodConfig]] | The configuration object for the likelihood module. | required |
noise_model | Optional[NoiseModel] | The noise model instance used to define the | None |
Returns:
Type | Description |
---|---|
Module | The likelihood module. |
Source code in src/careamics/models/lvae/likelihoods.py
log_normal(x, mean, logvar)
#
Compute the log-probability at x
of a Gaussian distribution with parameters (mean, exp(logvar))
.
NOTE: In the case of LVAE, the log-likeihood formula becomes: \mathbb{E}{z_1\sim{q\phi}}[\log{p_ heta(x|z_1)}]=- rac{1}{2}(\mathbb{E}{z_1\sim{q\phi}}[\log{2\pi\sigma_{p,0}^2(z_1)}] +\mathbb{E}{z_1\sim{q\phi}}[ rac{(x-\mu_{p,0}(z_1))^2}{\sigma_{p,0}^2(z_1)}])
Parameters:
Name | Type | Description | Default |
---|---|---|---|
x | Tensor | The ground-truth tensor. Shape is (batch, channels, dim1, dim2). | required |
mean | Tensor | The inferred mean of distribution. Shape is (batch, channels, dim1, dim2). | required |
logvar | Tensor | The inferred log-variance of distribution. Shape has to be either scalar or broadcastable. | required |