Add vision api for hapi (#24404)
* add vision * fix predict, test=develop * add unittest for vision apis, test=develop * fix typos * add hapi models api, test=develop * fix code format, test=develop * fix typos, test=develop * fix sample code import, test=develop * fix sample codes, test=develop * add decompress, test=develop * rm darknet, test=develop * rm debug code, test=developrelease/2.0-alpha
parent
046b7ebc03
commit
1a0d26a4db
@ -0,0 +1,132 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# 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.
|
||||
|
||||
# when test, you should add hapi root path to the PYTHONPATH,
|
||||
# export PYTHONPATH=PATH_TO_HAPI:$PYTHONPATH
|
||||
import unittest
|
||||
import os
|
||||
import tempfile
|
||||
import cv2
|
||||
import shutil
|
||||
import numpy as np
|
||||
|
||||
from paddle.incubate.hapi.datasets import DatasetFolder
|
||||
from paddle.incubate.hapi.vision.transforms import transforms
|
||||
|
||||
|
||||
class TestTransforms(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.data_dir = tempfile.mkdtemp()
|
||||
for i in range(2):
|
||||
sub_dir = os.path.join(self.data_dir, 'class_' + str(i))
|
||||
if not os.path.exists(sub_dir):
|
||||
os.makedirs(sub_dir)
|
||||
for j in range(2):
|
||||
if j == 0:
|
||||
fake_img = (np.random.random(
|
||||
(280, 350, 3)) * 255).astype('uint8')
|
||||
else:
|
||||
fake_img = (np.random.random(
|
||||
(400, 300, 3)) * 255).astype('uint8')
|
||||
cv2.imwrite(os.path.join(sub_dir, str(j) + '.jpg'), fake_img)
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.data_dir)
|
||||
|
||||
def do_transform(self, trans):
|
||||
dataset_folder = DatasetFolder(self.data_dir, transform=trans)
|
||||
|
||||
for _ in dataset_folder:
|
||||
pass
|
||||
|
||||
def test_trans_all(self):
|
||||
normalize = transforms.Normalize(
|
||||
mean=[123.675, 116.28, 103.53], std=[58.395, 57.120, 57.375])
|
||||
trans = transforms.Compose([
|
||||
transforms.RandomResizedCrop(224), transforms.GaussianNoise(),
|
||||
transforms.ColorJitter(
|
||||
brightness=0.4, contrast=0.4, saturation=0.4,
|
||||
hue=0.4), transforms.RandomHorizontalFlip(),
|
||||
transforms.Permute(mode='CHW'), normalize
|
||||
])
|
||||
|
||||
self.do_transform(trans)
|
||||
|
||||
def test_trans_resize(self):
|
||||
trans = transforms.Compose([
|
||||
transforms.Resize(300, [0, 1]),
|
||||
transforms.RandomResizedCrop((280, 280)),
|
||||
transforms.Resize(280, [0, 1]),
|
||||
transforms.Resize((256, 200)),
|
||||
transforms.Resize((180, 160)),
|
||||
transforms.CenterCrop(128),
|
||||
transforms.CenterCrop((128, 128)),
|
||||
])
|
||||
self.do_transform(trans)
|
||||
|
||||
def test_trans_centerCrop(self):
|
||||
trans = transforms.Compose([
|
||||
transforms.CenterCropResize(224),
|
||||
transforms.CenterCropResize(128, 160),
|
||||
])
|
||||
self.do_transform(trans)
|
||||
|
||||
def test_flip(self):
|
||||
trans = transforms.Compose([
|
||||
transforms.RandomHorizontalFlip(1.0),
|
||||
transforms.RandomHorizontalFlip(0.0),
|
||||
transforms.RandomVerticalFlip(0.0),
|
||||
transforms.RandomVerticalFlip(1.0),
|
||||
])
|
||||
self.do_transform(trans)
|
||||
|
||||
def test_color_jitter(self):
|
||||
trans = transforms.BatchCompose([
|
||||
transforms.BrightnessTransform(0.0),
|
||||
transforms.HueTransform(0.0),
|
||||
transforms.SaturationTransform(0.0),
|
||||
transforms.ContrastTransform(0.0),
|
||||
])
|
||||
self.do_transform(trans)
|
||||
|
||||
def test_exception(self):
|
||||
trans = transforms.Compose([transforms.Resize(-1)])
|
||||
|
||||
trans_batch = transforms.BatchCompose([transforms.Resize(-1)])
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
self.do_transform(trans)
|
||||
|
||||
with self.assertRaises(Exception):
|
||||
self.do_transform(trans_batch)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
transforms.ContrastTransform(-1.0)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
transforms.SaturationTransform(-1.0),
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
transforms.HueTransform(-1.0)
|
||||
|
||||
with self.assertRaises(ValueError):
|
||||
transforms.BrightnessTransform(-1.0)
|
||||
|
||||
def test_info(self):
|
||||
str(transforms.Compose([transforms.Resize((224, 224))]))
|
||||
str(transforms.BatchCompose([transforms.Resize((224, 224))]))
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
@ -0,0 +1,84 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# 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.
|
||||
|
||||
import unittest
|
||||
import numpy as np
|
||||
|
||||
import paddle.incubate.hapi.vision.models as models
|
||||
from paddle.incubate.hapi.model import Input
|
||||
|
||||
|
||||
class TestVisonModels(unittest.TestCase):
|
||||
def models_infer(self, arch, pretrained=False, batch_norm=False):
|
||||
|
||||
x = np.array(np.random.random((2, 3, 224, 224)), dtype=np.float32)
|
||||
if batch_norm:
|
||||
model = models.__dict__[arch](pretrained=pretrained,
|
||||
batch_norm=True)
|
||||
else:
|
||||
model = models.__dict__[arch](pretrained=pretrained)
|
||||
inputs = [Input([None, 3, 224, 224], 'float32', name='image')]
|
||||
|
||||
model.prepare(inputs=inputs)
|
||||
|
||||
model.test_batch(x)
|
||||
|
||||
def test_mobilenetv2_pretrained(self):
|
||||
self.models_infer('mobilenet_v2', pretrained=True)
|
||||
|
||||
def test_mobilenetv1(self):
|
||||
self.models_infer('mobilenet_v1')
|
||||
|
||||
def test_vgg11(self):
|
||||
self.models_infer('vgg11')
|
||||
|
||||
def test_vgg13(self):
|
||||
self.models_infer('vgg13')
|
||||
|
||||
def test_vgg16(self):
|
||||
self.models_infer('vgg16')
|
||||
|
||||
def test_vgg16_bn(self):
|
||||
self.models_infer('vgg16', batch_norm=True)
|
||||
|
||||
def test_vgg19(self):
|
||||
self.models_infer('vgg19')
|
||||
|
||||
def test_resnet18(self):
|
||||
self.models_infer('resnet18')
|
||||
|
||||
def test_resnet34(self):
|
||||
self.models_infer('resnet34')
|
||||
|
||||
def test_resnet50(self):
|
||||
self.models_infer('resnet50')
|
||||
|
||||
def test_resnet101(self):
|
||||
self.models_infer('resnet101')
|
||||
|
||||
def test_resnet152(self):
|
||||
self.models_infer('resnet152')
|
||||
|
||||
def test_lenet(self):
|
||||
lenet = models.__dict__['LeNet']()
|
||||
|
||||
inputs = [Input([None, 1, 28, 28], 'float32', name='x')]
|
||||
lenet.prepare(inputs=inputs)
|
||||
|
||||
x = np.array(np.random.random((2, 1, 28, 28)), dtype=np.float32)
|
||||
lenet.test_batch(x)
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
unittest.main()
|
||||
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,245 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# 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.
|
||||
|
||||
import paddle.fluid as fluid
|
||||
from paddle.fluid.dygraph.nn import Conv2D, Pool2D, BatchNorm, Linear
|
||||
from paddle.fluid.dygraph.container import Sequential
|
||||
|
||||
from ...model import Model
|
||||
from ...download import get_weights_path_from_url
|
||||
|
||||
__all__ = [
|
||||
'VGG',
|
||||
'vgg11',
|
||||
'vgg13',
|
||||
'vgg16',
|
||||
'vgg19',
|
||||
]
|
||||
|
||||
model_urls = {
|
||||
'vgg16': ('https://paddle-hapi.bj.bcebos.com/models/vgg16.pdparams',
|
||||
'c788f453a3b999063e8da043456281ee')
|
||||
}
|
||||
|
||||
|
||||
class Classifier(fluid.dygraph.Layer):
|
||||
def __init__(self, num_classes, classifier_activation='softmax'):
|
||||
super(Classifier, self).__init__()
|
||||
self.linear1 = Linear(512 * 7 * 7, 4096)
|
||||
self.linear2 = Linear(4096, 4096)
|
||||
self.linear3 = Linear(4096, num_classes, act=classifier_activation)
|
||||
|
||||
def forward(self, x):
|
||||
x = self.linear1(x)
|
||||
x = fluid.layers.relu(x)
|
||||
x = fluid.layers.dropout(x, 0.5)
|
||||
x = self.linear2(x)
|
||||
x = fluid.layers.relu(x)
|
||||
x = fluid.layers.dropout(x, 0.5)
|
||||
out = self.linear3(x)
|
||||
return out
|
||||
|
||||
|
||||
class VGG(Model):
|
||||
"""VGG model from
|
||||
`"Very Deep Convolutional Networks For Large-Scale Image Recognition" <https://arxiv.org/pdf/1409.1556.pdf>`_
|
||||
|
||||
Args:
|
||||
features (fluid.dygraph.Layer): vgg features create by function make_layers.
|
||||
num_classes (int): output dim of last fc layer. If num_classes <=0, last fc layer
|
||||
will not be defined. Default: 1000.
|
||||
classifier_activation (str): activation for the last fc layer. Default: 'softmax'.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
from paddle.incubate.hapi.vision.models import VGG
|
||||
from paddle.incubate.hapi.vision.models.vgg import make_layers
|
||||
|
||||
vgg11_cfg = [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M']
|
||||
|
||||
features = make_layers(vgg11_cfg)
|
||||
|
||||
vgg11 = VGG(features)
|
||||
|
||||
"""
|
||||
|
||||
def __init__(self,
|
||||
features,
|
||||
num_classes=1000,
|
||||
classifier_activation='softmax'):
|
||||
super(VGG, self).__init__()
|
||||
self.features = features
|
||||
self.num_classes = num_classes
|
||||
|
||||
if num_classes > 0:
|
||||
classifier = Classifier(num_classes, classifier_activation)
|
||||
self.classifier = self.add_sublayer("classifier",
|
||||
Sequential(classifier))
|
||||
|
||||
def forward(self, x):
|
||||
x = self.features(x)
|
||||
|
||||
if self.num_classes > 0:
|
||||
x = fluid.layers.flatten(x, 1)
|
||||
x = self.classifier(x)
|
||||
return x
|
||||
|
||||
|
||||
def make_layers(cfg, batch_norm=False):
|
||||
layers = []
|
||||
in_channels = 3
|
||||
|
||||
for v in cfg:
|
||||
if v == 'M':
|
||||
layers += [Pool2D(pool_size=2, pool_stride=2)]
|
||||
else:
|
||||
if batch_norm:
|
||||
conv2d = Conv2D(in_channels, v, filter_size=3, padding=1)
|
||||
layers += [conv2d, BatchNorm(v, act='relu')]
|
||||
else:
|
||||
conv2d = Conv2D(
|
||||
in_channels, v, filter_size=3, padding=1, act='relu')
|
||||
layers += [conv2d]
|
||||
in_channels = v
|
||||
return Sequential(*layers)
|
||||
|
||||
|
||||
cfgs = {
|
||||
'A': [64, 'M', 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
|
||||
'B':
|
||||
[64, 64, 'M', 128, 128, 'M', 256, 256, 'M', 512, 512, 'M', 512, 512, 'M'],
|
||||
'D': [
|
||||
64, 64, 'M', 128, 128, 'M', 256, 256, 256, 'M', 512, 512, 512, 'M', 512,
|
||||
512, 512, 'M'
|
||||
],
|
||||
'E': [
|
||||
64, 64, 'M', 128, 128, 'M', 256, 256, 256, 256, 'M', 512, 512, 512, 512,
|
||||
'M', 512, 512, 512, 512, 'M'
|
||||
],
|
||||
}
|
||||
|
||||
|
||||
def _vgg(arch, cfg, batch_norm, pretrained, **kwargs):
|
||||
model = VGG(make_layers(
|
||||
cfgs[cfg], batch_norm=batch_norm),
|
||||
num_classes=1000,
|
||||
**kwargs)
|
||||
|
||||
if pretrained:
|
||||
assert arch in model_urls, "{} model do not have a pretrained model now, you should set pretrained=False".format(
|
||||
arch)
|
||||
weight_path = get_weights_path_from_url(model_urls[arch][0],
|
||||
model_urls[arch][1])
|
||||
assert weight_path.endswith(
|
||||
'.pdparams'), "suffix of weight must be .pdparams"
|
||||
model.load(weight_path)
|
||||
|
||||
return model
|
||||
|
||||
|
||||
def vgg11(pretrained=False, batch_norm=False, **kwargs):
|
||||
"""VGG 11-layer model
|
||||
|
||||
Args:
|
||||
pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False.
|
||||
batch_norm (bool): If True, returns a model with batch_norm layer. Default: False.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
from paddle.incubate.hapi.vision.models import vgg11
|
||||
|
||||
# build model
|
||||
model = vgg11()
|
||||
|
||||
# build vgg11 model with batch_norm
|
||||
model = vgg11(batch_norm=True)
|
||||
"""
|
||||
model_name = 'vgg11'
|
||||
if batch_norm:
|
||||
model_name += ('_bn')
|
||||
return _vgg(model_name, 'A', batch_norm, pretrained, **kwargs)
|
||||
|
||||
|
||||
def vgg13(pretrained=False, batch_norm=False, **kwargs):
|
||||
"""VGG 13-layer model
|
||||
|
||||
Args:
|
||||
pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False.
|
||||
batch_norm (bool): If True, returns a model with batch_norm layer. Default: False.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
from paddle.incubate.hapi.vision.models import vgg13
|
||||
|
||||
# build model
|
||||
model = vgg13()
|
||||
|
||||
# build vgg13 model with batch_norm
|
||||
model = vgg13(batch_norm=True)
|
||||
"""
|
||||
model_name = 'vgg13'
|
||||
if batch_norm:
|
||||
model_name += ('_bn')
|
||||
return _vgg(model_name, 'B', batch_norm, pretrained, **kwargs)
|
||||
|
||||
|
||||
def vgg16(pretrained=False, batch_norm=False, **kwargs):
|
||||
"""VGG 16-layer model
|
||||
|
||||
Args:
|
||||
pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False.
|
||||
batch_norm (bool): If True, returns a model with batch_norm layer. Default: False.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
from paddle.incubate.hapi.vision.models import vgg16
|
||||
|
||||
# build model
|
||||
model = vgg16()
|
||||
|
||||
# build vgg16 model with batch_norm
|
||||
model = vgg16(batch_norm=True)
|
||||
"""
|
||||
model_name = 'vgg16'
|
||||
if batch_norm:
|
||||
model_name += ('_bn')
|
||||
return _vgg(model_name, 'D', batch_norm, pretrained, **kwargs)
|
||||
|
||||
|
||||
def vgg19(pretrained=False, batch_norm=False, **kwargs):
|
||||
"""VGG 19-layer model
|
||||
|
||||
Args:
|
||||
pretrained (bool): If True, returns a model pre-trained on ImageNet. Default: False.
|
||||
batch_norm (bool): If True, returns a model with batch_norm layer. Default: False.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
from paddle.incubate.hapi.vision.models import vgg19
|
||||
|
||||
# build model
|
||||
model = vgg19()
|
||||
|
||||
# build vgg19 model with batch_norm
|
||||
model = vgg19(batch_norm=True)
|
||||
"""
|
||||
model_name = 'vgg19'
|
||||
if batch_norm:
|
||||
model_name += ('_bn')
|
||||
return _vgg(model_name, 'E', batch_norm, pretrained, **kwargs)
|
||||
@ -0,0 +1,22 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# 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 . import transforms
|
||||
from . import functional
|
||||
|
||||
from .transforms import *
|
||||
from .functional import *
|
||||
|
||||
__all__ = transforms.__all__ \
|
||||
+ functional.__all__
|
||||
@ -0,0 +1,101 @@
|
||||
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# 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.
|
||||
|
||||
import sys
|
||||
import collections
|
||||
import random
|
||||
|
||||
import cv2
|
||||
import numpy as np
|
||||
|
||||
if sys.version_info < (3, 3):
|
||||
Sequence = collections.Sequence
|
||||
Iterable = collections.Iterable
|
||||
else:
|
||||
Sequence = collections.abc.Sequence
|
||||
Iterable = collections.abc.Iterable
|
||||
|
||||
__all__ = ['flip', 'resize']
|
||||
|
||||
|
||||
def flip(image, code):
|
||||
"""
|
||||
Accordding to the code (the type of flip), flip the input image
|
||||
|
||||
Args:
|
||||
image: Input image, with (H, W, C) shape
|
||||
code: Code that indicates the type of flip.
|
||||
-1 : Flip horizontally and vertically
|
||||
0 : Flip vertically
|
||||
1 : Flip horizontally
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
import numpy as np
|
||||
from paddle.incubate.hapi.vision.transforms import functional as F
|
||||
|
||||
fake_img = np.random.rand(224, 224, 3)
|
||||
|
||||
# flip horizontally and vertically
|
||||
F.flip(fake_img, -1)
|
||||
|
||||
# flip vertically
|
||||
F.flip(fake_img, 0)
|
||||
|
||||
# flip horizontally
|
||||
F.flip(fake_img, 1)
|
||||
"""
|
||||
return cv2.flip(image, flipCode=code)
|
||||
|
||||
|
||||
def resize(img, size, interpolation=cv2.INTER_LINEAR):
|
||||
"""
|
||||
resize the input data to given size
|
||||
|
||||
Args:
|
||||
input: Input data, could be image or masks, with (H, W, C) shape
|
||||
size: Target size of input data, with (height, width) shape.
|
||||
interpolation: Interpolation method.
|
||||
|
||||
Examples:
|
||||
.. code-block:: python
|
||||
|
||||
import numpy as np
|
||||
from paddle.incubate.hapi.vision.transforms import functional as F
|
||||
|
||||
fake_img = np.random.rand(256, 256, 3)
|
||||
|
||||
F.resize(fake_img, 224)
|
||||
|
||||
F.resize(fake_img, (200, 150))
|
||||
"""
|
||||
|
||||
if isinstance(interpolation, Sequence):
|
||||
interpolation = random.choice(interpolation)
|
||||
|
||||
if isinstance(size, int):
|
||||
h, w = img.shape[:2]
|
||||
if (w <= h and w == size) or (h <= w and h == size):
|
||||
return img
|
||||
if w < h:
|
||||
ow = size
|
||||
oh = int(size * h / w)
|
||||
return cv2.resize(img, (ow, oh), interpolation=interpolation)
|
||||
else:
|
||||
oh = size
|
||||
ow = int(size * w / h)
|
||||
return cv2.resize(img, (ow, oh), interpolation=interpolation)
|
||||
else:
|
||||
return cv2.resize(img, size[::-1], interpolation=interpolation)
|
||||
File diff suppressed because it is too large
Load Diff
Loading…
Reference in new issue