Skip to content

Commit

Permalink
basic python api
Browse files Browse the repository at this point in the history
  • Loading branch information
getnamo authored May 18, 2017
1 parent 40b3084 commit 4b2c88d
Showing 1 changed file with 111 additions and 0 deletions.
111 changes: 111 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,119 @@ Project examples found under [tensorflow-ue4-examples](https://github.com/getnam

It is also the main repository where all development is tracked for all plugin dependencies for tensorflow.

## Python API

You can either train directly or use a trained model inside UE4.

To start, add your python script file to _{Project Root Folder}/Content/Scripts_.

wrap your tensorflow python code inside this API:


#### MySubClass(TFPluginAPI)

import the following and subclass in your module file

```python
import tensorflow as tf
import unreal_engine as ue
from TFPluginAPI import TFPluginAPI
```

Add the following class functions

```python
class ExampleAPI(TFPluginAPI):

#expected api: setup your model for training and any member storage info
def setup(self):
pass
#expected api: using the stored session and class data, evaluate the json inputs
def runJsonInput(self, jsonInput):
result = {}
return result

#expected api: start training your network
def train(self):
pass

#NOTE: this is a module function, not a class function. Change your CLASSNAME to reflect your class
#required function to get our api
def getApi():
#return CLASSNAME.getInstance()
return ExampleAPI.getInstance()
```

Note the ```getApi()``` module function which needs to return a matching instance of your defined class. The rest of the functionality depends on what API you wish to use for your use case. At this stage all input/output from UE4 is JSON encoded.

If you have a trained model, simply setup your model/load it from memory and omit the training function.

If you wish to train ensure you check for ```self.shouldstop``` after each batch/epoch to handle early exit requests from the user.

A slightly more expanded example api:

```python
class ExampleAPI(TFPluginAPI):

#expected api: setup your model for training
def setup(self):
#setup or load your model and pass it into stored

#Usually store session, graph, and model if using keras
self.sess = tf.InteractiveSession()
self.graph = tf.get_default_graph()

#expected api: storedModel and session, json inputs
def runJsonInput(self, jsonInput):
#e.g. our json input could be a pixel array
#pixelarray = jsonInput['pixels']

#run input on your graph
#e.g. sess.run(model['y'], feed_dict)
# where y is your result graph and feed_dict is {x:[input]}

#...

#return a json you will parse e.g. a prediction
result = {}
result['prediction'] = -1

return result

#expected api: no params forwarded for training? TBC
def train(self):
#train here

#...

#inside your training loop check if we should stop early
#if(this.shouldstop):
# break
pass

#required function to get our api
def getApi():
#return CLASSNAME.getInstance()
return MnistSimple.getInstance()

```

a full example can be seen here: https://github.com/getnamo/tensorflow-ue4-examples/blob/master/Content/Scripts/mnistSimple.py

### Load your module from your TensorflowComponent
Select your TensorflowComponent

![select component](http://i.imgur.com/f9Syql1.png)

and change the TensorFlowModule to reflect your _filename_ without .py.

![change module name](http://i.imgur.com/mpzymgd.png)


## [License](https://github.com/getnamo/tensorflow-ue4/blob/master/LICENSE)
Plugin - [MIT](https://opensource.org/licenses/MIT)

TensorFlow and TensorFlow Icon - [Apache 2.0](http://www.apache.org/licenses/LICENSE-2.0)



0 comments on commit 4b2c88d

Please sign in to comment.