Java calling python function with tensorflow graph - java

So I have a neural network in tensorflow (python2.7) and I need to retrieve its output using Java. I have a simple python function getValue(input) which starts the session and retrieves the value. I am open to any suggestions. I believe Jython wont work cause tensorflow is not in the library. I need the call to be as fast as possible. JNI exists for Java calling C so can I convert with cython and compile then use JNI? Is there a way to pass the information in RAM or some other way I haven't thought of?

In Python, save the model (using saver.save) and the graph (using tf.train.write_graph).
In Java, use the org.bytedeco.javacpp-presets library to instantiate a GraphDef from the saved protobuf file and pass in your input features and get the output features within a Session.
See https://medium.com/google-cloud/how-to-invoke-a-trained-tensorflow-model-from-java-programs-27ed5f4f502d#.4su1s26fz for example code.

I've had the same problem, Java+Python+TensorFlow. I've ended up setting up a simple http server. If that's too slow for you, you can shave off some overhead by employing sockets directly.

Encapsulate your calling for TensorFlow into a script.py and then:
Process proc = Runtime.getRuntime().exec("python script.py");
Not sure whether it solves your case.

Related

Thread-safe use of GraphViz library via JNI

I am trying to use GraphViz 2.40.1 under Linux in a REST service that lays out graphs. The service is a Java Spring Boot application. My current approach is to load shared libraries into my JVM, and call native code via JNI, using functions from libraries cgraph and gvc. I'd like to do everything in-memory and avoid file I/O. But I have read the following caveat in the GraphViz lib guide, printed in bold face at the end of section 1: "N.B. Using Graphviz as a library is not thread-safe."
I am seeking help about the consequences I should draw from that statement. No details are given. For example, I can imagine that functions that keep mutable state about errors that have occurred in graph parsing are not thread-safe, but I'm not using these. I only use the following functions: agmemread and agclose from cgraph, and gvContext, gvParseArgs, gvLayout, gvRenderData, gvFreeRenderData, gvFreeLayout, gvFreeContext from gvc. I am caching nothing in Java, local vars and method params only. Would such a use of the library be thread-safe?
If not, does the non-thread-safety only affect uses of single functions, not across functions? So would it be enough to make my Java native methods static synchronized? Or would I have to synchronize on every REST request?
Alternatively, I could fork a new OS process for each request and do file operations with GraphViz's dot program with Runtime.exec().
What approach would scale best?

Java <-> Python: share objects

I have an embedded system using a python interface. Currently the system is using a (system-local) XML-file to persist data in case the system gets turned off. But normally the system is running the entire time. When the system starts, the XML-file is read in and information is stored in python-objects. The information then is used for processing. My aim is to edit this information remotely (over TCP/IP) even during process. I would like to use JAVA to get this done, and i have been thinking about something to share the objects. The problem is, that I'm missing some keywords to find the right technologies to get this done. What i found is SOAP, but i think it is not the right thing for this case, is that true? I'm grateful for any tips.
As I understand, you are using XML file to store start up configuration
And my assumptions on your interface between Java & Python apps
You want your Java application to retrieve objects over Python interface
And process them locally and send it back to Python interface to reload config ?
So, depending on your circumstances, you can workout something with the following
Jython
Pickle (if you have no restriction on startup config file format or can afford to do conversion)
https://pypi.python.org/pypi/Pyro4
Also you can get some ideas from here:
Sharing a complex object between Python processes?
You should ask your python application to open a XML-RPC socket which clients can connect on. This could let an outside application to execute an endpoint, which would manipulate your python object values in someway. There are several good choices for Java XML-RPC libraries, including the amazing org.apache.xmlrpc library.

How to retrieve a variable of php file with using java?

I am triying to get a variable from php file with using java code. How can I do that?
Thank u all...
I don't think you can do that (at least not easy).
You would have to call your java program from within your php program and supply all parameters you got from the database to the java program. Using a transfer language like SOAP could do that.
If it is possible in your environment I strongly suggest you quit using them both and instead settle on one of this two languages instead. Java has fine database support and most of the things you can do in java can be done in php as well.
If that’s not possible, using SOAP or (depending on the situation) HTTP Servlet calls or a command line call will give you the ability to transfer parameters from php to java and visa-verse.
Assuming you are talking about JavaScript instead of Java, the best way to do this is using AJAX. You need two pieces of code for this:
One piece in PHP that will print the variable (if it's a complex variable, a dataformat like JSON or XML is recommended)
One piece in Javascript that will call the .php script that prints the variable, and puts the variable into javascript (but don't use eval() for this).
Read more about AJAX and PHP at the w3schools intro or the more advanced example.

Java Input/Output streams for unnamed pipes created in native code?

Is there a way to easily create Java Input/Output streams for unnamed pipes created in native code?
Motivation: I need my own implementation of the Process class. The native code spawns me a new process with subprocess' IO streams redirected to unnamed pipes.
Problem: The file descriptors for correct ends of those pipes make their way to Java. At this point I get stuck as I cannot create a new FileDescriptor which I could pass to FileInput/FileOutput stream.
I have used reflection to get around the problem and got communication with a simple bouncer sub-process running. However I have a notion that it is not the cleanest way to go. Have you used this approach? Do you see any problems with this approach? (the platform will never change)
Searching around the internets revealed similar solution using native code.
Any thoughts before I dive into heavy testing of this approach are very welcome. I would like to give a shot to existing code before writing my own IO stream implementations...
Thank you.
I have run into this before as well. The only way I know to create FileDescriptor objects is using reflection (or from JNI) to set the private "int" field inside the FileDescriptor class.
But this works fine and is surely how it's done elsewhere in the standard library, so I believe it's as legitimate as you could expect.

How should I call a Perl Script in Java?

I read Runtime.getRuntime().exec("perl script.pl") is an option, but is this the best way to do it?
I'll need an answer from that script, so I'll have to read the script's return in some cases, although I might read it from a text file on other cases.
Anyway, is exec() a good way of calling a Perl Script from Java? I should note, I'm working on a Java Web Application, so security is an issue here.
You can use Runtime.getRuntime().exec() or use the Process API.
The Process API allows you to get the output of the script, so you can have both communicate.
exitValue() and getInputStream() seems to be what you need.
This outlines how to do it fairly elegantly, though it may be more effort than it's worth:
http://search.cpan.org/~nwclark/perl-5.8.9/jpl/docs/Tutorial.pod
Overview:
Well-supported by JPL, but it is a complicated process:
The JPL preprocessor parses the .jpl file and generates C code wrappers for Perl methods. It also generates Java and Perl source files.
The C compiler compiles the wrapper and links it to the libPerlInterpreter.so shared library, producing a shared library for the wrapper.
The Java compiler compiles the Java source file, which uses native methods to load the wrapper.
The wrapper connects the Java code to the Perl code in the Perl source file.
Fortunately, a generic Makefile.PL simplifies the process. This is a Perl script that generates a Makefile for you.
exec() is likely the best option and you can catch it's return value with exitValue(). You might also be interested in Inline::Java.
-John
keep in mind, whatever file the Perl script create, it is created in the Java working folder. just refer to that file as './myPerlCreatedFile.ext'

Categories