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

Deprecate older VGG API #270

Merged
merged 6 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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,6 +1,6 @@
name = "Metalhead"
uuid = "dbeba491-748d-5e0e-a39e-b530a07fa0cc"
version = "0.9.1"
version = "0.9.2"

[deps]
Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
Expand Down
92 changes: 51 additions & 41 deletions src/convnets/vgg.jl
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,11 @@ A VGG block of convolution layers
- `batchnorm`: set to `true` to include batch normalization after each convolution
"""
function vgg_block(ifilters::Integer, ofilters::Integer, depth::Integer, batchnorm::Bool)
k = (3, 3)
p = (1, 1)
layers = []
for _ in 1:depth
if batchnorm
append!(layers, conv_norm(k, ifilters, ofilters; pad = p))
else
push!(layers, Conv(k, ifilters => ofilters, relu; pad = p))
end
ifilters = ofilters
norm_layer = batchnorm ? BatchNorm : identity
layers = [conv_norm((3, 3), ifilters, ofilters; pad = (1, 1), norm_layer)...]
for i in 2:depth
append!(layers, conv_norm((3, 3), ofilters, ofilters; pad = (1, 1), norm_layer))
end
ifilters = ofilters
return layers
end

Expand Down Expand Up @@ -77,7 +70,8 @@ function vgg_classifier_layers(imsize::NTuple{3, <:Integer}, nclasses::Integer,
end

"""
vgg(imsize; config, inchannels, batchnorm = false, nclasses, fcsize, dropout_prob)
vgg(imsize::Dims{2}; config, batchnorm::Bool = false, fcsize::Integer = 4096,
dropout_prob = 0.0, inchannels::Integer = 3, nclasses::Integer = 1000)

Create a VGG model
([reference](https://arxiv.org/abs/1409.1556v6)).
Expand Down Expand Up @@ -110,10 +104,37 @@ const VGG_CONV_CONFIGS = Dict(:A => [(64, 1), (128, 1), (256, 2), (512, 2), (512
const VGG_CONFIGS = Dict(11 => :A, 13 => :B, 16 => :D, 19 => :E)

"""
VGG(imsize::Dims{2}; config, inchannels, batchnorm = false, nclasses, fcsize, dropout_prob)
VGG(depth::Integer; pretrain::Bool = false, batchnorm::Bool = false,
inchannels::Integer = 3, nclasses::Integer = 1000)

Create a VGG style model with specified `depth`.
([reference](https://arxiv.org/abs/1409.1556v6)).

!!! warning

`VGG` does not currently support pretrained weights for the `batchnorm = true` option.

# Arguments

- `depth`: the depth of the VGG model. Must be one of [11, 13, 16, 19].
- `pretrain`: set to `true` to load pre-trained model weights for ImageNet
- `batchnorm`: set to `true` to use batch normalization after each convolution
- `inchannels`: number of input channels
- `nclasses`: number of output classes

---

VGG(imsize::Dims{2}; config, batchnorm::Bool = false, dropout_prob = 0.5,
inchannels::Integer = 3, nclasses::Integer = 1000)

Construct a VGG model with the specified input image size. Typically, the image size is `(224, 224)`.

!!! warning

The `VGG(imsize; config, inchannels, batchnorm, nclasses)` function documented below
will be deprecated in a future release. Please use `vgg(imsize; config, inchannels,
batchnorm, nclasses)` instead for the same functionality.

theabhirath marked this conversation as resolved.
Show resolved Hide resolved
## Keyword Arguments:

- `config` : VGG convolutional block configuration. It is defined as a vector of tuples
Expand All @@ -124,40 +145,14 @@ Construct a VGG model with the specified input image size. Typically, the image
- `fcsize`: intermediate fully connected layer size
(see [`Metalhead.vgg_classifier_layers`](@ref))
- `dropout_prob`: dropout level between fully connected layers

See also [`vgg`](@ref).
"""
struct VGG
layers::Any
end
@functor VGG

function VGG(imsize::Dims{2}; config, batchnorm::Bool = false, dropout_prob = 0.5,
inchannels::Integer = 3, nclasses::Integer = 1000)
layers = vgg(imsize; config, inchannels, batchnorm, nclasses, dropout_prob)
return VGG(layers)
end

(m::VGG)(x) = m.layers(x)

backbone(m::VGG) = m.layers[1]
classifier(m::VGG) = m.layers[2]

"""
VGG(depth::Integer; pretrain::Bool = false, batchnorm::Bool = false,
inchannels::Integer = 3, nclasses::Integer = 1000)

Create a VGG style model with specified `depth`.
([reference](https://arxiv.org/abs/1409.1556v6)).

# Arguments

- `depth`: the depth of the VGG model. Must be one of [11, 13, 16, 19].
- `pretrain`: set to `true` to load pre-trained model weights for ImageNet
- `batchnorm`: set to `true` to use batch normalization after each convolution
- `inchannels`: number of input channels
- `nclasses`: number of output classes

See also [`vgg`](@ref).
"""
function VGG(depth::Integer; pretrain::Bool = false, batchnorm::Bool = false,
inchannels::Integer = 3, nclasses::Integer = 1000)
_checkconfig(depth, keys(VGG_CONFIGS))
Expand All @@ -174,3 +169,18 @@ function VGG(depth::Integer; pretrain::Bool = false, batchnorm::Bool = false,
end
return model
end

(m::VGG)(x) = m.layers(x)

backbone(m::VGG) = m.layers[1]
classifier(m::VGG) = m.layers[2]

# Deprecated; to be removed in a future release
function VGG(imsize::Dims{2}; config, batchnorm::Bool = false, dropout_prob = 0.5,
inchannels::Integer = 3, nclasses::Integer = 1000)
Base.depwarn("The `VGG(imsize; config, inchannels, batchnorm, nclasses)` constructor
will be deprecated in a future release. Please use `vgg(imsize; config,
inchannels, batchnorm, nclasses)` instead for the same functionality.", :VGG)
layers = vgg(imsize; config, inchannels, batchnorm, nclasses, dropout_prob)
return VGG(layers)
end
theabhirath marked this conversation as resolved.
Show resolved Hide resolved
Loading