1 Star 2 Fork 0

PaddlePaddle / hapi

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
克隆/下载
贡献代码
同步代码
取消
提示: 由于 Git 不支持空文件夾,创建文件夹后会生成空的 .keep 文件
Loading...
README
Apache-2.0

飞桨核心框架 高层API

简介 | 特性 | 快速使用 | 新增功能 | 使用示例

简介

飞桨框架2.0全新推出高层API,是对飞桨API的进一步封装与升级,提供了更加简洁易用的API,进一步提升了飞桨的易学易用性,并增强飞桨的功能。

飞桨高层API面向从深度学习小白到资深开发者的所有人群,对于AI初学者来说,使用高层API可以简单快速的构建深度学习项目,对于资深开发者来说,可以快速完成算法迭代。

飞桨高层API具有以下特点:

  • 易学易用: 高层API是对普通动态图API的进一步封装和优化,同时保持与普通API的兼容性,高层API使用更加易学易用,同样的实现使用高层API可以节省大量的代码。
  • 低代码开发: 使用飞桨高层API的一个明显特点是,用户可编程代码量大大缩减。
  • 动静转换: 高层API支持动静转换,用户只需要改一行代码即可实现将动态图代码在静态图模式下训练,既方便用户使用动态图调试模型,又提升了模型训练效率。

在功能增强与使用方式上,高层API有以下升级:

  • 模型训练方式升级: 高层API中封装了Model类,继承了Model类的神经网络可以仅用几行代码完成模型的训练。
  • 新增图像处理模块transform: 飞桨新增了图像预处理模块,其中包含数十种数据处理函数,基本涵盖了常用的数据处理、数据增强方法。
  • 提供常用的神经网络模型可供调用: 高层API中集成了计算机视觉领域和自然语言处理领域常用模型,包括但不限于mobilenet、resnet、yolov3、cyclegan、bert、transformer、seq2seq等等。同时发布了对应模型的预训练模型,用户可以直接使用这些模型或者在此基础上完成二次开发。

特性

易学易用

高层API基于飞桨动态图实现,兼容飞桨动态图的所有功能,既秉承了动态图易学、易用、易调试的特点,又对飞桨的动态图做了进一步的封装与优化。

低代码开发

相比较与动态图的算法实现,使用高层API实现的算法可编程代码量更少,原始的动态图训练代码需要20多行代码才能完成模型的训练,使用高层API后,仅用8行代码即可实现相同的功能。

使用普通API与高层API实现手写字符识别对比如下图,左边是普通动态图API的实现,右边是使用高层API的实现,可以明显发现,使用高层API的代码量更少。

动静统一

高层API中实现了动静统一,用户无需感知到静态图、动态图的区别,只需要改一行代码即可实现将动态图代码在静态图模式下训练。动态图更方便调试模型,静态图的训练方式训练效率更高。

高层API默认训练方式和主框架保持一致,采用动态图的训练方式,我们可以使用paddle.disable_static()来开启动态图训练模式,用paddle.enable_static()开启静态图模式训练。

# 一行代码切换动态图训练模式
## 动态图训练模式
paddle.disable_static()
## 静态图训练模式
# paddle.enable_static()

# 设置训练设备环境
paddle.set_device('gpu')

# 声明网络结构
model = paddle.Model(Mnist())
# 定义优化器
optimizer = paddle.optimizer.SGD(learning_rate=0.001, parameters=model.parameters())
# 调用prepare() 完成训练的配置
model.prepare(optimizer, CrossEntropy(), Accuracy())
# 调用 fit(),启动模型的训练
model.fit(train_dataset, val_dataset, batch_size=100, epochs=1, log_freq=100, save_dir="./output/")

快速使用

以mnist手写字符识别为例,介绍飞桨高层API的使用方式。

1. 搭建网络结构

使用高层API组建网络与动态图的组网方式完全相同,继承paddle.nn.Layer来定义网络结构即可。

高层API组网方式如下

import paddle

# 设置执行环境为GPU
paddle.set_device('gpu')
# 使用动态图训练方式
paddle.disable_static()

class Mnist(paddle.nn.Layer):
    def __init__(self):
        super(Mnist, self).__init__()
        self.fc = paddle.nn.Linear(input_dim=784, output_dim=10)

    # 定义网络结构的前向计算过程
    def forward(self, inputs):
        outputs = self.fc(inputs)

        return outputs

2. 训练准备

在开始训练前,需要定义优化器、损失函数、度量函数,准备数据等等。这些过程均可以在高层API Model类中的prepare函数中完成。

# 定义输入数据格式
inputs = [Input([None, 784], 'float32', name='image')]
labels = [Input([None, 1], 'int64', name='label')]

# 声明网络结构
model = paddle.Model(Mnist())
optimizer = paddle.optimizer.SGD(learning_rate=0.001,
                                 parameters=model.parameters())
# 使用高层API,prepare() 完成训练的配置
model.prepare(optimizer,
              paddle.nn.CrossEntropy(),
              paddle.metricAccuracy())

3. 启动训练

使用高层API完成训练迭代过程时,使用一行代码即可构建双层循环程序,去控制训练的轮数和数据读取过程。

from paddle.vision.datasets import MNIST as MnistDataset
# 定义数据读取器
train_dataset = MnistDataset(mode='train')
val_dataset = MnistDataset(mode='test')
# 启动训练
model.fit(train_dataset, val_dataset, batch_size=100, epochs=10, log_freq=100, save_dir="./output/")

高层API中通过fit函数完成训练的循环过程,只需要设置训练的数据读取器、batchsize大小,迭代的轮数epoch、训练日志打印频率log_freq,保存模型的路径即可。

新增功能

除了使用高层API实现一行代码启动训练外,还新增了以下功能:

  • paddle.vision.transforms 图像数据增强模块
  • paddle.vision.models 模型调用模块

transforms

paddle.vision.transforms。图像预处理模块transforms包括一系列的图像增强与图像处理实现,对处理计算机视觉相关的任务有很大帮助。

下表中列出Transforms支持的数据处理和数据增强API,如下所示:

transform的数据处理实现 函数功能
Compose 组合多种数据变换
BatchCompose 用于处理批数据的预处理接口组合
Resize 将图像转换为固定大小
RandomResizedCrop 根据输入比例对图像做随机剪切,然后resize到指定大小
CenterCrop 以图像的中心为中心对图像做剪切
CenterCropResize 对图像做padding,padding后的图像做centercrop,然后resize到指定大小
RandomHorizontalFlip 随机对图像做水平翻转
RandomVerticalFlip 随机对图像做垂直翻转
RandomCrop 在随机位置裁剪输入的图像
RandomErasing 随机选择图像中的一个矩形区域并将其像素删除
RandomRotate 按角度旋转图像
Permute 将数据的的维度换位
Normalize 用指定的均值和标准差对数据做归一化
GaussianNoise 给数据增加高斯噪声
BrightnessTransform 调整输入图像的亮度
SaturationTransform 调整输入图像的饱和度
ContrastTransform 调整输入图像的对比度
HueTransform 调整图像的色调
ColorJitter 随机调整图像的亮度、饱和度、对比度、和色调
Grayscale 将图像转换为灰度
Pad 使用特定的填充模式和填充值来对输入图像进行填充

使用方法如下:

from paddle.vision import transforms
import cv2

img_path = "./output/sample.jpg"
img = cv2.imread(img_path)

# 使用Compose 将可以将多个数据增强函数组合在一起
trans_funcs = transforms.Compose([transforms.RandomResizedCrop(224),
                                transforms.RandomHorizontalFlip(),
                                transforms.BrightnessTransform(0.2)])
label = None
img_processed, label = trans_funcs(img, label)

上述代码的效果图如下:

paddle.vision.models

paddle.vision.models中包含了高层API对常用模型的封装,包括ResNet、VGG、MobileNet、LeNet等。使用这些现有的模型,可以快速的完成神经网络的训练、finetune等。

使用paddle.vision中的模型可以简单快速的构建一个深度学习任务,比如13代码即可实现resnet在Cifar10数据集上的训练:

from paddle.vision.models import resnet50
from paddle.vision.datasets import Cifar10
from paddle.optimizer import Momentum
from paddle.regularizer import L2Decay
from paddle.nn import CrossEntropy
from paddle.metirc import Accuracy


# 调用resnet50模型
model = paddle.Model(resnet50(pretrained=False, num_classes=10))
# 使用Cifar10数据集
train_dataset = Cifar10(mode='train')
val_dataset = Cifar10(mode='test')
# 定义优化器
optimizer = Momentum(learning_rate=0.01,
                     momentum=0.9,
                     weight_decay=L2Decay(1e-4),
                     parameters=model.parameters())
# 进行训练前准备
model.prepare(optimizer, CrossEntropy(), Accuracy(topk=(1, 5)))
# 启动训练
model.fit(train_dataset,
          val_dataset,
          epochs=50,
          batch_size=64,
          save_dir="./output",
          num_workers=8)

更多使用示例

更多的高层API使用示例请参考:

Apache License Version 2.0, January 2004 http://www.apache.org/licenses/ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION 1. Definitions. "License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document. "Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License. "Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity. "You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License. "Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files. "Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types. "Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below). "Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof. "Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution." "Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work. 2. Grant of Copyright License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form. 3. Grant of Patent License. Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed. 4. Redistribution. You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions: (a) You must give any other recipients of the Work or Derivative Works a copy of this License; and (b) You must cause any modified files to carry prominent notices stating that You changed the files; and (c) You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and (d) If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License. You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License. 5. Submission of Contributions. Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions. 6. Trademarks. This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file. 7. Disclaimer of Warranty. Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License. 8. Limitation of Liability. In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages. 9. Accepting Warranty or Additional Liability. While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability. END OF TERMS AND CONDITIONS APPENDIX: How to apply the Apache License to your work. To apply the Apache License to your work, attach the following boilerplate notice, with the fields enclosed by brackets "[]" replaced with your own identifying information. (Don't include the brackets!) The text should be enclosed in the appropriate comment syntax for the file format. We also recommend that a file or class name and description of purpose be included on the same "printed page" as the copyright notice for easier identification within third-party archives. Copyright [yyyy] [name of copyright owner] 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.

简介

飞桨核心框架 高层API 展开 收起
Jupyter Notebook 等 3 种语言
Apache-2.0
取消

发行版

暂无发行版

贡献者

全部

近期动态

加载更多
不能加载更多了
1
https://gitee.com/paddlepaddle/hapi.git
git@gitee.com:paddlepaddle/hapi.git
paddlepaddle
hapi
hapi
master

搜索帮助