-
Notifications
You must be signed in to change notification settings - Fork 1
/
split_server.py
56 lines (50 loc) · 1.48 KB
/
split_server.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from SimpleXMLRPCServer import SimpleXMLRPCServer
from caffe.proto import caffe_pb2
import google.protobuf.text_format
import xmlrpclib
import caffe
import os
import numpy as np
import time
net = None
input_name = None
log = open("test.log", "w")
def load(layer_id):
global net, input_name
fname = "split/origin.7.%d.Model.prototxt" % layer_id
mname = "split/origin.7.%d.Model.caffemodel" % layer_id
param = caffe_pb2.NetParameter()
if(not os.path.exists(fname)):
return False
with open("split/origin.7.%d.Model.prototxt" % layer_id) as f:
google.protobuf.text_format.Merge(f.read(), param)
log.write("load %s\n" % layer_id)
input_name = str(param.input[0])
net = caffe.Net(fname, mname)
caffe.set_phase_test()
caffe.set_mode_gpu()
return True
def predict(data):
if net == None:
return -1
beg = time.time()
import cStringIO
inputdata = cStringIO.StringIO(data.data)
bn = np.load(inputdata)
mid = time.time()
#print(input_name)
#print(bn)
out2 = net.forward_all(**{input_name:bn})
i = int(out2['prob'][0].argmax(axis=0)[0][0])
end = time.time()
log.write("%f %f\n" % ( (mid-beg)*1000, (end-mid)*1000))
return i
def check():
print(net)
return True
server = SimpleXMLRPCServer(("", 8000))
print "Listening on port 8000..."
server.register_function(load, 'load')
server.register_function(predict, 'predict')
server.register_function(check, 'check')
server.serve_forever()