Class: DNN::Layers::UnPool2D

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

Instance Attribute Summary collapse

Attributes inherited from Layer

#input_shape, #name

Instance Method Summary collapse

Methods inherited from Layer

#built?, #call, call, from_hash

Constructor Details

#initialize(unpool_size) ⇒ UnPool2D



394
395
396
397
# File 'lib/dnn/core/cnn_layers.rb', line 394

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

Instance Attribute Details

#unpool_sizeObject (readonly)

Returns the value of attribute unpool_size.



391
392
393
# File 'lib/dnn/core/cnn_layers.rb', line 391

def unpool_size
  @unpool_size
end

Instance Method Details

#backward(dy) ⇒ Object



424
425
426
427
428
429
# File 'lib/dnn/core/cnn_layers.rb', line 424

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

#build(input_shape) ⇒ Object



399
400
401
402
403
404
405
406
407
408
409
410
# File 'lib/dnn/core/cnn_layers.rb', line 399

def build(input_shape)
  unless input_shape.length == 3
    raise DNN_ShapeError, "Input shape is #{input_shape}. But input shape must be 3 dimensional."
  end
  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



412
413
414
415
416
417
418
419
420
421
422
# File 'lib/dnn/core/cnn_layers.rb', line 412

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

#load_hash(hash) ⇒ Object



439
440
441
# File 'lib/dnn/core/cnn_layers.rb', line 439

def load_hash(hash)
  initialize(hash[:unpool_size])
end

#output_shapeObject



431
432
433
# File 'lib/dnn/core/cnn_layers.rb', line 431

def output_shape
  [*@out_size, @num_channel]
end

#to_hashObject



435
436
437
# File 'lib/dnn/core/cnn_layers.rb', line 435

def to_hash
  super(unpool_size: @unpool_size)
end