module DNN::Layers::Conv2DUtils
This module is used for convolution.
Public Instance Methods
calc_conv2d_out_size(prev_h, prev_w, fil_h, fil_w, pad_h, pad_w, strides)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
col[bsize * out_h * out_w, fil_h * fil_w * ch] to img[bsize, out_h, out_w, ch]
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
img[bsize, out_h, out_w, ch] to col[bsize * out_h * out_w, fil_h * fil_w * ch]
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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)
click to toggle source
# 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