From c9780d950eb34a13de73e8bdac6ebc6cebea32a1 Mon Sep 17 00:00:00 2001 From: MRXLT Date: Mon, 30 Mar 2020 08:15:07 +0000 Subject: [PATCH] fix demo for py3 --- python/examples/imagenet/image_http_client.py | 17 +++++++---------- python/examples/imagenet/image_rpc_client.py | 5 ++--- python/examples/imdb/imdb_reader.py | 16 ++++++++++++---- 3 files changed, 21 insertions(+), 17 deletions(-) diff --git a/python/examples/imagenet/image_http_client.py b/python/examples/imagenet/image_http_client.py index 45b0bc1e..2a2e9ea2 100644 --- a/python/examples/imagenet/image_http_client.py +++ b/python/examples/imagenet/image_http_client.py @@ -17,10 +17,16 @@ import base64 import json import time import os +import sys + +py_version = sys.version_info[0] def predict(image_path, server): - image = base64.b64encode(open(image_path).read()) + if py_version == 2: + image = base64.b64encode(open(image_path).read()) + else: + image = base64.b64encode(open(image_path, "rb").read()).decode("utf-8") req = json.dumps({"image": image, "fetch": ["score"]}) r = requests.post( server, data=req, headers={"Content-Type": "application/json"}) @@ -28,15 +34,6 @@ def predict(image_path, server): return r -def batch_predict(image_path, server): - image = base64.b64encode(open(image_path).read()) - req = json.dumps({"image": [image, image], "fetch": ["score"]}) - r = requests.post( - server, data=req, headers={"Content-Type": "application/json"}) - print(r.json()["result"][1]["score"][0]) - return r - - if __name__ == "__main__": server = "http://127.0.0.1:9393/image/prediction" image_list = os.listdir("./image_data/n01440764/") diff --git a/python/examples/imagenet/image_rpc_client.py b/python/examples/imagenet/image_rpc_client.py index 2367f509..76f3a043 100644 --- a/python/examples/imagenet/image_rpc_client.py +++ b/python/examples/imagenet/image_rpc_client.py @@ -19,16 +19,15 @@ import time client = Client() client.load_client_config(sys.argv[1]) -client.connect(["127.0.0.1:9295"]) +client.connect(["127.0.0.1:9393"]) reader = ImageReader() start = time.time() for i in range(1000): - with open("./data/n01440764_10026.JPEG") as f: + with open("./data/n01440764_10026.JPEG", "rb") as f: img = f.read() img = reader.process_image(img).reshape(-1) fetch_map = client.predict(feed={"image": img}, fetch=["score"]) - print(i) end = time.time() print(end - start) diff --git a/python/examples/imdb/imdb_reader.py b/python/examples/imdb/imdb_reader.py index 38a46c5c..a4ef3e16 100644 --- a/python/examples/imdb/imdb_reader.py +++ b/python/examples/imdb/imdb_reader.py @@ -19,15 +19,23 @@ import paddle import re import paddle.fluid.incubate.data_generator as dg +py_version = sys.version_info[0] + class IMDBDataset(dg.MultiSlotDataGenerator): def load_resource(self, dictfile): self._vocab = {} wid = 0 - with open(dictfile) as f: - for line in f: - self._vocab[line.strip()] = wid - wid += 1 + if py_version == 2: + with open(dictfile) as f: + for line in f: + self._vocab[line.strip()] = wid + wid += 1 + else: + with open(dictfile, encoding="utf-8") as f: + for line in f: + self._vocab[line.strip()] = wid + wid += 1 self._unk_id = len(self._vocab) self._pattern = re.compile(r'(;|,|\.|\?|!|\s|\(|\))') self.return_value = ("words", [1, 2, 3, 4, 5, 6]), ("label", [0]) -- GitLab