Java Web App calls Python's model of deep learning training

Source: Internet
Author: User
Tags sendmsg java web

  Prior to the China Software Cup competition, we used the relevant algorithms of deep learning in the contest, and also trained some simple models. The project on-line platform is a Web application written in Java, and deep learning uses the Python language, which involves the method of invoking the Python language in Java code.

In order to be able to use the Python language training algorithm model in the java application, I searched the net for a long time. I've probably found three ways.

1. Java code can invoke Python code directly, only need to download the corresponding jar package on the line. I didn't try it this way, just to think that doing so makes Java apps too dependent on Python's environment. There are also online ways to package Python code into jars, which can then be called by Java code, but many third-party libraries cannot be packaged as jar packages.

2. Save the model parameters of the Python training to the text and reproduce the model's predictive algorithm with Java code. I've done this before. This is obviously a lot of work, and the odds of a bug increase dramatically. The most important thing is that there is no way to use the framework of deep learning.

3. Use the Python process to run a trained model in deep learning and invoke the services provided by the Python process in a Java application. This method I think is the best. The Python language program is the most efficient execution in a python environment. and Python apps and Java apps can run on different servers and call through remote access to the process.

Here is the Python code portion of my Java application that accesses the Python process. The process can only communicate through the socket. I had intended to write a Web application in Python, to provide HTTP services to Java, and later felt that this also requires a Web server, the environment is too large, and the communication between the two processes is very simple, so simply call with the socket directly

ImportSocketImportSYSImportThreadingImportJSONImportNumPy as NP fromTagImporttrain2#nn=network.getnetwork ()#cnn = Conv.main (False)#Deep learning training for neural networks, using TensorFlow trained neural network models, saved in FilesNnservice = train2. Nnservice (model='model/20180731.ckpt-1000')defMain ():#Create a server socketServerSocket =Socket.socket (Socket.af_inet,socket. SOCK_STREAM)#get the local host nameHost =Socket.gethostname ()#set up a portPort = 12345#binding sockets to local hosts and portsServersocket.bind ((host,port))#Setting the maximum number of listening connectionsServersocket.listen (5)    #Get connection information for the local serverMYADDR =Serversocket.getsockname ()Print("server address:%s"%str (MYADDR))#Loop waiting for client information to be accepted     whileTrue:#get a Client connectionCLIENTSOCKET,ADDR =serversocket.accept ()Print("Connection address:%s"%str (addr))Try: T= Serverthreading (Clientsocket)#open a processing thread for each requestT.start ()Pass        exceptException as identifier:Print(identifier)Pass        Passserversocket.close ()Passclassserverthreading (Threading. Thread):#words = Text2vec.load_lexicon ()    def __init__(self,clientsocket,recvsize=1024*1024,encoding="Utf-8"): Threading. Thread.__init__(self) self._socket=Clientsocket self._recvsize=recvsize self._encoding=encodingPass    defRun (self):Print("Open Thread .....")        Try:            #Accept Datamsg ="'             whileTrue:#Read Recvsize bytesREC =self._socket.recv (self._recvsize)#decodingmsg + =Rec.decode (self._encoding)#text acceptance is complete, because the Python socket cannot determine whether the received data is complete,                #so you need to customize the protocol flag data acceptance is complete                ifMsg.strip (). EndsWith (' Over'): Msg=msg[:-4]                     Break            #parsing data in JSON formatRe =json.loads (msg)#Call the neural network model to process the requestres = Nnservice.hand (re['content']) sendmsg=Json.dumps (RES)#Send DataSelf._socket.send (("%s"%sendmsg). Encode (self._encoding))Pass        exceptException as Identifier:self._socket.send (" -". Encode (self._encoding))Print(identifier)Pass        finally: Self._socket.close ()Print("end of Task .....")                Pass    def __del__(self):Passif __name__=="__main__": Main ()

Code to access the Python process in Java code:

    PrivateObject Remotecall (String content) {Jsonobject Jsonobject=NewJsonobject (); Jsonobject.put ("Content", content); String Str=jsonobject.tojsonstring (); //access the socket for the service processSocket socket =NULL; List<Question> questions =NewArraylist<>(); Log.info ("Call Remote interface:host=>" +host+ ",port=>" +PORT); Try {            //initializes the socket, sets the host and process port number of the access service, hosts the host name that accesses the Python process, either an IP address or a domain name, and port is the port number that the Python process binds toSocket =NewSocket (Host,port); //get output Stream objectOutputStream OS =Socket.getoutputstream (); PrintStream out=Newprintstream (OS); //Send contentout.print (str); //tell the service process that the content is sent and ready to processOut.print ("Over"); //gets the input stream of the service processInputStream is =Socket.getinputstream (); BufferedReader BR=NewBufferedReader (NewInputStreamReader (IS, "Utf-8")); String tmp=NULL; StringBuilder SB=NewStringBuilder (); //Read Content             while((Tmp=br.readline ())! =NULL) sb.append (TMP). Append (' \ n '); //parsing ResultsJsonarray res =Json.parsearray (sb.tostring ()); returnRes; } Catch(IOException e) {e.printstacktrace (); } finally {            Try{if(socket!=NULL) Socket.close ();}Catch(IOException e) {} log.info ("The remote interface call ends."); }        return NULL; }

Java Web App calls Python's model of deep learning training

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.