-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathserver.py
101 lines (87 loc) · 3.1 KB
/
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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
from SimpleXMLRPCServer import SimpleXMLRPCServer
import xmlrpclib
import caffe
caffe_dir = "../caffe"
with open("synset_words.txt") as f:
words = f.readlines()
words = map(lambda x: x.strip(), words)
clients = {}
shared_net = None
import numpy as np
import uuid
import pickle
def load_digests(filename):
with open(filename) as f:
return pickle.load(f)
def check_shared(digests):
shared = []
digests = load_digests(digests)
for layer_name in shared_net[0]._layer_names:
if(layer_name not in digests.keys()):
shared.append(layer_name)
continue
if(shared_net[1][layer_name] == digests[layer_name]):
shared.append(layer_name)
else:
break
return shared
def register(model_file, pretrained, digests, split=False):
global shared_net
if(shared_net != None):
print(check_shared(digests))
return "tttt"
else:
print("new shared")
net = caffe.Classifier(model_file, pretrained,
mean=np.load(caffe_dir + '/python/caffe/imagenet/ilsvrc_2012_mean.npy'),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
net.set_phase_test()
net.set_mode_gpu()
clientID = str(uuid.uuid1())
clients[clientID] = (net, split)
if(shared_net == None):
shared_net = (net, load_digests(digests))
return clientID
def change(clientID, model_file, pretrained, split):
net = caffe.Classifier(model_file, pretrained,
mean=np.load(caffe_dir + '/python/caffe/imagenet/ilsvrc_2012_mean.npy'),
channel_swap=(2,1,0),
raw_scale=255,
image_dims=(256, 256))
net.set_phase_test()
net.set_mode_gpu()
clients[clientID] = (net, split)
return clientID
def predict(clientID, image_file):
input_image = caffe.io.load_image(image_file)
net = clients[clientID][0]
split = clients[clientID][1]
prediction = net.forward_all(data=np.asarray([net.preprocess('data', input_image)]))
if(split):
import cStringIO
output = cStringIO.StringIO()
np.save(output, prediction["pool5"])
proxy = xmlrpclib.ServerProxy("http://localhost:8001/")
r = proxy.predict(xmlrpclib.Binary(output.getvalue()))
return r
else:
i = prediction["prob"].argmax()
return words[i]
def unregister(clientID):
global shared_net
del clients[clientID]
if(len(clients) == 0):
shared_net = None
return True
#MODEL_FILE = caffe_dir + "/models/bvlc_reference_caffenet/deploy.prototxt"
#PRETRAINED = caffe_dir + "/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"
#IMAGE_FILE = "images/cat.png"
server = SimpleXMLRPCServer(("localhost", 8000))
print "Listening on port 8000..."
server.register_function(register, 'register')
server.register_function(change, 'change')
server.register_function(predict, 'predict')
server.register_function(unregister, 'unregister')
server.serve_forever()