Skip to content

Commit da44023

Browse files
committed
Add DropBlock
1 parent 07ad654 commit da44023

File tree

6 files changed

+84
-30
lines changed

6 files changed

+84
-30
lines changed

Project.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,14 @@ version = "0.7.3-DEV"
55
[deps]
66
Artifacts = "56f22d72-fd6d-98f1-02f0-08ddc0907c33"
77
BSON = "fbb218c0-5317-5bc6-957e-2ee96dd4b1f0"
8+
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
89
Flux = "587475ba-b771-5e3f-ad9e-33799f191a9c"
910
Functors = "d9f16b24-f501-4c13-a1f2-28368ffc5196"
1011
LazyArtifacts = "4af54fe1-eca0-43a8-85a7-787d91b784e3"
1112
MLUtils = "f1d291b0-491e-4a28-83b9-f70985020b54"
1213
NNlib = "872c559c-99b0-510c-b3b7-b6c96a88d5cd"
1314
Random = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
15+
NNlibCUDA = "a00861dc-f156-4864-bf3c-e6376f28a68d"
1416
Statistics = "10745b16-79ce-11e8-11f9-7d13ad32a3b2"
1517

1618
[compat]

src/convnets/densenet.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ Create a DenseNet model
100100
- `reduction`: the factor by which the number of feature maps is scaled across each transition
101101
- `nclasses`: the number of output classes
102102
"""
103-
function densenet(nblocks; growth_rate = 32, reduction = 0.5, nclasses = 1000)
103+
function densenet(nblocks::NTuple{N, <:Integer}; growth_rate = 32, reduction = 0.5, nclasses = 1000)
104104
return densenet(2 * growth_rate, [fill(growth_rate, n) for n in nblocks];
105105
reduction = reduction, nclasses = nclasses)
106106
end

src/layers/Layers.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,21 @@
11
module Layers
22

33
using Flux
4-
using Flux: outputsize, Zygote
4+
using NNlib
5+
using NNlibCUDA
56
using Functors
7+
using ChainRulesCore
68
using Statistics
79
using MLUtils
810

911
include("../utilities.jl")
1012

1113
include("attention.jl")
1214
include("embeddings.jl")
13-
include("mlp.jl")
15+
include("mlp-linear.jl")
1416
include("normalise.jl")
1517
include("conv.jl")
16-
include("others.jl")
18+
include("drop.jl")
1719

1820
export MHAttention,
1921
PatchEmbedding, ViPosEmbedding, ClassTokens,

src/layers/drop.jl

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
"""
2+
DropBlock(drop_prob = 0.1, block_size = 7)
3+
4+
Implements DropBlock, a regularization method for convolutional networks.
5+
([reference](https://arxiv.org/pdf/1810.12890.pdf))
6+
"""
7+
struct DropBlock{F}
8+
drop_prob::F
9+
block_size::Integer
10+
end
11+
@functor DropBlock
12+
13+
(m::DropBlock)(x) = dropblock(x, m.drop_prob, m.block_size)
14+
15+
DropBlock(drop_prob = 0.1, block_size = 7) = DropBlock(drop_prob, block_size)
16+
17+
function _dropblock_checks(x, drop_prob, T)
18+
if !(T <: AbstractArray)
19+
throw(ArgumentError("x must be an `AbstractArray`"))
20+
end
21+
if ndims(x) != 4
22+
throw(ArgumentError("x must have 4 dimensions (H, W, C, N) for `DropBlock`"))
23+
end
24+
@assert drop_prob < 0 || drop_prob > 1 "drop_prob must be between 0 and 1, got $drop_prob"
25+
end
26+
ChainRulesCore.@non_differentiable _dropblock_checks(x, drop_prob, T)
27+
28+
function dropblock(x::T, drop_prob, block_size::Integer) where {T}
29+
_dropblock_checks(x, drop_prob, T)
30+
if drop_prob == 0
31+
return x
32+
end
33+
return _dropblock(x, drop_prob, block_size)
34+
end
35+
36+
function _dropblock(x::AbstractArray{T, 4}, drop_prob, block_size) where {T}
37+
gamma = drop_prob / (block_size ^ 2)
38+
mask = rand_like(x, Float32, (size(x, 1), size(x, 2), size(x, 3)))
39+
mask .<= gamma
40+
block_mask = maxpool(reshape(mask, (size(mask)[1:3]..., 1)), (block_size, block_size);
41+
pad = block_size ÷ 2, stride = (1, 1))
42+
if block_size % 2 == 0
43+
block_mask = block_mask[1:(end - 1), 1:(end - 1), :, :]
44+
end
45+
block_mask = 1 .- dropdims(block_mask; dims = 4)
46+
out = (x .* reshape(block_mask, (size(block_mask)[1:3]..., 1))) * length(block_mask) /
47+
sum(block_mask)
48+
return out
49+
end
50+
51+
"""
52+
DropPath(p)
53+
54+
Implements Stochastic Depth - equivalent to `Dropout(p; dims = 4)` when `p` ≥ 0.
55+
([reference](https://arxiv.org/abs/1603.09382))
56+
57+
# Arguments
58+
59+
- `p`: rate of Stochastic Depth.
60+
"""
61+
DropPath(p) = p 0 ? Dropout(p; dims = 4) : identity

src/layers/mlp.jl renamed to src/layers/mlp-linear.jl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,18 @@
1+
"""
2+
LayerScale(λ, planes::Integer)
3+
4+
Creates a `Flux.Scale` layer that performs "`LayerScale`"
5+
([reference](https://arxiv.org/abs/2103.17239)).
6+
7+
# Arguments
8+
9+
- `planes`: Size of channel dimension in the input.
10+
- `λ`: initialisation value for the learnable diagonal matrix.
11+
"""
12+
function LayerScale(planes::Integer, λ)
13+
return λ > 0 ? Flux.Scale(fill(Float32(λ), planes), false) : identity
14+
end
15+
116
"""
217
mlp_block(inplanes::Integer, hidden_planes::Integer, outplanes::Integer = inplanes;
318
dropout = 0., activation = gelu)

src/layers/others.jl

Lines changed: 0 additions & 26 deletions
This file was deleted.

0 commit comments

Comments
 (0)