Skip to content

Latest commit

 

History

History
134 lines (113 loc) · 4.14 KB

README.md

File metadata and controls

134 lines (113 loc) · 4.14 KB

go-mxnet-predictor

Build Status Go Report Card License

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 :)

Part 1. Steps to build your own linux dev environment

Dockerfile offered for building mxnet and go env. You could skip this part by using Docker

1.1 Install mxnet prerequisites and go
  • for mxnet prerequisites check here
  • for go installation check here
1.2 Get mxnet and build
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

Part 2. Steps to build and run flower example

2.1 Get go-mxnet-predictor and do some configuration
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
2.2 Build flowers example
go build examples/flowers/predict.go
2.3 Download example files

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.

dropbox
pan.baidu.com
2.4 Run example
./predict
2.5 Python version of flower example

You might need this mxnet-flower-python

Part 3. Steps to do inference with go-mxnet-predictor

3.1 Load pre-trained model, mean image and create go predictor
// 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
3.2 Load input data and do preprocess
// 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)
}
3.3 Set input data to preditor
// set input
if err := p.SetInput("data", res); err != nil {
	panic(err)
}
3.4 Do prediction
// do predict
if err := p.Forward(); err != nil {
	panic(err)
}
3.5 Get result
// get predict result
data, err := p.GetOutput(0)
if err != nil {
	panic(err)
}
// see more details in examples/flowers/predict.go