Class: DNN::Layers::Conv2D

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

Returns a new instance of Conv2D.

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 | Bool) (defaults to: false) —

    Padding size or whether to padding. Padding size is of the form [height, width].



109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/dnn/core/cnn_layers.rb', line 109

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



88
89
90
# File 'lib/dnn/core/cnn_layers.rb', line 88

def filter_size
  @filter_size
end

#num_filters ⇒ Integer (readonly)

Returns number of filters.

Returns:

  • (Integer) —

    number of filters.



86
87
88
# File 'lib/dnn/core/cnn_layers.rb', line 86

def num_filters
  @num_filters
end

#padding ⇒ Array | Bool (readonly)

Return padding size or whether to padding.

Returns:

  • (Array | Bool) —

    Return padding size or whether to padding.



92
93
94
# File 'lib/dnn/core/cnn_layers.rb', line 92

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



90
91
92
# File 'lib/dnn/core/cnn_layers.rb', line 90

def strides
  @strides
end

Class Method Details

.from_hash(hash) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/dnn/core/cnn_layers.rb', line 94

def self.from_hash(hash)
  Conv2D.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



151
152
153
154
155
156
157
158
159
160
# File 'lib/dnn/core/cnn_layers.rb', line 151

def backward(dy)
  dy = dy.reshape(dy.shape[0..2].reduce(:*), dy.shape[3])
  if @trainable
    @weight.grad += @col.transpose.dot(dy)
    @bias.grad += dy.sum(0) if @bias
  end
  dcol = dy.dot(@weight.data.transpose)
  dx = col2im(dcol, @x_shape, *@out_size, *@filter_size, @strides)
  @padding ? zero_padding_bwd(dx, @pad_size) : dx
end

#build(input_shape) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
# File 'lib/dnn/core/cnn_layers.rb', line 125

def build(input_shape)
  super
  prev_h, prev_w, num_prev_filter = *input_shape
  @weight.data = Xumo::SFloat.new(@filter_size.reduce(:*) * num_prev_filter, @num_filters)
  @bias.data = Xumo::SFloat.new(@num_filters) if @bias
  init_weight_and_bias
  if @padding == true
    out_h, out_w = calc_conv2d_out_size(prev_h, prev_w, *@filter_size, 0, 0, @strides)
    @pad_size = calc_padding_size(prev_h, prev_w, out_h, out_w, @strides)
  elsif @padding.is_a?(Array)
    @pad_size = @padding
  else
    @pad_size = [0, 0]
  end
  @out_size = calc_conv2d_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.



167
168
169
170
# File 'lib/dnn/core/cnn_layers.rb', line 167

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

#filters=(filters) ⇒ Object

Parameters:

  • filters (Numo::SFloat) —

    Convert weight to filters and set.



173
174
175
176
# File 'lib/dnn/core/cnn_layers.rb', line 173

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

#forward(x) ⇒ Object



142
143
144
145
146
147
148
149
# File 'lib/dnn/core/cnn_layers.rb', line 142

def forward(x)
  x = zero_padding(x, @pad_size) if @padding
  @x_shape = x.shape
  @col = im2col(x, *@out_size, *@filter_size, @strides)
  y = @col.dot(@weight.data)
  y += @bias.data if @bias
  y.reshape(x.shape[0], *@out_size, y.shape[3])
end

#output_shape ⇒ Object



162
163
164
# File 'lib/dnn/core/cnn_layers.rb', line 162

def output_shape
  [*@out_size, @num_filters]
end

#to_hash ⇒ Object



178
179
180
181
182
183
# File 'lib/dnn/core/cnn_layers.rb', line 178

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