Skip to content

Instantly share code, notes, and snippets.

@antimon2
Last active December 10, 2020 23:59
Show Gist options
  • Save antimon2/333e4760e31f7b87b7af6cb91bbd7d50 to your computer and use it in GitHub Desktop.
Save antimon2/333e4760e31f7b87b7af6cb91bbd7d50 to your computer and use it in GitHub Desktop.
im2col_sample.v16x.jl.ipynb
Display the source blob
Display the rendered blob
Raw
{
"cells": [
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:51:19.611Z",
"end_time": "2020-12-11T08:51:19.189000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "versioninfo()",
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"text": "Julia Version 1.6.0-DEV.1699\nCommit 8a5ac94e96* (2020-12-08 03:24 UTC)\nPlatform Info:\n OS: Linux (x86_64-pc-linux-gnu)\n CPU: Intel(R) Core(TM) i7-9750H CPU @ 2.60GHz\n WORD_SIZE: 64\n LIBM: libopenlibm\n LLVM: libLLVM-11.0.0 (ORCJIT, skylake)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:51:52.120Z",
"end_time": "2020-12-11T08:51:50.907000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function zeropad0(a::AbstractArray{T,N}, pad_width::NTuple{N,Tuple{Int,Int}}) where {T,N}\n # sizes = [b+p1+p2 for (b,(p1,p2))=zip(size(a),pad_width)]\n # r = zeros(T, sizes...)\n r = zeros(T, (b+p1+p2 for (b,(p1,p2))=zip(size(a),pad_width))...)\n ranges = [p1+1:p1+b for (b,(p1,_))=zip(size(a),pad_width)]\n r[ranges...] .= a\n r\nend\n\n@inline zeropad0(a::AbstractArray{T,N}, pad_width::Vararg{Tuple{Int,Int}}) where {T,N} = zeropad0(a, pad_width)",
"execution_count": 2,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 2,
"data": {
"text/plain": "zeropad0 (generic function with 2 methods)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:51:53.615Z",
"end_time": "2020-12-11T08:51:53.183000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "A = [20*n+2*(2*c+h)+w-26 for w=1:2,h=1:2,c=1:3,n=1:2]",
"execution_count": 3,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 3,
"data": {
"text/plain": "2×2×3×2 Array{Int64, 4}:\n[:, :, 1, 1] =\n 1 3\n 2 4\n\n[:, :, 2, 1] =\n 5 7\n 6 8\n\n[:, :, 3, 1] =\n 9 11\n 10 12\n\n[:, :, 1, 2] =\n 21 23\n 22 24\n\n[:, :, 2, 2] =\n 25 27\n 26 28\n\n[:, :, 3, 2] =\n 29 31\n 30 32"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:51:58.379Z",
"end_time": "2020-12-11T08:51:56.930000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "zeropad0(A, (1,1), (1,1), (0,0), (0,0))",
"execution_count": 4,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 4,
"data": {
"text/plain": "4×4×3×2 Array{Int64, 4}:\n[:, :, 1, 1] =\n 0 0 0 0\n 0 1 3 0\n 0 2 4 0\n 0 0 0 0\n\n[:, :, 2, 1] =\n 0 0 0 0\n 0 5 7 0\n 0 6 8 0\n 0 0 0 0\n\n[:, :, 3, 1] =\n 0 0 0 0\n 0 9 11 0\n 0 10 12 0\n 0 0 0 0\n\n[:, :, 1, 2] =\n 0 0 0 0\n 0 21 23 0\n 0 22 24 0\n 0 0 0 0\n\n[:, :, 2, 2] =\n 0 0 0 0\n 0 25 27 0\n 0 26 28 0\n 0 0 0 0\n\n[:, :, 3, 2] =\n 0 0 0 0\n 0 29 31 0\n 0 30 32 0\n 0 0 0 0"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:52:31.368Z",
"end_time": "2020-12-11T08:52:29.595000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function zeropad(a::AbstractArray{T,N}, pad_width::NTuple{N,Tuple{Int,Int}}) where {T,N}\n # sizes = [b+p1+p2 for (b,(p1,p2))=zip(size(a),pad_width)]\n # r = zeros(T, sizes...)\n r = zeros(T, (b+p1+p2 for (b,(p1,p2))=zip(size(a),pad_width))...)\n idxs = CartesianIndices(((p1+1:p1+b for (b,(p1,_))=zip(size(a),pad_width))...,))\n r[idxs] .= a\n r\nend\n\n@inline zeropad(a::AbstractArray{T,N}, pad_width::Vararg{Tuple{Int,Int}}) where {T,N} = zeropad(a, pad_width)",
"execution_count": 5,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 5,
"data": {
"text/plain": "zeropad (generic function with 2 methods)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:52:40.820Z",
"end_time": "2020-12-11T08:52:39.088000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "zeropad(A, (1,1), (1,1), (0,0), (0,0))",
"execution_count": 6,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 6,
"data": {
"text/plain": "4×4×3×2 Array{Int64, 4}:\n[:, :, 1, 1] =\n 0 0 0 0\n 0 1 3 0\n 0 2 4 0\n 0 0 0 0\n\n[:, :, 2, 1] =\n 0 0 0 0\n 0 5 7 0\n 0 6 8 0\n 0 0 0 0\n\n[:, :, 3, 1] =\n 0 0 0 0\n 0 9 11 0\n 0 10 12 0\n 0 0 0 0\n\n[:, :, 1, 2] =\n 0 0 0 0\n 0 21 23 0\n 0 22 24 0\n 0 0 0 0\n\n[:, :, 2, 2] =\n 0 0 0 0\n 0 25 27 0\n 0 26 28 0\n 0 0 0 0\n\n[:, :, 3, 2] =\n 0 0 0 0\n 0 29 31 0\n 0 30 32 0\n 0 0 0 0"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:52:57.720Z",
"end_time": "2020-12-11T08:52:55.880000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@code_warntype zeropad0(A, (1,1), (1,1), (0,0), (0,0))",
"execution_count": 8,
"outputs": [
{
"output_type": "stream",
"text": "Variables\n #self#\u001b[36m::Core.Const(zeropad0)\u001b[39m\n a\u001b[36m::Array{Int64, 4}\u001b[39m\n pad_width\u001b[36m::NTuple{4, Tuple{Int64, Int64}}\u001b[39m\n\nBody\u001b[36m::Array{Int64, 4}\u001b[39m\n\u001b[90m1 ─\u001b[39m nothing\n\u001b[90m│ \u001b[39m %2 = Main.zeropad0(a, pad_width)\u001b[36m::Array{Int64, 4}\u001b[39m\n\u001b[90m└──\u001b[39m return %2\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:07.308Z",
"end_time": "2020-12-11T08:53:05.590000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@code_warntype zeropad0(A, ((1,1), (1,1), (0,0), (0,0)))",
"execution_count": 9,
"outputs": [
{
"output_type": "stream",
"text": "Variables\n #self#\u001b[36m::Core.Const(zeropad0)\u001b[39m\n a\u001b[36m::Array{Int64, 4}\u001b[39m\n pad_width\u001b[36m::NTuple{4, Tuple{Int64, Int64}}\u001b[39m\n #4\u001b[36m::var\"#4#6\"\u001b[39m\n #3\u001b[36m::var\"#3#5\"\u001b[39m\n ranges\u001b[36m::Vector{UnitRange{Int64}}\u001b[39m\n r\u001b[36m::Array{Int64, 4}\u001b[39m\n\nBody\u001b[36m::Array{Int64, 4}\u001b[39m\n\u001b[90m1 ─\u001b[39m %1 = Core.tuple($(Expr(:static_parameter, 1)))\u001b[36m::Core.Const((Int64,))\u001b[39m\n\u001b[90m│ \u001b[39m (#3 = %new(Main.:(var\"#3#5\")))\n\u001b[90m│ \u001b[39m %3 = #3\u001b[36m::Core.Const(var\"#3#5\"())\u001b[39m\n\u001b[90m│ \u001b[39m %4 = Main.size(a)\u001b[36m::NTuple{4, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %5 = Main.zip(%4, pad_width)\u001b[36m::Base.Iterators.Zip{Tuple{NTuple{4, Int64}, NTuple{4, Tuple{Int64, Int64}}}}\u001b[39m\n\u001b[90m│ \u001b[39m %6 = Base.Generator(%3, %5)\u001b[36m::Base.Generator{Base.Iterators.Zip{Tuple{NTuple{4, Int64}, NTuple{4, Tuple{Int64, Int64}}}}, var\"#3#5\"}\u001b[39m\n\u001b[90m│ \u001b[39m (r = Core._apply_iterate(Base.iterate, Main.zeros, %1, %6))\n\u001b[90m│ \u001b[39m (#4 = %new(Main.:(var\"#4#6\")))\n\u001b[90m│ \u001b[39m %9 = #4\u001b[36m::Core.Const(var\"#4#6\"())\u001b[39m\n\u001b[90m│ \u001b[39m %10 = Main.size(a)\u001b[36m::NTuple{4, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %11 = Main.zip(%10, pad_width)\u001b[36m::Base.Iterators.Zip{Tuple{NTuple{4, Int64}, NTuple{4, Tuple{Int64, Int64}}}}\u001b[39m\n\u001b[90m│ \u001b[39m %12 = Base.Generator(%9, %11)\u001b[36m::Base.Generator{Base.Iterators.Zip{Tuple{NTuple{4, Int64}, NTuple{4, Tuple{Int64, Int64}}}}, var\"#4#6\"}\u001b[39m\n\u001b[90m│ \u001b[39m (ranges = Base.collect(%12))\n\u001b[90m│ \u001b[39m %14 = Core.tuple(r)\u001b[36m::Tuple{Array{Int64, 4}}\u001b[39m\n\u001b[90m│ \u001b[39m %15 = Core._apply_iterate(Base.iterate, Base.dotview, %14, ranges)\u001b[91m\u001b[1m::Any\u001b[22m\u001b[39m\n\u001b[90m│ \u001b[39m %16 = Base.broadcasted(Base.identity, a)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{4}, Nothing, typeof(identity), Tuple{Array{Int64, 4}}}\u001b[39m\n\u001b[90m│ \u001b[39m Base.materialize!(%15, %16)\n\u001b[90m└──\u001b[39m return r\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:10.466Z",
"end_time": "2020-12-11T08:53:08.620000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@code_warntype zeropad(A, (1,1), (1,1), (0,0), (0,0))",
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"text": "Variables\n #self#\u001b[36m::Core.Const(zeropad)\u001b[39m\n a\u001b[36m::Array{Int64, 4}\u001b[39m\n pad_width\u001b[36m::NTuple{4, Tuple{Int64, Int64}}\u001b[39m\n\nBody\u001b[36m::Array{Int64, 4}\u001b[39m\n\u001b[90m1 ─\u001b[39m nothing\n\u001b[90m│ \u001b[39m %2 = Main.zeropad(a, pad_width)\u001b[36m::Array{Int64, 4}\u001b[39m\n\u001b[90m└──\u001b[39m return %2\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:11.726Z",
"end_time": "2020-12-11T08:53:10.081000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@code_warntype zeropad(A, ((1,1), (1,1), (0,0), (0,0)))",
"execution_count": 11,
"outputs": [
{
"output_type": "stream",
"text": "Variables\n #self#\u001b[36m::Core.Const(zeropad)\u001b[39m\n a\u001b[36m::Array{Int64, 4}\u001b[39m\n pad_width\u001b[36m::NTuple{4, Tuple{Int64, Int64}}\u001b[39m\n #10\u001b[36m::var\"#10#12\"\u001b[39m\n #9\u001b[36m::var\"#9#11\"\u001b[39m\n idxs\u001b[36m::CartesianIndices{4, NTuple{4, UnitRange{Int64}}}\u001b[39m\n r\u001b[36m::Array{Int64, 4}\u001b[39m\n\nBody\u001b[36m::Array{Int64, 4}\u001b[39m\n\u001b[90m1 ─\u001b[39m %1 = Core.tuple($(Expr(:static_parameter, 1)))\u001b[36m::Core.Const((Int64,))\u001b[39m\n\u001b[90m│ \u001b[39m (#9 = %new(Main.:(var\"#9#11\")))\n\u001b[90m│ \u001b[39m %3 = #9\u001b[36m::Core.Const(var\"#9#11\"())\u001b[39m\n\u001b[90m│ \u001b[39m %4 = Main.size(a)\u001b[36m::NTuple{4, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %5 = Main.zip(%4, pad_width)\u001b[36m::Base.Iterators.Zip{Tuple{NTuple{4, Int64}, NTuple{4, Tuple{Int64, Int64}}}}\u001b[39m\n\u001b[90m│ \u001b[39m %6 = Base.Generator(%3, %5)\u001b[36m::Base.Generator{Base.Iterators.Zip{Tuple{NTuple{4, Int64}, NTuple{4, Tuple{Int64, Int64}}}}, var\"#9#11\"}\u001b[39m\n\u001b[90m│ \u001b[39m (r = Core._apply_iterate(Base.iterate, Main.zeros, %1, %6))\n\u001b[90m│ \u001b[39m (#10 = %new(Main.:(var\"#10#12\")))\n\u001b[90m│ \u001b[39m %9 = #10\u001b[36m::Core.Const(var\"#10#12\"())\u001b[39m\n\u001b[90m│ \u001b[39m %10 = Main.size(a)\u001b[36m::NTuple{4, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %11 = Main.zip(%10, pad_width)\u001b[36m::Base.Iterators.Zip{Tuple{NTuple{4, Int64}, NTuple{4, Tuple{Int64, Int64}}}}\u001b[39m\n\u001b[90m│ \u001b[39m %12 = Base.Generator(%9, %11)\u001b[36m::Base.Generator{Base.Iterators.Zip{Tuple{NTuple{4, Int64}, NTuple{4, Tuple{Int64, Int64}}}}, var\"#10#12\"}\u001b[39m\n\u001b[90m│ \u001b[39m %13 = Core._apply_iterate(Base.iterate, Core.tuple, %12)\u001b[36m::NTuple{4, UnitRange{Int64}}\u001b[39m\n\u001b[90m│ \u001b[39m (idxs = Main.CartesianIndices(%13))\n\u001b[90m│ \u001b[39m %15 = Base.dotview(r, idxs)\u001b[36m::Core.PartialStruct(SubArray{Int64, 4, Array{Int64, 4}, NTuple{4, UnitRange{Int64}}, false}, Any[Array{Int64, 4}, NTuple{4, UnitRange{Int64}}, Core.Const(0), Core.Const(0)])\u001b[39m\n\u001b[90m│ \u001b[39m %16 = Base.broadcasted(Base.identity, a)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{4}, Nothing, typeof(identity), Tuple{Array{Int64, 4}}}\u001b[39m\n\u001b[90m│ \u001b[39m Base.materialize!(%15, %16)\n\u001b[90m└──\u001b[39m return r\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:14.489Z",
"end_time": "2020-12-11T08:53:24.228000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "]add BenchmarkTools",
"execution_count": 12,
"outputs": [
{
"output_type": "stream",
"text": "\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m registry at `~/.julia/registries/General`\n\u001b[32m\u001b[1m Updating\u001b[22m\u001b[39m git-repo `git@github.com:JuliaRegistries/General.git`\n\u001b[32m\u001b[1m Resolving\u001b[22m\u001b[39m package versions...\n\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Project.toml`\n\u001b[32m\u001b[1mNo Changes\u001b[22m\u001b[39m to `~/.julia/environments/v1.6/Manifest.toml`\n",
"name": "stderr"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:26.913Z",
"end_time": "2020-12-11T08:53:25.066000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "using BenchmarkTools",
"execution_count": 13,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:27.596Z",
"end_time": "2020-12-11T08:53:28.713000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "GC.gc(true)\n@btime zeropad0($A, (1,1), (1,1), (0,0), (0,0));",
"execution_count": 14,
"outputs": [
{
"output_type": "stream",
"text": " 564.112 ns (17 allocations: 1.62 KiB)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:31.631Z",
"end_time": "2020-12-11T08:53:31.987000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "GC.gc(true)\n@btime zeropad($A, (1,1), (1,1), (0,0), (0,0));",
"execution_count": 15,
"outputs": [
{
"output_type": "stream",
"text": " 159.929 ns (2 allocations: 944 bytes)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:55.226Z",
"end_time": "2020-12-11T08:53:53.471000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function im2col0(input_data::AbstractArray{T,4}, filter_w::Int, filter_h::Int; stride::Int=1, pad::Int=0) where {T}\n W, H, C, N = size(input_data)\n out_h = (H + 2pad - filter_h) ÷ stride + 1\n out_w = (W + 2pad - filter_w) ÷ stride + 1\n img = pad==0 ? input_data : zeropad(input_data, (pad, pad), (pad, pad), (0, 0), (0, 0))\n col = zeros(T, (out_w, out_h, filter_w, filter_h, C, N))\n for y = 1:filter_h\n y_max = y + stride*out_h - 1\n for x = 1:filter_w\n x_max = x + stride*out_w - 1\n col[:, :, x, y, :, :] .= img[x:stride:x_max, y:stride:y_max, :, :]\n end\n end\n reshape(permutedims(col, (3, 4, 5, 1, 2, 6)), filter_w*filter_h*C, out_w*out_h*N)\nend",
"execution_count": 16,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 16,
"data": {
"text/plain": "im2col0 (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:53:58.573Z",
"end_time": "2020-12-11T08:53:57.933000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "C = im2col0(A, 3, 3, stride=1, pad=1)",
"execution_count": 17,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 17,
"data": {
"text/plain": "27×8 Matrix{Int64}:\n 0 0 0 1 0 0 0 21\n 0 0 1 2 0 0 21 22\n 0 0 2 0 0 0 22 0\n 0 1 0 3 0 21 0 23\n 1 2 3 4 21 22 23 24\n 2 0 4 0 22 0 24 0\n 0 3 0 0 0 23 0 0\n 3 4 0 0 23 24 0 0\n 4 0 0 0 24 0 0 0\n 0 0 0 5 0 0 0 25\n 0 0 5 6 0 0 25 26\n 0 0 6 0 0 0 26 0\n 0 5 0 7 0 25 0 27\n ⋮ ⋮ \n 0 7 0 0 0 27 0 0\n 7 8 0 0 27 28 0 0\n 8 0 0 0 28 0 0 0\n 0 0 0 9 0 0 0 29\n 0 0 9 10 0 0 29 30\n 0 0 10 0 0 0 30 0\n 0 9 0 11 0 29 0 31\n 9 10 11 12 29 30 31 32\n 10 0 12 0 30 0 32 0\n 0 11 0 0 0 31 0 0\n 11 12 0 0 31 32 0 0\n 12 0 0 0 32 0 0 0"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:54:37.504Z",
"end_time": "2020-12-11T08:54:35.769000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function im2col(input_data::AbstractArray{T,4}, filter_w::Int, filter_h::Int; stride::Int=1, pad::Int=0) where {T}\n W, H, C, N = size(input_data)\n out_h = (H + 2pad - filter_h) ÷ stride + 1\n out_w = (W + 2pad - filter_w) ÷ stride + 1\n img = pad==0 ? input_data : zeropad(input_data, (pad, pad), (pad, pad), (0, 0), (0, 0))\n col = [img[x+(w-1)*stride, y+(h-1)*stride, c, n] for x=1:filter_w, y=1:filter_h, c=1:C, w=1:out_w, h=1:out_h, n=1:N]::Array{T,6}\n reshape(col, filter_w*filter_h*C, out_w*out_h*N)\nend",
"execution_count": 18,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 18,
"data": {
"text/plain": "im2col (generic function with 1 method)"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:54:40.055Z",
"end_time": "2020-12-11T08:54:38.364000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "C = im2col(A, 3, 3, stride=1, pad=1)",
"execution_count": 19,
"outputs": [
{
"output_type": "execute_result",
"execution_count": 19,
"data": {
"text/plain": "27×8 Matrix{Int64}:\n 0 0 0 1 0 0 0 21\n 0 0 1 2 0 0 21 22\n 0 0 2 0 0 0 22 0\n 0 1 0 3 0 21 0 23\n 1 2 3 4 21 22 23 24\n 2 0 4 0 22 0 24 0\n 0 3 0 0 0 23 0 0\n 3 4 0 0 23 24 0 0\n 4 0 0 0 24 0 0 0\n 0 0 0 5 0 0 0 25\n 0 0 5 6 0 0 25 26\n 0 0 6 0 0 0 26 0\n 0 5 0 7 0 25 0 27\n ⋮ ⋮ \n 0 7 0 0 0 27 0 0\n 7 8 0 0 27 28 0 0\n 8 0 0 0 28 0 0 0\n 0 0 0 9 0 0 0 29\n 0 0 9 10 0 0 29 30\n 0 0 10 0 0 0 30 0\n 0 9 0 11 0 29 0 31\n 9 10 11 12 29 30 31 32\n 10 0 12 0 30 0 32 0\n 0 11 0 0 0 31 0 0\n 11 12 0 0 31 32 0 0\n 12 0 0 0 32 0 0 0"
},
"metadata": {}
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:54:58.402Z",
"end_time": "2020-12-11T08:54:59.120000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "GC.gc(true)\n@btime im2col0($A, 3, 3, stride=1, pad=1);",
"execution_count": 20,
"outputs": [
{
"output_type": "stream",
"text": " 2.865 μs (26 allocations: 7.92 KiB)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:55:09.348Z",
"end_time": "2020-12-11T08:55:08.891000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "GC.gc(true)\n@btime im2col($A, 3, 3, stride=1, pad=1);",
"execution_count": 21,
"outputs": [
{
"output_type": "stream",
"text": " 1.082 μs (6 allocations: 3.06 KiB)\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:55:37.518Z",
"end_time": "2020-12-11T08:55:35.673000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@code_warntype im2col0(A, 3, 3)",
"execution_count": 22,
"outputs": [
{
"output_type": "stream",
"text": "Variables\n #self#\u001b[36m::Core.Const(im2col0)\u001b[39m\n input_data\u001b[36m::Array{Int64, 4}\u001b[39m\n filter_w\u001b[36m::Int64\u001b[39m\n filter_h\u001b[36m::Int64\u001b[39m\n\nBody\u001b[36m::Matrix{Int64}\u001b[39m\n\u001b[90m1 ─\u001b[39m %1 = Main.:(var\"#im2col0#15\")(1, 0, #self#, input_data, filter_w, filter_h)\u001b[36m::Matrix{Int64}\u001b[39m\n\u001b[90m└──\u001b[39m return %1\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:56:40.218Z",
"end_time": "2020-12-11T08:56:38.921000+09:00"
},
"scrolled": false,
"trusted": true
},
"cell_type": "code",
"source": "# _im2col0_15 = getfield(Main, Symbol(\"#im2col0#15\"))\n_im2col0_15 = var\"#im2col0#15\"\n@code_warntype _im2col0_15(1, 1, im2col0, A, 3, 3)",
"execution_count": 25,
"outputs": [
{
"output_type": "stream",
"text": "Variables\n #im2col0#15\u001b[36m::Core.Const(var\"#im2col0#15\")\u001b[39m\n stride\u001b[36m::Int64\u001b[39m\n pad\u001b[36m::Int64\u001b[39m\n @_4\u001b[36m::Core.Const(im2col0)\u001b[39m\n input_data\u001b[36m::Array{Int64, 4}\u001b[39m\n filter_w\u001b[36m::Int64\u001b[39m\n filter_h\u001b[36m::Int64\u001b[39m\n @_8\u001b[33m\u001b[1m::Union{Nothing, Tuple{Int64, Int64}}\u001b[22m\u001b[39m\n @_9\u001b[36m::Int64\u001b[39m\n col\u001b[36m::Array{Int64, 6}\u001b[39m\n img\u001b[36m::Array{Int64, 4}\u001b[39m\n out_w\u001b[36m::Int64\u001b[39m\n out_h\u001b[36m::Int64\u001b[39m\n N\u001b[36m::Int64\u001b[39m\n C\u001b[36m::Int64\u001b[39m\n H\u001b[36m::Int64\u001b[39m\n W\u001b[36m::Int64\u001b[39m\n @_18\u001b[33m\u001b[1m::Union{Nothing, Tuple{Int64, Int64}}\u001b[22m\u001b[39m\n y\u001b[36m::Int64\u001b[39m\n y_max\u001b[36m::Int64\u001b[39m\n x\u001b[36m::Int64\u001b[39m\n x_max\u001b[36m::Int64\u001b[39m\n @_23\u001b[36m::Array{Int64, 4}\u001b[39m\n\nBody\u001b[36m::Matrix{Int64}\u001b[39m\n\u001b[90m1 ──\u001b[39m nothing\n\u001b[90m│ \u001b[39m Core.NewvarNode(:(@_8))\n\u001b[90m│ \u001b[39m Core.NewvarNode(:(col))\n\u001b[90m│ \u001b[39m Core.NewvarNode(:(img))\n\u001b[90m│ \u001b[39m %5 = Main.size(input_data)\u001b[36m::NTuple{4, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %6 = Base.indexed_iterate(%5, 1)\u001b[36m::Core.PartialStruct(Tuple{Int64, Int64}, Any[Int64, Core.Const(2)])\u001b[39m\n\u001b[90m│ \u001b[39m (W = Core.getfield(%6, 1))\n\u001b[90m│ \u001b[39m (@_9 = Core.getfield(%6, 2))\n\u001b[90m│ \u001b[39m %9 = Base.indexed_iterate(%5, 2, @_9::Core.Const(2))\u001b[36m::Core.PartialStruct(Tuple{Int64, Int64}, Any[Int64, Core.Const(3)])\u001b[39m\n\u001b[90m│ \u001b[39m (H = Core.getfield(%9, 1))\n\u001b[90m│ \u001b[39m (@_9 = Core.getfield(%9, 2))\n\u001b[90m│ \u001b[39m %12 = Base.indexed_iterate(%5, 3, @_9::Core.Const(3))\u001b[36m::Core.PartialStruct(Tuple{Int64, Int64}, Any[Int64, Core.Const(4)])\u001b[39m\n\u001b[90m│ \u001b[39m (C = Core.getfield(%12, 1))\n\u001b[90m│ \u001b[39m (@_9 = Core.getfield(%12, 2))\n\u001b[90m│ \u001b[39m %15 = Base.indexed_iterate(%5, 4, @_9::Core.Const(4))\u001b[36m::Core.PartialStruct(Tuple{Int64, Int64}, Any[Int64, Core.Const(5)])\u001b[39m\n\u001b[90m│ \u001b[39m (N = Core.getfield(%15, 1))\n\u001b[90m│ \u001b[39m %17 = H\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %18 = (2 * pad)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %19 = (%17 + %18)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m + = (%19 - filter_h)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %21 = (+ ÷ stride)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m (out_h = %21 + 1)\n\u001b[90m│ \u001b[39m %23 = W\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %24 = (2 * pad)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %25 = (%23 + %24)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %26 = (%25 - filter_w)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %27 = (%26 ÷ stride)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m (out_w = %27 + 1)\n\u001b[90m│ \u001b[39m %29 = (pad == 0)\u001b[36m::Bool\u001b[39m\n\u001b[90m└───\u001b[39m goto #3 if not %29\n\u001b[90m2 ──\u001b[39m (@_23 = input_data)\n\u001b[90m└───\u001b[39m goto #4\n\u001b[90m3 ──\u001b[39m %33 = Core.tuple(pad, pad)\u001b[36m::Tuple{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %34 = Core.tuple(pad, pad)\u001b[36m::Tuple{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %35 = Core.tuple(0, 0)\u001b[36m::Core.Const((0, 0))\u001b[39m\n\u001b[90m│ \u001b[39m %36 = Core.tuple(0, 0)\u001b[36m::Core.Const((0, 0))\u001b[39m\n\u001b[90m└───\u001b[39m (@_23 = Main.zeropad(input_data, %33, %34, %35, %36))\n\u001b[90m4 ┄─\u001b[39m (img = @_23)\n\u001b[90m│ \u001b[39m %39 = $(Expr(:static_parameter, 1))\u001b[36m::Core.Const(Int64)\u001b[39m\n\u001b[90m│ \u001b[39m %40 = Core.tuple(out_w, out_h, filter_w, filter_h, C, N)\u001b[36m::NTuple{6, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m (col = Main.zeros(%39, %40))\n\u001b[90m│ \u001b[39m %42 = (1:filter_h)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m (@_8 = Base.iterate(%42))\n\u001b[90m│ \u001b[39m %44 = (@_8 === nothing)\u001b[36m::Bool\u001b[39m\n\u001b[90m│ \u001b[39m %45 = Base.not_int(%44)\u001b[36m::Bool\u001b[39m\n\u001b[90m└───\u001b[39m goto #10 if not %45\n\u001b[90m5 ┄─\u001b[39m %47 = @_8::Tuple{Int64, Int64}\u001b[36m::Tuple{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m (y = Core.getfield(%47, 1))\n\u001b[90m│ \u001b[39m %49 = Core.getfield(%47, 2)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %50 = y\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %51 = (stride * out_h)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %52 = (%50 + %51)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m (y_max = %52 - 1)\n\u001b[90m│ \u001b[39m %54 = (1:filter_w)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m (@_18 = Base.iterate(%54))\n\u001b[90m│ \u001b[39m %56 = (@_18 === nothing)\u001b[36m::Bool\u001b[39m\n\u001b[90m│ \u001b[39m %57 = Base.not_int(%56)\u001b[36m::Bool\u001b[39m\n\u001b[90m└───\u001b[39m goto #8 if not %57\n\u001b[90m6 ┄─\u001b[39m %59 = @_18::Tuple{Int64, Int64}\u001b[36m::Tuple{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m (x = Core.getfield(%59, 1))\n\u001b[90m│ \u001b[39m %61 = Core.getfield(%59, 2)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %62 = x\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %63 = (stride * out_w)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %64 = (%62 + %63)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m (x_max = %64 - 1)\n\u001b[90m│ \u001b[39m %66 = Base.dotview(col, Main.:(:), Main.:(:), x, y, Main.:(:), Main.:(:))\u001b[36m::Core.PartialStruct(SubArray{Int64, 4, Array{Int64, 6}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}, Int64, Int64, Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, false}, Any[Array{Int64, 6}, Tuple{Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}, Int64, Int64, Base.Slice{Base.OneTo{Int64}}, Base.Slice{Base.OneTo{Int64}}}, Core.Const(0), Core.Const(0)])\u001b[39m\n\u001b[90m│ \u001b[39m %67 = img\u001b[36m::Array{Int64, 4}\u001b[39m\n\u001b[90m│ \u001b[39m %68 = (x:stride:x_max)\u001b[36m::StepRange{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %69 = (y:stride:y_max)\u001b[36m::StepRange{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %70 = Base.getindex(%67, %68, %69, Main.:(:), Main.:(:))\u001b[36m::Array{Int64, 4}\u001b[39m\n\u001b[90m│ \u001b[39m %71 = Base.broadcasted(Base.identity, %70)\u001b[36m::Base.Broadcast.Broadcasted{Base.Broadcast.DefaultArrayStyle{4}, Nothing, typeof(identity), Tuple{Array{Int64, 4}}}\u001b[39m\n\u001b[90m│ \u001b[39m Base.materialize!(%66, %71)\n\u001b[90m│ \u001b[39m (@_18 = Base.iterate(%54, %61))\n\u001b[90m│ \u001b[39m %74 = (@_18 === nothing)\u001b[36m::Bool\u001b[39m\n\u001b[90m│ \u001b[39m %75 = Base.not_int(%74)\u001b[36m::Bool\u001b[39m\n\u001b[90m└───\u001b[39m goto #8 if not %75\n\u001b[90m7 ──\u001b[39m goto #6\n\u001b[90m8 ┄─\u001b[39m (@_8 = Base.iterate(%42, %49))\n\u001b[90m│ \u001b[39m %79 = (@_8 === nothing)\u001b[36m::Bool\u001b[39m\n\u001b[90m│ \u001b[39m %80 = Base.not_int(%79)\u001b[36m::Bool\u001b[39m\n\u001b[90m└───\u001b[39m goto #10 if not %80\n\u001b[90m9 ──\u001b[39m goto #5\n\u001b[90m10 ┄\u001b[39m %83 = col\u001b[36m::Array{Int64, 6}\u001b[39m\n\u001b[90m│ \u001b[39m %84 = Core.tuple(3, 4, 5, 1, 2, 6)\u001b[36m::Core.Const((3, 4, 5, 1, 2, 6))\u001b[39m\n\u001b[90m│ \u001b[39m %85 = Main.permutedims(%83, %84)\u001b[36m::Array{Int64, 6}\u001b[39m\n\u001b[90m│ \u001b[39m %86 = (filter_w * filter_h * C)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %87 = (out_w * out_h * N)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %88 = Main.reshape(%85, %86, %87)\u001b[36m::Matrix{Int64}\u001b[39m\n\u001b[90m└───\u001b[39m return %88\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:56:52.664Z",
"end_time": "2020-12-11T08:56:50.812000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "@code_warntype im2col(A, 3, 3)",
"execution_count": 26,
"outputs": [
{
"output_type": "stream",
"text": "Variables\n #self#\u001b[36m::Core.Const(im2col)\u001b[39m\n input_data\u001b[36m::Array{Int64, 4}\u001b[39m\n filter_w\u001b[36m::Int64\u001b[39m\n filter_h\u001b[36m::Int64\u001b[39m\n\nBody\u001b[36m::Matrix{Int64}\u001b[39m\n\u001b[90m1 ─\u001b[39m %1 = Main.:(var\"#im2col#16\")(1, 0, #self#, input_data, filter_w, filter_h)\u001b[36m::Matrix{Int64}\u001b[39m\n\u001b[90m└──\u001b[39m return %1\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:57:19.368Z",
"end_time": "2020-12-11T08:57:17.552000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "# _im2col_16 = getfield(Main, Symbol(\"#im2col#16\"))\n_im2col_16 = var\"#im2col#16\"\n@code_warntype _im2col_16(1, 1, im2col, A, 3, 3)",
"execution_count": 27,
"outputs": [
{
"output_type": "stream",
"text": "Variables\n #im2col#16\u001b[36m::Core.Const(var\"#im2col#16\")\u001b[39m\n stride\u001b[36m::Int64\u001b[39m\n pad\u001b[36m::Int64\u001b[39m\n @_4\u001b[36m::Core.Const(im2col)\u001b[39m\n input_data\u001b[36m::Array{Int64, 4}\u001b[39m\n filter_w\u001b[36m::Int64\u001b[39m\n filter_h\u001b[36m::Int64\u001b[39m\n #17\u001b[36m::var\"#17#18\"{Int64, Array{Int64, 4}}\u001b[39m\n @_9\u001b[36m::Int64\u001b[39m\n col\u001b[36m::Array{Int64, 6}\u001b[39m\n img\u001b[36m::Array{Int64, 4}\u001b[39m\n out_w\u001b[36m::Int64\u001b[39m\n out_h\u001b[36m::Int64\u001b[39m\n N\u001b[36m::Int64\u001b[39m\n C\u001b[36m::Int64\u001b[39m\n H\u001b[36m::Int64\u001b[39m\n W\u001b[36m::Int64\u001b[39m\n @_18\u001b[36m::Array{Int64, 4}\u001b[39m\n\nBody\u001b[36m::Matrix{Int64}\u001b[39m\n\u001b[90m1 ─\u001b[39m nothing\n\u001b[90m│ \u001b[39m Core.NewvarNode(:(#17))\n\u001b[90m│ \u001b[39m Core.NewvarNode(:(col))\n\u001b[90m│ \u001b[39m Core.NewvarNode(:(img))\n\u001b[90m│ \u001b[39m %5 = Main.size(input_data)\u001b[36m::NTuple{4, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %6 = Base.indexed_iterate(%5, 1)\u001b[36m::Core.PartialStruct(Tuple{Int64, Int64}, Any[Int64, Core.Const(2)])\u001b[39m\n\u001b[90m│ \u001b[39m (W = Core.getfield(%6, 1))\n\u001b[90m│ \u001b[39m (@_9 = Core.getfield(%6, 2))\n\u001b[90m│ \u001b[39m %9 = Base.indexed_iterate(%5, 2, @_9::Core.Const(2))\u001b[36m::Core.PartialStruct(Tuple{Int64, Int64}, Any[Int64, Core.Const(3)])\u001b[39m\n\u001b[90m│ \u001b[39m (H = Core.getfield(%9, 1))\n\u001b[90m│ \u001b[39m (@_9 = Core.getfield(%9, 2))\n\u001b[90m│ \u001b[39m %12 = Base.indexed_iterate(%5, 3, @_9::Core.Const(3))\u001b[36m::Core.PartialStruct(Tuple{Int64, Int64}, Any[Int64, Core.Const(4)])\u001b[39m\n\u001b[90m│ \u001b[39m (C = Core.getfield(%12, 1))\n\u001b[90m│ \u001b[39m (@_9 = Core.getfield(%12, 2))\n\u001b[90m│ \u001b[39m %15 = Base.indexed_iterate(%5, 4, @_9::Core.Const(4))\u001b[36m::Core.PartialStruct(Tuple{Int64, Int64}, Any[Int64, Core.Const(5)])\u001b[39m\n\u001b[90m│ \u001b[39m (N = Core.getfield(%15, 1))\n\u001b[90m│ \u001b[39m %17 = H\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %18 = (2 * pad)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %19 = (%17 + %18)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m + = (%19 - filter_h)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %21 = (+ ÷ stride)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m (out_h = %21 + 1)\n\u001b[90m│ \u001b[39m %23 = W\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %24 = (2 * pad)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %25 = (%23 + %24)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %26 = (%25 - filter_w)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %27 = (%26 ÷ stride)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m (out_w = %27 + 1)\n\u001b[90m│ \u001b[39m %29 = (pad == 0)\u001b[36m::Bool\u001b[39m\n\u001b[90m└──\u001b[39m goto #3 if not %29\n\u001b[90m2 ─\u001b[39m (@_18 = input_data)\n\u001b[90m└──\u001b[39m goto #4\n\u001b[90m3 ─\u001b[39m %33 = Core.tuple(pad, pad)\u001b[36m::Tuple{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %34 = Core.tuple(pad, pad)\u001b[36m::Tuple{Int64, Int64}\u001b[39m\n\u001b[90m│ \u001b[39m %35 = Core.tuple(0, 0)\u001b[36m::Core.Const((0, 0))\u001b[39m\n\u001b[90m│ \u001b[39m %36 = Core.tuple(0, 0)\u001b[36m::Core.Const((0, 0))\u001b[39m\n\u001b[90m└──\u001b[39m (@_18 = Main.zeropad(input_data, %33, %34, %35, %36))\n\u001b[90m4 ┄\u001b[39m (img = @_18)\n\u001b[90m│ \u001b[39m %39 = Main.:(var\"#17#18\")\u001b[36m::Core.Const(var\"#17#18\")\u001b[39m\n\u001b[90m│ \u001b[39m %40 = Core.typeof(stride)\u001b[36m::Core.Const(Int64)\u001b[39m\n\u001b[90m│ \u001b[39m %41 = Core.typeof(img)\u001b[36m::Core.Const(Array{Int64, 4})\u001b[39m\n\u001b[90m│ \u001b[39m %42 = Core.apply_type(%39, %40, %41)\u001b[36m::Core.Const(var\"#17#18\"{Int64, Array{Int64, 4}})\u001b[39m\n\u001b[90m│ \u001b[39m (#17 = %new(%42, stride, img))\n\u001b[90m│ \u001b[39m %44 = #17\u001b[36m::var\"#17#18\"{Int64, Array{Int64, 4}}\u001b[39m\n\u001b[90m│ \u001b[39m %45 = (1:filter_w)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m %46 = (1:filter_h)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m %47 = (1:C)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m %48 = (1:out_w)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m %49 = (1:out_h)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m %50 = (1:N)\u001b[36m::Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])\u001b[39m\n\u001b[90m│ \u001b[39m %51 = Base.product(%45, %46, %47, %48, %49, %50)\u001b[36m::Core.PartialStruct(Base.Iterators.ProductIterator{NTuple{6, UnitRange{Int64}}}, Any[Core.PartialStruct(NTuple{6, UnitRange{Int64}}, Any[Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])])])\u001b[39m\n\u001b[90m│ \u001b[39m %52 = Base.Generator(%44, %51)\u001b[36m::Core.PartialStruct(Base.Generator{Base.Iterators.ProductIterator{NTuple{6, UnitRange{Int64}}}, var\"#17#18\"{Int64, Array{Int64, 4}}}, Any[var\"#17#18\"{Int64, Array{Int64, 4}}, Core.PartialStruct(Base.Iterators.ProductIterator{NTuple{6, UnitRange{Int64}}}, Any[Core.PartialStruct(NTuple{6, UnitRange{Int64}}, Any[Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64]), Core.PartialStruct(UnitRange{Int64}, Any[Core.Const(1), Int64])])])])\u001b[39m\n\u001b[90m│ \u001b[39m %53 = Base.collect(%52)\u001b[36m::Array{Int64, 6}\u001b[39m\n\u001b[90m│ \u001b[39m %54 = Core.apply_type(Main.Array, $(Expr(:static_parameter, 1)), 6)\u001b[36m::Core.Const(Array{Int64, 6})\u001b[39m\n\u001b[90m│ \u001b[39m (col = Core.typeassert(%53, %54))\n\u001b[90m│ \u001b[39m %56 = col\u001b[36m::Array{Int64, 6}\u001b[39m\n\u001b[90m│ \u001b[39m %57 = (filter_w * filter_h * C)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %58 = (out_w * out_h * N)\u001b[36m::Int64\u001b[39m\n\u001b[90m│ \u001b[39m %59 = Main.reshape(%56, %57, %58)\u001b[36m::Matrix{Int64}\u001b[39m\n\u001b[90m└──\u001b[39m return %59\n",
"name": "stdout"
}
]
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:30:26.165Z",
"end_time": "2020-12-11T08:30:24.428000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "function col2im(col::AbstractArray{T,2}, input_shape::NTuple{4,Int}, filter_h::Int, filter_w::Int; stride::Int=1, pad::Int=0) where {T}\n W, H, C, N = input_shape\n out_h = (H + 2pad - filter_h) ÷ stride + 1\n out_w = (W + 2pad - filter_w) ÷ stride + 1\n _col = permutedims(reshape(col, filter_w, filter_h, C, out_w, out_h, N), (4, 5, 1, 2, 3, 6))\n\n img = zeros(T, (W + 2*pad + stride - 1, H + 2*pad + stride - 1, C, N))\n @inbounds for y = 1:filter_h\n y_max = y + stride*out_h - 1\n for x = 1:filter_w\n x_max = x + stride*out_w - 1\n img[x:stride:x_max, y:stride:y_max, :, :] .+= _col[:, :, x, y, :, :]\n end\n end\n\n return img[pad+1:pad+W, pad+1:pad+H, :, :]\nend",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"ExecuteTime": {
"start_time": "2020-12-10T23:30:27.659Z",
"end_time": "2020-12-11T08:30:26.099000+09:00"
},
"trusted": true
},
"cell_type": "code",
"source": "col2im(C, size(A), 3, 3, stride=1, pad=1)",
"execution_count": null,
"outputs": []
},
{
"metadata": {
"trusted": true
},
"cell_type": "code",
"source": "",
"execution_count": null,
"outputs": []
}
],
"metadata": {
"kernelspec": {
"name": "julia-1.6",
"display_name": "Julia 1.6.0-DEV",
"language": "julia"
},
"language_info": {
"file_extension": ".jl",
"name": "julia",
"mimetype": "application/julia",
"version": "1.6.0"
},
"gist": {
"id": "",
"data": {
"description": "toybox/im2col_sample.v1x.jl-Copy1.ipynb",
"public": true
}
}
},
"nbformat": 4,
"nbformat_minor": 1
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment