前提・実現したいこと
im2colというC言語のプログラムをJavascript に移植してみました。しかし、正しく動いていないような出力が得られました。実際は、JSコードでのサンプルデータでの場合、9x9の行列だと思うのですが、よろしくお願いします。
移植元のC言語
https://github.com/pjreddie/darknet/blob/master/src/im2col.c
欲しい出力(JSのサンプルデータの場合)
[
[1,1,1,2,2,2,3,3,3],
[1,1,1,2,2,2,3,3,3],
[1,1,1,2,2,2,3,3,3],
[2,2,2,3,3,3,4,4,4],
[2,2,2,3,3,3,4,4,4],
[2,2,2,3,3,3,4,4,4],
[3,3,3,4,4,4,5,5,5],
[3,3,3,4,4,4,5,5,5],
[3,3,3,4,4,4,5,5,5],
]
発生している問題・出力結果
(81) [Array(5), undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined, undefined]
該当のソースコード JavaScript
<html> <script> // Image data for debug var data_im=[ [ [1,1,1,1,1],[2,2,2,2,2],[3,3,3,3,3],[4,4,4,4,4],[5,5,5,5,5] ] ]; var channels = 1; // Number of channel. 1:mono 3:color var height = 5; // Image height var width = 5; // Image width var ksize = 3; // Filter size (width or height) var stride = 1; // Filter movement width var pad = 0; // Padding of output image var data_col = im2col_cpu(data_im, channels, height, width, ksize, stride, pad); console.log(data_col); /* * float *im, * int height, * int width, * int channels, * int row, * int col, * int channel, * int pad */ function im2col_get_pixel(im, height, width, channels, row, col, channel, pad) { row -= pad; col -= pad; if (row < 0 || col < 0 || row >= height || col >= width) return 0; var out = im[col + width*(row + height*channel)]; return out; } /* * float* data_im * int channels, * int height, * int width * int ksize, * int stride, * int pad, * float* data_col */ function im2col_cpu(data_im, channels, height, width, ksize, stride, pad) { var c,h,w; var height_col = (height + 2*pad - ksize) / stride + 1; var width_col = (width + 2*pad - ksize) / stride + 1; var data_col = []; var channels_col = channels * ksize * ksize; for (c = 0; c < channels_col; ++c) { var w_offset = c % ksize; var h_offset = (c / ksize) % ksize; var c_im = c / ksize / ksize; for (h = 0; h < height_col; ++h) { for (w = 0; w < width_col; ++w) { var im_row = h_offset + h * stride; var im_col = w_offset + w * stride; var col_index = (c * height_col + h) * width_col + w; data_col[col_index] = im2col_get_pixel(data_im, height, width, channels, im_row, im_col, c_im, pad); } } } return data_col; } </script>
試したこと
変数のデータ型の宣言以外は、C言語とおなじだと思います。
本来ならば、投入データに、画像の数値データが入りますが、わかりやすくするために、整数値を用意しました。
補足情報(FW/ツールのバージョンなど)
ここにより詳細な情報を記載してください。
回答3件
あなたの回答
tips
プレビュー