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

add logdet option to glow and example of training with flux #92

Merged
merged 3 commits into from
Oct 3, 2023
Merged
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 <p.witte@ymail.com>", "Ali Siahkoohi <alisk@gatech.edu>", "Mathias Louboutin <mlouboutin3@gatech.edu>", "Gabrio Rizzuti <g.rizzuti@umcutrecht.nl>", "Rafael Orozco <rorozco@gatech.edu>", "Felix J. herrmann <fherrmann@gatech.edu>"]
version = "2.2.5"
version = "2.2.6"

[deps]
CUDA = "052768ef-5323-5732-b1bb-66c8b64840ba"
Expand Down
24 changes: 24 additions & 0 deletions examples/chainrules/train_with_flux.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Train networks with flux. Only guaranteed to work with logdet=false for now.
# So you can train them as invertible networks like this, not as normalizing flows.
using InvertibleNetworks, Flux

# Glow Network
model = NetworkGlow(2, 32, 2, 5; logdet=false)

# dummy input & target
X = randn(Float32, 16, 16, 2, 2)
Y = 2 .* X .+ 1

# loss fn
loss(model, X, Y) = Flux.mse(Y, model(X))

θ = Flux.params(model)
opt = ADAM(0.0001f0)

for i = 1:500
l, grads = Flux.withgradient(θ) do
loss(model, X, Y)
end
@show l
Flux.update!(opt, θ, grads)
end
25 changes: 14 additions & 11 deletions src/networks/invertible_network_glow.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ export NetworkGlow, NetworkGlow3D

- `squeeze_type` : squeeze type that happens at each multiscale level

- `logdet` : boolean to turn on/off logdet term tracking and gradient calculation

*Output*:

- `G`: invertible Glow network.
Expand Down Expand Up @@ -67,12 +69,13 @@ struct NetworkGlow <: InvertibleNetwork
K::Int64
squeezer::Squeezer
split_scales::Bool
logdet::Bool
end

@Flux.functor NetworkGlow

# Constructor
function NetworkGlow(n_in, n_hidden, L, K; nx=nothing, dense=false, freeze_conv=false, split_scales=false, k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, ndims=2, squeezer::Squeezer=ShuffleLayer(), activation::ActivationFunction=SigmoidLayer())
function NetworkGlow(n_in, n_hidden, L, K; logdet=true,nx=nothing, dense=false, freeze_conv=false, split_scales=false, k1=3, k2=1, p1=1, p2=0, s1=1, s2=1, ndims=2, squeezer::Squeezer=ShuffleLayer(), activation::ActivationFunction=SigmoidLayer())
(n_in == 1) && (split_scales = true) # Need extra channels for coupling layer
(dense && isnothing(nx)) && error("Dense network needs nx as kwarg input")

Expand All @@ -91,29 +94,28 @@ function NetworkGlow(n_in, n_hidden, L, K; nx=nothing, dense=false, freeze_conv=
n_in *= channel_factor # squeeze if split_scales is turned on
(dense && split_scales) && (nx = Int64(nx/2))
for j=1:K
AN[i, j] = ActNorm(n_in; logdet=true)
CL[i, j] = CouplingLayerGlow(n_in, n_hidden; nx=nx, dense=dense, freeze_conv=freeze_conv, k1=k1, k2=k2, p1=p1, p2=p2, s1=s1, s2=s2, logdet=true, activation=activation, ndims=ndims)
AN[i, j] = ActNorm(n_in; logdet=logdet)
CL[i, j] = CouplingLayerGlow(n_in, n_hidden; nx=nx, dense=dense, freeze_conv=freeze_conv, k1=k1, k2=k2, p1=p1, p2=p2, s1=s1, s2=s2, logdet=logdet, activation=activation, ndims=ndims)
end
(i < L && split_scales) && (n_in = Int64(n_in/2); ) # split
end

return NetworkGlow(AN, CL, Z_dims, L, K, squeezer, split_scales)
return NetworkGlow(AN, CL, Z_dims, L, K, squeezer, split_scales,logdet)
end

NetworkGlow3D(args; kw...) = NetworkGlow(args...; kw..., ndims=3)

# Forward pass and compute logdet
function forward(X::AbstractArray{T, N}, G::NetworkGlow) where {T, N}
function forward(X::AbstractArray{T, N}, G::NetworkGlow;) where {T, N}
G.split_scales && (Z_save = array_of_array(X, max(G.L-1,1)))


logdet = 0
logdet_ = 0
for i=1:G.L
(G.split_scales) && (X = G.squeezer.forward(X))
for j=1:G.K
X, logdet1 = G.AN[i, j].forward(X)
X, logdet2 = G.CL[i, j].forward(X)
logdet += (logdet1 + logdet2)
G.logdet ? (X, logdet1) = G.AN[i, j].forward(X) : X = G.AN[i, j].forward(X)
G.logdet ? (X, logdet2) = G.CL[i, j].forward(X) : X = G.CL[i, j].forward(X)
G.logdet && (logdet_ += (logdet1 + logdet2))
end
if G.split_scales && (i < G.L || i == 1) # don't split after last iteration
X, Z = tensor_split(X)
Expand All @@ -122,7 +124,8 @@ function forward(X::AbstractArray{T, N}, G::NetworkGlow) where {T, N}
end
end
G.split_scales && (X = cat_states(Z_save, X))
return X, logdet

G.logdet ? (return X, logdet_) : (return X)
end

# Inverse pass
Expand Down
21 changes: 18 additions & 3 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,15 +67,30 @@ if test_suite == "all" || test_suite == "layers"
end
end

# Networks
max_attempts=3
if test_suite == "all" || test_suite == "networks"
@testset verbose = true "Networks" begin
for t=networks
@testset "Test $t" begin
@timeit TIMEROUTPUT "$t" begin include(t) end
for attempt in 1:max_attempts
println("Running tests, attempt $attempt...")
try
results = @testset "Test $t" begin
@timeit TIMEROUTPUT "$t" begin include(t) end
end

if all(record->record.status == :pass, results.results)
println("Tests passed on attempt $attempt.")
return
end
catch e
println("Tests failed on attempt $attempt. Retrying...")
end
end
println("Tests failed after $max_attempts attempts.")
end
end
end



show(TIMEROUTPUT; compact=true, sortby=:firstexec)
Loading
Loading