TensorFlow GNU Radio block

TensorFlow Block

I now present a GNU Radio block which is capable of loading TensorFlow graphs from a file, enabling data to be passed to the TensorFlow model, while producing PMT messages from the model. PMT (Polymorphic Type) messages can contain a large range of different types of data, I make use of them to represent the outputs of a TensorFlow model.

The aim of last week’s work was to make a generic block capable of allowing users to load their own TensorFlow models with GNU Radio. This block can be utilised for a large range of different types of TensorFlow models, for instance you could load a model which performs Automatic Modulation Classification.

I succeeded in creating a block in Python, which is capable of allowing a user to load a TensorFlow model. I tested the block with a simple XOR ANN, which is essentially an Artificial Neural Network model, which is capable of producing an output which represents the bitwise xor of two inputs. The block is then capable of producing PMT output for the output neurons of the xor model.

You may wonder how this block differs from the excellent gr-tf block [6], with gr-tf there are a number of great examples which make use of TensorFlow, however currently there are no generic blocks in that repository capable of loading a TensorFlow model.

I created two simple functions to load and save a TensorFlow graph [4] based on the code at [5].  With the block I created the names of the input/output tensors are user specified via parameters of the block.

It is also important to make sure that the size of the vector that you pass to the TFModel block, matches the shape of inputs of the model.

XOR model:

I created an xor model [1], which is based on the excellent Stack Overflow answer at [2].

Step 1

In order to create the model using tensor_ann.py, you need to obtain the freeze_graph.py file from [3]. I had to tweak it slightly, by changing:

from tensorflow.python.framework import graph_util


from tensorflow.python.client import graph_util

for my version of TensorFlow, I then renamed it freezegraph.py, which is then imported by the tensor.py file.

Step 2

You can now run the tensor_ann.py program to create the XOR ANN and save a TensorFlow model file.

Step 3

You can now run the tensor_ann_load.py program to load and run the TensorFlow graph we just saved and test it with sample input

Step 4

To install the TFModel block you need to run the following

git clone https://github.com/chrisruk/gr-inspector.git
cd gr-inspector
git checkout dev_amc
mkdir build
cd build
cmake ../
sudo make install
sudo ldconfig

Step 5

You can now open the examples directory in the gr-inspector directory and

gnuradio-companion tensor_xor_test.grc

You can now run the GNU Radio graph


PMT output

The TFModel block produces PMT output, for each of the models output neurons, in this case there is only one neuron for output.

******* MESSAGE DEBUG PRINT ********
((out0 . 0.021576))
******* MESSAGE DEBUG PRINT ********
((out0 . 0.021576))
******* MESSAGE DEBUG PRINT ********
((out0 . 0.985577))
******* MESSAGE DEBUG PRINT ********
((out0 . 0.985577))
******* MESSAGE DEBUG PRINT ********
((out0 . 0.021576))

Saving/Loading a TensorFlow model

The important API calls to load/save a TensorFlow model are outlined below:

from tensor import *
sess, inp, out = load_graph("/tmp/output_graph.pb")
print(sess.run(out,feed_dict={inp: [[0., 1. ]]}))

GNU Radio Block Repository

You can obtain the GNU Radio block from the following repository – gr-inspector/tree/dev_amc

(The dev_amc branch)

To do:

It is now necessary to make the block also work with PMT input, from Sebastian’s signal separator block

I will now work on creating an ANN model for automatic modulation classification using output from the gr-specest FAM function


  1. https://github.com/chrisruk/scf/blob/master/tensor_ann.py
  2. http://stackoverflow.com/questions/33997823/tensorflow-mlp-not-training-xor
  3. https://github.com/tensorflow/tensorflow/tree/master/tensorflow/python/tools
  4. https://github.com/chrisruk/scf/blob/master/tensor.py
  5. https://github.com/tensorflow/tensorflow/blob/master/tensorflow/python/tools/freeze_graph_test.py
  6. https://github.com/osh/gr-tf

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s