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

take off actnorm from conditional glow #88

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "InvertibleNetworks"
uuid = "b7115f24-5f92-4794-81e8-23b0ddb121d3"
authors = ["Philipp Witte <[email protected]>", "Ali Siahkoohi <[email protected]>", "Mathias Louboutin <[email protected]>", "Gabrio Rizzuti <[email protected]>", "Rafael Orozco <[email protected]>", "Felix J. herrmann <[email protected]>"]
version = "2.2.5"
version = "2.2.6"

[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand Down
14 changes: 10 additions & 4 deletions src/conditional_layers/conditional_layer_glow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -75,10 +75,13 @@ end

# Constructor from input dimensions
function ConditionalLayerGlow(n_in::Int64, n_cond::Int64, n_hidden::Int64;freeze_conv=false, k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, logdet=false, activation::ActivationFunction=SigmoidLayer(), rb_activation::ActivationFunction=RELUlayer(), ndims=2)

# 1x1 Convolution and residual block for invertible layers
C = Conv1x1(n_in; freeze=freeze_conv)
RB = ResidualBlock(Int(n_in/2)+n_cond, n_hidden; n_out=n_in, activation=rb_activation, k1=k1, k2=k2, p1=p1, p2=p2, s1=s1, s2=s2, fan=true, ndims=ndims)

split_num = Int(round(n_in/2))
in_chan = n_in-split_num
out_chan = 2*split_num

RB = ResidualBlock(in_chan+n_cond, n_hidden; n_out=out_chan, activation=rb_activation, k1=k1, k2=k2, p1=p1, p2=p2, s1=s1, s2=s2, fan=true, ndims=ndims)

return ConditionalLayerGlow(C, RB, logdet, activation)
end
Expand Down Expand Up @@ -143,7 +146,10 @@ function backward(ΔY::AbstractArray{T, N}, Y::AbstractArray{T, N}, C::AbstractA

# Backpropagate RB
ΔX2_ΔC = L.RB.backward(tensor_cat(L.activation.backward(ΔS, S), ΔT), (tensor_cat(X2, C)))
ΔX2, ΔC = tensor_split(ΔX2_ΔC; split_index=Int(size(ΔY)[N-1]/2))

n_in = size(ΔY)[N-1]
split_num = Int(round(n_in/2))
ΔX2, ΔC = tensor_split(ΔX2_ΔC; split_index=n_in-split_num)
ΔX2 += ΔY2

# Backpropagate 1x1 conv
Expand Down
25 changes: 10 additions & 15 deletions src/networks/invertible_network_conditional_glow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@
export NetworkConditionalGlow, NetworkConditionalGlow3D

"""
G = NetworkGlow(n_in, n_cond, n_hidden, L, K; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1)
G = NetworkConditionalGlow(n_in, n_cond, n_hidden, L, K; split_scales=fals)

G = NetworkGlow3D(n_in, n_cond, n_hidden, L, K; k1=3, k2=1, p1=1, p2=0, s1=1, s2=1)
G = NetworkConditionalGlow3D(n_in, n_cond, n_hidden, L, K; split_scales=false)

Create a conditional invertible network based on the Glow architecture. Each flow step in the inner loop
consists of an activation normalization layer, followed by an invertible coupling layer with
1x1 convolutions and a residual block. The outer loop performs a squeezing operation prior
to the inner loop, and a splitting operation afterwards.
consists of an activation normalization layer, followed by an invertible glow conditional coupling layer with
1x1 convolutions and a residual block that takes the condition as an input.
The outer loop performs a squeezing operation prior to the inner loop, and a splitting operation afterwards.

*Input*:

Expand Down Expand Up @@ -44,7 +44,7 @@ export NetworkConditionalGlow, NetworkConditionalGlow3D

*Output*:

- `G`: invertible Glow network.
- `G`: invertible conditional Glow network.

*Usage:*

Expand All @@ -56,14 +56,13 @@ export NetworkConditionalGlow, NetworkConditionalGlow3D

- None in `G` itself

- Trainable parameters in activation normalizations `G.AN[i,j]` and coupling layers `G.C[i,j]`,
- Trainable parameters in activation normalizations `G.AN[i,j]` and coupling layers `G.CL[i,j]`,
where `i` and `j` range from `1` to `L` and `K` respectively.

See also: [`ActNorm`](@ref), [`CouplingLayerGlow!`](@ref), [`get_params`](@ref), [`clear_grad!`](@ref)
See also: [`ActNorm`](@ref), [`ConditionalLayerGlow!`](@ref), [`get_params`](@ref), [`clear_grad!`](@ref)
"""
struct NetworkConditionalGlow <: InvertibleNetwork
AN::AbstractArray{ActNorm, 2}
AN_C::ActNorm
CL::AbstractArray{ConditionalLayerGlow, 2}
Z_dims::Union{Array{Array, 1}, Nothing}
L::Int64
Expand All @@ -77,7 +76,6 @@ end
# Constructor
function NetworkConditionalGlow(n_in, n_cond, n_hidden, L, K; freeze_conv=false, split_scales=false, rb_activation::ActivationFunction=ReLUlayer(), k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, ndims=2, squeezer::Squeezer=ShuffleLayer(), activation::ActivationFunction=SigmoidLayer())
AN = Array{ActNorm}(undef, L, K) # activation normalization
AN_C = ActNorm(n_cond; logdet=false) # activation normalization for condition
CL = Array{ConditionalLayerGlow}(undef, L, K) # coupling layers w/ 1x1 convolution and residual block

if split_scales
Expand All @@ -98,7 +96,7 @@ function NetworkConditionalGlow(n_in, n_cond, n_hidden, L, K; freeze_conv=false,
(i < L && split_scales) && (n_in = Int64(n_in/2)) # split
end

return NetworkConditionalGlow(AN, AN_C, CL, Z_dims, L, K, squeezer, split_scales)
return NetworkConditionalGlow(AN, CL, Z_dims, L, K, squeezer, split_scales)
end

NetworkConditionalGlow3D(args; kw...) = NetworkConditionalGlow(args...; kw..., ndims=3)
Expand All @@ -108,8 +106,6 @@ function forward(X::AbstractArray{T, N}, C::AbstractArray{T, N}, G::NetworkCondi
G.split_scales && (Z_save = array_of_array(X, G.L-1))
orig_shape = size(X)

C = G.AN_C.forward(C)

logdet = 0
for i=1:G.L
(G.split_scales) && (X = G.squeezer.forward(X))
Expand Down Expand Up @@ -176,6 +172,5 @@ function backward(ΔX::AbstractArray{T, N}, X::AbstractArray{T, N}, C::AbstractA
end
end

ΔC, C = G.AN_C.backward(ΔC, C)
return ΔX, X, ΔC
end
end
Loading