From e93c01e45896c0b58903f5bf15143989388f70ae Mon Sep 17 00:00:00 2001 From: wuzewu Date: Fri, 30 Oct 2020 21:02:59 +0800 Subject: [PATCH] Rename paddlehub.process -> paddlehub.transforms --- demo/colorization/predict.py | 2 +- demo/colorization/train.py | 2 +- demo/image_classification/train.py | 2 +- demo/style_transfer/train.py | 2 +- .../user_guided_colorization/module.py | 2 +- .../openpose_body_estimation/module.py | 18 ++++++----- .../openpose_hands_estimation/module.py | 16 ++++++---- modules/image/style_transfer/msgnet/module.py | 32 +++++++++---------- paddlehub/datasets/canvas.py | 2 +- paddlehub/datasets/minicoco.py | 2 +- paddlehub/module/cv_module.py | 4 +-- .../detect_transforms.py | 15 ++++++--- .../{process => transforms}/functional.py | 0 .../{process => transforms}/transforms.py | 30 +++++++++-------- 14 files changed, 70 insertions(+), 59 deletions(-) rename paddlehub/{process => transforms}/detect_transforms.py (98%) rename paddlehub/{process => transforms}/functional.py (100%) rename paddlehub/{process => transforms}/transforms.py (96%) diff --git a/demo/colorization/predict.py b/demo/colorization/predict.py index f24dab79..e054f0c0 100644 --- a/demo/colorization/predict.py +++ b/demo/colorization/predict.py @@ -2,5 +2,5 @@ import paddle import paddlehub as hub if __name__ == '__main__': - model = hub.Module(name='user_guided_colorization', load_checkpoint='/PATH/TO/CHECKPOINT') + model = hub.Module(name='user_guided_colorization') result = model.predict(images='house.png') diff --git a/demo/colorization/train.py b/demo/colorization/train.py index d8c24537..1576d112 100644 --- a/demo/colorization/train.py +++ b/demo/colorization/train.py @@ -1,6 +1,6 @@ import paddle import paddlehub as hub -import paddlehub.process.transforms as T +import paddlehub.transforms.transforms as T from paddlehub.finetune.trainer import Trainer from paddlehub.datasets import Canvas diff --git a/demo/image_classification/train.py b/demo/image_classification/train.py index 5619ace6..59798a2f 100644 --- a/demo/image_classification/train.py +++ b/demo/image_classification/train.py @@ -1,6 +1,6 @@ import paddle import paddlehub as hub -import paddlehub.process.transforms as T +import paddlehub.transforms.transforms as T from paddlehub.finetune.trainer import Trainer from paddlehub.datasets import Flowers diff --git a/demo/style_transfer/train.py b/demo/style_transfer/train.py index 12cc7f13..f370e62a 100644 --- a/demo/style_transfer/train.py +++ b/demo/style_transfer/train.py @@ -1,6 +1,6 @@ import paddle import paddlehub as hub -import paddlehub.process.transforms as T +import paddlehub.transforms.transforms as T from paddlehub.finetune.trainer import Trainer from paddlehub.datasets import MiniCOCO diff --git a/modules/image/colorization/user_guided_colorization/module.py b/modules/image/colorization/user_guided_colorization/module.py index 8983d28b..2255143c 100644 --- a/modules/image/colorization/user_guided_colorization/module.py +++ b/modules/image/colorization/user_guided_colorization/module.py @@ -18,7 +18,7 @@ import paddle import paddle.nn as nn from paddle.nn import Conv2D, Conv2DTranspose from paddlehub.module.module import moduleinfo -import paddlehub.process.transforms as T +import paddlehub.transforms.transforms as T from paddlehub.module.cv_module import ImageColorizeModule from user_guided_colorization.data_feed import ColorizePreprocess diff --git a/modules/image/keypoint_detection/openpose_body_estimation/module.py b/modules/image/keypoint_detection/openpose_body_estimation/module.py index 457f21a6..af499c71 100644 --- a/modules/image/keypoint_detection/openpose_body_estimation/module.py +++ b/modules/image/keypoint_detection/openpose_body_estimation/module.py @@ -20,18 +20,19 @@ import cv2 import paddle import paddle.nn as nn import numpy as np -from paddlehub.module.module import moduleinfo -import paddlehub.process.transforms as T +from paddlehub.transforms.module import moduleinfo +import paddlehub.transforms.transforms as T import openpose_body_estimation.processor as P -@moduleinfo(name="openpose_body_estimation", - type="CV/image_editing", - author="paddlepaddle", - author_email="", - summary="Openpose_body_estimation is a body pose estimation model based on Realtime Multi-Person 2D Pose \ +@moduleinfo( + name="openpose_body_estimation", + type="CV/image_editing", + author="paddlepaddle", + author_email="", + summary="Openpose_body_estimation is a body pose estimation model based on Realtime Multi-Person 2D Pose \ Estimation using Part Affinity Fields.", - version="1.0.0") + version="1.0.0") class BodyPoseModel(nn.Layer): """ BodyposeModel @@ -40,6 +41,7 @@ class BodyPoseModel(nn.Layer): load_checkpoint(str): Checkpoint save path, default is None. visualization (bool): Whether to save the estimation result. Default is True. """ + def __init__(self, load_checkpoint: str = None, visualization: bool = True): super(BodyPoseModel, self).__init__() diff --git a/modules/image/keypoint_detection/openpose_hands_estimation/module.py b/modules/image/keypoint_detection/openpose_hands_estimation/module.py index fedbde9d..0533dd9b 100644 --- a/modules/image/keypoint_detection/openpose_hands_estimation/module.py +++ b/modules/image/keypoint_detection/openpose_hands_estimation/module.py @@ -25,18 +25,19 @@ from skimage.measure import label from scipy.ndimage.filters import gaussian_filter from paddlehub.module.module import moduleinfo from paddlehub.process.functional import npmax -import paddlehub.process.transforms as T +import paddlehub.transforms.transforms as T import openpose_hands_estimation.processor as P -@moduleinfo(name="openpose_hands_estimation", - type="CV/image_editing", - author="paddlepaddle", - author_email="", - summary="Openpose_hands_estimation is a hand pose estimation model based on Hand Keypoint Detection in \ +@moduleinfo( + name="openpose_hands_estimation", + type="CV/image_editing", + author="paddlepaddle", + author_email="", + summary="Openpose_hands_estimation is a hand pose estimation model based on Hand Keypoint Detection in \ Single Images using Multiview Bootstrapping.", - version="1.0.0") + version="1.0.0") class HandPoseModel(nn.Layer): """ HandposeModel @@ -45,6 +46,7 @@ class HandPoseModel(nn.Layer): load_checkpoint(str): Checkpoint save path, default is None. visualization (bool): Whether to save the estimation result. Default is True. """ + def __init__(self, load_checkpoint: str = None, visualization: bool = True): super(HandPoseModel, self).__init__() self.visualization = visualization diff --git a/modules/image/style_transfer/msgnet/module.py b/modules/image/style_transfer/msgnet/module.py index 63eddcd4..3b23db3a 100644 --- a/modules/image/style_transfer/msgnet/module.py +++ b/modules/image/style_transfer/msgnet/module.py @@ -7,12 +7,13 @@ import paddle.nn.functional as F from paddlehub.env import MODULE_HOME from paddlehub.module.module import moduleinfo -from paddlehub.process.transforms import Compose, Resize, CenterCrop, SetType +from paddlehub.transforms.transforms import Compose, Resize, CenterCrop, SetType from paddlehub.module.cv_module import StyleTransferModule class GramMatrix(nn.Layer): """Calculate gram matrix""" + def forward(self, y): (b, ch, h, w) = y.shape features = y.reshape((b, ch, w * h)) @@ -23,6 +24,7 @@ class GramMatrix(nn.Layer): class ConvLayer(nn.Layer): """Basic conv layer with reflection padding layer""" + def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int): super(ConvLayer, self).__init__() pad = int(np.floor(kernel_size / 2)) @@ -50,6 +52,7 @@ class UpsampleConvLayer(nn.Layer): Return: img(paddle.Tensor): UpsampleConvLayer output. """ + def __init__(self, in_channels: int, out_channels: int, kernel_size: int, stride: int, upsample=None): super(UpsampleConvLayer, self).__init__() self.upsample = upsample @@ -84,6 +87,7 @@ class Bottleneck(nn.Layer): Return: img(paddle.Tensor): Bottleneck output. """ + def __init__(self, inplanes: int, planes: int, @@ -97,8 +101,8 @@ class Bottleneck(nn.Layer): self.residual_layer = nn.Conv2D(inplanes, planes * self.expansion, kernel_size=1, stride=stride) conv_block = (norm_layer(inplanes), nn.ReLU(), nn.Conv2D(inplanes, planes, kernel_size=1, stride=1), norm_layer(planes), nn.ReLU(), ConvLayer(planes, planes, kernel_size=3, stride=stride), - norm_layer(planes), nn.ReLU(), nn.Conv2D(planes, planes * self.expansion, kernel_size=1, - stride=1)) + norm_layer(planes), nn.ReLU(), nn.Conv2D( + planes, planes * self.expansion, kernel_size=1, stride=1)) self.conv_block = nn.Sequential(*conv_block) def forward(self, x: paddle.Tensor): @@ -124,14 +128,12 @@ class UpBottleneck(nn.Layer): Return: img(paddle.Tensor): UpBottleneck output. """ + def __init__(self, inplanes: int, planes: int, stride: int = 2, norm_layer: nn.Layer = nn.BatchNorm2D): super(UpBottleneck, self).__init__() self.expansion = 4 - self.residual_layer = UpsampleConvLayer(inplanes, - planes * self.expansion, - kernel_size=1, - stride=1, - upsample=stride) + self.residual_layer = UpsampleConvLayer( + inplanes, planes * self.expansion, kernel_size=1, stride=1, upsample=stride) conv_block = [] conv_block += [norm_layer(inplanes), nn.ReLU(), nn.Conv2D(inplanes, planes, kernel_size=1, stride=1)] conv_block += [ @@ -162,6 +164,7 @@ class Inspiration(nn.Layer): Return: img(paddle.Tensor): UpBottleneck output. """ + def __init__(self, C: int, B: int = 1): super(Inspiration, self).__init__() @@ -178,8 +181,8 @@ class Inspiration(nn.Layer): self.P = paddle.bmm(self.weight.expand_as(self.G), self.G) x = paddle.bmm( - self.P.transpose((0, 2, 1)).expand((X.shape[0], self.C, self.C)), X.reshape( - (X.shape[0], X.shape[1], -1))).reshape(X.shape) + self.P.transpose((0, 2, 1)).expand((X.shape[0], self.C, self.C)), X.reshape((X.shape[0], X.shape[1], + -1))).reshape(X.shape) return x def __repr__(self): @@ -189,6 +192,7 @@ class Inspiration(nn.Layer): class Vgg16(nn.Layer): """ First four layers from Vgg16.""" + def __init__(self): super(Vgg16, self).__init__() self.conv1_1 = nn.Conv2D(3, 64, kernel_size=3, stride=1, padding=1) @@ -263,12 +267,8 @@ class MSGNet(nn.Layer): Return: img(paddle.Tensor): MSGNet output. """ - def __init__(self, - input_nc=3, - output_nc=3, - ngf=128, - n_blocks=6, - norm_layer=nn.InstanceNorm2D, + + def __init__(self, input_nc=3, output_nc=3, ngf=128, n_blocks=6, norm_layer=nn.InstanceNorm2D, load_checkpoint=None): super(MSGNet, self).__init__() self.gram = GramMatrix() diff --git a/paddlehub/datasets/canvas.py b/paddlehub/datasets/canvas.py index cdec23d9..a7f9b9e3 100644 --- a/paddlehub/datasets/canvas.py +++ b/paddlehub/datasets/canvas.py @@ -18,7 +18,7 @@ import os import numpy as np import paddle -from paddlehub.process.functional import get_img_file +from paddlehub.transforms.functional import get_img_file from paddlehub.env import DATA_HOME from typing import Callable from paddlehub.utils.download import download_data diff --git a/paddlehub/datasets/minicoco.py b/paddlehub/datasets/minicoco.py index 515fdf22..4a187b76 100644 --- a/paddlehub/datasets/minicoco.py +++ b/paddlehub/datasets/minicoco.py @@ -17,7 +17,7 @@ import os from typing import Callable import paddle -from paddlehub.process.functional import get_img_file +from paddlehub.transforms.functional import get_img_file from paddlehub.env import DATA_HOME from paddlehub.utils.download import download_data diff --git a/paddlehub/module/cv_module.py b/paddlehub/module/cv_module.py index ad1d625c..bb3e7bb1 100644 --- a/paddlehub/module/cv_module.py +++ b/paddlehub/module/cv_module.py @@ -27,8 +27,8 @@ from PIL import Image from paddlehub.module.module import serving, RunModule from paddlehub.utils.utils import base64_to_cv2 -import paddlehub.process.transforms as T -import paddlehub.process.functional as Func +import paddlehub.transforms.transforms as T +import paddlehub.transforms.functional as Func class ImageServing(object): diff --git a/paddlehub/process/detect_transforms.py b/paddlehub/transforms/detect_transforms.py similarity index 98% rename from paddlehub/process/detect_transforms.py rename to paddlehub/transforms/detect_transforms.py index 5a50ffb0..7ccd1627 100644 --- a/paddlehub/process/detect_transforms.py +++ b/paddlehub/transforms/detect_transforms.py @@ -9,7 +9,7 @@ import PIL from PIL import Image, ImageEnhance from matplotlib import pyplot as plt -from paddlehub.process.functional import * +from paddlehub.transforms.functional import * matplotlib.use('Agg') @@ -27,6 +27,7 @@ class RandomDistort: data(dict): Image info and label info. """ + def __init__(self, lower: float = 0.5, upper: float = 1.5): self.lower = lower self.upper = upper @@ -70,6 +71,7 @@ class RandomExpand: data(dict): Image info and label info. """ + def __init__(self, max_ratio: float = 4., fill: list = None, keep_ratio: bool = True, thresh: float = 0.5): self.max_ratio = max_ratio @@ -129,10 +131,8 @@ class RandomCrop: data(dict): Image info and label info. """ - def __init__(self, - scales: list = [0.3, 1.0], - max_ratio: float = 2.0, - constraints: list = None, + + def __init__(self, scales: list = [0.3, 1.0], max_ratio: float = 2.0, constraints: list = None, max_trial: int = 50): self.scales = scales self.max_ratio = max_ratio @@ -198,6 +198,7 @@ class RandomFlip: img(np.ndarray): Distorted image. data(dict): Image info and label info. """ + def __init__(self, thresh: float = 0.5): self.thresh = thresh @@ -221,6 +222,7 @@ class Compose: img(np.ndarray): Preprocessed image. data(dict): Image info and label info, default is None. """ + def __init__(self, transforms: list): if not isinstance(transforms, list): raise TypeError('The transforms must be a list!') @@ -263,6 +265,7 @@ class Resize: img(np.ndarray): Preprocessed image. data(dict): Image info and label info, default is None. """ + def __init__(self, target_size: int = 512, interp: str = 'RANDOM'): self.interp_dict = { 'NEAREST': cv2.INTER_NEAREST, @@ -309,6 +312,7 @@ class Normalize: img(np.ndarray): Preprocessed image. data(dict): Image info and label info, default is None. """ + def __init__(self, mean=[0.5, 0.5, 0.5], std=[0.5, 0.5, 0.5]): self.mean = mean self.std = std @@ -332,6 +336,7 @@ class Normalize: class ShuffleBox: """Shuffle detection information for corresponding input image.""" + def __call__(self, img, data): gt = np.concatenate([data['gt_boxes'], data['gt_labels'][:, np.newaxis], data['gt_scores'][:, np.newaxis]], axis=1) diff --git a/paddlehub/process/functional.py b/paddlehub/transforms/functional.py similarity index 100% rename from paddlehub/process/functional.py rename to paddlehub/transforms/functional.py diff --git a/paddlehub/process/transforms.py b/paddlehub/transforms/transforms.py similarity index 96% rename from paddlehub/process/transforms.py rename to paddlehub/transforms/transforms.py index 36217669..cf2a53db 100644 --- a/paddlehub/process/transforms.py +++ b/paddlehub/transforms/transforms.py @@ -29,7 +29,7 @@ from matplotlib import pyplot as plt from matplotlib.figure import Figure from scipy.ndimage.filters import gaussian_filter from matplotlib.backends.backend_agg import FigureCanvasAgg as FigureCanvas -from paddlehub.process.functional import * +from paddlehub.transforms.functional import * matplotlib.use('Agg') @@ -252,13 +252,8 @@ class RandomPaddingCrop: pad_height = max(crop_height - img_height, 0) pad_width = max(crop_width - img_width, 0) if (pad_height > 0 or pad_width > 0): - im = cv2.copyMakeBorder(im, - 0, - pad_height, - 0, - pad_width, - cv2.BORDER_CONSTANT, - value=self.im_padding_value) + im = cv2.copyMakeBorder( + im, 0, pad_height, 0, pad_width, cv2.BORDER_CONSTANT, value=self.im_padding_value) if crop_height > 0 and crop_width > 0: h_off = np.random.randint(img_height - crop_height + 1) @@ -313,12 +308,13 @@ class RandomRotation: r[0, 2] += (nw / 2) - cx r[1, 2] += (nh / 2) - cy dsize = (nw, nh) - im = cv2.warpAffine(im, - r, - dsize=dsize, - flags=cv2.INTER_LINEAR, - borderMode=cv2.BORDER_CONSTANT, - borderValue=self.im_padding_value) + im = cv2.warpAffine( + im, + r, + dsize=dsize, + flags=cv2.INTER_LINEAR, + borderMode=cv2.BORDER_CONSTANT, + borderValue=self.im_padding_value) return im @@ -425,6 +421,7 @@ class RGB2LAB: """ Convert color space from RGB to LAB. """ + def rgb2xyz(self, rgb: np.ndarray) -> np.ndarray: """ Convert color space from RGB to XYZ. @@ -491,6 +488,7 @@ class LAB2RGB: """ Convert color space from LAB to RGB. """ + def __init__(self, mode: str = 'RGB2LAB'): self.mode = mode @@ -568,6 +566,7 @@ class ColorPostprocess: Return: img(np.ndarray): Image in range of 0-255. """ + def __init__(self, type: type = np.uint8): self.type = type @@ -588,6 +587,7 @@ class CenterCrop: Return: img(np.ndarray): Croped image. """ + def __init__(self, crop_size: int): self.crop_size = crop_size @@ -608,6 +608,7 @@ class SetType: Return: img(np.ndarray): Transformed image. """ + def __init__(self, datatype: type = 'float32'): self.type = datatype @@ -623,6 +624,7 @@ class ResizeScaling: target(int): Target image size. interp(Callable): Interpolation method. """ + def __init__(self, target: int = 368, interp: Callable = cv2.INTER_CUBIC): self.target = target self.interp = interp -- GitLab