Coder Social home page Coder Social logo

patwie / tf_zmq Goto Github PK

View Code? Open in Web Editor NEW
35.0 5.0 10.0 24 KB

TensorFlow operation for reading data from sockets (lightweight c++)

License: Apache License 2.0

Shell 7.03% C++ 86.27% Python 6.71%
tensorflow tensorflow-op cpp11 deep-learning data-reader tensorflow-operation tensorflow-ops

tf_zmq's Introduction

ZeroMQ for TensorFlow

Build Status

This library contains methods to send data from your C++ application directly into the TensorFlow graph without any large dependency. Unfortunately, generating data within a C++ application for TensorFlow is not envisaged and communicated as not requested for the TF core. Even the discussion about the redesign of the input pipeline seems to converge to put all data-processing into the graph.

This is hilarious as most solutions for interesting problems are not based on a mindless iteration over a set of images. This small library helps to send data directly from your favorite C++ application (game-engine, renderer, emulator, [put here what you want]) to TensorFlow. Reading data is as simple as (see read_tf.py):

import tensorflow as tf
import zmq_op

image, label = zmq_op.pull('ipc:///tmp/ipc-socket-0', [tf.float32, tf.int32])
with tf.Session() as sess:
    print(sess.run([image, label]))

The C++ code to send data is about 45 lines in write.cpp. It uses ZMQ for an easy distributed messaging and msgPack for a fast serialization. You are free to change the shape of each tensor over time. If you want to receive the tensors in native python without TensorFlow dependencies, you can use read_py.py. This library supports all combinations:

sender receiver
write.py TF (read_tf.py)
write.cpp TF (read_tf.py)
write.py native python (read_py.py)
write.cpp native python (read_py.py)
write.py plain C++ (read.cpp)
write.cpp plain C++ (read.cpp)

Install dependencies

You basically need two dependencies (see .travis.yml).

# compile ZMQ library for c++
cd /path/to/your_lib_folder
git clone https://github.com/zeromq/libzmq
cd libzmq
./autogen.sh
./configure
./configure --prefix=/path/to/your_lib_folder/libzmq/dist
make
make install
# compile MSGPACK library for c++
cd ../
git clone https://github.com/msgpack/msgpack-c
cd msgpack-c
mkdir build
cd build
cmake .. -DCMAKE_INSTALL_PREFIX:PATH=/path/to/your_lib_folder/msgpack-c/dist
make
make install

Compile TensorFlow op and example

export PKG_CONFIG_PATH=/path/to/your_lib_folder/libzmq/dist/lib/pkgconfig/:$PKG_CONFIG_PATH
export PKG_CONFIG_PATH=/path/to/your_lib_folder/msgpack-c/dist/lib/pkgconfig/:$PKG_CONFIG_PATH
git clone https://github.com/PatWie/tf_zmq.git
cd tf_zmq
./compile.sh

related projects:

There are 2 alternative attempts for solving this problem:

  • Tensorpack: This solution requires a python script to send data (it is based on TensorProto protobuf (currently, symbols for this are not exported by TF))
  • pull-request TF: Here you need to specify the shape dimensions in advance (also pure-python because the TensorProto dependency)

You probably do not want to link a game engine or another large codebase with the TensorFlow core using bazel, given compiling TensorFlow alone can be a nightmare and is only recommended for adventurer.

tf_zmq's People

Contributors

patwie 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

Watchers

 avatar  avatar  avatar  avatar  avatar

tf_zmq's Issues

some systems have an unicode error

A dump reveals that when sending from C++ the message is

buf: 92 93 01 92 04 04 c4 40 00 00 00 00 00 00 80 3f 00 00 00 40 00 00 40 40 00 00 80 40 00 00 a0 40 00 00 c0 40 00 00 e0 40 00 00 00 41 00 00 10 41 00 00 20 41 00 00 30 41 00 00 40 41 00 00 50 41 00 00 60 41 00 00 70 41 93 00 91 01 c4 04 2a 00 00 00 
buf: 0092 0093 0001 0092 0004 0004 00c4 0040 0000 0000 0000 0000 0000 0000 0080 003f 0000 0000 0000 0040 0000 0000 0040 0040 0000 0000 0080 0040 0000 0000 00a0 0040 0000 0000 00c0 0040 0000 0000 00e0 0040 0000 0000 0000 0041 0000 0000 0010 0041 0000 0000 0020 0041 0000 0000 0030 0041 0000 0000 0040 0041 0000 0000 0050 0041 0000 0000 0060 0041 0000 0000 0070 0041 0093 0000 0091 0001 00c4 0004 002a 0000 0000 0000 
obj: [[1, [4, 4], "�?@@@�@�@�@�@A�A A0A@APA`ApA"], [0, [1], "*"]]

while python uses (unicode)

buf: 92 93 01 92 04 04 da 00 40 00 00 00 00 00 00 80 40 00 00 00 41 00 00 40 41 00 00 80 3f 00 00 a0 40 00 00 10 41 00 00 50 41 00 00 00 40 00 00 c0 40 00 00 20 41 00 00 60 41 00 00 40 40 00 00 e0 40 00 00 30 41 00 00 70 41 93 00 91 01 a4 00 00 30 42 
buf: 0092 0093 0001 0092 0004 0004 00da 0000 0040 0000 0000 0000 0000 0000 0000 0080 0040 0000 0000 0000 0041 0000 0000 0040 0041 0000 0000 0080 003f 0000 0000 00a0 0040 0000 0000 0010 0041 0000 0000 0050 0041 0000 0000 0000 0040 0000 0000 00c0 0040 0000 0000 0020 0041 0000 0000 0060 0041 0000 0000 0040 0040 0000 0000 00e0 0040 0000 0000 0030 0041 0000 0000 0070 0041 0093 0000 0091 0001 00a4 0000 0000 0030 0042 
obj: [[1, [4, 4], "\u0000\u0000\u0000\u0000\u0000\u0000�@\u0000\u0000\u0000A\u0000\u0000@A\u0000\u0000�?\u0000\u0000�@\u0000\u0000\u0010A\u0000\u0000PA\u0000\u0000\u0000@\u0000\u0000�@\u0000\u0000 A\u0000\u0000`A\u0000\u0000@@\u0000\u0000�@\u0000\u00000A\u0000\u0000pA"], [0, [1], "\u0000\u00000B"]]

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.