Class: DNN::Layers::Conv2D_Transpose

Inherits:
Connection show all
Includes:
Conv2D_Utils
Defined in:
lib/dnn/core/cnn_layers.rb

Instance Attribute Summary collapse

Attributes inherited from Connection

#bias_initializer, #bias_regularizer, #weight_initializer, #weight_regularizer

Attributes inherited from HasParamLayer

#params, #trainable

Attributes inherited from Layer

#input_shape, #learning_phase

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#regularizers, #use_bias

Methods inherited from Layer

#built?

Constructor Details

#initialize(num_filters, filter_size, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, weight_regularizer: nil, bias_regularizer: nil, use_bias: true, strides: 1, padding: false) ⇒ Conv2D_Transpose

Returns a new instance of Conv2D_Transpose.

Parameters:

  • num_filters (Integer) —

    Number of filters.

  • filter_size (Array | Integer) —

    Filter size. Filter size is of the form [height, width].

  • strides (Array | Integer) (defaults to: 1) —

    Stride length. Stride length is of the form [height, width].

  • padding (Array) (defaults to: false) —

    Padding size. Padding size is of the form [height, width].



214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
# File 'lib/dnn/core/cnn_layers.rb', line 214

def initialize(num_filters, filter_size,
               weight_initializer: Initializers::RandomNormal.new,
               bias_initializer: Initializers::Zeros.new,
               weight_regularizer: nil,
               bias_regularizer: nil,
               use_bias: true,
               strides: 1,
               padding: false)
  super(weight_initializer: weight_initializer, bias_initializer: bias_initializer,
        weight_regularizer: weight_regularizer, bias_regularizer: bias_regularizer, use_bias: use_bias)
  @num_filters = num_filters
  @filter_size = filter_size.is_a?(Integer) ? [filter_size, filter_size] : filter_size
  @strides = strides.is_a?(Integer) ? [strides, strides] : strides
  @padding = padding.is_a?(Integer) ? [padding, padding] : padding
end

Instance Attribute Details

#filter_size ⇒ Array (readonly)

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

Returns:

  • (Array) —

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



193
194
195
# File 'lib/dnn/core/cnn_layers.rb', line 193

def filter_size
  @filter_size
end

#num_filters ⇒ Integer (readonly)

Returns number of filters.

Returns:

  • (Integer) —

    number of filters.



191
192
193
# File 'lib/dnn/core/cnn_layers.rb', line 191

def num_filters
  @num_filters
end

#padding ⇒ Array (readonly)

Return padding size.

Returns:

  • (Array) —

    Return padding size.



197
198
199
# File 'lib/dnn/core/cnn_layers.rb', line 197

def padding
  @padding
end

#strides ⇒ Array (readonly)

Return stride length. stride length is of the form [height, width].

Returns:

  • (Array) —

    Return stride length. stride length is of the form [height, width].



195
196
197
# File 'lib/dnn/core/cnn_layers.rb', line 195

def strides
  @strides
end

Class Method Details

.from_hash(hash) ⇒ Object



199
200
201
202
203
204
205
206
207
208
# File 'lib/dnn/core/cnn_layers.rb', line 199

def self.from_hash(hash)
  Conv2D_Transpose.new(hash[:num_filters], hash[:filter_size],
             weight_initializer: Utils.from_hash(hash[:weight_initializer]),
             bias_initializer: Utils.from_hash(hash[:bias_initializer]),
             weight_regularizer: Utils.from_hash(hash[:weight_regularizer]),
             bias_regularizer: Utils.from_hash(hash[:bias_regularizer]),
             use_bias: hash[:use_bias],
             strides: hash[:strides],
             padding: hash[:padding])
end

Instance Method Details

#backward(dy) ⇒ Object



263
264
265
266
267
268
269
270
271
272
# File 'lib/dnn/core/cnn_layers.rb', line 263

def backward(dy)
  dy = zero_padding(dy, @pad_size) if @padding
  col = im2col(dy, *input_shape[0..1], *@filter_size, @strides)
  if @trainable
    @weight.grad += col.transpose.dot(@x)
    @bias.grad += col.reshape(col.shape[0] * @filter_size.reduce(:*), @num_filters).sum(0) if @bias
  end
  dx = col.dot(@weight.data)
  dx.reshape(dy.shape[0], *input_shape)
end

#build(input_shape) ⇒ Object



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
# File 'lib/dnn/core/cnn_layers.rb', line 230

def build(input_shape)
  super
  prev_h, prev_w, num_prev_filter = *input_shape
  @weight.data = Xumo::SFloat.new(@filter_size.reduce(:*) * @num_filters, num_prev_filter)
  @weight_initializer.init_param(self, @weight)
  @weight_regularizer.param = @weight if @weight_regularizer
  if @bias
    @bias.data = Xumo::SFloat.new(@num_filters)
    @bias_initializer.init_param(self, @bias)
    @bias_regularizer.param = @bias if @bias_regularizer
  end
  if @padding == true
    out_h, out_w = calc_deconv2d_out_size(prev_h, prev_w, *@filter_size, 0, 0, @strides)
    @pad_size = calc_padding_size(out_h, out_w, prev_h, prev_w, @strides)
  elsif @padding.is_a?(Array)
    @pad_size = @padding
  else
    @pad_size = [0, 0]
  end
  @out_size = calc_deconv2d_out_size(prev_h, prev_w, *@filter_size, *@pad_size, @strides)
end

#filters ⇒ Numo::SFloat

Returns Convert weight to filter and return.

Returns:

  • (Numo::SFloat) —

    Convert weight to filter and return.



279
280
281
282
# File 'lib/dnn/core/cnn_layers.rb', line 279

def filters
  num_prev_filter = @input_shape[2]
  @weight.data.reshape(*@filter_size, @num_filters, num_prev_filter)
end

#filters=(filters) ⇒ Object

Parameters:

  • filters (Numo::SFloat) —

    Convert weight to filters and set.



285
286
287
288
# File 'lib/dnn/core/cnn_layers.rb', line 285

def filters=(filters)
  num_prev_filter = @input_shape[2]
  @weight.data = filters.reshape(@filter_size.reduce(:*) * @num_filters, num_prev_filter)
end

#forward(x) ⇒ Object



252
253
254
255
256
257
258
259
260
261
# File 'lib/dnn/core/cnn_layers.rb', line 252

def forward(x)
  bsize = x.shape[0]
  x = x.reshape(x.shape[0..2].reduce(:*), x.shape[3])
  @x = x
  col = x.dot(@weight.data.transpose)
  img_shape = [bsize, @out_size[0] + @pad_size[0], @out_size[1] + @pad_size[1], @num_filters]
  y = col2im(col, img_shape, *input_shape[0..1], *@filter_size, @strides)
  y += @bias.data if @bias
  @padding ? zero_padding_bwd(y, @pad_size) : y
end

#output_shape ⇒ Object



274
275
276
# File 'lib/dnn/core/cnn_layers.rb', line 274

def output_shape
  [*@out_size, @num_filters]
end

#to_hash ⇒ Object



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

def to_hash
  super({num_filters: @num_filters,
         filter_size: @filter_size,
         strides: @strides,
         padding: @padding})
end