diff --git a/demo/colorization/predict.py b/demo/colorization/predict.py index f24dab7995a223c7bb272ba29ef0e287704af6c5..e054f0c0e7b68efa1abc9d8bf15b1717f89be12a 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 d8c24537762f3da24dded330a45a745539fd00b6..1576d112193c0f0728387bcbd7f5aad297f03e59 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 5619ace6ac77056c80d4623548e738625fd282cf..59798a2fbbae90245e3794f4ff123bf1166baf0f 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 12cc7f13e91db56be386b2df66529415e398e1b3..f370e62a52aa740c5daa96e073c8e8163061745a 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 8983d28bdf8104dc25cb962e18ba614fcc86b59d..2255143c25668ec1846bb7cc56e6769f4b0a1468 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 457f21a6a529e0830782137863a660c18b51674f..af499c71b54f88dba496a69a0d3b08d8491d7c9e 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 fedbde9df66d6084a1a56b0ff5fe4f657c46908e..0533dd9bca3e4e8a0a5b2760ef41b9b6c0ea5bf8 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 63eddcd4b00aa66c76ea39c95506b46dd34a4fbf..3b23db3a8a7c04f1c714640df27c2fefc312ff76 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 cdec23d937514f6103e5bce98888d518b56231a2..a7f9b9e3eb907827d6bdd9de9c195c2acbe4e528 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 515fdf22aa317f61e6ff5fdeb0fdc08993dcec21..4a187b7627db7db00074c1be4163244e592f2c0c 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 ad1d625cff3834cd1b25d6938087970916649c20..bb3e7bb1fda299ba17cf201940b7302daaa436d9 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 5a50ffb01fcaf6106a25ff74a07beb822237400c..7ccd162774379082199f26460c1ec82cf9c50b17 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 36217669ab623f3d089903ebd1036e3133558613..cf2a53dbeb68932fd5d276f2413deeb75bff0c9d 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