Module: DNN::Layers::Convert

Included in:
Conv2D, MaxPool2D
Defined in:
lib/dnn/core/layers.rb

Instance Method Summary collapse

Instance Method Details

#col2im(col, img_shape, out_h, out_w, fh, fw, strides) ⇒ Object



136
137
138
139
140
141
142
143
144
145
146
147
148
# File 'lib/dnn/core/layers.rb', line 136

def col2im(col, img_shape, out_h, out_w, fh, fw, strides)
  bs, fn, ih, iw = img_shape
  col = col.reshape(bs, out_h, out_w, fn, fh, fw).transpose(0, 3, 4, 5, 1, 2)
  img = SFloat.zeros(bs, fn, ih, iw)
  (0...fh).each do |i|
    i_range = (i...(i + strides[0] * out_h)).step(strides[0]).to_a
    (0...fw).each do |j|
      j_range = (j...(j + strides[1] * out_w)).step(strides[1]).to_a
      img[true, true, i_range, j_range] += col[true, true, i, j, true, true]
    end
  end
  img
end

#im2col(img, out_h, out_w, fh, fw, strides) ⇒ Object



123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/dnn/core/layers.rb', line 123

def im2col(img, out_h, out_w, fh, fw, strides)
  bs, fn = img.shape[0..1]
  col = SFloat.zeros(bs, fn, fh, fw, out_h, out_w)
  (0...fh).each do |i|
    i_range = (i...(i + strides[0] * out_h)).step(strides[0]).to_a
    (0...fw).each do |j|
      j_range = (j...(j + strides[1] * out_w)).step(strides[1]).to_a
      col[true, true, i, j, true, true] = img[true, true, i_range, j_range]
    end
  end
  col.transpose(0, 4, 5, 1, 2, 3).reshape(bs * out_h * out_w, fn * fh * fw)
end

#padding(img, pad) ⇒ Object



150
151
152
153
154
155
156
157
# File 'lib/dnn/core/layers.rb', line 150

def padding(img, pad)
  bs, c, ih, iw = img.shape
  ih2 = ih + pad * 2
  iw2 = iw + pad * 2
  img2 = SFloat.zeros(bs, c, ih2, iw2)
  img2[true, true, pad...(ih + pad), pad...(iw + pad)] = img
  img2
end