Module: DNN::Layers::Conv2DUtils
- Included in:
- Conv2D, Conv2DTranspose, Pool2D, UnPool2D
- Defined in:
- lib/dnn/core/layers/cnn_layers.rb
Overview
This module is used for convolution.
Class Method Summary collapse
- .calc_conv2d_out_size(prev_h, prev_w, fil_h, fil_w, pad_h, pad_w, strides) ⇒ Object
- .calc_conv2d_padding_size(prev_h, prev_w, fil_h, fil_w, strides) ⇒ Object
- .calc_conv2d_transpose_out_size(prev_h, prev_w, fil_h, fil_w, pad_h, pad_w, strides) ⇒ Object
- .calc_conv2d_transpose_padding_size(prev_h, prev_w, fil_h, fil_w, strides) ⇒ Object
-
.col2im(*args) ⇒ Object
col[bsize * out_h * out_w, fil_h * fil_w * ch] to img[bsize, out_h, out_w, ch].
- .col2im_cpu(col, img_shape, out_h, out_w, fil_h, fil_w, strides) ⇒ Object
- .col2im_gpu(col, img_shape, out_h, out_w, fil_h, fil_w, strides) ⇒ Object
-
.im2col(*args) ⇒ Object
img[bsize, out_h, out_w, ch] to col[bsize * out_h * out_w, fil_h * fil_w * ch].
- .im2col_cpu(img, out_h, out_w, fil_h, fil_w, strides) ⇒ Object
- .im2col_gpu(img, out_h, out_w, fil_h, fil_w, strides) ⇒ Object
- .zero_padding(img, pad) ⇒ Object
- .zero_padding_bwd(img, pad) ⇒ Object
Class Method Details
.calc_conv2d_out_size(prev_h, prev_w, fil_h, fil_w, pad_h, pad_w, strides) ⇒ Object
85 86 87 88 89 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 85 def calc_conv2d_out_size(prev_h, prev_w, fil_h, fil_w, pad_h, pad_w, strides) out_h = (prev_h + pad_h - fil_h) / strides[0] + 1 out_w = (prev_w + pad_w - fil_w) / strides[1] + 1 [out_h, out_w] end |
.calc_conv2d_padding_size(prev_h, prev_w, fil_h, fil_w, strides) ⇒ Object
97 98 99 100 101 102 103 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 97 def calc_conv2d_padding_size(prev_h, prev_w, fil_h, fil_w, strides) out_h = prev_h / strides[0] out_w = prev_w / strides[1] pad_h = out_h * strides[0] - prev_h + fil_h - strides[0] pad_w = out_w * strides[1] - prev_w + fil_w - strides[1] [pad_h, pad_w] end |
.calc_conv2d_transpose_out_size(prev_h, prev_w, fil_h, fil_w, pad_h, pad_w, strides) ⇒ Object
91 92 93 94 95 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 91 def calc_conv2d_transpose_out_size(prev_h, prev_w, fil_h, fil_w, pad_h, pad_w, strides) out_h = (prev_h - 1) * strides[0] + fil_h - pad_h out_w = (prev_w - 1) * strides[1] + fil_w - pad_w [out_h, out_w] end |
.calc_conv2d_transpose_padding_size(prev_h, prev_w, fil_h, fil_w, strides) ⇒ Object
105 106 107 108 109 110 111 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 105 def calc_conv2d_transpose_padding_size(prev_h, prev_w, fil_h, fil_w, strides) out_h = prev_h * strides[0] out_w = prev_w * strides[1] pad_h = (prev_h - 1) * strides[0] + fil_h - out_h pad_w = (prev_w - 1) * strides[1] + fil_w - out_w [pad_h, pad_w] end |
.col2im(*args) ⇒ Object
col[bsize * out_h * out_w, fil_h * fil_w * ch] to img[bsize, out_h, out_w, ch]
18 19 20 21 22 23 24 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 18 def col2im(*args) if DNN.use_cumo? col2im_gpu(*args) else col2im_cpu(*args) end end |
.col2im_cpu(col, img_shape, out_h, out_w, fil_h, fil_w, strides) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 46 def col2im_cpu(col, img_shape, out_h, out_w, fil_h, fil_w, strides) bsize, img_h, img_w, ch = img_shape col = col.reshape(bsize, out_h, out_w, fil_h, fil_w, ch) img = col.class.zeros(bsize, img_h, img_w, ch) (0...fil_h).each do |i| i_range = (i...(i + strides[0] * out_h)).step(strides[0]).to_a (0...fil_w).each do |j| j_range = (j...(j + strides[1] * out_w)).step(strides[1]).to_a img[true, i_range, j_range, true] += col[true, true, true, i, j, true] end end img end |
.col2im_gpu(col, img_shape, out_h, out_w, fil_h, fil_w, strides) ⇒ Object
60 61 62 63 64 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 60 def col2im_gpu(col, img_shape, out_h, out_w, fil_h, fil_w, strides) col = Utils.cumo2numo(col) img = col2im_cpu(col, img_shape, out_h, out_w, fil_h, fil_w, strides) Utils.numo2cumo(img) end |
.im2col(*args) ⇒ Object
img[bsize, out_h, out_w, ch] to col[bsize * out_h * out_w, fil_h * fil_w * ch]
9 10 11 12 13 14 15 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 9 def im2col(*args) if DNN.use_cumo? im2col_gpu(*args) else im2col_cpu(*args) end end |
.im2col_cpu(img, out_h, out_w, fil_h, fil_w, strides) ⇒ Object
26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 26 def im2col_cpu(img, out_h, out_w, fil_h, fil_w, strides) bsize = img.shape[0] ch = img.shape[3] col = img.class.zeros(bsize, out_h, out_w, fil_h, fil_w, ch) (0...fil_h).each do |i| i_range = (i...(i + strides[0] * out_h)).step(strides[0]).to_a (0...fil_w).each do |j| j_range = (j...(j + strides[1] * out_w)).step(strides[1]).to_a col[true, true, true, i, j, true] = img[true, i_range, j_range, true] end end col.reshape(bsize * out_h * out_w, fil_h * fil_w * ch) end |
.im2col_gpu(img, out_h, out_w, fil_h, fil_w, strides) ⇒ Object
40 41 42 43 44 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 40 def im2col_gpu(img, out_h, out_w, fil_h, fil_w, strides) img = Utils.cumo2numo(img) col = im2col_cpu(img, out_h, out_w, fil_h, fil_w, strides) Utils.numo2cumo(col) end |
.zero_padding(img, pad) ⇒ Object
66 67 68 69 70 71 72 73 74 75 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 66 def zero_padding(img, pad) bsize, img_h, img_w, ch = img.shape img2 = Xumo::SFloat.zeros(bsize, img_h + pad[0], img_w + pad[1], ch) i_begin = pad[0] / 2 i_end = i_begin + img_h j_begin = pad[1] / 2 j_end = j_begin + img_w img2[true, i_begin...i_end, j_begin...j_end, true] = img img2 end |
.zero_padding_bwd(img, pad) ⇒ Object
77 78 79 80 81 82 83 |
# File 'lib/dnn/core/layers/cnn_layers.rb', line 77 def zero_padding_bwd(img, pad) i_begin = pad[0] / 2 i_end = img.shape[1] - (pad[0] / 2.0).round j_begin = pad[1] / 2 j_end = img.shape[2] - (pad[1] / 2.0).round img[true, i_begin...i_end, j_begin...j_end, true] end |