-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #208 from FluxML/image-builder
Add preliminary Metalhead.jl integration
- Loading branch information
Showing
16 changed files
with
344 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,146 @@ | ||
#= | ||
TODO: After https://github.com/FluxML/Metalhead.jl/issues/176: | ||
- Export and externally document `image_builder` method | ||
- Delete definition of `ResNetHack` below | ||
- Change default builder in ImageClassifier (see /src/types.jl) from | ||
`image_builder(ResNetHack)` to `image_builder(Metalhead.ResNet)`. | ||
=# | ||
|
||
const DISALLOWED_KWARGS = [:imsize, :inchannels, :nclasses] | ||
const human_disallowed_kwargs = join(map(s->"`$s`", DISALLOWED_KWARGS), ", ", " and ") | ||
const ERR_METALHEAD_DISALLOWED_KWARGS = ArgumentError( | ||
"Keyword arguments $human_disallowed_kwargs are disallowed "* | ||
"as their values are inferred from data. " | ||
) | ||
|
||
# # WRAPPING | ||
|
||
struct MetalheadBuilder{F} <: MLJFlux.Builder | ||
metalhead_constructor::F | ||
args | ||
kwargs | ||
end | ||
|
||
function Base.show(io::IO, ::MIME"text/plain", w::MetalheadBuilder) | ||
println(io, "builder wrapping $(w.metalhead_constructor)") | ||
if !isempty(w.args) | ||
println(io, " args:") | ||
for (i, arg) in enumerate(w.args) | ||
println(io, " 1: $arg") | ||
end | ||
end | ||
if !isempty(w.kwargs) | ||
println(io, " kwargs:") | ||
for kwarg in w.kwargs | ||
println(io, " $(first(kwarg)) = $(last(kwarg))") | ||
end | ||
end | ||
end | ||
|
||
Base.show(io::IO, w::MetalheadBuilder) = | ||
print(io, "image_builder($(repr(w.metalhead_constructor)), …)") | ||
|
||
|
||
""" | ||
image_builder(metalhead_constructor, args...; kwargs...) | ||
Return an MLJFlux builder object based on the Metalhead.jl constructor/type | ||
`metalhead_constructor` (eg, `Metalhead.ResNet`). Here `args` and `kwargs` are | ||
passed to the `MetalheadType` constructor at "build time", along with | ||
the extra keyword specifiers `imsize=...`, `inchannels=...` and | ||
`nclasses=...`, with values inferred from the data. | ||
# Example | ||
If in Metalhead.jl you would do | ||
```julia | ||
using Metalhead | ||
model = ResNet(50, pretrain=true, inchannels=1, nclasses=10) | ||
``` | ||
then in MLJFlux, it suffices to do | ||
```julia | ||
using MLJFlux, Metalhead | ||
builder = image_builder(ResNet, 50, pretrain=true) | ||
``` | ||
which can be used in `ImageClassifier` as in | ||
```julia | ||
clf = ImageClassifier( | ||
builder=builder, | ||
epochs=500, | ||
optimiser=Flux.Adam(0.001), | ||
loss=Flux.crossentropy, | ||
batch_size=5, | ||
) | ||
``` | ||
The keyord arguments `imsize`, `inchannels` and `nclasses` are | ||
dissallowed in `kwargs` (see above). | ||
""" | ||
function image_builder( | ||
metalhead_constructor, | ||
args...; | ||
kwargs... | ||
) | ||
kw_names = keys(kwargs) | ||
isempty(intersect(kw_names, DISALLOWED_KWARGS)) || | ||
throw(ERR_METALHEAD_DISALLOWED_KWARGS) | ||
return MetalheadBuilder(metalhead_constructor, args, kwargs) | ||
end | ||
|
||
MLJFlux.build( | ||
b::MetalheadBuilder, | ||
rng, | ||
n_in, | ||
n_out, | ||
n_channels | ||
) = b.metalhead_constructor( | ||
b.args...; | ||
b.kwargs..., | ||
imsize=n_in, | ||
inchannels=n_channels, | ||
nclasses=n_out | ||
) | ||
|
||
# See above "TODO" list. | ||
function VGGHack( | ||
depth::Integer=16; | ||
imsize=(242,242), | ||
inchannels=3, | ||
nclasses=1000, | ||
batchnorm=false, | ||
pretrain=false, | ||
) | ||
|
||
# Adapted from | ||
# https://github.com/FluxML/Metalhead.jl/blob/9edff63222720ff84671b8087dd71eb370a6c35a/src/convnets/vgg.jl#L165 | ||
# But we do not ignore `imsize`. | ||
|
||
@assert( | ||
depth in keys(Metalhead.vgg_config), | ||
"depth must be from one in $(sort(collect(keys(Metalhead.vgg_config))))" | ||
) | ||
model = Metalhead.VGG(imsize; | ||
config = Metalhead.vgg_conv_config[Metalhead.vgg_config[depth]], | ||
inchannels, | ||
batchnorm, | ||
nclasses, | ||
fcsize = 4096, | ||
dropout = 0.5) | ||
if pretrain && !batchnorm | ||
Metalhead.loadpretrain!(model, string("VGG", depth)) | ||
elseif pretrain | ||
Metalhead.loadpretrain!(model, "VGG$(depth)-BN)") | ||
end | ||
return model | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
# # IMAGE COERCION | ||
|
||
# Taken from ScientificTypes.jl to avoid as dependency. | ||
|
||
_4Dcollection = AbstractArray{<:Real, 4} | ||
|
||
function coerce(y::_4Dcollection, T2::Type{GrayImage}) | ||
size(y, 3) == 1 || error("Multiple color channels encountered. "* | ||
"Perhaps you want to use `coerce(image_collection, ColorImage)`.") | ||
y = dropdims(y, dims=3) | ||
return [ColorTypes.Gray.(y[:,:,idx]) for idx=1:size(y,3)] | ||
end | ||
|
||
function coerce(y::_4Dcollection, T2::Type{ColorImage}) | ||
return [broadcast(ColorTypes.RGB, y[:,:,1, idx], y[:,:,2,idx], y[:,:,3, idx]) for idx=1:size(y,4)] | ||
end | ||
|
||
|
||
# # SYNTHETIC IMAGES | ||
|
||
""" | ||
make_images(rng; image_size=(6, 6), n_classes=33, n_images=50, color=false, noise=0.05) | ||
Return synthetic data of the form `(images, labels)` suitable for use | ||
with MLJ's `ImageClassifier` model. All `images` are distortions of | ||
`n_classes` fixed images. Two images with the same label correspond to | ||
the same undistorted image. | ||
""" | ||
function make_images(rng; image_size=(6, 6), n_classes=33, n_images=50, color=false, noise=0.05) | ||
n_channels = color ? 3 : 1 | ||
image_bag = map(1:n_classes) do _ | ||
rand(rng, Float32, image_size..., n_channels) | ||
end | ||
labels = rand(rng, 1:3, n_images) | ||
images = map(labels) do j | ||
image_bag[j] + noise*rand(rng, Float32, image_size..., n_channels) | ||
end | ||
T = color ? ColorImage : GrayImage | ||
X = coerce(cat(images...; dims=4), T) | ||
y = categorical(labels) | ||
return X, y | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.