Skip to content

Commit ad10923

Browse files
author
Avik Pal
authored
Merge branch 'master' into depthwiseconv
2 parents 3e5b77e + a93ee1a commit ad10923

File tree

4 files changed

+108
-59
lines changed

4 files changed

+108
-59
lines changed

src/impl/conv.jl

Lines changed: 39 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@ function psize(p, x)
1010
end
1111
end
1212

13-
function im2col_2d!{T}(img::AbstractArray{T,3}, col::AbstractArray{T,2}, width::Int, height::Int, channels::Int,
13+
function im2col_2d!(img::AbstractArray{T,3}, col::AbstractArray{T,2}, width::Int, height::Int, channels::Int,
1414
kernel_w::Int, kernel_h::Int, pad_w::Int, pad_h::Int, stride_w::Int, stride_h::Int,
15-
dil_w::Int, dil_h::Int, mode::Int)
15+
dil_w::Int, dil_h::Int, mode::Int) where T
1616

1717
height_col = div(height + 2pad_h - (kernel_h - 1) * dil_h - 1, stride_h) + 1
1818
width_col = div(width + 2pad_w - (kernel_w - 1) * dil_w - 1, stride_w) + 1
@@ -42,9 +42,9 @@ function im2col_2d!{T}(img::AbstractArray{T,3}, col::AbstractArray{T,2}, width::
4242
end
4343
end
4444

45-
function col2im_2d!{T}(col::AbstractArray{T,2}, img::AbstractArray{T,3}, width::Int, height::Int,
45+
function col2im_2d!(col::AbstractArray{T,2}, img::AbstractArray{T,3}, width::Int, height::Int,
4646
channels::Int, kernel_w::Int, kernel_h::Int, pad_w::Int, pad_h::Int, stride_w::Int,
47-
stride_h::Int, dil_h::Int, dil_w::Int, mode::Int)
47+
stride_h::Int, dil_w::Int, dil_h::Int, mode::Int) where T
4848

4949
height_col = div(height + 2pad_h - (kernel_h - 1) * dil_h - 1, stride_h) + 1
5050
width_col = div(width + 2pad_w - (kernel_w - 1) * dil_w - 1, stride_w) + 1
@@ -71,9 +71,9 @@ function col2im_2d!{T}(col::AbstractArray{T,2}, img::AbstractArray{T,3}, width::
7171
end
7272
end
7373

74-
function im2col_3d!{T}(img::AbstractArray{T,4}, col::AbstractArray{T,2}, width::Int, height::Int, depth::Int,
74+
function im2col_3d!(img::AbstractArray{T,4}, col::AbstractArray{T,2}, width::Int, height::Int, depth::Int,
7575
channels::Int, kernel_w::Int, kernel_h::Int, kernel_d::Int, pad_w::Int, pad_h::Int, pad_d::Int,
76-
stride_w::Int, stride_h::Int, stride_d::Int, dil_w::Int, dil_h::Int, dil_d::Int, mode::Int)
76+
stride_w::Int, stride_h::Int, stride_d::Int, dil_w::Int, dil_h::Int, dil_d::Int, mode::Int) where T
7777

7878
height_col = div(height + 2pad_h - (kernel_h - 1) * dil_h - 1, stride_h) + 1
7979
width_col = div(width + 2pad_w - (kernel_w - 1) * dil_w - 1, stride_w) + 1
@@ -107,10 +107,10 @@ function im2col_3d!{T}(img::AbstractArray{T,4}, col::AbstractArray{T,2}, width::
107107
end
108108
end
109109

110-
function col2im_3d!{T}(col::AbstractArray{T,2}, img::AbstractArray{T,4}, width::Int, height::Int,
110+
function col2im_3d!(col::AbstractArray{T,2}, img::AbstractArray{T,4}, width::Int, height::Int,
111111
depth::Int, channels::Int, kernel_w::Int, kernel_h::Int, kernel_d::Int,
112112
pad_w::Int, pad_h::Int, pad_d::Int, stride_w::Int, stride_h::Int, stride_d::Int,
113-
dil_w::Int, dil_h::Int, dil_d::Int, mode::Int)
113+
dil_w::Int, dil_h::Int, dil_d::Int, mode::Int) where T
114114

115115
height_col = div(height + 2pad_h - (kernel_h - 1) * dil_h - 1, stride_h) + 1
116116
width_col = div(width + 2pad_w - (kernel_w - 1) * dil_w - 1, stride_w) + 1
@@ -159,15 +159,14 @@ function dilation_dims(w, dilation = 1)
159159
end
160160
end
161161

162-
function im2col_dims(w,y,dilation=1)
162+
function im2col_dims(w,y)
163163
N = ndims(y)
164-
dil = dilation_dims(w, dilation)
165164
r,c = 1,1
166165
for i=1:N-2
167166
r *= size(y,i)
168-
c *= dil[i]
167+
c *= size(w,i)
169168
end
170-
c *= dil[N-1]
169+
c *= size(w,N-1)
171170
return (r, c)
172171
end
173172

@@ -183,7 +182,7 @@ function im2col_dims(w::NTuple{4, Int}, y)
183182
end
184183

185184
function depthwiseconv2d!(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4};
186-
padding = 0, stride = 1, mode = 1, alpha = T(1)) where {T<:Real}
185+
padding = 0, stride = 1, mode = 1, alpha = T(1)) where T
187186
Wx,Hx,Cx,Nx = size(x)
188187
Ww,Hw,Cm,Cw = size(w) # Cm = Channel Multiplier
189188
@assert Cx == Cw DimensionMismatch()
@@ -206,7 +205,7 @@ function depthwiseconv2d!(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::Abstr
206205
end
207206

208207
function depthwiseconv2d_grad_w!(dw::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}, dy::AbstractArray{T,4};
209-
padding=0, stride=1, mode=0, alpha=1) where {T<:Real}
208+
padding=0, stride=1, mode=0, alpha=1) where T
210209
Wx,Hx,Cx,Nx = size(x)
211210
Ww,Hw,Cm,Cw = size(w) # Cm = Channel Multiplier
212211
@assert Cx == Cw DimensionMismatch()
@@ -232,8 +231,8 @@ function depthwiseconv2d_grad_w!(dw::AbstractArray{T,4}, x::AbstractArray{T,4},
232231
return dw
233232
end
234233

235-
function depthwiseconv2d_grad_x!{T}(dx::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}, dy::AbstractArray{T,4};
236-
padding=0, stride=1, mode=0, alpha=1)
234+
function depthwiseconv2d_grad_x!(dx::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}, dy::AbstractArray{T,4};
235+
padding=0, stride=1, mode=0, alpha=1) where T
237236
Wx,Hx,Cx,Nx = size(x)
238237
Ww,Hw,Cm,Cw = size(w) # Cm = Channel Multiplier
239238
@assert Cx == Cw DimensionMismatch()
@@ -257,14 +256,14 @@ function depthwiseconv2d_grad_x!{T}(dx::AbstractArray{T,4}, x::AbstractArray{T,4
257256
return dx
258257
end
259258

260-
function conv2d!{T}(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4};
261-
padding=0, stride=1, dilation=1, mode=0, alpha=T(1))
259+
function conv2d!(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4};
260+
padding=0, stride=1, dilation=1, mode=0, alpha=T(1)) where T
262261
if mode != 0 && mode != 1; throw(ArgumentError("conv2d only supports mode=0 or 1.")); end
263262
Wx,Hx,Cx,Nx = size(x)
264-
Ww,Hw,C1,C2 = dilation_dims(w, dilation)
263+
Ww,Hw,C1,C2 = size(w)
265264
if Cx!=C1; throw(DimensionMismatch()); end
266265
Wy,Hy,Cy,Ny = size(y)
267-
x2dims = im2col_dims(w,y,dilation)
266+
x2dims = im2col_dims(w,y)
268267
x2 = similar(x, x2dims)
269268
(p1,p2) = psize(padding,x)
270269
(s1,s2) = psize(stride,x)
@@ -279,15 +278,15 @@ function conv2d!{T}(y::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArr
279278
return y
280279
end
281280

282-
function conv2d_grad_w!{T}(dw::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}, dy::AbstractArray{T,4};
283-
padding=0, stride=1, dilation=1, mode=0, alpha=1)
281+
function conv2d_grad_w!(dw::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}, dy::AbstractArray{T,4};
282+
padding=0, stride=1, dilation=1, mode=0, alpha=1) where T
284283
# dw = x'*dy
285284
Wx,Hx,Cx,Nx = size(x)
286-
Ww,Hw,C1,C2 = dilation_dims(w, dilation)
285+
Ww,Hw,C1,C2 = size(w)
287286
Wy,Hy,Cy,Ny = size(dy)
288287
# if mode != 0 && mode != 1; throw(ArgumentError("conv2d only supports mode=0 or 1.")); end
289288
# @assert Cx==C1 && Cy==C2 && Ny==Nx
290-
x2dims = im2col_dims(w,dy,dilation)
289+
x2dims = im2col_dims(w,dy)
291290
x2 = similar(x, x2dims)
292291
# op(A) is an m-by-k matrix, op(B) is a k-by-n matrix, C is an m-by-n matrix.
293292
Y,M,N,K = Wy*Hy*Cy,Ww*Hw*Cx,Cy,Wy*Hy
@@ -304,15 +303,15 @@ function conv2d_grad_w!{T}(dw::AbstractArray{T,4}, x::AbstractArray{T,4}, w::Abs
304303
return dw
305304
end
306305

307-
function conv2d_grad_x!{T}(dx::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}, dy::AbstractArray{T,4};
308-
padding=0, stride=1, dilation=1, mode=0, alpha=1)
306+
function conv2d_grad_x!(dx::AbstractArray{T,4}, x::AbstractArray{T,4}, w::AbstractArray{T,4}, dy::AbstractArray{T,4};
307+
padding=0, stride=1, dilation=1, mode=0, alpha=1) where T
309308
# dx = dy*w'
310309
Wx,Hx,Cx,Nx = size(x)
311-
Ww,Hw,C1,C2 = dilation_dims(w, dilation)
310+
Ww,Hw,C1,C2 = size(w)
312311
Wy,Hy,Cy,Ny = size(dy)
313312
# if mode != 0 && mode != 1; throw(ArgumentError("conv2d only supports mode=0 or 1.")); end
314313
@assert Cx==C1 && Cy==C2 && Ny==Nx
315-
x2dims = im2col_dims(w,dy,dilation)
314+
x2dims = im2col_dims(w,dy)
316315
x2 = similar(x, x2dims)
317316
# op(A) is an m-by-k matrix, op(B) is a k-by-n matrix, C is an m-by-n matrix.
318317
Y,M,N,K = Wy*Hy*Cy,Wy*Hy,Ww*Hw*Cx,Cy
@@ -367,15 +366,15 @@ function col2im2d!(w::AbstractArray{T,4}, x::AbstractArray{T,4}, x2::AbstractArr
367366
return x
368367
end
369368

370-
function conv3d!{T}(y::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArray{T,5};
371-
padding=0, stride=1, dilation = 1, mode=0, alpha=T(1))
369+
function conv3d!(y::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArray{T,5};
370+
padding=0, stride=1, dilation = 1, mode=0, alpha=T(1)) where T
372371
if mode != 0 && mode != 1; throw(ArgumentError("conv3d only supports mode=0 or 1.")); end
373372
Wx,Hx,Dx,Cx,Nx = size(x)
374-
Ww,Hw,Dw,C1,C2 = dilation_dims(w, dilation)
373+
Ww,Hw,Dw,C1,C2 = size(w)
375374
if Cx!=C1; throw(DimensionMismatch()); end
376375
Wy,Hy,Dy,Cy,Ny = size(y)
377376
# @assert Cy==C2 && Ny==Nx
378-
x2dims = im2col_dims(w,y,dilation)
377+
x2dims = im2col_dims(w,y)
379378
x2 = similar(x, x2dims)
380379
(p1,p2,p3) = psize(padding,x)
381380
(s1,s2,s3) = psize(stride,x)
@@ -391,15 +390,15 @@ function conv3d!{T}(y::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArr
391390
return y
392391
end
393392

394-
function conv3d_grad_w!{T}(dw::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArray{T,5}, dy::AbstractArray{T,5};
395-
padding=0, stride=1, dilation = 1, mode=0, alpha=1)
393+
function conv3d_grad_w!(dw::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArray{T,5}, dy::AbstractArray{T,5};
394+
padding=0, stride=1, dilation = 1, mode=0, alpha=1) where T
396395
# dw = x'*dy
397396
Wx,Hx,Dx,Cx,Nx = size(x)
398-
Ww,Hw,Dw,C1,C2 = dilation_dims(w, dilation)
397+
Ww,Hw,Dw,C1,C2 = size(w)
399398
Wy,Hy,Dy,Cy,Ny = size(dy)
400399
# if mode != 0 && mode != 1; throw(ArgumentError("conv2d only supports mode=0 or 1.")); end
401400
# @assert Cx==C1 && Cy==C2 && Ny==Nx
402-
x2dims = im2col_dims(w,dy,dilation)
401+
x2dims = im2col_dims(w,dy)
403402
x2 = similar(x, x2dims)
404403
# op(A) is an m-by-k matrix, op(B) is a k-by-n matrix, C is an m-by-n matrix.
405404
Y,M,N,K = Wy*Hy*Dy*Cy,Ww*Hw*Dw*Cx,Cy,Wy*Hy*Dy
@@ -416,15 +415,15 @@ function conv3d_grad_w!{T}(dw::AbstractArray{T,5}, x::AbstractArray{T,5}, w::Abs
416415
return dw
417416
end
418417

419-
function conv3d_grad_x!{T}(dx::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArray{T,5}, dy::AbstractArray{T,5};
420-
padding=0, stride=1, dilation = 1, mode=0, alpha=1)
418+
function conv3d_grad_x!(dx::AbstractArray{T,5}, x::AbstractArray{T,5}, w::AbstractArray{T,5}, dy::AbstractArray{T,5};
419+
padding=0, stride=1, dilation = 1, mode=0, alpha=1) where T
421420
# dx = dy*w'
422421
Wx,Hx,Dx,Cx,Nx = size(x)
423-
Ww,Hw,Dw,C1,C2 = dilation_dims(w, dilation)
422+
Ww,Hw,Dw,C1,C2 = size(w)
424423
Wy,Hy,Dy,Cy,Ny = size(dy)
425424
# if mode != 0 && mode != 1; throw(ArgumentError("conv2d only supports mode=0 or 1.")); end
426425
@assert Cx==C1 && Cy==C2 && Ny==Nx
427-
x2dims = im2col_dims(w,dy,dilation)
426+
x2dims = im2col_dims(w,dy)
428427
x2 = similar(x, x2dims)
429428
# op(A) is an m-by-k matrix, op(B) is a k-by-n matrix, C is an m-by-n matrix.
430429
Y,M,N,K = Wy*Hy*Dy*Cy,Wy*Hy*Dy,Ww*Hw*Dw*Cx,Cy

src/impl/pool.jl

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
function max_pooling2d_fwd!{T}(x::AbstractArray{T,4}, y::AbstractArray{T,4},
2-
width::Int, height::Int, channels::Int, num::Int, pooled_width::Int,
3-
pooled_height::Int, kernel_w::Int, kernel_h::Int, pad_w::Int, pad_h::Int,
4-
stride_w::Int, stride_h::Int)
1+
function max_pooling2d_fwd!(x::AbstractArray{T,4}, y::AbstractArray{T,4},
2+
width::Int, height::Int, channels::Int, num::Int, pooled_width::Int,
3+
pooled_height::Int, kernel_w::Int, kernel_h::Int, pad_w::Int, pad_h::Int,
4+
stride_w::Int, stride_h::Int) where T
55
for n = 1:num, c = 1:channels, ph = 1:pooled_height, pw = 1:pooled_width
66
hstart = (ph - 1)*stride_h - pad_h
77
wstart = (pw - 1)*stride_w - pad_w
@@ -27,10 +27,10 @@ function maxpool2d!(y::AbstractArray{T,4}, x::AbstractArray{T,4};
2727
return y
2828
end
2929

30-
function max_pooling2d_bwd!{T}(x::AbstractArray{T,4}, y::AbstractArray{T,4},
30+
function max_pooling2d_bwd!(x::AbstractArray{T,4}, y::AbstractArray{T,4},
3131
grad_output::AbstractArray{T,4}, grad_input::AbstractArray{T,4}, width::Int, height::Int,
3232
channels::Int, num::Int, pooled_width::Int, pooled_height::Int, kernel_w::Int,
33-
kernel_h::Int, pad_w::Int, pad_h::Int, stride_w::Int, stride_h::Int)
33+
kernel_h::Int, pad_w::Int, pad_h::Int, stride_w::Int, stride_h::Int) where T
3434

3535
grad_input[:, :, :, :] = 0
3636
#pragma omp parallel for
@@ -64,10 +64,10 @@ function maxpool2d_grad!(dx::AbstractArray{T,4}, dy::AbstractArray{T,4}, y::Abst
6464
end
6565

6666

67-
function mean_pooling2d_fwd!{T}(x::AbstractArray{T,4}, y::AbstractArray{T,4},
67+
function mean_pooling2d_fwd!(x::AbstractArray{T,4}, y::AbstractArray{T,4},
6868
width::Int, height::Int, channels::Int, num::Int, pooled_width::Int,
6969
pooled_height::Int, kernel_w::Int, kernel_h::Int,pad_w::Int, pad_h::Int,
70-
stride_w::Int, stride_h::Int)
70+
stride_w::Int, stride_h::Int) where T
7171
kernel_size = kernel_w * kernel_h
7272
for n = 1:num, c = 1:channels, ph = 1:pooled_height, pw = 1:pooled_width
7373
hstart = (ph - 1) * stride_h - pad_h
@@ -94,10 +94,10 @@ function meanpool2d!(y::AbstractArray{T,4}, x::AbstractArray{T,4};
9494
return y
9595
end
9696

97-
function mean_pooling2d_bwd!{T}(x::AbstractArray{T,4}, y::AbstractArray{T,4},
97+
function mean_pooling2d_bwd!(x::AbstractArray{T,4}, y::AbstractArray{T,4},
9898
width::Int, height::Int, channels::Int, num::Int, pooled_width::Int,
9999
pooled_height::Int, kernel_w::Int, kernel_h::Int, pad_w::Int, pad_h::Int,
100-
stride_w::Int, stride_h::Int)
100+
stride_w::Int, stride_h::Int) where T
101101

102102
x[:, :, :, :] = 0
103103
kernel_size = kernel_w * kernel_h
@@ -128,10 +128,10 @@ function meanpool2d_grad!(dx::AbstractArray{T,4}, dy::AbstractArray{T,4}, y::Abs
128128
return dx
129129
end
130130

131-
function max_pooling3d_fwd!{T}(x::AbstractArray{T,5}, y::AbstractArray{T,5},
131+
function max_pooling3d_fwd!(x::AbstractArray{T,5}, y::AbstractArray{T,5},
132132
width::Int, height::Int, depth::Int, channels::Int, num::Int, pooled_width::Int,
133133
pooled_height::Int, pooled_depth::Int, kernel_w::Int, kernel_h::Int, kernel_d::Int,
134-
pad_w::Int, pad_h::Int, pad_d::Int, stride_w::Int, stride_h::Int, stride_d::Int)
134+
pad_w::Int, pad_h::Int, pad_d::Int, stride_w::Int, stride_h::Int, stride_d::Int) where T
135135
for n = 1:num, c = 1:channels, pd = 1:pooled_depth, ph = 1:pooled_height, pw = 1:pooled_width
136136
dstart = (pd - 1)* stride_d - pad_d
137137
hstart = (ph - 1)* stride_h - pad_h
@@ -162,11 +162,11 @@ function maxpool3d!(y::AbstractArray{T,5}, x::AbstractArray{T,5};
162162
return y
163163
end
164164

165-
function max_pooling3d_bwd!{T}(x::AbstractArray{T,5}, y::AbstractArray{T,5},
165+
function max_pooling3d_bwd!(x::AbstractArray{T,5}, y::AbstractArray{T,5},
166166
grad_output::AbstractArray{T,5}, grad_input::AbstractArray{T,5}, width::Int, height::Int, depth::Int,
167167
channels::Int, num::Int, pooled_width::Int, pooled_height::Int, pooled_depth::Int,
168168
kernel_w::Int, kernel_h::Int, kernel_d::Int, pad_w::Int, pad_h::Int, pad_d::Int,
169-
stride_w::Int, stride_h::Int, stride_d::Int)
169+
stride_w::Int, stride_h::Int, stride_d::Int) where T
170170

171171
grad_input[:, :, :, :, :] = 0
172172

@@ -206,10 +206,10 @@ function maxpool3d_grad!(dx::AbstractArray{T,5}, dy::AbstractArray{T,5}, y::Abst
206206
return dx
207207
end
208208

209-
function mean_pooling3d_fwd!{T}(x::AbstractArray{T,5}, y::AbstractArray{T,5},
209+
function mean_pooling3d_fwd!(x::AbstractArray{T,5}, y::AbstractArray{T,5},
210210
width::Int, height::Int, depth::Int, channels::Int, num::Int, pooled_width::Int,
211211
pooled_height::Int, pooled_depth::Int, kernel_w::Int, kernel_h::Int, kernel_d::Int,
212-
pad_w::Int, pad_h::Int, pad_d::Int, stride_w::Int, stride_h::Int, stride_d::Int)
212+
pad_w::Int, pad_h::Int, pad_d::Int, stride_w::Int, stride_h::Int, stride_d::Int) where T
213213

214214
kernel_size = kernel_w * kernel_h * kernel_d
215215
#pragma omp parallel for
@@ -243,10 +243,10 @@ function meanpool3d!(y::AbstractArray{T,5}, x::AbstractArray{T,5};
243243
return y
244244
end
245245

246-
function mean_pooling3d_bwd!{T}(grad_input::AbstractArray{T,5}, grad_output::AbstractArray{T,5},
246+
function mean_pooling3d_bwd!(grad_input::AbstractArray{T,5}, grad_output::AbstractArray{T,5},
247247
width::Int, height::Int, depth::Int, channels::Int, num::Int, pooled_width::Int,
248248
pooled_height::Int, pooled_depth::Int, kernel_w::Int, kernel_h::Int, kernel_d::Int,
249-
pad_w::Int, pad_h::Int, pad_d::Int, stride_w::Int, stride_h::Int, stride_d::Int)
249+
pad_w::Int, pad_h::Int, pad_d::Int, stride_w::Int, stride_h::Int, stride_d::Int) where T
250250

251251
kernel_size = kernel_w * kernel_h * kernel_d
252252
fill!(grad_input, 0.0)

src/softmax.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
using Base.Threads
22

3-
function softmax!(out::AbstractVecOrMat, xs::AbstractVecOrMat)
3+
function softmax!(out::AbstractVecOrMat{T}, xs::AbstractVecOrMat{T}) where T<:AbstractFloat
44
@threads for j = 1:size(xs, 2)
55
@inbounds begin
66
# out[end, :] .= maximum(xs, 1)

0 commit comments

Comments
 (0)