This repo provides train/inference procedure of MNIST model known as Hello World of Deep Learning on Julia runtime.
These techniques are based on Chainer and PyCall.jl.
NEW: Add feature to convert ResNet50/Chainer -> ResNet50/Flux.jl
Usage
How to install
This package is not registered as official julia package, so called 野良(nora), which means we should specify repository url:
Note that Package Gomah.jl depends on PyCall.jl. So before installing, We recommend set environment variable in Julia.
$ julia
julia>ENV["PYTHON"] = Sys.which("python3")
pkg> add https://github.com/terasakisatoshi/Gomah.jl.git
julia>using Gomah
Call Chainer script from Julia via Gomah.jl
PyCall.jl provides interface between Python and Julia.
This means you can construct training script of Chainer on Julia environment.
If you are familiar with some Deep learnig framework, checkout our src/mnist.jl.
We provide an example of training MNIST classifier.
We found the structure (shape) of parameter i.e. weight of Chainer is similar that of Flux.
The structure of weight of Convolution of Chainer is NCHW. On the other hand, Conv of Flux.jl has weight its shape is WHCN, where N is batchsize, H (resp. W) is height (resp. width) of kernel and C is num of channel.
We provided script to convert ResNet50 of Chainer to that of Flux.jl
Here is a example of How to use converted model. What you have to do is ...
Install chainer and chainercv
Install Flux.jl, PyCall, Gomah.jl
Prepare sample RGB image. e.g. pineapple.png
Run the following the script.
using Gomah
using Gomah: L, np, reversedims
using Flux
using PyCall
using Test
using BenchmarkTools
py"""import chainerimport chainercvimport numpy as npnum =50PyResNet = chainercv.links.model.resnet.ResNetresnet = PyResNet(num, pretrained_model="imagenet")img=chainercv.utils.read_image("pineapple.png",dtype=np.float32,alpha="ignore")img=chainercv.transforms.resize(img,(224,224))_imagenet_mean = np.array( [123.15163084, 115.90288257, 103.0626238],dtype=np.float32 )[:, np.newaxis, np.newaxis]img=img-_imagenet_meanimg=np.expand_dims(img,axis=0)resnet.pick=resnet.layer_nameswith chainer.using_config('train', False): pyret=resnet(img) result=np.squeeze(pyret[-1].array) chidx=int(np.argmax(result)) chprob=100*float(result[chidx])print(chidx)print(chprob)"""@testset"regression"begin
num =50
myres =ResNet(num)
Flux.testmode!.(myres.layers)
img =reversedims(py"img")
@showsize(img), typeof(img)
ret, name2data =myres(img)
for (i,name) inenumerate(myres.layer_names)
pyr =reversedims(py"pyret[$i-1].array")
flr = name2data[name]
@show name, size(flr)
@testsize(pyr) ==size(flr)
@showmaximum(abs.(pyr .- flr))
end
flidx =argmax(ret)
flprob =100ret[argmax(ret)]
@show flidx,flprob
@testInt(py"chidx") == flidx[1]-1@showFloat32(py"chprob") - flprob
end@testset"benchmark"begin
num=50
img =reversedims(py"img")
myres =ResNet(num)
chainmodel =Chain(myres.layers...)
Flux.testmode!(chainmodel)
@timechainmodel(img)
@timechainmodel(img)
@timechainmodel(img)
@timechainmodel(img)
@timechainmodel(img)
@timechainmodel(img)
end