Beispiel #1
0
 def init_op(self):
     self.seq = Sequential([
         Resize(256), CenterCrop(224), RGB2BGR(), Transpose((2, 0, 1)),
         Div(255), Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225],
                             True)
     ])
     self.label_dict = {}
     label_idx = 0
     with open("imagenet.label") as fin:
         for line in fin:
             self.label_dict[label_idx] = line.strip()
             label_idx += 1
Beispiel #2
0
    def test_resize(self):
        height = 9
        width = 5
        channels = 3
        img = np.arange(height * width).reshape([height, width, 1]) * np.ones(
            (1, channels))

        # input size is an int
        for new_size in [3, 10]:
            seq_gpu = Sequential([
                pp.Image2Gpubuffer(),
                pp.Resize(new_size),
                pp.Gpubuffer2Image()
            ])
            seq_paddle = Sequential([Resize(new_size)])
            result_gpu = seq_gpu(img)
            result_paddle = seq_paddle(img)
            self.assertEqual(result_gpu.shape, result_paddle.shape)
            for i in range(0, result_gpu.shape[0]):
                for j in range(0, result_gpu.shape[1]):
                    for k in range(0, result_gpu.shape[2]):
                        self.assertAlmostEqual(result_gpu[i][j][k],
                                               result_paddle[i][j][k], 5)

        # input size is a sequence
        for new_height, new_width in [(7, 3), (15, 10)]:
            seq_gpu = Sequential([
                pp.Image2Gpubuffer(),
                pp.Resize((new_width, new_height)),
                pp.Gpubuffer2Image()
            ])
            seq_paddle = Sequential([Resize((new_width, new_height))])
            result_gpu = seq_gpu(img)
            result_paddle = seq_paddle(img)
            self.assertEqual(result_gpu.shape, result_paddle.shape)
            for i in range(0, result_gpu.shape[0]):
                for j in range(0, result_gpu.shape[1]):
                    for k in range(0, result_gpu.shape[2]):
                        self.assertAlmostEqual(result_gpu[i][j][k],
                                               result_paddle[i][j][k], 5)
Beispiel #3
0
def single_func(idx, resource):
    total_number = 0
    profile_flags = False
    latency_flags = False
    if os.getenv("FLAGS_profile_client"):
        profile_flags = True
    if os.getenv("FLAGS_serving_latency"):
        latency_flags = True
        latency_list = []

    if args.request == "rpc":
        client = Client()
        client.load_client_config(args.model)
        client.connect([resource["endpoint"][idx % len(resource["endpoint"])]])
        start = time.time()
        for i in range(turns):
            if args.batch_size >= 1:
                l_start = time.time()
                seq = Sequential([
                    File2Image(),
                    Resize(256),
                    CenterCrop(224),
                    RGB2BGR(),
                    Transpose((2, 0, 1)),
                    Div(255),
                    Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225],
                              True)
                ])
                image_file = "daisy.jpg"
                img = seq(image_file)
                feed_data = np.array(img)
                feed_data = np.expand_dims(feed_data,
                                           0).repeat(args.batch_size, axis=0)
                result = client.predict(
                    feed={"image": feed_data},
                    fetch=["save_infer_model/scale_0.tmp_0"],
                    batch=True)
                l_end = time.time()
                if latency_flags:
                    latency_list.append(l_end * 1000 - l_start * 1000)
                total_number = total_number + 1
            else:
                print("unsupport batch size {}".format(args.batch_size))

    else:
        raise ValueError("not implemented {} request".format(args.request))
    end = time.time()
    if latency_flags:
        return [[end - start], latency_list, [total_number]]
    else:
        return [[end - start]]
Beispiel #4
0
def preprocess_img(img_list):
    """
    Brief:
        prepare img data for benchmark
    Args:
        img_list(list): list for img file path
    Returns:
        image content binary list after preprocess
    """
    preprocess = Sequential([File2Image(), Resize((512, 512))])
    result_list = []
    for img in img_list:
        img_tmp = preprocess(img)
        result_list.append(img_tmp)
    return result_list
Beispiel #5
0
    def resize_norm_img(self, img, max_wh_ratio):
        imgC, imgH, imgW = self.rec_image_shape
        if self.character_type == "ch":
            imgW = int(32 * max_wh_ratio)
        h = img.shape[0]
        w = img.shape[1]
        ratio = w / float(h)
        if math.ceil(imgH * ratio) > imgW:
            resized_w = imgW
        else:
            resized_w = int(math.ceil(imgH * ratio))

        seq = Sequential([
            Resize(imgH, resized_w),
            Transpose((2, 0, 1)),
            Div(255),
            Normalize([0.5, 0.5, 0.5], [0.5, 0.5, 0.5], True)
        ])
        resized_image = seq(img)
        padding_im = np.zeros((imgC, imgH, imgW), dtype=np.float32)
        padding_im[:, :, 0:resized_w] = resized_image

        return padding_im
we recommend use Proto data format in HTTP-body, set True(which is default)
if you want use JSON data format in HTTP-body, set False
'''
#client.set_http_proto(True)
client.connect(["127.0.0.1:9696"])

label_dict = {}
label_idx = 0
with open("imagenet.label") as fin:
    for line in fin:
        label_dict[label_idx] = line.strip()
        label_idx += 1

seq = Sequential([
    URL2Image(),
    Resize(256),
    CenterCrop(224),
    RGB2BGR(),
    Transpose((2, 0, 1)),
    Div(255),
    Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True)
])

start = time.time()
image_file = "https://paddle-serving.bj.bcebos.com/imagenet-example/daisy.jpg"
for i in range(10):
    img = seq(image_file)
    fetch_map = client.predict(feed={"image": img},
                               fetch=["score"],
                               batch=False)
    print(fetch_map)
Beispiel #7
0
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle_serving_client import Client
from paddle_serving_app.reader import Sequential, File2Image, Resize, Transpose, BGR2RGB, SegPostprocess
import sys
import cv2

client = Client()
client.load_client_config("unet_client/serving_client_conf.prototxt")
client.connect(["127.0.0.1:9494"])

preprocess = Sequential(
    [File2Image(),
     Resize((512, 512), interpolation=cv2.INTER_LINEAR)])

postprocess = SegPostprocess(2)

filename = "N0060.jpg"
im = preprocess(filename)
fetch_map = client.predict(feed={"image": im}, fetch=["output"])
fetch_map["filename"] = filename
postprocess(fetch_map)
Beispiel #8
0
from paddle_serving_app.reader import CenterCrop, RGB2BGR, Transpose, Div, Normalize
import time

client = Client()
client.load_client_config(sys.argv[1])
client.connect(["127.0.0.1:9696"])

label_dict = {}
label_idx = 0
with open("imagenet.label") as fin:
    for line in fin:
        label_dict[label_idx] = line.strip()
        label_idx += 1

seq = Sequential([
    URL2Image(), Resize(256), CenterCrop(224), RGB2BGR(), Transpose((2, 0, 1)),
    Div(255), Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True)
])

start = time.time()
image_file = "https://paddle-serving.bj.bcebos.com/imagenet-example/daisy.jpg"
for i in range(10):
    img = seq(image_file)
    fetch_map = client.predict(
        feed={"image": img}, fetch=["score"], batch=False)
    prob = max(fetch_map["score"][0])
    label = label_dict[fetch_map["score"][0].tolist().index(prob)].strip(
    ).replace(",", "")
    print("prediction: {}, probability: {}".format(label, prob))

end = time.time()
Beispiel #9
0
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

from paddle_serving_client import Client
from paddle_serving_app.reader import Sequential, File2Image, Resize, Transpose, BGR2RGB, SegPostprocess
import sys
import cv2

client = Client()
client.load_client_config("deeplabv3_client/serving_client_conf.prototxt")
client.connect(["127.0.0.1:9494"])

preprocess = Sequential(
    [File2Image(), Resize(
        (512, 512), interpolation=cv2.INTER_LINEAR)])

postprocess = SegPostprocess(2)

filename = "N0060.jpg"
im = preprocess(filename)
fetch_map = client.predict(feed={"image": im}, fetch=["output"])
fetch_map["filename"] = filename
postprocess(fetch_map)