go-mxnet-predictor is go binding for mxnet c_predict_api. It's as raw as original C api, wish further development for higher level APIs. Feel free to join us :)
Dockerfile offered for building mxnet and go env. You could skip this part by using Docker
mkdir /root/MXNet/
cd /root/MXNet/ && git clone https://github.com/dmlc/mxnet.git --recursive
cd /root/MXNet/mxnet && make -j2
ln -s /root/MXNet/mxnet/lib/libmxnet.so /usr/lib/libmxnet.so
go get github.com/anthonynsimon/bild
go get -u -v github.com/songtianyi/go-mxnet-predictor
cd $GOPATH/src/github.com/songtianyi/go-mxnet-predictor
sed -i "/prefix=/c prefix=\/root\/MXNet\/mxnet" travis/mxnet.pc
cp travis/mxnet.pc /usr/lib/pkgconfig/
pkg-config --libs mxnet
go build examples/flowers/predict.go
To run this example, you need to download model files, mean.bin and input image. Then put them in correct path. These files are shared in dropbox and baidu storage service.
./predict
You might need this mxnet-flower-python
// load model
symbol, err := ioutil.ReadFile("/data/102flowers-symbol.json")
if err != nil {
panic(err)
}
params, err := ioutil.ReadFile("/data/102flowers-0260.params")
if err != nil {
panic(err)
}
// load mean image from file
nd, err := mxnet.CreateNDListFromFile("/data/mean.bin")
if err != nil {
panic(err)
}
// free ndarray list operator before exit
defer nd.Free()
// create Predictor
p, err := mxnet.CreatePredictor(symbol, params, mxnet.Device{mxnet.CPU_DEVICE, 0}, []mxnet.InputNode{{Key: "data", Shape: []uint32{1, 3, 299, 299}}})
if err != nil {
panic(err)
}
defer p.Free()
// see more details in examples/flowers/predict.go
// load test image for predction
img, err := imgio.Open("/data/flowertest.jpg")
if err != nil {
panic(err)
}
// preprocess
resized := transform.Resize(img, 299, 299, transform.Linear)
res, err := utils.CvtImageTo1DArray(resized, item.Data)
if err != nil {
panic(err)
}
// set input
if err := p.SetInput("data", res); err != nil {
panic(err)
}
// do predict
if err := p.Forward(); err != nil {
panic(err)
}
// get predict result
data, err := p.GetOutput(0)
if err != nil {
panic(err)
}
// see more details in examples/flowers/predict.go