1
- """
2
- DropBlock(drop_block_prob = 0.1, block_size = 7, gamma_scale = 1.0)
1
+ function dropblock (rng:: AbstractRNG , x:: AbstractArray{T, 4} , drop_block_prob, block_size,
2
+ gamma_scale, active:: Bool = true ) where {T}
3
+ active || return x
4
+ H, W, _, _ = size (x)
5
+ total_size = H * W
6
+ clipped_block_size = min (block_size, min (H, W))
7
+ gamma = gamma_scale * drop_block_prob * total_size / clipped_block_size^ 2 /
8
+ ((W - block_size + 1 ) * (H - block_size + 1 ))
9
+ block_mask = rand_like (rng, x) .< gamma
10
+ block_mask = maxpool (block_mask, (clipped_block_size, clipped_block_size);
11
+ stride = 1 , pad = clipped_block_size ÷ 2 )
12
+ block_mask = 1 .- block_mask
13
+ normalize_scale = convert (T, (length (block_mask) / sum (block_mask) .+ 1e-6 ))
14
+ return x .* block_mask .* normalize_scale
15
+ end
16
+ dropout_mask (rng:: CUDA.RNG , x:: CuArray , p; kwargs... ) = _dropout_mask (rng, x, p; kwargs... )
17
+ function dropblock (rng, x:: CuArray , p; kwargs... )
18
+ throw (ArgumentError (" x isa CuArray, but rng isa $(typeof (rng)) . dropblock only support CUDA.RNG for CuArrays." ))
19
+ end
3
20
4
- Implements DropBlock, a regularization method for convolutional networks.
5
- ([reference](https://arxiv.org/pdf/1810.12890.pdf))
6
- """
7
- struct DropBlock{F}
21
+ struct DropBlock{F, R <: AbstractRNG }
8
22
drop_block_prob:: F
9
23
block_size:: Integer
10
24
gamma_scale:: F
25
+ active:: Union{Bool, Nothing}
26
+ rng:: R
11
27
end
12
- @functor DropBlock
13
-
14
- (m:: DropBlock )(x) = dropblock (x, m. drop_block_prob, m. block_size, m. gamma_scale)
15
28
16
- function DropBlock (drop_block_prob = 0.1 , block_size = 7 , gamma_scale = 1.0 )
17
- if drop_block_prob == 0.0
18
- return identity
19
- end
20
- @assert drop_block_prob < 0 || drop_block_prob > 1
21
- " drop_block_prob must be between 0 and 1, got $drop_block_prob "
22
- @assert gamma_scale < 0 || gamma_scale > 1
23
- " gamma_scale must be between 0 and 1, got $gamma_scale "
24
- return DropBlock (drop_block_prob, block_size, gamma_scale)
25
- end
29
+ @functor DropBlock
30
+ trainable (a:: DropBlock ) = (;)
26
31
27
32
function _dropblock_checks (x:: T ) where {T}
28
33
if ! (T <: AbstractArray )
@@ -34,20 +39,26 @@ function _dropblock_checks(x::T) where {T}
34
39
end
35
40
ChainRulesCore. @non_differentiable _dropblock_checks (x)
36
41
37
- function dropblock (x:: AbstractArray{T, 4} , drop_block_prob, block_size,
38
- gamma_scale) where {T}
42
+ function (m:: DropBlock )(x)
39
43
_dropblock_checks (x)
40
- H, W, _, _ = size (x)
41
- total_size = H * W
42
- clipped_block_size = min (block_size, min (H, W))
43
- gamma = gamma_scale * drop_block_prob * total_size / clipped_block_size^ 2 /
44
- ((W - block_size + 1 ) * (H - block_size + 1 ))
45
- block_mask = rand_like (x) .< gamma
46
- block_mask = maxpool (block_mask, (clipped_block_size, clipped_block_size);
47
- stride = 1 , pad = clipped_block_size ÷ 2 )
48
- block_mask = 1 .- block_mask
49
- normalize_scale = convert (T, (length (block_mask) / sum (block_mask) .+ 1e-6 ))
50
- return x .* block_mask .* normalize_scale
44
+ Flux. _isactive (m) || return x
45
+ return dropblock (m. rng, x, m. drop_block_prob, m. block_size, m. gamma_scale)
46
+ end
47
+
48
+ function Flux. testmode! (m:: DropBlock , mode = true )
49
+ return (m. active = (isnothing (mode) || mode == :auto ) ? nothing : ! mode; m)
50
+ end
51
+
52
+ function DropBlock (drop_block_prob = 0.1 , block_size = 7 , gamma_scale = 1.0 ,
53
+ rng = Flux. rng_from_array ())
54
+ if drop_block_prob == 0.0
55
+ return identity
56
+ end
57
+ @assert 0 ≤ drop_block_prob ≤ 1
58
+ " drop_block_prob must be between 0 and 1, got $drop_block_prob "
59
+ @assert 0 ≤ gamma_scale ≤ 1
60
+ " gamma_scale must be between 0 and 1, got $gamma_scale "
61
+ return DropBlock (drop_block_prob, block_size, gamma_scale, nothing , rng)
51
62
end
52
63
53
64
"""
0 commit comments