Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

added untrained model Inception v3 #19

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion src/VisionModel/VisionModel.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@ include("alexnet.jl")
include("vgg.jl")
include("resnet.jl")
include("googlenet.jl")
include("inception.jl")

export alexnet,
vgg11, vgg11bn, vgg13, vgg13bn, vgg16, vgg16bn, vgg19, vgg19bn,
ResNet18, ResNet34, ResNet50, ResNet101, ResNet152,
googlenet
googlenet, inception3

end
176 changes: 176 additions & 0 deletions src/VisionModel/inception.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,176 @@
function conv_block(kernelsize::Tuple{Int64,Int64}, inplanes::Int64, outplanes::Int64; stride::Int64=1, pad::Union{Int64,Tuple{Int64,Int64}}=0)
conv_layer = []
push!(conv_layer, Conv(kernelsize, inplanes => outplanes, stride=stride, pad=pad))
push!(conv_layer, BatchNorm(outplanes, relu))
return conv_layer
end

function inception_a(inplanes, pool_proj)
branch1x1 = Chain(conv_block((1, 1), inplanes, 64)...)

branch5x5_1 = Chain(conv_block((1, 1), inplanes, 48)...)
branch5x5_2 = Chain(conv_block((5, 5), 48, 64; pad=2)...)

branch3x3dbl_1 = Chain(conv_block((1, 1), inplanes, 64)...)
branch3x3dbl_2 = Chain(conv_block((3, 3), 64, 96; pad=1)...)
branch3x3dbl_3 = Chain(conv_block((3, 3), 96, 96; pad=1)...)

branch_pool = Chain(MeanPool((3, 3), pad=1, stride=1),
conv_block((1, 1), inplanes, pool_proj)...)

layer = x -> begin
y1 = branch1x1(x)

y2 = branch5x5_1(x)
y2 = branch5x5_2(y2)

y3 = branch3x3dbl_1(x)
y3 = branch3x3dbl_2(y3)
y3 = branch3x3dbl_3(y3)

y4 = branch_pool(x)

return cat(y1, y2, y3, y4; dims=3)
end
end

function inception_b(inplanes)
branch3x3 = Chain(conv_block((3, 3), inplanes, 384; stride=2)...)

branch3x3dbl_1 = Chain(conv_block((1, 1), inplanes, 64)...)
branch3x3dbl_2 = Chain(conv_block((3, 3), 64, 96; pad=1)...)
branch3x3dbl_3 = Chain(conv_block((3, 3), 96, 96; stride=2)...)

branch_pool = Chain(MaxPool((3, 3), stride=2))

layer = x -> begin
y1 = branch3x3(x)

y2 = branch3x3dbl_1(x)
y2 = branch3x3dbl_2(y2)
y2 = branch3x3dbl_3(y2)

y3 = branch_pool(x)

return cat(y1, y2, y3; dims=3)
end
end

function inception_c(inplanes, c7)
branch1x1 = Chain(conv_block((1, 1), inplanes, 192)...)

branch7x7_1 = Chain(conv_block((1, 1), inplanes, c7)...)
branch7x7_2 = Chain(conv_block((1, 7), c7, c7; pad=(0, 3))...)
branch7x7_3 = Chain(conv_block((7, 1), c7, 192; pad=(3, 0))...)

branch7x7dbl_1 = Chain(conv_block((1, 1), inplanes, c7)...)
branch7x7dbl_2 = Chain(conv_block((7, 1), c7, c7; pad=(3, 0))...)
branch7x7dbl_3 = Chain(conv_block((1, 7), c7, c7; pad=(0, 3))...)
branch7x7dbl_4 = Chain(conv_block((7, 1), c7, c7; pad=(3, 0))...)
branch7x7dbl_5 = Chain(conv_block((1, 7), c7, 192; pad=(0, 3))...)

branch_pool = Chain(MeanPool((3, 3), pad=1, stride=1),
conv_block((1, 1), inplanes, 192)...)

layer = x -> begin
y1 = branch1x1(x)

y2 = branch7x7_1(x)
y2 = branch7x7_2(y2)
y2 = branch7x7_3(y2)

y3 = branch7x7dbl_1(x)
y3 = branch7x7dbl_2(y3)
y3 = branch7x7dbl_3(y3)
y3 = branch7x7dbl_4(y3)
y3 = branch7x7dbl_5(y3)

y4 = branch_pool(x)

return cat(y1, y2, y3, y4; dims=3)
end
end

function inception_d(inplanes)
branch3x3_1 = Chain(conv_block((1, 1), inplanes, 192)...)
branch3x3_2 = Chain(conv_block((3, 3), 192, 320; stride=2)...)

branch7x7x3_1 = Chain(conv_block((1, 1), inplanes, 192)...)
branch7x7x3_2 = Chain(conv_block((1, 7), 192, 192; pad=(0, 3))...)
branch7x7x3_3 = Chain(conv_block((7, 1), 192, 192; pad=(3, 0))...)
branch7x7x3_4 = Chain(conv_block((3, 3), 192, 192; stride=2)...)

branch_pool = Chain(MaxPool((3, 3), stride=2))

layer = x -> begin
y1 = branch3x3_1(x)
y1 = branch3x3_2(y1)

y2 = branch7x7x3_1(x)
y2 = branch7x7x3_2(y2)
y2 = branch7x7x3_3(y2)
y2 = branch7x7x3_4(y2)

y3 = branch_pool(x)

return cat(y1, y2, y3; dims=3)
end
end

function inception_e(inplanes)
branch1x1 = Chain(conv_block((1, 1), inplanes, 320)...)

branch3x3_1 = Chain(conv_block((1, 1), inplanes, 384)...)
branch3x3_2a = Chain(conv_block((1, 3), 384, 384; pad=(0, 1))...)
branch3x3_2b = Chain(conv_block((3, 1), 384, 384; pad=(1, 0))...)

branch3x3dbl_1 = Chain(conv_block((1, 1), inplanes, 448)...)
branch3x3dbl_2 = Chain(conv_block((3, 3), 448, 384; pad=1)...)
branch3x3dbl_3a = Chain(conv_block((1, 3), 384, 384; pad=(0, 1))...)
branch3x3dbl_3b = Chain(conv_block((3, 1), 384, 384; pad=(1, 0))...)

branch_pool = Chain(MeanPool((3, 3), pad=1, stride=1),
conv_block((1, 1), inplanes, 192)...)

layer = x -> begin
y1 = branch1x1(x)

y2 = branch3x3_1(x)
y2 = cat(branch3x3_2a(y2), branch3x3_2b(y2); dims=3)

y3 = branch3x3dbl_1(x)
y3 = branch3x3dbl_2(y3)
y3 = cat(branch3x3dbl_3a(y3), branch3x3dbl_3b(y3); dims=3)

y4 = branch_pool(x)

return cat(y1, y2, y3, y4; dims=3)
end
end

function inception3()
layer = Chain(conv_block((3, 3), 3, 32; stride=2)...,
conv_block((3, 3), 32, 32)...,
conv_block((3, 3), 32, 64; pad=1)...,
MaxPool((3, 3), stride=2),
conv_block((1, 1), 64, 80)...,
conv_block((3, 3), 80, 192)...,
MaxPool((3, 3), stride=2),
inception_a(192, 32),
inception_a(256, 64),
inception_a(288, 64),
inception_b(288),
inception_c(768, 128),
inception_c(768, 160),
inception_c(768, 160),
inception_c(768, 192),
inception_d(768),
inception_e(1280),
inception_e(2048),
AdaptiveMeanPool((1, 1)),
Dropout(0.2),
flatten,
Dense(2048, 1000))
Flux.testmode!(layer, false)
return layer
end
4 changes: 4 additions & 0 deletions test/VisionModel/inception.jl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
@testset "InceptionV3" begin
m = inception3()
@test size(m(rand(Float32, 299, 299, 3, 50))) == (1000, 50)
end
1 change: 1 addition & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@ using Test
include("VisionModel/resnet.jl")
include("VisionModel/vgg.jl")
include("VisionModel/googlenet.jl")
include("VisionModel/inception.jl")
end