1最大似然估计
张振虎的博客:https://www.zhangzhenhu.com/glm/source/%E6%9C%80%E5%A4%A7%E4%BC%BC%E7%84%B6%E4%BC%B0%E8%AE%A1/content.html
2flow based generative model
https://zhuanlan.zhihu.com/p/351479696
https://ch3nye.top/Flow-based-Generative-Model/
理解jacobian matrix
理解矩阵行列式det
理解变量变换定理(change of variable theorem)
然后理解 最大似然估计的公式:
因此关键就是求解 G的逆 和 G的jacobi矩阵的det
最后介绍了2个模块,这两个模块 求G的逆和 G的juacobi矩阵的行列式的值都比较容易计算
设计模块的时候也要考虑的逆 和行列式的计算。
这两个模块分别是Coupling Layer 和 1x1 Convolution
以上就是 flow based model的基本原理
3一些相关函数det(), logdet(), slogdet()
det计算行列式
logdet计算 行列式后,再log
slogdet 计算行列式后,取绝对值再取log, 返回行列式的符号和 最终结果
4.Glow 代码阅读
https://github.com/rosinality/glow-pytorch/blob/master/model.py
import torch
from torch import nn
from torch.nn import functional as F
from math import log, pi, exp
import numpy as np
from scipy import linalg as la
logabs = lambda x: torch.log(torch.abs(x))
# ActNorm 就是对input 按通道 norm: 减去均值后,除以方差
# logdet ?
class ActNorm(nn.Module):
def __init__(self, in_channel, logdet=True):
super().__init__()
self.loc = nn.Parameter(torch.zeros(1, in_channel, 1, 1))
self.scale = nn.Parameter(torch.ones(1, in_channel, 1, 1))
self.register_buffer("initialized", torch.tensor(0, dtype=torch.uint8))
self.logdet = logdet
# 按通道计算均值和方差
def initialize(self, input):
with torch.no_grad():
flatten = input.permute(1, 0, 2, 3).contiguous().view(input.shape[1], -1)
mean = (
flatten.mean(1)
.unsqueeze(1)
.unsqueeze(2)
.unsqueeze(3)
.permute(1, 0, 2, 3)
)
std = (
flatten.std(1)
.unsqueeze(1)
.unsqueeze(2)
.unsqueeze(3)
.permute(1, 0, 2, 3)
)
self.loc.data.copy_(-mean)
self.scale.data.copy_(1 / (std + 1e-6))
def forward(self, input):
_, _, height, width = input.shape
if self.initialized.item() == 0:
self.initialize(input)
self.initialized.fill_(1)
log_abs = logabs(self.scale)
logdet = height * width * torch.sum(log_abs)
if self.logdet:
return self.scale * (input + self.loc), logdet
else:
return self.scale * (input + self.loc)
def reverse(self, output):
return output / self.scale - self.loc
class Flow 主要是actnorm, invconv2d, affinecoupling三个模块组成。三个模块都是可逆的。
loss:
5. Modeling sRGB Camera Noise with Normalizing Flows
Noise2NoiseFlow: Realistic Camera Noise Modeling without Clean Images
这两篇生成噪声的论文用的 flow based generation model