GithubHelp home page GithubHelp logo

songtianyi / go-mxnet-predictor Goto Github PK

View Code? Open in Web Editor NEW
55.0 6.0 15.0 1.75 MB

go binding for mxnet c_predict_api to do inference with pre-trained model

License: Apache License 2.0

Go 96.24% Shell 3.76%
machine-learning mxnet golang cgo inference deep-learning

go-mxnet-predictor's Introduction

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

go-mxnet-predictor's People

Contributors

oskca avatar sergeykolychev avatar songtianyi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar

go-mxnet-predictor's Issues

你好,为什么我开启了USE_CUDA=1,但是使用mxnet.GPU_DEVICE还是提示有问题呢?

为什么我开启了USE_CUDA=1,但是使用mxnet.GPU_DEVICE还是提示有问题呢?

[root@localhost go-mxnet-predictor]# ldd /lib/libmxnet.so
linux-vdso.so.1 => (0x00007ffd771cd000)
libcudart.so.9.1 => /usr/local/cuda/lib64/libcudart.so.9.1 (0x00007f424087c000)
libcublas.so.9.1 => /usr/local/cuda/lib64/libcublas.so.9.1 (0x00007f423d2e4000)
libcurand.so.9.1 => /usr/local/cuda/lib64/libcurand.so.9.1 (0x00007f4239361000)
libcusolver.so.9.1 => /usr/local/cuda/lib64/libcusolver.so.9.1 (0x00007f4233bec000)
libsatlas.so.3 => /usr/lib64/atlas/libsatlas.so.3 (0x00007f4232f7e000)
librt.so.1 => /lib64/librt.so.1 (0x00007f4232d76000)
libopencv_calib3d.so.2.4 => /lib64/libopencv_calib3d.so.2.4 (0x00007f4232ae0000)
libopencv_contrib.so.2.4 => /lib64/libopencv_contrib.so.2.4 (0x00007f4232800000)
libopencv_core.so.2.4 => /lib64/libopencv_core.so.2.4 (0x00007f42323c7000)
libopencv_features2d.so.2.4 => /lib64/libopencv_features2d.so.2.4 (0x00007f4232121000)
libopencv_flann.so.2.4 => /lib64/libopencv_flann.so.2.4 (0x00007f4231eb0000)
libopencv_highgui.so.2.4 => /lib64/libopencv_highgui.so.2.4 (0x00007f4231c69000)
libopencv_imgproc.so.2.4 => /lib64/libopencv_imgproc.so.2.4 (0x00007f42317eb000)
libopencv_legacy.so.2.4 => /lib64/libopencv_legacy.so.2.4 (0x00007f42314cd000)
libopencv_ml.so.2.4 => /lib64/libopencv_ml.so.2.4 (0x00007f4231253000)
libopencv_objdetect.so.2.4 => /lib64/libopencv_objdetect.so.2.4 (0x00007f4230fd5000)
libopencv_photo.so.2.4 => /lib64/libopencv_photo.so.2.4 (0x00007f4230db7000)
libopencv_stitching.so.2.4 => /lib64/libopencv_stitching.so.2.4 (0x00007f4230b50000)
libopencv_superres.so.2.4 => /lib64/libopencv_superres.so.2.4 (0x00007f4230930000)
libopencv_video.so.2.4 => /lib64/libopencv_video.so.2.4 (0x00007f42306db000)
libopencv_videostab.so.2.4 => /lib64/libopencv_videostab.so.2.4 (0x00007f423049e000)
libcufft.so.9.1 => /usr/local/cuda/lib64/libcufft.so.9.1 (0x00007f4228fb1000)
libcuda.so.1 => /lib64/libcuda.so.1 (0x00007f4228410000)
libnvrtc.so.9.1 => /usr/local/cuda/lib64/libnvrtc.so.9.1 (0x00007f4226ce4000)
libstdc++.so.6 => /lib64/libstdc++.so.6 (0x00007f42269dc000)
libm.so.6 => /lib64/libm.so.6 (0x00007f42266d9000)
libgomp.so.1 => /lib64/libgomp.so.1 (0x00007f42264b3000)
libgcc_s.so.1 => /lib64/libgcc_s.so.1 (0x00007f422629d000)
libpthread.so.0 => /lib64/libpthread.so.0 (0x00007f4226080000)
libc.so.6 => /lib64/libc.so.6 (0x00007f4225cbf000)
/lib64/ld-linux-x86-64.so.2 (0x00007f4251340000)
libdl.so.2 => /lib64/libdl.so.2 (0x00007f4225abb000)
libgfortran.so.3 => /lib64/libgfortran.so.3 (0x00007f4225798000)
libz.so.1 => /lib64/libz.so.1 (0x00007f4225582000)
libjpeg.so.62 => /lib64/libjpeg.so.62 (0x00007f422532c000)
libpng15.so.15 => /lib64/libpng15.so.15 (0x00007f4225101000)
libtiff.so.5 => /lib64/libtiff.so.5 (0x00007f4224e8d000)
libjasper.so.1 => /lib64/libjasper.so.1 (0x00007f4224c32000)
libImath.so.6 => /lib64/libImath.so.6 (0x00007f4224a20000)
libIlmImf.so.7 => /lib64/libIlmImf.so.7 (0x00007f4224758000)
libIex.so.6 => /lib64/libIex.so.6 (0x00007f4224538000)
libHalf.so.6 => /lib64/libHalf.so.6 (0x00007f42242f5000)
libIlmThread.so.6 => /lib64/libIlmThread.so.6 (0x00007f42240ee000)
libgtk-x11-2.0.so.0 => /lib64/libgtk-x11-2.0.so.0 (0x00007f4223a69000)
libgdk-x11-2.0.so.0 => /lib64/libgdk-x11-2.0.so.0 (0x00007f42237a8000)
libatk-1.0.so.0 => /lib64/libatk-1.0.so.0 (0x00007f4223582000)
libgio-2.0.so.0 => /lib64/libgio-2.0.so.0 (0x00007f422320b000)
libpangoft2-1.0.so.0 => /lib64/libpangoft2-1.0.so.0 (0x00007f4222ff6000)
libpangocairo-1.0.so.0 => /lib64/libpangocairo-1.0.so.0 (0x00007f4222de9000)
libgdk_pixbuf-2.0.so.0 => /lib64/libgdk_pixbuf-2.0.so.0 (0x00007f4222bc2000)
libcairo.so.2 => /lib64/libcairo.so.2 (0x00007f422289b000)
libpango-1.0.so.0 => /lib64/libpango-1.0.so.0 (0x00007f4222650000)
libfontconfig.so.1 => /lib64/libfontconfig.so.1 (0x00007f4222413000)
libgobject-2.0.so.0 => /lib64/libgobject-2.0.so.0 (0x00007f42221c3000)
libglib-2.0.so.0 => /lib64/libglib-2.0.so.0 (0x00007f4221e8c000)
libfreetype.so.6 => /lib64/libfreetype.so.6 (0x00007f4221be5000)
libgthread-2.0.so.0 => /lib64/libgthread-2.0.so.0 (0x00007f42219e3000)
libgstbase-0.10.so.0 => /lib64/libgstbase-0.10.so.0 (0x00007f422178f000)
libgstreamer-0.10.so.0 => /lib64/libgstreamer-0.10.so.0 (0x00007f42214a5000)
libgmodule-2.0.so.0 => /lib64/libgmodule-2.0.so.0 (0x00007f42212a1000)
libxml2.so.2 => /lib64/libxml2.so.2 (0x00007f4220f37000)
libgstapp-0.10.so.0 => /lib64/libgstapp-0.10.so.0 (0x00007f4220d2a000)
libgstvideo-0.10.so.0 => /lib64/libgstvideo-0.10.so.0 (0x00007f4220b0d000)
libv4l1.so.0 => /lib64/libv4l1.so.0 (0x00007f4220907000)
libnvidia-fatbinaryloader.so.390.25 => /lib64/libnvidia-fatbinaryloader.so.390.25 (0x00007f42206ba000)
libquadmath.so.0 => /lib64/libquadmath.so.0 (0x00007f422047d000)
libjbig.so.2.0 => /lib64/libjbig.so.2.0 (0x00007f4220271000)
libIexMath.so.6 => /lib64/libIexMath.so.6 (0x00007f422006b000)
libX11.so.6 => /lib64/libX11.so.6 (0x00007f421fd2d000)
libXfixes.so.3 => /lib64/libXfixes.so.3 (0x00007f421fb26000)
libXrender.so.1 => /lib64/libXrender.so.1 (0x00007f421f91c000)
libXinerama.so.1 => /lib64/libXinerama.so.1 (0x00007f421f719000)
libXi.so.6 => /lib64/libXi.so.6 (0x00007f421f508000)
libXrandr.so.2 => /lib64/libXrandr.so.2 (0x00007f421f2fe000)
libXcursor.so.1 => /lib64/libXcursor.so.1 (0x00007f421f0f3000)
libXcomposite.so.1 => /lib64/libXcomposite.so.1 (0x00007f421eeef000)
libXdamage.so.1 => /lib64/libXdamage.so.1 (0x00007f421ecec000)
libXext.so.6 => /lib64/libXext.so.6 (0x00007f421eada000)
libffi.so.6 => /lib64/libffi.so.6 (0x00007f421e8d1000)
libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f421e6ac000)
libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f421e492000)
libharfbuzz.so.0 => /lib64/libharfbuzz.so.0 (0x00007f421e238000)
libpixman-1.so.0 => /lib64/libpixman-1.so.0 (0x00007f421df8b000)
libEGL.so.1 => /lib64/libEGL.so.1 (0x00007f421dd60000)
libxcb-shm.so.0 => /lib64/libxcb-shm.so.0 (0x00007f421db5c000)
libxcb-render.so.0 => /lib64/libxcb-render.so.0 (0x00007f421d952000)
libxcb.so.1 => /lib64/libxcb.so.1 (0x00007f421d72f000)
libGL.so.1 => /lib64/libGL.so.1 (0x00007f421d4bf000)
libexpat.so.1 => /lib64/libexpat.so.1 (0x00007f421d295000)
liblzma.so.5 => /lib64/liblzma.so.5 (0x00007f421d06f000)
liborc-0.4.so.0 => /lib64/liborc-0.4.so.0 (0x00007f421cded000)
libv4l2.so.0 => /lib64/libv4l2.so.0 (0x00007f421cbde000)
libpcre.so.1 => /lib64/libpcre.so.1 (0x00007f421c97d000)
libgraphite2.so.3 => /lib64/libgraphite2.so.3 (0x00007f421c760000)
libX11-xcb.so.1 => /lib64/libX11-xcb.so.1 (0x00007f421c55e000)
libxcb-dri2.so.0 => /lib64/libxcb-dri2.so.0 (0x00007f421c359000)
libxcb-dri3.so.0 => /lib64/libxcb-dri3.so.0 (0x00007f421c155000)
libxcb-present.so.0 => /lib64/libxcb-present.so.0 (0x00007f421bf52000)
libxcb-randr.so.0 => /lib64/libxcb-randr.so.0 (0x00007f421bd44000)
libxcb-xfixes.so.0 => /lib64/libxcb-xfixes.so.0 (0x00007f421bb3b000)
libxcb-shape.so.0 => /lib64/libxcb-shape.so.0 (0x00007f421b937000)
libxcb-sync.so.1 => /lib64/libxcb-sync.so.1 (0x00007f421b730000)
libxshmfence.so.1 => /lib64/libxshmfence.so.1 (0x00007f421b52c000)
libgbm.so.1 => /lib64/libgbm.so.1 (0x00007f421b320000)
libdrm.so.2 => /lib64/libdrm.so.2 (0x00007f421b111000)
libXau.so.6 => /lib64/libXau.so.6 (0x00007f421af0c000)
libglapi.so.0 => /lib64/libglapi.so.0 (0x00007f421acdd000)
libxcb-glx.so.0 => /lib64/libxcb-glx.so.0 (0x00007f421aac3000)
libXxf86vm.so.1 => /lib64/libXxf86vm.so.1 (0x00007f421a8bc000)
libv4lconvert.so.0 => /lib64/libv4lconvert.so.0 (0x00007f421a643000)

================================================================
报错信息如下
[10:43:23] src/nnvm/legacy_json_util.cc:209: Loading symbol saved by previous version v1.2.0. Attempting to upgrade...
[10:43:23] src/nnvm/legacy_json_util.cc:217: Symbol successfully upgraded!
panic: [10:43:23] src/storage/storage.cc:137: Compile with USE_CUDA=1 to enable GPU usage

Stack trace returned 10 entries:
[bt] (0) /lib/libmxnet.so(dmlc::StackTrace()+0x4a) [0x7f2b9d77b26a]
[bt] (1) /lib/libmxnet.so(dmlc::LogMessageFatal::~LogMessageFatal()+0x21) [0x7f2b9d77b911]
[bt] (2) /lib/libmxnet.so(+0x38ee782) [0x7f2ba0390782]
[bt] (3) /lib/libmxnet.so(+0x38f0729) [0x7f2ba0392729]
[bt] (4) /lib/libmxnet.so(mxnet::StorageImpl::Alloc(mxnet::Storage::Handle*)+0x3f) [0x7f2ba03930ef]
[bt] (5) /lib/libmxnet.so(+0x3925610) [0x7f2ba03c7610]
[bt] (6) /lib/libmxnet.so(MXPredCreatePartialOut+0x1ed2) [0x7f2ba03cdb32]
[bt] (7) /lib/libmxnet.so(MXPredCreate+0x41) [0x7f2ba03cee71]
[bt] (8) ./predict(_cgo_4c298109471a_C2func_MXPredCreate+0x60) [0x4bc070]
[bt] (9) ./predict() [0x450720]

goroutine 1 [running]:
main.main()
/root/mygo/src/github.com/songtianyi/go-mxnet-predictor/examples/flowers/predict.go:63 +0x71e

Can't create predictor on GPU device

I've been trying to create predictors using the GPU, but can't seem to make it work. For example, using the provided test script (tests/pr3/test.go), the go script compiles and runs just fine as-is. However, if I change line 37:

mxnet.Device{mxnet.CPU_DEVICE, 0},

to:

mxnet.Device{mxnet.GPU_DEVICE, 0},

the code will compile, but fail to create the predictor, with the following error:

~/golang/src/github.com/songtianyi/go-mxnet-predictor/tests/pr3$ ./test
[14:13:20] src/nnvm/legacy_json_util.cc:209: Loading symbol saved by previous version v0.9.4. Attempting to upgrade...
[14:13:20] src/nnvm/legacy_json_util.cc:217: Symbol successfully upgraded!
panic: file exists
panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x1 addr=0x0 pc=0x491de2]

goroutine 1 [running]:
github.com/songtianyi/go-mxnet-predictor/mxnet.(*Predictor).Free(0x0, 0xc42009e000, 0xc8)
/home/bruno/golang/src/github.com/songtianyi/go-mxnet-predictor/mxnet/predictor.go:283 +0x22
panic(0x4ad4a0, 0xc4200180d0)
/usr/local/go/src/runtime/panic.go:502 +0x229
main.main()
/home/bruno/golang/src/github.com/songtianyi/go-mxnet-predictor/tests/pr3/test.go:46 +0x29c

This is with a CUDA enabled build of mxnet 1.2.0 and a K80 GPU. The mxnet examples (both python and C++) work fine and can use the GPU.

I presume there's something obvious I'm missing but I'm not too familiar with mxnet itself, so you might have a better insight. I can see that the mxnet.GPU_DEVICE effectively becomes the int value 2 through the enum, and that seems to be the value corresponding to the GPU device type in mxnet indeed, and GPU 0 is the only one available on the machine I'm using.

GPU device returns error: file exists

code like this:

	p, err := mxnet.CreatePredictor(symbol,
		params,
		mxnet.Device{Type: mxnet.GPU_DEVICE, Id: 0},
		[]mxnet.InputNode{{Key: m.inputKey, Shape: m.inputShape}},
	)
	if err != nil {
		return err // error occurs here
        }

Is GUP not supported in this bindings?

[Memory Layout] How to do batch prediction?

When I use the input shape []uint32{3, 3, 224, 224} to create the batch mode Predictor, the GetOutput(0) would return an []float32 array of the shape: [3, 4096], but the result not does not match non batch mode result. Can you help me with:

  1. How to get the real output [][]float32 array from the flatten array []float32 GetOutput(0) ?
  2. How to feed the batch image into SetInput(key string, data []float32) ?

My current solution which does not work is:

  1. get the first result[0:4096] slice, then result[4096:8192], then result[8192:len(result)] from the flatten array result := GetOutput(0)
  2. make a intput []float32 of the size 3*3*224*224, then feed the first 3*244*244 slice with CvtImageTo1DArray(img1), and then the CvtImageTo1DArray(img2), etc

Can you help to do batch predictions correctly?

thanks in advance for your help

fatal error: mxnet/c_predict_api.h: No such file or directory while building the Golang app

I need to make predictions on a pre-trained MXNet model in Golnag. I try to use your library for making the predictions, however, while building the application I get the following error:


 # github.com/songtianyi/go-mxnet-predictor/mxnet  
 ../../songtianyi/go-mxnet-predictor/mxnet/error.go:20:33: fatal error: mxnet/c_predict_api.h: No such file or directory
compilation terminated. 

How can this be resolved?

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.