From 109fa2b441014cd88238e9dd4598f2662b52a07f Mon Sep 17 00:00:00 2001 From: guru4elephant Date: Tue, 14 Jan 2020 15:03:58 +0800 Subject: [PATCH] fix python client --- core/general-client/include/general_model.h | 1 + core/general-client/src/general_model.cpp | 3 + examples/demo-serving/kvdb/LOG | 56 ++++++++++++++++++- python/examples/imdb/inference.conf | 6 ++ python/examples/imdb/test_client.py | 16 ++++++ python/examples/util/get_acc.py | 13 +++++ .../paddle_serving/serving_client/__init__.py | 34 +++++++++-- 7 files changed, 123 insertions(+), 6 deletions(-) create mode 100644 python/examples/imdb/inference.conf create mode 100644 python/examples/imdb/test_client.py create mode 100644 python/examples/util/get_acc.py diff --git a/core/general-client/include/general_model.h b/core/general-client/include/general_model.h index 1739fea6..cec57bdd 100644 --- a/core/general-client/include/general_model.h +++ b/core/general-client/include/general_model.h @@ -77,6 +77,7 @@ class PredictorClient { std::map _fetch_name_to_idx; std::map _fetch_name_to_var_name; std::vector > _shape; + std::vector _type; }; } // namespace general_model diff --git a/core/general-client/src/general_model.cpp b/core/general-client/src/general_model.cpp index 3fc9e68a..2b18543c 100644 --- a/core/general-client/src/general_model.cpp +++ b/core/general-client/src/general_model.cpp @@ -45,6 +45,7 @@ void PredictorClient::init(const std::string & conf_file) { std::string fetch_var_name; int shape_num = 0; int dim = 0; + int type_value = 0; for (int i = 0; i < feed_var_num; ++i) { fin >> name; _feed_name_to_idx[name] = i; @@ -54,6 +55,8 @@ void PredictorClient::init(const std::string & conf_file) { fin >> dim; tmp_feed_shape.push_back(dim); } + fin >> type_value; + _type.push_back(type_value); _shape.push_back(tmp_feed_shape); } diff --git a/examples/demo-serving/kvdb/LOG b/examples/demo-serving/kvdb/LOG index 40267581..1369fa6d 100644 --- a/examples/demo-serving/kvdb/LOG +++ b/examples/demo-serving/kvdb/LOG @@ -22041,4 +22041,58 @@ AddFile(L0 Files): cumulative 0, interval 0 AddFile(Keys): cumulative 0, interval 0 Cumulative compaction: 0.00 GB write, 0.00 MB/s write, 0.00 GB read, 0.00 MB/s read, 0.0 seconds Interval compaction: 0.00 GB write, 0.00 MB/s write, 0.00 GB read, 0.00 MB/s read, 0.0 seconds -Stalls(count): 0 level0_slowdown, 0 level0_ \ No newline at end of file +Stalls(count): 0 level0_slowdown, 0 level0_slowdown_with_compaction, 0 level0_numfiles, 0 level0_numfiles_with_compaction, 0 stop for pending_compaction_bytes, 0 slowdown for pending_compaction_bytes, 0 memtable_compaction, 0 memtable_slowdown, interval 0 total count + +** File Read Latency Histogram By Level [default] ** +2020/01/14-10:37:24.283818 7fcc9bdf2700 [/db_impl.cc:779] ------- DUMPING STATS ------- +2020/01/14-10:37:24.283872 7fcc9bdf2700 [/db_impl.cc:780] +** DB Stats ** +Uptime(secs): 252000.1 total, 600.0 interval +Cumulative writes: 0 writes, 0 keys, 0 commit groups, 0.0 writes per commit group, ingest: 0.00 GB, 0.00 MB/s +Cumulative WAL: 0 writes, 0 syncs, 0.00 writes per sync, written: 0.00 GB, 0.00 MB/s +Cumulative stall: 00:00:0.000 H:M:S, 0.0 percent +Interval writes: 0 writes, 0 keys, 0 commit groups, 0.0 writes per commit group, ingest: 0.00 MB, 0.00 MB/s +Interval WAL: 0 writes, 0 syncs, 0.00 writes per sync, written: 0.00 MB, 0.00 MB/s +Interval stall: 00:00:0.000 H:M:S, 0.0 percent + +** Compaction Stats [default] ** +Level Files Size Score Read(GB) Rn(GB) Rnp1(GB) Write(GB) Wnew(GB) Moved(GB) W-Amp Rd(MB/s) Wr(MB/s) Comp(sec) CompMergeCPU(sec) Comp(cnt) Avg(sec) KeyIn KeyDrop +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Sum 0/0 0.00 KB 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.00 0.00 0 0.000 0 0 + Int 0/0 0.00 KB 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.00 0.00 0 0.000 0 0 + +** Compaction Stats [default] ** +Priority Files Size Score Read(GB) Rn(GB) Rnp1(GB) Write(GB) Wnew(GB) Moved(GB) W-Amp Rd(MB/s) Wr(MB/s) Comp(sec) CompMergeCPU(sec) Comp(cnt) Avg(sec) KeyIn KeyDrop +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Uptime(secs): 252000.1 total, 600.0 interval +Flush(GB): cumulative 0.000, interval 0.000 +AddFile(GB): cumulative 0.000, interval 0.000 +AddFile(Total Files): cumulative 0, interval 0 +AddFile(L0 Files): cumulative 0, interval 0 +AddFile(Keys): cumulative 0, interval 0 +Cumulative compaction: 0.00 GB write, 0.00 MB/s write, 0.00 GB read, 0.00 MB/s read, 0.0 seconds +Interval compaction: 0.00 GB write, 0.00 MB/s write, 0.00 GB read, 0.00 MB/s read, 0.0 seconds +Stalls(count): 0 level0_slowdown, 0 level0_slowdown_with_compaction, 0 level0_numfiles, 0 level0_numfiles_with_compaction, 0 stop for pending_compaction_bytes, 0 slowdown for pending_compaction_bytes, 0 memtable_compaction, 0 memtable_slowdown, interval 0 total count + +** File Read Latency Histogram By Level [default] ** + +** Compaction Stats [default] ** +Level Files Size Score Read(GB) Rn(GB) Rnp1(GB) Write(GB) Wnew(GB) Moved(GB) W-Amp Rd(MB/s) Wr(MB/s) Comp(sec) CompMergeCPU(sec) Comp(cnt) Avg(sec) KeyIn KeyDrop +---------------------------------------------------------------------------------------------------------------------------------------------------------------------------- + Sum 0/0 0.00 KB 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.00 0.00 0 0.000 0 0 + Int 0/0 0.00 KB 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.00 0.00 0 0.000 0 0 + +** Compaction Stats [default] ** +Priority Files Size Score Read(GB) Rn(GB) Rnp1(GB) Write(GB) Wnew(GB) Moved(GB) W-Amp Rd(MB/s) Wr(MB/s) Comp(sec) CompMergeCPU(sec) Comp(cnt) Avg(sec) KeyIn KeyDrop +------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- +Uptime(secs): 252000.1 total, 0.0 interval +Flush(GB): cumulative 0.000, interval 0.000 +AddFile(GB): cumulative 0.000, interval 0.000 +AddFile(Total Files): cumulative 0, interval 0 +AddFile(L0 Files): cumulative 0, interval 0 +AddFile(Keys): cumulative 0, interval 0 +Cumulative compaction: 0.00 GB write, 0.00 MB/s write, 0.00 GB read, 0.00 MB/s read, 0.0 seconds +Interval compaction: 0.00 GB write, 0.00 MB/s write, 0.00 GB read, 0.00 MB/s read, 0.0 seconds +Stalls(count): 0 level0_slowdown, 0 level0_slowdown_with_compaction, 0 level0_numfiles, 0 level0_numfiles_with_compaction, 0 stop for pending_compaction_bytes, 0 slowdown for pending_compaction_bytes, 0 memtable_compaction, 0 memtable_slowdown, interval 0 total count + +** File Read Latency Histogram By Level [default] ** diff --git a/python/examples/imdb/inference.conf b/python/examples/imdb/inference.conf new file mode 100644 index 00000000..bbb056b8 --- /dev/null +++ b/python/examples/imdb/inference.conf @@ -0,0 +1,6 @@ +2 3 +words 1 -1 0 +label 1 1 0 +cost mean_0.tmp_0 +acc accuracy_0.tmp_0 +prediction fc_1.tmp_2 diff --git a/python/examples/imdb/test_client.py b/python/examples/imdb/test_client.py new file mode 100644 index 00000000..ab52c2dc --- /dev/null +++ b/python/examples/imdb/test_client.py @@ -0,0 +1,16 @@ +from paddle_serving import Client +import sys + +client = Client() +client.load_client_config(sys.argv[1]) +client.connect(["127.0.0.1:9292"]) + +for line in sys.stdin: + group = line.strip().split() + words = [int(x) for x in group[1:int(group[0])]] + label = [int(group[-1])] + feed = {"words": words, "label": label} + fetch = ["acc", "cost", "prediction"] + fetch_map = client.predict(feed=feed, fetch=fetch) + print("{} {}".format(fetch_map["prediction"][1], label[0])) + diff --git a/python/examples/util/get_acc.py b/python/examples/util/get_acc.py new file mode 100644 index 00000000..ce4885a6 --- /dev/null +++ b/python/examples/util/get_acc.py @@ -0,0 +1,13 @@ +import sys +import os + +total = 0 +acc = 0 +for line in sys.stdin: + line = line.strip() + group = line.split() + if (float(group[0]) - 0.5) * (float(group[1]) - 0.5) > 0: + acc += 1 + total += 1 + +print(float(acc) / float(total)) diff --git a/python/paddle_serving/serving_client/__init__.py b/python/paddle_serving/serving_client/__init__.py index c113dd9a..d12dc2b4 100644 --- a/python/paddle_serving/serving_client/__init__.py +++ b/python/paddle_serving/serving_client/__init__.py @@ -16,6 +16,9 @@ from .serving_client import PredictorClient from ..proto import sdk_configure_pb2 as sdk import time +int_type = 0 +float_type = 1 + class SDKConfig(object): def __init__(self): self.sdk_desc = sdk.SDKConf() @@ -65,7 +68,7 @@ class Client(object): self.fetch_names_ = [] self.client_handle_ = None self.feed_shapes_ = [] - self.feed_types_ = [] + self.feed_types_ = {} self.feed_names_to_idx_ = {} def load_client_config(self, path): @@ -75,6 +78,28 @@ class Client(object): # map feed names to index self.client_handle_ = PredictorClient() self.client_handle_.init(path) + self.feed_names_ = [] + self.fetch_names_ = [] + self.feed_shapes_ = [] + self.feed_types_ = {} + self.feed_names_to_idx_ = {} + + with open(path) as fin: + group = fin.readline().strip().split() + feed_num = int(group[0]) + fetch_num = int(group[1]) + for i in range(feed_num): + group = fin.readline().strip().split() + self.feed_names_.append(group[0]) + tmp_shape = [] + for s in group[2:-1]: + tmp_shape.append(int(s)) + self.feed_shapes_.append(tmp_shape) + self.feed_types_[group[0]] = int(group[-1]) + self.feed_names_to_idx_[group[0]] = i + for i in range(fetch_num): + group = fin.readline().strip().split() + self.fetch_names_.append(group[0]) return def connect(self, endpoints): @@ -84,7 +109,6 @@ class Client(object): predictor_sdk = SDKConfig() predictor_sdk.set_server_endpoints(endpoints) sdk_desc = predictor_sdk.gen_desc() - print(sdk_desc) timestamp = time.asctime(time.localtime(time.time())) predictor_path = "/tmp/" predictor_file = "%s_predictor.conf" % timestamp @@ -100,7 +124,7 @@ class Client(object): def get_fetch_names(self): return self.fetch_names_ - def predict(self, feed={}, fetch={}): + def predict(self, feed={}, fetch=[]): int_slot = [] float_slot = [] int_feed_names = [] @@ -111,10 +135,10 @@ class Client(object): continue if self.feed_types_[key] == int_type: int_feed_names.append(key) - int_slot.append(feed_map[key]) + int_slot.append(feed[key]) elif self.feed_types_[key] == float_type: float_feed_names.append(key) - float_slot.append(feed_map[key]) + float_slot.append(feed[key]) for key in fetch: if key in self.fetch_names_: -- GitLab