Class: Chainer::Functions::Pooling::MaxPooling2D

Inherits:
Pooling2D show all
Defined in:
lib/chainer/functions/pooling/max_pooling_2d.rb

Instance Attribute Summary

Attributes inherited from Chainer::Function

#inputs, #output_data, #outputs, #rank, #retain_after_backward

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Pooling2D

#initialize

Methods inherited from Chainer::Function

#backward, #call, #forward, #initialize, #retain_inputs, #retain_outputs

Constructor Details

This class inherits a constructor from Chainer::Functions::Pooling::Pooling2D

Class Method Details

.max_pooling_2d(x, ksize, stride: nil, pad: 0, cover_all: true) ⇒ Chainer::Variable

Spatial max pooling function

Parameters:

  • x (Chainer::Variable)

    Input variable

  • Size (integer || 2D integer array)

    of pooling window

  • Stride (integer || 2D integer array)

    of pooling applications

  • Spatial (integer || 2D integer array)

    padding width for the input array

  • If (boolean)

    ‘true`, all spatial locations are pooled int some output pixels

Returns:



13
14
15
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 13

def self.max_pooling_2d(x, ksize, stride: nil, pad: 0, cover_all: true)
  self.new(ksize, stride: stride, pad: pad, cover_all: cover_all).(x)
end

Instance Method Details

#backward_cpu(x, gy) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 36

def backward_cpu(x, gy)
  n, c, out_h, out_w = gy[0].shape
  h, w  = @in_shape[2..-1]
  kh, kw = @kh, @kw

  gcol = @in_dtype.zeros(n * c * out_h * out_w * kh * kw)

  indexes = @indexes.flatten
  indexes += Numo::Int64.new((indexes.size * kh * kw) / (kh * kw)).seq(0, kh * kw)
 
  gcol[indexes] = gy[0].flatten.dup
  gcol = gcol.reshape(n, c, out_h, out_w, kh, kw)
  gcol = gcol.swapaxes(2, 4)
  gcol = gcol.swapaxes(3, 5)

  gx = Chainer::Utils::Conv.col2im_cpu(gcol, @sy, @sx, @ph, @pw, h, w)
  [gx]
end

#forward_cpu(x) ⇒ Object



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/chainer/functions/pooling/max_pooling_2d.rb', line 17

def forward_cpu(x)
  retain_inputs([])
  @in_shape = x[0].shape
  @in_dtype = x[0].class

  col = Chainer::Utils::Conv.im2col_cpu(x[0], @kh, @kw, @sy, @sx, @ph, @pw, pval: -Float::INFINITY, cover_all: @cover_all)
  n, c, kh, kw, out_h, out_w = col.shape
  col = col.reshape(n , c, kh * kw, out_h, out_w)

  # TODO: numpy.argmax(axis=2)
  d = col.shape[3..-1].reduce(:*) || 1
  dx = col.shape[2..-1].reduce(:*) || 1
  max_index = col.max_index(2)
  @indexes = max_index.flatten.map_with_index { |val, idx| (val - (dx * (idx / d))) / d }.reshape(*max_index.shape)

  y = col.max(axis: 2)
  [y]
end