Class: DNN::Layers::UnPool2D

Inherits:
Layer
  • Object
show all
Includes:
Conv2D_Utils
Defined in:
lib/dnn/core/cnn_layers.rb

Instance Attribute Summary collapse

Attributes inherited from Layer

#input_shape, #learning_phase

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Layer

#built?

Constructor Details

#initialize(unpool_size) ⇒ UnPool2D

Returns a new instance of UnPool2D.

Parameters:

  • unpool_size (Array or Integer) —

    Unpooling size. unpooling size is of the form [height, width].



416
417
418
419
# File 'lib/dnn/core/cnn_layers.rb', line 416

def initialize(unpool_size)
  super()
  @unpool_size = unpool_size.is_a?(Integer) ? [unpool_size, unpool_size] : unpool_size
end

Instance Attribute Details

#unpool_size ⇒ Array (readonly)

Return unpooling size. unpooling size is of the form [height, width].

Returns:

  • (Array) —

    Return unpooling size. unpooling size is of the form [height, width].



413
414
415
# File 'lib/dnn/core/cnn_layers.rb', line 413

def unpool_size
  @unpool_size
end

Class Method Details

.from_hash(hash) ⇒ Object



421
422
423
# File 'lib/dnn/core/cnn_layers.rb', line 421

def self.from_hash(hash)
  UnPool2D.new(hash[:unpool_size])
end

Instance Method Details

#backward(dy) ⇒ Object



447
448
449
450
451
452
453
# File 'lib/dnn/core/cnn_layers.rb', line 447

def backward(dy)
  in_size = input_shape[0..1]
  col = im2col(dy, *input_shape[0..1], *@unpool_size, @unpool_size)
  col = col.reshape(dy.shape[0] * in_size.reduce(:*), @unpool_size.reduce(:*), dy.shape[3]).transpose(0, 2, 1)
           .reshape(dy.shape[0] * in_size.reduce(:*) * dy.shape[3], @unpool_size.reduce(:*))
  col.sum(1).reshape(dy.shape[0], *in_size, dy.shape[3])
end

#build(input_shape) ⇒ Object



425
426
427
428
429
430
431
432
433
# File 'lib/dnn/core/cnn_layers.rb', line 425

def build(input_shape)
  super
  prev_h, prev_w = input_shape[0..1]
  unpool_h, unpool_w = @unpool_size
  out_h = prev_h * unpool_h
  out_w = prev_w * unpool_w
  @out_size = [out_h, out_w]
  @num_channel = input_shape[2]
end

#forward(x) ⇒ Object



435
436
437
438
439
440
441
442
443
444
445
# File 'lib/dnn/core/cnn_layers.rb', line 435

def forward(x)
  @x_shape = x.shape
  unpool_h, unpool_w = @unpool_size
  x2 = Xumo::SFloat.zeros(x.shape[0], x.shape[1], unpool_h, x.shape[2], unpool_w, @num_channel)
  unpool_h.times do |i|
    unpool_w.times do |j|
      x2[true, true, i, true, j, true] = x
    end
  end
  x2.reshape(x.shape[0], *@out_size, x.shape[3])
end

#output_shape ⇒ Object



455
456
457
# File 'lib/dnn/core/cnn_layers.rb', line 455

def output_shape
  [*@out_size, @num_channel]
end

#to_hash ⇒ Object



459
460
461
# File 'lib/dnn/core/cnn_layers.rb', line 459

def to_hash
  super({unpool_size: @unpool_size})
end