From b806e3fdbbc390bd7a095d65ebcfb5399d7afb81 Mon Sep 17 00:00:00 2001 From: felixhjh <852142024@qq.com> Date: Mon, 18 Oct 2021 07:19:12 +0000 Subject: [PATCH 1/7] modify detection examples preprocess --- python/examples/cascade_rcnn/test_client.py | 17 +- .../faster_rcnn_hrnetv2p_w18_1x/README.md | 2 +- .../test_client.py | 15 +- .../test_client.py | 19 +- .../fcos_dcn_r50_fpn_1x_coco/test_client.py | 21 ++- .../ppyolo_r50vd_dcn_1x_coco/test_client.py | 16 +- .../ssd_vgg16_300_240e_voc/test_client.py | 22 ++- .../yolov3_darknet53_270e_coco/test_client.py | 17 +- python/examples/yolov4/test_client.py | 2 +- .../paddle_serving_app/reader/image_reader.py | 177 +++++++++++++----- 10 files changed, 203 insertions(+), 105 deletions(-) diff --git a/python/examples/cascade_rcnn/test_client.py b/python/examples/cascade_rcnn/test_client.py index 5dbdd5567..cd90e5f15 100644 --- a/python/examples/cascade_rcnn/test_client.py +++ b/python/examples/cascade_rcnn/test_client.py @@ -17,27 +17,30 @@ from paddle_serving_client import Client from paddle_serving_app.reader import * import cv2 + preprocess = Sequential([ - File2Image(), BGR2RGB(), Resize( - (608, 608), interpolation=cv2.INTER_LINEAR), Div(255.0), Transpose( - (2, 0, 1)) + File2Image(), + Resize((800, 1333), True, interpolation=2), + Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + Transpose((2,0,1)), + PadStride(32) ]) -postprocess = RCNNPostprocess("label_list.txt", "output", [608, 608]) +postprocess = RCNNPostprocess("label_list.txt", "output") client = Client() client.load_client_config("serving_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9292']) -im = preprocess('000000570688.jpg') +im, im_info = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im, "im_shape": np.array(list(im.shape[1:])).reshape(-1), - "scale_factor": np.array([1.0, 1.0]).reshape(-1), + "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], batch=False) print(fetch_map) -fetch_map["image"] = '000000570688.jpg' +fetch_map["image"] = sys.argv[1] postprocess(fetch_map) diff --git a/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/README.md b/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/README.md index efd8b9f0f..3c0fb8dbe 100644 --- a/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/README.md +++ b/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/README.md @@ -9,7 +9,7 @@ wget --no-check-certificate https://paddle-serving.bj.bcebos.com/pddet_demo/fast ### Start the service ``` -tar xf faster_rcnn_hrnetv2p_w18_1x.tar +tar xf faster_rcnn_hrnetv2p_w18_1x.tar.gz python3 -m paddle_serving_server.serve --model serving_server --port 9494 --gpu_ids 0 ``` diff --git a/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/test_client.py b/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/test_client.py index 22cc1451c..b68a0c7b4 100644 --- a/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/test_client.py +++ b/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/test_client.py @@ -17,24 +17,27 @@ from paddle_serving_client import Client from paddle_serving_app.reader import * import cv2 + preprocess = Sequential([ - File2Image(), BGR2RGB(), Resize( - (608, 608), interpolation=cv2.INTER_LINEAR), Div(255.0), Transpose( - (2, 0, 1)) + File2Image(), + Resize((800, 1333), True, interpolation=2), + Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + Transpose((2,0,1)), + PadStride(32) ]) -postprocess = RCNNPostprocess("label_list.txt", "output", [608, 608]) +postprocess = RCNNPostprocess("label_list.txt", "output") client = Client() client.load_client_config("serving_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9494']) -im = preprocess(sys.argv[1]) +im, im_info = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im, "im_shape": np.array(list(im.shape[1:])).reshape(-1), - "scale_factor": np.array([1.0, 1.0]).reshape(-1), + "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], batch=False) diff --git a/python/examples/detection/faster_rcnn_r50_fpn_1x_coco/test_client.py b/python/examples/detection/faster_rcnn_r50_fpn_1x_coco/test_client.py index a81f53dce..5d5e0f2b1 100644 --- a/python/examples/detection/faster_rcnn_r50_fpn_1x_coco/test_client.py +++ b/python/examples/detection/faster_rcnn_r50_fpn_1x_coco/test_client.py @@ -12,15 +12,19 @@ # 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 * import sys import numpy as np +from paddle_serving_client import Client +from paddle_serving_app.reader import * +import cv2 preprocess = Sequential([ - File2Image(), BGR2RGB(), Div(255.0), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], False), - Resize(640, 640), Transpose((2, 0, 1)) + File2Image(), + Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + Resize( + (800, 1333), True, interpolation=cv2.INTER_LINEAR), + Transpose((2,0,1)), + PadStride(128) ]) postprocess = RCNNPostprocess("label_list.txt", "output") @@ -29,15 +33,14 @@ client.load_client_config("serving_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9494']) -im = preprocess(sys.argv[1]) +im, im_info = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im, "im_shape": np.array(list(im.shape[1:])).reshape(-1), - "scale_factor": np.array([1.0, 1.0]).reshape(-1), + "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], batch=False) -print(fetch_map) fetch_map["image"] = sys.argv[1] postprocess(fetch_map) diff --git a/python/examples/detection/fcos_dcn_r50_fpn_1x_coco/test_client.py b/python/examples/detection/fcos_dcn_r50_fpn_1x_coco/test_client.py index bf5504105..f7cdc745b 100644 --- a/python/examples/detection/fcos_dcn_r50_fpn_1x_coco/test_client.py +++ b/python/examples/detection/fcos_dcn_r50_fpn_1x_coco/test_client.py @@ -12,15 +12,19 @@ # 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 * import sys import numpy as np +from paddle_serving_client import Client +from paddle_serving_app.reader import * +import cv2 preprocess = Sequential([ - File2Image(), BGR2RGB(), Div(255.0), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], False), - Resize(640, 640), Transpose((2, 0, 1)) + File2Image(), + Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + Resize( + (800, 1333), True, interpolation=cv2.INTER_LINEAR), + Transpose((2,0,1)), + PadStride(128) ]) postprocess = RCNNPostprocess("label_list.txt", "output") @@ -29,12 +33,13 @@ client.load_client_config("serving_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9494']) -im = preprocess(sys.argv[1]) +im, im_info = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im, - "scale_factor": np.array([1.0, 1.0]).reshape(-1), + "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], batch=False) -print(fetch_map) +fetch_map["image"] = sys.argv[1] +postprocess(fetch_map) diff --git a/python/examples/detection/ppyolo_r50vd_dcn_1x_coco/test_client.py b/python/examples/detection/ppyolo_r50vd_dcn_1x_coco/test_client.py index 22cc1451c..f26e13446 100644 --- a/python/examples/detection/ppyolo_r50vd_dcn_1x_coco/test_client.py +++ b/python/examples/detection/ppyolo_r50vd_dcn_1x_coco/test_client.py @@ -17,27 +17,29 @@ from paddle_serving_client import Client from paddle_serving_app.reader import * import cv2 + preprocess = Sequential([ - File2Image(), BGR2RGB(), Resize( - (608, 608), interpolation=cv2.INTER_LINEAR), Div(255.0), Transpose( - (2, 0, 1)) + File2Image(), + Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + Resize( + (608, 608), False, interpolation=2), + Transpose((2,0,1)) ]) -postprocess = RCNNPostprocess("label_list.txt", "output", [608, 608]) +postprocess = RCNNPostprocess("label_list.txt", "output") client = Client() client.load_client_config("serving_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9494']) -im = preprocess(sys.argv[1]) +im, im_info = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im, "im_shape": np.array(list(im.shape[1:])).reshape(-1), - "scale_factor": np.array([1.0, 1.0]).reshape(-1), + "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], batch=False) -print(fetch_map) fetch_map["image"] = sys.argv[1] postprocess(fetch_map) diff --git a/python/examples/detection/ssd_vgg16_300_240e_voc/test_client.py b/python/examples/detection/ssd_vgg16_300_240e_voc/test_client.py index 3409ef885..f7cdc745b 100644 --- a/python/examples/detection/ssd_vgg16_300_240e_voc/test_client.py +++ b/python/examples/detection/ssd_vgg16_300_240e_voc/test_client.py @@ -12,15 +12,19 @@ # 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 * import sys import numpy as np +from paddle_serving_client import Client +from paddle_serving_app.reader import * +import cv2 preprocess = Sequential([ - File2Image(), BGR2RGB(), - Normalize([123.675, 116.28, 103.53], [58.395, 57.12, 57.375], False), - Resize((512, 512)), Transpose((2, 0, 1)) + File2Image(), + Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + Resize( + (800, 1333), True, interpolation=cv2.INTER_LINEAR), + Transpose((2,0,1)), + PadStride(128) ]) postprocess = RCNNPostprocess("label_list.txt", "output") @@ -29,13 +33,13 @@ client.load_client_config("serving_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9494']) -im = preprocess(sys.argv[1]) +im, im_info = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im, - "im_shape": np.array([512, 512]), - "scale_factor": np.array([1.0, 1.0]).reshape(-1), + "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], batch=False) -print(fetch_map) +fetch_map["image"] = sys.argv[1] +postprocess(fetch_map) diff --git a/python/examples/detection/yolov3_darknet53_270e_coco/test_client.py b/python/examples/detection/yolov3_darknet53_270e_coco/test_client.py index 22cc1451c..5d5e0f2b1 100644 --- a/python/examples/detection/yolov3_darknet53_270e_coco/test_client.py +++ b/python/examples/detection/yolov3_darknet53_270e_coco/test_client.py @@ -17,27 +17,30 @@ from paddle_serving_client import Client from paddle_serving_app.reader import * import cv2 + preprocess = Sequential([ - File2Image(), BGR2RGB(), Resize( - (608, 608), interpolation=cv2.INTER_LINEAR), Div(255.0), Transpose( - (2, 0, 1)) + File2Image(), + Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + Resize( + (800, 1333), True, interpolation=cv2.INTER_LINEAR), + Transpose((2,0,1)), + PadStride(128) ]) -postprocess = RCNNPostprocess("label_list.txt", "output", [608, 608]) +postprocess = RCNNPostprocess("label_list.txt", "output") client = Client() client.load_client_config("serving_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9494']) -im = preprocess(sys.argv[1]) +im, im_info = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im, "im_shape": np.array(list(im.shape[1:])).reshape(-1), - "scale_factor": np.array([1.0, 1.0]).reshape(-1), + "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], batch=False) -print(fetch_map) fetch_map["image"] = sys.argv[1] postprocess(fetch_map) diff --git a/python/examples/yolov4/test_client.py b/python/examples/yolov4/test_client.py index dfcd58610..3db9b3ec3 100644 --- a/python/examples/yolov4/test_client.py +++ b/python/examples/yolov4/test_client.py @@ -29,7 +29,7 @@ client.load_client_config("yolov4_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9393']) -im = preprocess(sys.argv[1]) +im, im_info = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im, diff --git a/python/paddle_serving_app/reader/image_reader.py b/python/paddle_serving_app/reader/image_reader.py index ca97af94f..3a323a4b3 100644 --- a/python/paddle_serving_app/reader/image_reader.py +++ b/python/paddle_serving_app/reader/image_reader.py @@ -484,10 +484,15 @@ class Sequential(object): def __init__(self, transforms): self.transforms = transforms - def __call__(self, img): + def __call__(self, im): + im_info = { + 'scale_factor': np.array( + [1., 1.], dtype=np.float32), + 'im_shape': None, + } for t in self.transforms: - img = t(img) - return img + im, im_info = t(im, im_info) + return im, im_info def __repr__(self): format_string_ = self.__class__.__name__ + '(' @@ -513,8 +518,8 @@ class BGR2RGB(object): def __init__(self): pass - def __call__(self, img): - return img[:, :, ::-1] + def __call__(self, img, img_info = False): + return img[:, :, ::-1], img_info def __repr__(self): return self.__class__.__name__ + "()" @@ -537,7 +542,7 @@ class File2Image(object): def __init__(self): pass - def __call__(self, img_path): + def __call__(self, img_path, im_info): if py_version == 2: fin = open(img_path) else: @@ -545,13 +550,16 @@ def __call__(self, img_path): sample = fin.read() data = np.fromstring(sample, np.uint8) img = cv2.imdecode(data, cv2.IMREAD_COLOR) + img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) ''' img = cv2.imread(img_path, -1) channels = img.shape[2] ori_h = img.shape[0] ori_w = img.shape[1] ''' - return img + im_info['im_shape'] = np.array(img.shape[:2], dtype=np.float32) + im_info['scale_factor'] = np.array([1., 1.], dtype=np.float32) + return img, im_info def __repr__(self): return self.__class__.__name__ + "()" @@ -592,7 +600,7 @@ class Div(object): def __init__(self, value): self.value = value - def __call__(self, img): + def __call__(self, img, img_info = False): """ Args: img (numpy array): (int8 numpy array) @@ -602,7 +610,7 @@ def __call__(self, img): """ img = img.astype('float32') / self.value - return img + return img, img_info def __repr__(self): return self.__class__.__name__ + "({})".format(self.value) @@ -620,23 +628,33 @@ class Normalize(object): Args: mean (sequence): Sequence of means for each channel. std (sequence): Sequence of standard deviations for each channel. + is_scale (bool): whether need im / 255 """ - def __init__(self, mean, std, channel_first=False): + def __init__(self, mean, std, is_scale=True): self.mean = mean self.std = std - self.channel_first = channel_first + self.is_scale = is_scale - def __call__(self, img): + def __call__(self, im, im_info = False): """ Args: - img (numpy array): (C, H, W) to be normalized. - + im (np.ndarray): image (np.ndarray) + im_info (dict): info of image Returns: - Tensor: Normalized Tensor image. + im (np.ndarray): processed image (np.ndarray) + im_info (dict): info of processed image """ - return F.normalize(img, self.mean, self.std, self.channel_first) + im = im.astype(np.float32, copy=False) + mean = np.array(self.mean)[np.newaxis, np.newaxis, :] + std = np.array(self.std)[np.newaxis, np.newaxis, :] + + if self.is_scale: + im = im / 255.0 + im -= mean + im /= std + return im, im_info def __repr__(self): return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, @@ -690,26 +708,72 @@ def __repr__(self): return self.__class__.__name__ + '(size={0})'.format(self.size) -class Resize(object): - """Resize the input numpy array Image to the given size. +class Resize(object): + """resize image by target_size and max_size Args: - size (sequence or int): Desired output size. If size is a sequence like - (w, h), output size will be matched to this. If size is an int, - smaller edge of the image will be matched to this number. - i.e, if height > width, then image will be rescaled to - (size * height / width, size) - interpolation (int, optional): Desired interpolation. Default is - ``None`` + target_size (int): the target size of image + keep_ratio (bool): whether keep_ratio or not, default true + interp (int): method of resize """ - def __init__(self, size, max_size=2147483647, interpolation=None): - self.size = size - self.max_size = max_size + def __init__(self, target_size, keep_ratio=True, interpolation=cv2.INTER_LINEAR): + if isinstance(target_size, int): + target_size = [target_size, target_size] + self.target_size = target_size + self.keep_ratio = keep_ratio self.interpolation = interpolation - def __call__(self, img): - return F.resize(img, self.size, self.max_size, self.interpolation) + def __call__(self, im, im_info): + """ + Args: + im (np.ndarray): image (np.ndarray) + im_info (dict): info of image + Returns: + im (np.ndarray): processed image (np.ndarray) + im_info (dict): info of processed image + """ + assert len(self.target_size) == 2 + assert self.target_size[0] > 0 and self.target_size[1] > 0 + im_channel = im.shape[2] + im_scale_y, im_scale_x = self.generate_scale(im) + im = cv2.resize( + im, + None, + None, + fx=im_scale_x, + fy=im_scale_y, + interpolation=self.interpolation) + im_info['im_shape'] = np.array(im.shape[:2]).astype('float32') + im_info['scale_factor'] = np.array( + [im_scale_y, im_scale_x]).astype('float32') + return im, im_info + + def generate_scale(self, im): + """ + Args: + im (np.ndarray): image (np.ndarray) + Returns: + im_scale_x: the resize ratio of X + im_scale_y: the resize ratio of Y + """ + origin_shape = im.shape[:2] + im_c = im.shape[2] + if self.keep_ratio: + im_size_min = np.min(origin_shape) + im_size_max = np.max(origin_shape) + target_size_min = np.min(self.target_size) + target_size_max = np.max(self.target_size) + im_scale = float(target_size_min) / float(im_size_min) + if np.round(im_scale * im_size_max) > target_size_max: + im_scale = float(target_size_max) / float(im_size_max) + im_scale_x = im_scale + im_scale_y = im_scale + else: + resize_h, resize_w = self.target_size + im_scale_y = resize_h / float(origin_shape[0]) + im_scale_x = resize_w / float(origin_shape[1]) + return im_scale_y, im_scale_x def __repr__(self): return self.__class__.__name__ + '(size={0}, max_size={1}, interpolation={2})'.format( @@ -717,6 +781,35 @@ def __repr__(self): _cv2_interpolation_to_str[self.interpolation]) +class PadStride(object): + """ padding image for model with FPN, instead PadBatch(pad_to_stride) in original config + Args: + stride (bool): model with FPN need image shape % stride == 0 + """ + + def __init__(self, stride=0): + self.coarsest_stride = stride + + def __call__(self, im, im_info = False): + """ + Args: + im (np.ndarray): image (np.ndarray) + im_info (dict): info of image + Returns: + im (np.ndarray): processed image (np.ndarray) + im_info (dict): info of processed image + """ + coarsest_stride = self.coarsest_stride + if coarsest_stride <= 0: + return im + im_c, im_h, im_w = im.shape + pad_h = int(np.ceil(float(im_h) / coarsest_stride) * coarsest_stride) + pad_w = int(np.ceil(float(im_w) / coarsest_stride) * coarsest_stride) + padding_im = np.zeros((im_c, pad_h, pad_w), dtype=np.float32) + padding_im[:, :im_h, :im_w] = im + return padding_im, im_info + + class ResizeByFactor(object): """Resize the input numpy array Image to a size multiple of factor which is usually required by a network @@ -768,31 +861,13 @@ def __repr__(self): self.factor, self.max_side_len) -class PadStride(object): - def __init__(self, stride): - self.coarsest_stride = stride - - def __call__(self, img): - coarsest_stride = self.coarsest_stride - if coarsest_stride == 0: - return img - im_c, im_h, im_w = img.shape - pad_h = int(np.ceil(float(im_h) / coarsest_stride) * coarsest_stride) - pad_w = int(np.ceil(float(im_w) / coarsest_stride) * coarsest_stride) - padding_im = np.zeros((im_c, pad_h, pad_w), dtype=np.float32) - padding_im[:, :im_h, :im_w] = img - im_info = {} - im_info['resize_shape'] = padding_im.shape[1:] - return padding_im - - class Transpose(object): def __init__(self, transpose_target): self.transpose_target = transpose_target - def __call__(self, img): - return F.transpose(img, self.transpose_target) - return img + def __call__(self, im, im_info = False): + im = F.transpose(im, self.transpose_target) + return im, im_info def __repr__(self): format_string = self.__class__.__name__ + \ From 1659fad659885d43d005c7fb3f65b24a3f7d6331 Mon Sep 17 00:00:00 2001 From: huangjianhui <852142024@qq.com> Date: Mon, 18 Oct 2021 16:08:34 +0800 Subject: [PATCH 2/7] Update image_reader.py --- python/paddle_serving_app/reader/image_reader.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/python/paddle_serving_app/reader/image_reader.py b/python/paddle_serving_app/reader/image_reader.py index 3a323a4b3..380079c28 100644 --- a/python/paddle_serving_app/reader/image_reader.py +++ b/python/paddle_serving_app/reader/image_reader.py @@ -637,7 +637,7 @@ def __init__(self, mean, std, is_scale=True): self.std = std self.is_scale = is_scale - def __call__(self, im, im_info = False): + def __call__(self, im, im_info = None): """ Args: im (np.ndarray): image (np.ndarray) @@ -790,7 +790,7 @@ class PadStride(object): def __init__(self, stride=0): self.coarsest_stride = stride - def __call__(self, im, im_info = False): + def __call__(self, im, im_info = None): """ Args: im (np.ndarray): image (np.ndarray) @@ -865,7 +865,7 @@ class Transpose(object): def __init__(self, transpose_target): self.transpose_target = transpose_target - def __call__(self, im, im_info = False): + def __call__(self, im, im_info = None): im = F.transpose(im, self.transpose_target) return im, im_info From da0f7b12e82be25aef40b3d6878b54c5ccd2e978 Mon Sep 17 00:00:00 2001 From: huangjianhui <852142024@qq.com> Date: Mon, 18 Oct 2021 17:19:46 +0800 Subject: [PATCH 3/7] Update test_client.py --- .../ttfnet_darknet53_1x_coco/test_client.py | 21 ++++++++++++------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/python/examples/detection/ttfnet_darknet53_1x_coco/test_client.py b/python/examples/detection/ttfnet_darknet53_1x_coco/test_client.py index 59024d010..114f43c1b 100644 --- a/python/examples/detection/ttfnet_darknet53_1x_coco/test_client.py +++ b/python/examples/detection/ttfnet_darknet53_1x_coco/test_client.py @@ -11,16 +11,18 @@ # 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 * import sys import numpy as np +from paddle_serving_client import Client +from paddle_serving_app.reader import * +import cv2 preprocess = Sequential([ - File2Image(), BGR2RGB(), - Normalize([123.675, 116.28, 103.53], [58.395, 57.12, 57.375], False), - Resize((512, 512)), Transpose((2, 0, 1)) + File2Image(), + Resize( + (512, 512), False, interpolation=cv2.INTER_LINEAR), + Normalize([123.675, 116.28, 103.53], [58.395, 57.12, 57.375], False), + Transpose((2,0,1)) ]) postprocess = RCNNPostprocess("label_list.txt", "output") @@ -29,11 +31,14 @@ client.load_client_config("serving_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9494']) -im = preprocess(sys.argv[1]) +im, im_info = preprocess(sys.argv[1]) + + fetch_map = client.predict( feed={ "image": im, - "scale_factor": np.array([1.0, 1.0]).reshape(-1), + "im_shape": np.array(list(im.shape[1:])).reshape(-1), + "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], batch=False) From 26ff9e79e9af9b2276624685bf007f44915bbc86 Mon Sep 17 00:00:00 2001 From: huangjianhui <852142024@qq.com> Date: Mon, 18 Oct 2021 18:19:06 +0800 Subject: [PATCH 4/7] Update README.md --- python/examples/detection/ttfnet_darknet53_1x_coco/README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/examples/detection/ttfnet_darknet53_1x_coco/README.md b/python/examples/detection/ttfnet_darknet53_1x_coco/README.md index d6ffb912c..adf5de2ab 100644 --- a/python/examples/detection/ttfnet_darknet53_1x_coco/README.md +++ b/python/examples/detection/ttfnet_darknet53_1x_coco/README.md @@ -4,7 +4,7 @@ ### Get Model ``` -wget --no-check-certificate https://paddle-serving.bj.bcebos.com/pddet_demo/2.0/ttfnet_darknet53_1x_coco.tar +wget --no-check-certificate https://paddle-serving.bj.bcebos.com/pddet_demo/ttfnet_darknet53_1x_coco.tar ``` ### Start the service From 601e2beb7ae6a242f682420837271247d1ee76ce Mon Sep 17 00:00:00 2001 From: huangjianhui <852142024@qq.com> Date: Mon, 18 Oct 2021 18:19:39 +0800 Subject: [PATCH 5/7] Update README_CN.md --- python/examples/detection/ttfnet_darknet53_1x_coco/README_CN.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/examples/detection/ttfnet_darknet53_1x_coco/README_CN.md b/python/examples/detection/ttfnet_darknet53_1x_coco/README_CN.md index 7a1d3d1ab..7a2c86096 100644 --- a/python/examples/detection/ttfnet_darknet53_1x_coco/README_CN.md +++ b/python/examples/detection/ttfnet_darknet53_1x_coco/README_CN.md @@ -4,7 +4,7 @@ ## 获得模型 ``` -wget --no-check-certificate https://paddle-serving.bj.bcebos.com/pddet_demo/2.0/ttfnet_darknet53_1x_coco.tar +wget --no-check-certificate https://paddle-serving.bj.bcebos.com/pddet_demo/ttfnet_darknet53_1x_coco.tar ``` From cfdc087f66724bb9fd41827787f5be06cb38dde8 Mon Sep 17 00:00:00 2001 From: felixhjh <852142024@qq.com> Date: Tue, 19 Oct 2021 03:20:00 +0000 Subject: [PATCH 6/7] modify detection examples preprocess fix detection examples --- python/examples/cascade_rcnn/README.md | 2 +- python/examples/cascade_rcnn/README_CN.md | 2 +- python/examples/cascade_rcnn/test_client.py | 12 +- .../faster_rcnn_hrnetv2p_w18_1x/README_CN.md | 2 +- .../test_client.py | 12 +- .../test_client.py | 12 +- .../fcos_dcn_r50_fpn_1x_coco/test_client.py | 14 +- .../ppyolo_r50vd_dcn_1x_coco/test_client.py | 10 +- .../ssd_vgg16_300_240e_voc/label_list.txt | 88 ++------ .../ssd_vgg16_300_240e_voc/test_client.py | 16 +- .../ttfnet_darknet53_1x_coco/test_client.py | 10 +- .../yolov3_darknet53_270e_coco/test_client.py | 13 +- python/paddle_serving_app/reader/__init__.py | 1 + .../paddle_serving_app/reader/image_reader.py | 204 ++++++++++++++++-- 14 files changed, 258 insertions(+), 140 deletions(-) diff --git a/python/examples/cascade_rcnn/README.md b/python/examples/cascade_rcnn/README.md index 0f831a400..8029f39a1 100644 --- a/python/examples/cascade_rcnn/README.md +++ b/python/examples/cascade_rcnn/README.md @@ -15,7 +15,7 @@ python3 -m paddle_serving_server.serve --model serving_server --port 9292 --gpu_ ### Perform prediction ``` -python3 test_client.py +python3 test_client.py 000000570688.jpg ``` Image with bounding boxes and json result would be saved in `output` folder. diff --git a/python/examples/cascade_rcnn/README_CN.md b/python/examples/cascade_rcnn/README_CN.md index 0cc65ed68..828aba8a9 100644 --- a/python/examples/cascade_rcnn/README_CN.md +++ b/python/examples/cascade_rcnn/README_CN.md @@ -15,7 +15,7 @@ python3 -m paddle_serving_server.serve --model serving_server --port 9292 --gpu_ ### 执行预测 ``` -python3 test_client.py +python3 test_client.py 000000570688.jpg ``` 客户端已经为图片做好了后处理,在`output`文件夹下存放各个框的json格式信息还有后处理结果图片。 diff --git a/python/examples/cascade_rcnn/test_client.py b/python/examples/cascade_rcnn/test_client.py index cd90e5f15..aac9f6721 100644 --- a/python/examples/cascade_rcnn/test_client.py +++ b/python/examples/cascade_rcnn/test_client.py @@ -18,12 +18,12 @@ from paddle_serving_app.reader import * import cv2 -preprocess = Sequential([ - File2Image(), - Resize((800, 1333), True, interpolation=2), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), - Transpose((2,0,1)), - PadStride(32) +preprocess = DetectionSequential([ + DetectionFile2Image(), + DetectionResize((800, 1333), True, interpolation=2), + DetectionNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + DetectionTranspose((2,0,1)), + DetectionPadStride(32) ]) postprocess = RCNNPostprocess("label_list.txt", "output") diff --git a/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/README_CN.md b/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/README_CN.md index 95263591e..11dcbd85f 100644 --- a/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/README_CN.md +++ b/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/README_CN.md @@ -10,7 +10,7 @@ wget --no-check-certificate https://paddle-serving.bj.bcebos.com/pddet_demo/fast ### 启动服务 ``` -tar xf faster_rcnn_hrnetv2p_w18_1x.tar +tar xf faster_rcnn_hrnetv2p_w18_1x.tar.gz python3 -m paddle_serving_server.serve --model serving_server --port 9494 --gpu_ids 0 ``` 该模型支持TensorRT,如果想要更快的预测速度,可以开启`--use_trt`选项,但此时需要额外设置子图的TRT变长最大最小最优shape. diff --git a/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/test_client.py b/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/test_client.py index b68a0c7b4..329f6effb 100644 --- a/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/test_client.py +++ b/python/examples/detection/faster_rcnn_hrnetv2p_w18_1x/test_client.py @@ -18,12 +18,12 @@ from paddle_serving_app.reader import * import cv2 -preprocess = Sequential([ - File2Image(), - Resize((800, 1333), True, interpolation=2), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), - Transpose((2,0,1)), - PadStride(32) +preprocess = DetectionSequential([ + DetectionFile2Image(), + DetectionResize((800, 1333), True, interpolation=2), + DetectionNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + DetectionTranspose((2,0,1)), + DetectionPadStride(32) ]) postprocess = RCNNPostprocess("label_list.txt", "output") diff --git a/python/examples/detection/faster_rcnn_r50_fpn_1x_coco/test_client.py b/python/examples/detection/faster_rcnn_r50_fpn_1x_coco/test_client.py index 5d5e0f2b1..b6b2c534b 100644 --- a/python/examples/detection/faster_rcnn_r50_fpn_1x_coco/test_client.py +++ b/python/examples/detection/faster_rcnn_r50_fpn_1x_coco/test_client.py @@ -18,13 +18,13 @@ from paddle_serving_app.reader import * import cv2 -preprocess = Sequential([ - File2Image(), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), - Resize( +preprocess = DetectionSequential([ + DetectionFile2Image(), + DetectionNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + DetectionResize( (800, 1333), True, interpolation=cv2.INTER_LINEAR), - Transpose((2,0,1)), - PadStride(128) + DetectionTranspose((2,0,1)), + DetectionPadStride(128) ]) postprocess = RCNNPostprocess("label_list.txt", "output") diff --git a/python/examples/detection/fcos_dcn_r50_fpn_1x_coco/test_client.py b/python/examples/detection/fcos_dcn_r50_fpn_1x_coco/test_client.py index 4c441f5db..7ad59d75b 100644 --- a/python/examples/detection/fcos_dcn_r50_fpn_1x_coco/test_client.py +++ b/python/examples/detection/fcos_dcn_r50_fpn_1x_coco/test_client.py @@ -18,16 +18,16 @@ from paddle_serving_app.reader import * import cv2 -preprocess = Sequential([ - File2Image(), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), - Resize( +preprocess = DetectionSequential([ + DetectionFile2Image(), + DetectionNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + DetectionResize( (800, 1333), True, interpolation=cv2.INTER_LINEAR), - Transpose((2,0,1)), - PadStride(128) + DetectionTranspose((2,0,1)), + DetectionPadStride(128) ]) -postprocess = RCNNPostprocess("label_list.txt", "output", [608, 608]) +postprocess = RCNNPostprocess("label_list.txt", "output") client = Client() client.load_client_config("serving_client/serving_client_conf.prototxt") diff --git a/python/examples/detection/ppyolo_r50vd_dcn_1x_coco/test_client.py b/python/examples/detection/ppyolo_r50vd_dcn_1x_coco/test_client.py index f26e13446..f40f2d5c8 100644 --- a/python/examples/detection/ppyolo_r50vd_dcn_1x_coco/test_client.py +++ b/python/examples/detection/ppyolo_r50vd_dcn_1x_coco/test_client.py @@ -18,12 +18,12 @@ from paddle_serving_app.reader import * import cv2 -preprocess = Sequential([ - File2Image(), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), - Resize( +preprocess = DetectionSequential([ + DetectionFile2Image(), + DetectionNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + DetectionResize( (608, 608), False, interpolation=2), - Transpose((2,0,1)) + DetectionTranspose((2,0,1)) ]) postprocess = RCNNPostprocess("label_list.txt", "output") diff --git a/python/examples/detection/ssd_vgg16_300_240e_voc/label_list.txt b/python/examples/detection/ssd_vgg16_300_240e_voc/label_list.txt index 941cb4e13..8420ab35e 100644 --- a/python/examples/detection/ssd_vgg16_300_240e_voc/label_list.txt +++ b/python/examples/detection/ssd_vgg16_300_240e_voc/label_list.txt @@ -1,80 +1,20 @@ -person +aeroplane bicycle -car -motorcycle -airplane -bus -train -truck -boat -traffic light -fire hydrant -stop sign -parking meter -bench bird +boat +bottle +bus +car cat +chair +cow +diningtable dog horse +motorbike +person +pottedplant sheep -cow -elephant -bear -zebra -giraffe -backpack -umbrella -handbag -tie -suitcase -frisbee -skis -snowboard -sports ball -kite -baseball bat -baseball glove -skateboard -surfboard -tennis racket -bottle -wine glass -cup -fork -knife -spoon -bowl -banana -apple -sandwich -orange -broccoli -carrot -hot dog -pizza -donut -cake -chair -couch -potted plant -bed -dining table -toilet -tv -laptop -mouse -remote -keyboard -cell phone -microwave -oven -toaster -sink -refrigerator -book -clock -vase -scissors -teddy bear -hair drier -toothbrush +sofa +train +tvmonitor diff --git a/python/examples/detection/ssd_vgg16_300_240e_voc/test_client.py b/python/examples/detection/ssd_vgg16_300_240e_voc/test_client.py index 4c441f5db..1df635c89 100644 --- a/python/examples/detection/ssd_vgg16_300_240e_voc/test_client.py +++ b/python/examples/detection/ssd_vgg16_300_240e_voc/test_client.py @@ -18,16 +18,15 @@ from paddle_serving_app.reader import * import cv2 -preprocess = Sequential([ - File2Image(), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), - Resize( - (800, 1333), True, interpolation=cv2.INTER_LINEAR), - Transpose((2,0,1)), - PadStride(128) +preprocess = DetectionSequential([ + DetectionFile2Image(), + DetectionResize( + (300, 300), False, interpolation=cv2.INTER_LINEAR), + DetectionNormalize([104.0, 117.0, 123.0], [1.0, 1.0, 1.0], False), + DetectionTranspose((2,0,1)), ]) -postprocess = RCNNPostprocess("label_list.txt", "output", [608, 608]) +postprocess = RCNNPostprocess("label_list.txt", "output") client = Client() client.load_client_config("serving_client/serving_client_conf.prototxt") @@ -37,6 +36,7 @@ fetch_map = client.predict( feed={ "image": im, + "im_shape": np.array(list(im.shape[1:])).reshape(-1), "scale_factor": im_info['scale_factor'], }, fetch=["save_infer_model/scale_0.tmp_1"], diff --git a/python/examples/detection/ttfnet_darknet53_1x_coco/test_client.py b/python/examples/detection/ttfnet_darknet53_1x_coco/test_client.py index 114f43c1b..f735c01bc 100644 --- a/python/examples/detection/ttfnet_darknet53_1x_coco/test_client.py +++ b/python/examples/detection/ttfnet_darknet53_1x_coco/test_client.py @@ -17,12 +17,12 @@ from paddle_serving_app.reader import * import cv2 -preprocess = Sequential([ - File2Image(), - Resize( +preprocess = DetectionSequential([ + DetectionFile2Image(), + DetectionResize( (512, 512), False, interpolation=cv2.INTER_LINEAR), - Normalize([123.675, 116.28, 103.53], [58.395, 57.12, 57.375], False), - Transpose((2,0,1)) + DetectionNormalize([123.675, 116.28, 103.53], [58.395, 57.12, 57.375], False), + DetectionTranspose((2,0,1)) ]) postprocess = RCNNPostprocess("label_list.txt", "output") diff --git a/python/examples/detection/yolov3_darknet53_270e_coco/test_client.py b/python/examples/detection/yolov3_darknet53_270e_coco/test_client.py index 5d5e0f2b1..04f21b32a 100644 --- a/python/examples/detection/yolov3_darknet53_270e_coco/test_client.py +++ b/python/examples/detection/yolov3_darknet53_270e_coco/test_client.py @@ -18,13 +18,12 @@ from paddle_serving_app.reader import * import cv2 -preprocess = Sequential([ - File2Image(), - Normalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), - Resize( - (800, 1333), True, interpolation=cv2.INTER_LINEAR), - Transpose((2,0,1)), - PadStride(128) +preprocess = DetectionSequential([ + DetectionFile2Image(), + DetectionResize( + (608, 608), False, interpolation=2), + DetectionNormalize([0.485, 0.456, 0.406], [0.229, 0.224, 0.225], True), + DetectionTranspose((2,0,1)), ]) postprocess = RCNNPostprocess("label_list.txt", "output") diff --git a/python/paddle_serving_app/reader/__init__.py b/python/paddle_serving_app/reader/__init__.py index 7d7ebad6f..185b20992 100644 --- a/python/paddle_serving_app/reader/__init__.py +++ b/python/paddle_serving_app/reader/__init__.py @@ -13,6 +13,7 @@ # limitations under the License. from .chinese_bert_reader import ChineseBertReader from .image_reader import ImageReader, File2Image, URL2Image, Sequential, Normalize, Base64ToImage +from .image_reader import DetectionFile2Image, DetectionSequential, DetectionNormalize, DetectionTranspose, DetectionResize, DetectionBGR2RGB, DetectionPadStride from .image_reader import CenterCrop, Resize, Transpose, Div, RGB2BGR, BGR2RGB, ResizeByFactor from .image_reader import RCNNPostprocess, SegPostprocess, PadStride, BlazeFacePostprocess from .image_reader import DBPostProcess, FilterBoxes, GetRotateCropImage, SortedBoxes diff --git a/python/paddle_serving_app/reader/image_reader.py b/python/paddle_serving_app/reader/image_reader.py index 380079c28..24d1759a0 100644 --- a/python/paddle_serving_app/reader/image_reader.py +++ b/python/paddle_serving_app/reader/image_reader.py @@ -481,6 +481,37 @@ class Sequential(object): >>> ]) """ + def __init__(self, transforms): + self.transforms = transforms + + def __call__(self, img): + for t in self.transforms: + img = t(img) + return img + + def __repr__(self): + format_string_ = self.__class__.__name__ + '(' + for t in self.transforms: + format_string_ += '\n' + format_string_ += ' {0}'.format(t) + format_string_ += '\n)' + return format_string_ + + +class DetectionSequential(object): + """ + Args: + sequence (sequence of ``Transform`` objects): list of transforms to chain. + + This API references some of the design pattern of torchvision + Users can simply use this API in training as well + + Example: + >>> image_reader.Sequnece([ + >>> transforms.CenterCrop(10), + >>> ]) + """ + def __init__(self, transforms): self.transforms = transforms @@ -518,7 +549,18 @@ class BGR2RGB(object): def __init__(self): pass - def __call__(self, img, img_info = False): + def __call__(self, img): + return img[:, :, ::-1] + + def __repr__(self): + return self.__class__.__name__ + "()" + + +class DetectionBGR2RGB(object): + def __init__(self): + pass + + def __call__(self, img, img_info=None): return img[:, :, ::-1], img_info def __repr__(self): @@ -542,7 +584,30 @@ class File2Image(object): def __init__(self): pass - def __call__(self, img_path, im_info): + def __call__(self, img_path): + if py_version == 2: + fin = open(img_path) + else: + fin = open(img_path, "rb") + sample = fin.read() + data = np.fromstring(sample, np.uint8) + img = cv2.imdecode(data, cv2.IMREAD_COLOR) + ''' + img = cv2.imread(img_path, -1) + channels = img.shape[2] + ori_h = img.shape[0] + ori_w = img.shape[1] + ''' + return img + + def __repr__(self): + return self.__class__.__name__ + "()" + +class DetectionFile2Image(object): + def __init__(self): + pass + + def __call__(self, img_path, im_info=None): if py_version == 2: fin = open(img_path) else: @@ -557,8 +622,9 @@ def __call__(self, img_path, im_info): ori_h = img.shape[0] ori_w = img.shape[1] ''' - im_info['im_shape'] = np.array(img.shape[:2], dtype=np.float32) - im_info['scale_factor'] = np.array([1., 1.], dtype=np.float32) + if im_info is not None: + im_info['im_shape'] = np.array(img.shape[:2], dtype=np.float32) + im_info['scale_factor'] = np.array([1., 1.], dtype=np.float32) return img, im_info def __repr__(self): @@ -600,7 +666,28 @@ class Div(object): def __init__(self, value): self.value = value - def __call__(self, img, img_info = False): + def __call__(self, img): + """ + Args: + img (numpy array): (int8 numpy array) + + Returns: + img (numpy array): (float32 numpy array) + """ + img = img.astype('float32') / self.value + + return img + + def __repr__(self): + return self.__class__.__name__ + "({})".format(self.value) + +class DetectionDiv(object): + """ divide by some float number """ + + def __init__(self, value): + self.value = value + + def __call__(self, img, img_info=None): """ Args: img (numpy array): (int8 numpy array) @@ -622,6 +709,41 @@ class Normalize(object): will normalize each channel of the input ``torch.*Tensor`` i.e. ``output[channel] = (input[channel] - mean[channel]) / std[channel]`` + .. note:: + This transform acts out of place, i.e., it does not mutate the input tensor. + + Args: + mean (sequence): Sequence of means for each channel. + std (sequence): Sequence of standard deviations for each channel. + + """ + + def __init__(self, mean, std, channel_first=False): + self.mean = mean + self.std = std + self.channel_first = channel_first + + def __call__(self, img): + """ + Args: + img (numpy array): (C, H, W) to be normalized. + + Returns: + Tensor: Normalized Tensor image. + """ + return F.normalize(img, self.mean, self.std, self.channel_first) + + def __repr__(self): + return self.__class__.__name__ + '(mean={0}, std={1})'.format(self.mean, + self.std) + + +class DetectionNormalize(object): + """Normalize a tensor image with mean and standard deviation. + Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform + will normalize each channel of the input ``torch.*Tensor`` i.e. + ``output[channel] = (input[channel] - mean[channel]) / std[channel]`` + .. note:: This transform acts out of place, i.e., it does not mutate the input tensor. @@ -637,7 +759,7 @@ def __init__(self, mean, std, is_scale=True): self.std = std self.is_scale = is_scale - def __call__(self, im, im_info = None): + def __call__(self, im, im_info=None): """ Args: im (np.ndarray): image (np.ndarray) @@ -708,8 +830,33 @@ def __repr__(self): return self.__class__.__name__ + '(size={0})'.format(self.size) - class Resize(object): + """Resize the input numpy array Image to the given size. + + Args: + size (sequence or int): Desired output size. If size is a sequence like + (w, h), output size will be matched to this. If size is an int, + smaller edge of the image will be matched to this number. + i.e, if height > width, then image will be rescaled to + (size * height / width, size) + interpolation (int, optional): Desired interpolation. Default is + ``None`` + """ + + def __init__(self, size, max_size=2147483647, interpolation=None): + self.size = size + self.max_size = max_size + self.interpolation = interpolation + + def __call__(self, img): + return F.resize(img, self.size, self.max_size, self.interpolation) + + def __repr__(self): + return self.__class__.__name__ + '(size={0}, max_size={1}, interpolation={2})'.format( + self.size, self.max_size, + _cv2_interpolation_to_str[self.interpolation]) + +class DetectionResize(object): """resize image by target_size and max_size Args: target_size (int): the target size of image @@ -724,7 +871,7 @@ def __init__(self, target_size, keep_ratio=True, interpolation=cv2.INTER_LINEAR) self.keep_ratio = keep_ratio self.interpolation = interpolation - def __call__(self, im, im_info): + def __call__(self, im, im_info=None): """ Args: im (np.ndarray): image (np.ndarray) @@ -744,9 +891,10 @@ def __call__(self, im, im_info): fx=im_scale_x, fy=im_scale_y, interpolation=self.interpolation) - im_info['im_shape'] = np.array(im.shape[:2]).astype('float32') - im_info['scale_factor'] = np.array( - [im_scale_y, im_scale_x]).astype('float32') + if im_info is not None: + im_info['im_shape'] = np.array(im.shape[:2]).astype('float32') + im_info['scale_factor'] = np.array( + [im_scale_y, im_scale_x]).astype('float32') return im, im_info def generate_scale(self, im): @@ -782,6 +930,23 @@ def __repr__(self): class PadStride(object): + def __init__(self, stride): + self.coarsest_stride = stride + + def __call__(self, img): + coarsest_stride = self.coarsest_stride + if coarsest_stride == 0: + return img + im_c, im_h, im_w = img.shape + pad_h = int(np.ceil(float(im_h) / coarsest_stride) * coarsest_stride) + pad_w = int(np.ceil(float(im_w) / coarsest_stride) * coarsest_stride) + padding_im = np.zeros((im_c, pad_h, pad_w), dtype=np.float32) + padding_im[:, :im_h, :im_w] = img + im_info = {} + im_info['resize_shape'] = padding_im.shape[1:] + return padding_im + +class DetectionPadStride(object): """ padding image for model with FPN, instead PadBatch(pad_to_stride) in original config Args: stride (bool): model with FPN need image shape % stride == 0 @@ -790,7 +955,7 @@ class PadStride(object): def __init__(self, stride=0): self.coarsest_stride = stride - def __call__(self, im, im_info = None): + def __call__(self, im, im_info=None): """ Args: im (np.ndarray): image (np.ndarray) @@ -865,7 +1030,20 @@ class Transpose(object): def __init__(self, transpose_target): self.transpose_target = transpose_target - def __call__(self, im, im_info = None): + def __call__(self, img): + return F.transpose(img, self.transpose_target) + return img + + def __repr__(self): + format_string = self.__class__.__name__ + \ + "({})".format(self.transpose_target) + return format_string + +class DetectionTranspose(object): + def __init__(self, transpose_target): + self.transpose_target = transpose_target + + def __call__(self, im, im_info=None): im = F.transpose(im, self.transpose_target) return im, im_info From 60ad0d35a682774ae23a508c00cf2ff326b43cc5 Mon Sep 17 00:00:00 2001 From: huangjianhui <852142024@qq.com> Date: Tue, 19 Oct 2021 12:14:46 +0800 Subject: [PATCH 7/7] Update test_client.py --- python/examples/yolov4/test_client.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/python/examples/yolov4/test_client.py b/python/examples/yolov4/test_client.py index 3db9b3ec3..dfcd58610 100644 --- a/python/examples/yolov4/test_client.py +++ b/python/examples/yolov4/test_client.py @@ -29,7 +29,7 @@ client.load_client_config("yolov4_client/serving_client_conf.prototxt") client.connect(['127.0.0.1:9393']) -im, im_info = preprocess(sys.argv[1]) +im = preprocess(sys.argv[1]) fetch_map = client.predict( feed={ "image": im,