Skip to content

Commit bf6bfa9

Browse files
committed
test scripts
1 parent 2b0e56a commit bf6bfa9

File tree

6 files changed

+199
-10
lines changed

6 files changed

+199
-10
lines changed

client.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,10 @@ def __init__(self, proxy, clientID):
1212
def do_predict(self, line):
1313
cls = self.proxy.predict(self.clientID, line.strip())
1414
print(cls)
15+
16+
def do_change(self, line):
17+
cmd = line.strip().split()
18+
res = self.proxy.change(self.clientID, cmd[0], cmd[1], bool(cmd[2]))
1519

1620
def do_EOF(self, line):
1721
return True
@@ -32,7 +36,7 @@ def main():
3236
exit(0)
3337

3438
proxy = xmlrpclib.ServerProxy("http://localhost:8000/")
35-
clientID = proxy.register(args.model, args.pretrained)
39+
clientID = proxy.register(args.model, args.pretrained, False)
3640
dnncmd = DNNCmd(proxy, clientID)
3741
dnncmd.cmdloop()
3842
proxy.unregister(clientID)

server.py

+28-6
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
clients = {}
1313
import numpy as np
1414
import uuid
15-
def register(model_file, pretrained):
15+
def register(model_file, pretrained, split=False):
1616
net = caffe.Classifier(model_file, pretrained,
1717
mean=np.load(caffe_dir + '/python/caffe/imagenet/ilsvrc_2012_mean.npy'),
1818
channel_swap=(2,1,0),
@@ -22,15 +22,36 @@ def register(model_file, pretrained):
2222
net.set_phase_test()
2323
net.set_mode_gpu()
2424
clientID = str(uuid.uuid1())
25-
clients[clientID] = net
25+
clients[clientID] = (net, split)
26+
return clientID
27+
28+
def change(clientID, model_file, pretrained, split):
29+
net = caffe.Classifier(model_file, pretrained,
30+
mean=np.load(caffe_dir + '/python/caffe/imagenet/ilsvrc_2012_mean.npy'),
31+
channel_swap=(2,1,0),
32+
raw_scale=255,
33+
image_dims=(256, 256))
34+
35+
net.set_phase_test()
36+
net.set_mode_gpu()
37+
clients[clientID] = (net, split)
2638
return clientID
2739

2840
def predict(clientID, image_file):
2941
input_image = caffe.io.load_image(image_file)
30-
net = clients[clientID]
31-
prediction = net.predict([input_image])
32-
i = prediction[0].argmax()
33-
return words[i]
42+
net = clients[clientID][0]
43+
split = clients[clientID][1]
44+
prediction = net.forward_all(data=np.asarray([net.preprocess('data', input_image)]))
45+
if(split):
46+
import cStringIO
47+
output = cStringIO.StringIO()
48+
np.save(output, prediction["pool5"])
49+
proxy = xmlrpclib.ServerProxy("http://localhost:8001/")
50+
r = proxy.predict(xmlrpclib.Binary(output.getvalue()))
51+
return r
52+
else:
53+
i = prediction["prob"].argmax()
54+
return words[i]
3455

3556
def unregister(clientID):
3657
del clients[clientID]
@@ -43,6 +64,7 @@ def unregister(clientID):
4364
server = SimpleXMLRPCServer(("localhost", 8000))
4465
print "Listening on port 8000..."
4566
server.register_function(register, 'register')
67+
server.register_function(change, 'change')
4668
server.register_function(predict, 'predict')
4769
server.register_function(unregister, 'unregister')
4870

sp_client.py

+25
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
import caffe
2+
import numpy as np
3+
import xmlrpclib
4+
5+
MODEL_FILE = "./models/caffe1.prototxt"
6+
PRETRAINED = "./models/caffemodel1"
7+
caffe_dir = "../caffe"
8+
IMAGE_FILE = "../cat.jpg"
9+
net1 = caffe.Classifier(MODEL_FILE, PRETRAINED,
10+
mean=np.load(caffe_dir + '/python/caffe/imagenet/ilsvrc_2012_mean.npy'),
11+
channel_swap=(2,1,0),
12+
raw_scale=255,
13+
image_dims=(256, 256))
14+
15+
net1.set_phase_test()
16+
net1.set_mode_gpu()
17+
input_image = caffe.io.load_image(IMAGE_FILE)
18+
out = net1.forward_all(data=np.asarray([net1.preprocess('data', input_image)]))
19+
proxy = xmlrpclib.ServerProxy("http://localhost:8001/")
20+
import cStringIO
21+
output = cStringIO.StringIO()
22+
np.save(output, out["pool5"])
23+
#print(proxy.predict(xmlrpclib.Binary(out["pool5"])))
24+
print(proxy.predict(xmlrpclib.Binary(output.getvalue())))
25+

sp_server.py

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
from SimpleXMLRPCServer import SimpleXMLRPCServer
2+
import xmlrpclib
3+
4+
import caffe
5+
6+
caffe_dir = "../caffe"
7+
8+
with open("synset_words.txt") as f:
9+
words = f.readlines()
10+
words = map(lambda x: x.strip(), words)
11+
12+
MODEL_FILE2 = "./models/caffe2.prototxt"
13+
PRETRAINED2 = "./models/caffemodel2"
14+
net2 = caffe.Net(MODEL_FILE2, PRETRAINED2)
15+
net2.set_phase_test()
16+
net2.set_mode_gpu()
17+
#out2 = net2.forward_all(**out)
18+
19+
clients = {}
20+
import numpy as np
21+
import uuid
22+
23+
def predict(data):
24+
import cStringIO
25+
inputdata = cStringIO.StringIO(data.data)
26+
bn = np.load(inputdata)
27+
out2 = net2.forward_all(**{"pool5":bn})
28+
i = out2['prob'][0].argmax(axis=0)
29+
return words[i]
30+
31+
#MODEL_FILE = caffe_dir + "/models/bvlc_reference_caffenet/deploy.prototxt"
32+
#PRETRAINED = caffe_dir + "/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"
33+
#IMAGE_FILE = "images/cat.png"
34+
35+
server = SimpleXMLRPCServer(("localhost", 8001))
36+
print "Listening on port 8001..."
37+
server.register_function(predict, 'predict')
38+
39+
server.serve_forever()

test.py

+78-3
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import sys
12
#from memory_profiler import profile
23
#@profile
34
def main():
@@ -20,8 +21,10 @@ def main():
2021
net.set_phase_test()
2122
net.set_mode_gpu()
2223
input_image = caffe.io.load_image(IMAGE_FILE)
23-
prediction = net.predict([input_image])
24-
i = prediction[0].argmax()
24+
#prediction = net.predict([input_image])
25+
prediction = net.forward_all(data=np.asarray([net.preprocess('data', input_image)]))
26+
27+
i = prediction["prob"].argmax()
2528
print(i)
2629
print(words[i])
2730
# del net
@@ -30,5 +33,77 @@ def main():
3033
# print(i)
3134
# print(words[i])
3235

33-
main()
36+
SETTINGS = {
37+
"imagenet": {
38+
"model_file": "tlc/var2.6.Model.prototxt",
39+
"pretrained": "tlc/var2.6.Model.caffemodel",
40+
"image_dims": (256, 256),
41+
"input_dims": [224, 224],
42+
"oversample": False,
43+
},
44+
"deepface": {
45+
"model_file": "tlc/faces/var5.5.Model.prototxt",
46+
"pretrained": "tlc/faces/var5.5.Model.caffemodel",
47+
#"model_file": "tlc/faces/faces.var5.rm6.prototxt",
48+
#"pretrained": "tlc/faces/faces.var5.rm6.caffemodel",
49+
"image_dims": (152, 152),
50+
"input_dims": [152, 152],
51+
"oversample": False,
52+
},
53+
"test": {
54+
"model_file": "test.prototxt",
55+
"pretrained": "test.caffemodel",
56+
"image_dims": (3, 3),
57+
"input_dims": [3, 3],
58+
"oversample": False,
59+
}
60+
}
61+
62+
def main_tlc():
63+
import caffe
64+
import numpy as np
65+
import skimage.io
66+
np.set_printoptions(threshold='nan')
67+
68+
options = SETTINGS["imagenet"]
69+
#IMAGE_FILE = "../cat.jpg"
70+
IMAGE_FILE = "../fish.jpg"
71+
mean = np.zeros([3] + list(options['input_dims']))
72+
mean.fill(128.0)
73+
#with open("synset_words.txt") as f:
74+
# words = f.readlines()
75+
#words = map(lambda x: x.strip(), words)
76+
77+
net = caffe.Classifier(options["model_file"], options["pretrained"],
78+
mean=mean, input_scale=0.0078125,
79+
image_dims=options["image_dims"])
80+
sys.stderr.write("model file: %s\n" % options["model_file"])
81+
sys.stderr.write("pretrained: %s\n" % options["pretrained"])
82+
83+
net.set_phase_test()
84+
net.set_mode_gpu()
85+
#net.set_mode_cpu()
86+
"""
87+
with open("rawinput.txt") as f:
88+
content = f.read()
89+
rawinput = content.strip(' ,\t\r\n').split(',')
90+
rawinput = map(lambda x: eval(x), rawinput)
91+
rawinput = np.asarray(rawinput).reshape([1,3] + options['input_dims'])
92+
prediction = net.predict_raw(rawinput)
93+
return
94+
"""
95+
96+
input_image = skimage.io.imread(IMAGE_FILE)
97+
prediction = net.predict([input_image], oversample=True)
98+
#prediction = net.forward_all(data=np.asarray([net.preprocess('data', input_image)]))
99+
#return
100+
label = prediction.argmax()
101+
#for i,v in enumerate(prediction[0]):
102+
# print i, v
103+
print label
104+
#print words[label]
105+
#print input_image
106+
107+
#main()
108+
main_tlc()
34109

test_split.py

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import caffe
2+
import numpy as np
3+
from util import predict
4+
5+
caffe_dir = "../caffe"
6+
MODEL_FILE = caffe_dir + "/models/bvlc_reference_caffenet/deploy.prototxt"
7+
PRETRAINED = caffe_dir + "/models/bvlc_reference_caffenet/bvlc_reference_caffenet.caffemodel"
8+
IMAGE_FILE = "../cat.png"
9+
with open("synset_words.txt") as f:
10+
words = f.readlines()
11+
words = map(lambda x: x.strip(), words)
12+
13+
net = caffe.Classifier(MODEL_FILE, PRETRAINED,
14+
mean=np.load(caffe_dir + '/python/caffe/imagenet/ilsvrc_2012_mean.npy'),
15+
channel_swap=(2,1,0),
16+
raw_scale=255,
17+
image_dims=(256, 256))
18+
net.set_phase_test()
19+
net.set_mode_gpu()
20+
input_image = caffe.io.load_image(IMAGE_FILE)
21+
#print(list(net._layer_names))
22+
predict([input_image], (256, 256), net.crop_dims, net)
23+
#r = net.forward_all([input_image])
24+

0 commit comments

Comments
 (0)