Class: DNN::Layers::UnPool2D

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

Instance Attribute Summary collapse

Attributes inherited from Layer

#input_shape

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].



292
293
294
295
# File 'lib/dnn/core/cnn_layers.rb', line 292

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].



289
290
291
# File 'lib/dnn/core/cnn_layers.rb', line 289

def unpool_size
  @unpool_size
end

Class Method Details

.load_hash(hash) ⇒ Object



297
298
299
# File 'lib/dnn/core/cnn_layers.rb', line 297

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

Instance Method Details

#backward(dout) ⇒ Object



325
326
327
328
329
330
331
# File 'lib/dnn/core/cnn_layers.rb', line 325

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

#build(input_shape) ⇒ Object



301
302
303
304
305
306
307
308
309
# File 'lib/dnn/core/cnn_layers.rb', line 301

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



313
314
315
316
317
318
319
320
321
322
323
# File 'lib/dnn/core/cnn_layers.rb', line 313

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



333
334
335
# File 'lib/dnn/core/cnn_layers.rb', line 333

def output_shape
  [*@out_size, @num_channel]
end

#to_hash ⇒ Object



337
338
339
# File 'lib/dnn/core/cnn_layers.rb', line 337

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