diff --git a/README.md b/README.md index b0bc619..9ed878b 100644 --- a/README.md +++ b/README.md @@ -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) + + +