Need help to translate this Pytorch part to Flux

i already made some progress

Pytorch code

class ResidualBlock(nn.Module):
    def __init__(self, in_channels, channels=32, residual_beta=0.2):
        super().__init__()
        self.residual_beta = residual_beta
        self.blocks = nn.ModuleList()

        for i in range(5):
            self.blocks.append(
                ConvBlock(
                    in_channels + channels * i,
                    channels if i <= 3 else in_channels,
                    kernel_size=3,
                    stride=1,
                    padding=1,
                    use_act=True if i <= 3 else False,
                )
            )

    def forward(self, x):
        new_inputs = x
        for block in self.blocks:
            out = block(new_inputs)
            new_inputs = torch.cat([new_inputs, out], dim=1)
        return self.residual_beta * out + x

Julia code

struct ResidualBlock
    residual_beta 
    blocks
end

@functor ResidualBlock
function ResidualBlock(
    in_channels::Int,
    channels::Int=32,
    residual_beta::Float32 = 0.2f0,
) 
return ResidualBlock(
   #this part is incorrect
    blocks = []
    for i in range(1, length=5)
        push!(blocks, 
            3,
            in_channels + channels * i,
            i <= 3 ? channels : in_channels,
            i <= 3 ? use_act=true : false,
            1,
            1)
    end
)  
end

function (net::ResidualBlock)(x)
    new_inputs = x
    for block in net.blocks
        out = block(new_inputs)
        new_inputs = cat([new_inputs, out], dims=1)
    end
    return net.residual_beta * out + x
end

It would be good, if you more clearly state the problem, and create the minimal (not)working example, where people can just cut-and-paste it to start to work on the problem. That means, adding all libraries that you use, generate input data, how your struct is called, etc. As the request is stated by now, it is really difficult to help.

1 Like

sure

Can you make it minimal to better see, what is causing a problem? I do not have a GPU, therefore I might not be able to help.

1 Like

You have a type in

Flux.indentity

but that is not the bug you are after

i found example at flux-zoo how to build correctly models, i’am reimplementing it.
thank you anyway

Would this be of any help?

using Flux
using Zygote
using Functors


struct Prelu{T<:AbstractVector}
    slope::T
end

Prelu(dim::Int=1; init::Real=0.25f0) = Prelu(fill(float(init), dim))

@functor Prelu

function (p::Prelu)(x::AbstractArray)
	triv = x isa AbstractVector ? () : ntuple(_ -> 1, ndims(x) - 2)
	a = reshape(p.slope, triv..., :)  # channel dim is 2nd-last, unless x is a vector
	leakyrelu.(x, a)
end

struct ConvBlock
    cnn
    act
end

@functor ConvBlock


function ConvBlock(
    kernel_size::Int,
    in_channels::Int,
    out_channels::Int,
    act::Bool,
    s::Int,
    p::Int,
) return ConvBlock(
    Conv((kernel_size, kernel_size), in_channels => out_channels; stride=s, pad=p,bias=true),
    if act 
        x -> leakyrelu.(x, 0.2)
    else 
        Flux.identity
    end
)  
end

function (net::ConvBlock)(x)
    return net.act(net.cnn(x))
end


in_channels = 4
channels = 1
kernel_size = 3

net = Chain([ ConvBlock(
		kernel_size,
        in_channels,
        in_channels,
        i <= 3,
        1,
        1,
    ) for i in 1:5]...)

net(randn(24,24,4,5))
1 Like