GithubHelp home page GithubHelp logo

mistshi / go-mxnet-predictor Goto Github PK

View Code? Open in Web Editor NEW

This project forked from songtianyi/go-mxnet-predictor

0.0 2.0 0.0 1.73 MB

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

License: Apache License 2.0

Go 95.31% Shell 4.69%

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 nearly as raw as original C api, wish further development for higher level APIs.

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 && git checkout v0.8.0
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.

2.4 Run example
	./predict

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

songtianyi avatar

Watchers

 avatar  avatar

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.