Class: DNN::Layers::Conv2D

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

Instance Attribute Summary collapse

Attributes inherited from Connection

#bias_initializer, #l1_lambda, #l2_lambda, #weight_initializer

Attributes inherited from HasParamLayer

#params, #trainable

Attributes inherited from Layer

#input_shape

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Connection

#regularizers, #use_bias

Methods inherited from HasParamLayer

#update

Methods inherited from Layer

#built?

Constructor Details

#initialize(num_filters, filter_size, weight_initializer: Initializers::RandomNormal.new, bias_initializer: Initializers::Zeros.new, strides: 1, padding: false, l1_lambda: 0, l2_lambda: 0, use_bias: true) ⇒ Conv2D

Returns a new instance of Conv2D.

Parameters:

  • num_filters (Integer) —

    number of filters.

  • filter_size (Array or Integer) —

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

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

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

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

    Whether to padding.



95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/dnn/core/cnn_layers.rb', line 95

def initialize(num_filters, filter_size,
               weight_initializer: Initializers::RandomNormal.new,
               bias_initializer: Initializers::Zeros.new,
               strides: 1,
               padding: false,
               l1_lambda: 0,
               l2_lambda: 0,
               use_bias: true)
  super(weight_initializer: weight_initializer, bias_initializer: bias_initializer,
        l1_lambda: l1_lambda, l2_lambda: l2_lambda, 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
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].



76
77
78
# File 'lib/dnn/core/cnn_layers.rb', line 76

def filter_size
  @filter_size
end

#num_filters ⇒ Integer (readonly)

Returns number of filters.

Returns:

  • (Integer) —

    number of filters.



74
75
76
# File 'lib/dnn/core/cnn_layers.rb', line 74

def num_filters
  @num_filters
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].



78
79
80
# File 'lib/dnn/core/cnn_layers.rb', line 78

def strides
  @strides
end

Class Method Details

.load_hash(hash) ⇒ Object



80
81
82
83
84
85
86
87
88
89
# File 'lib/dnn/core/cnn_layers.rb', line 80

def self.load_hash(hash)
  Conv2D.new(hash[:num_filters], hash[:filter_size],
             weight_initializer: Utils.load_hash(hash[:weight_initializer]),
             bias_initializer: Utils.load_hash(hash[:bias_initializer]),
             strides: hash[:strides],
             padding: hash[:padding],
             l1_lambda: hash[:l1_lambda],
             l2_lambda: hash[:l2_lambda],
             use_bias: hash[:use_bias])
end

Instance Method Details

#backward(dout) ⇒ Object



130
131
132
133
134
135
136
137
# File 'lib/dnn/core/cnn_layers.rb', line 130

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

#build(input_shape) ⇒ Object



111
112
113
114
115
116
117
118
119
# File 'lib/dnn/core/cnn_layers.rb', line 111

def build(input_shape)
  super
  prev_h, prev_w = input_shape[0..1]
  @out_size = out_size(prev_h, prev_w, *@filter_size, @strides)
  if @padding
    @pad_size = padding_size(prev_h, prev_w, *@out_size, @strides)
    @out_size = [@out_size[0] + @pad_size[0], @out_size[1] + @pad_size[1]]
  end
end

#filters ⇒ Numo::SFloat

Returns Convert weight to filter and return.

Returns:

  • (Numo::SFloat) —

    Convert weight to filter and return.



149
150
151
152
# File 'lib/dnn/core/cnn_layers.rb', line 149

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.



155
156
157
158
# File 'lib/dnn/core/cnn_layers.rb', line 155

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



121
122
123
124
125
126
127
128
# File 'lib/dnn/core/cnn_layers.rb', line 121

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

#output_shape ⇒ Object



139
140
141
# File 'lib/dnn/core/cnn_layers.rb', line 139

def output_shape
  [*@out_size, @num_filters]
end

#padding? ⇒ Bool

Returns whether to padding.

Returns:

  • (Bool) —

    whether to padding.



144
145
146
# File 'lib/dnn/core/cnn_layers.rb', line 144

def padding?
  @padding
end

#to_hash ⇒ Object



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

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