提交 21e75382 编写于 作者: B BBuf

delete x2oneflow

上级 eb340bd4
......@@ -13,15 +13,10 @@ OneFlow 相关的模型转换工具
#### 简介
oneflow_onnx 工具包含两个功能,一个是将 OneFlow 导出 ONNX,另外一个是将各个训练框架导出的 ONNX 模型转换为 OneFlow 的模型。本工程已经适配了 TensorFlow/Pytorch/PaddlePaddle 框架的预训练模型通过导出 ONNX 转换为 OneFlow(我们将这一功能叫作 X2OneFlow)。
- OneFlow2ONNX 模型支持,支持 OneFlow 静态图模型转为 ONNX,可转换由 [flow.checkpoint.save ](https://docs.oneflow.org/basics_topics/model_load_save.html) 方法保存下来的 OneFlow 模型,详情可以参考 [OneFlow2ONNX 模型列表](docs/oneflow2onnx/oneflow2onnx_model_zoo.md)
- X2OneFlow 模型支持,支持将 TensorFlow/Pytorch/PaddlePaddle 的模型通过 ONNX 转换为 OneFlow 的模型,详情可以参考 [X2OneFlow 模型列表](docs/x2oneflow/x2oneflow_model_zoo.md)
- OneFlow2ONNX 算子支持,目前稳定支持导出 ONNX Opset10,部分 OneFlow 算子支持更低的 ONNX Opset 转换,详情可以参考 [OneFlow2ONNX 算子列表](docs/oneflow2onnx/op_list.md)
- X2OneFlow 算子支持,目前稳定支持 TensorFlow/Pytorch/PaddlePaddle 中涵盖大部分 CV 场景的算子,详情可以参考 [X2OneFlow 算子列表](docs/x2oneflow/op_list.md)
- 代码生成支持,支持将 TensorFlow/Pytorch/PaddlePaddle 的模型通过 ONNX 转换为 OneFlow 的模型并同时生成 OneFlow 的代码,详情可以参考 [X2OneFlow 代码生成模型列表](docs/x2oneflow/code_gen.md)
> 目前 OneFlow2ONNX 支持80+的 OneFlow OP 导出为 ONNX OP。X2OneFlow 支持80个 ONNX OP,50+个 TensorFlow OP,80+个 Pytorch OP,50+个 PaddlePaddle OP,覆盖了大部分 CV 分类模型常用的操作。注意我们支持的 OP 和模型均为动态图 API 下的 OP 和模型,要求 PaddlePaddle 的版本>=2.0.0,TensorFlow >=2.0.0,Pytorch 无明确版本要求。目前 X2OneFlow 已经成功转换了50+个 TensorFlow/Pytorch/PaddlePaddle 官方模型。欢迎体验此项目。
#### 环境依赖
......@@ -33,18 +28,7 @@ onnx>=1.8.0
onnx-simplifier>=0.3.3
onnxoptimizer>=0.2.5
onnxruntime>=1.6.0
oneflow (https://github.com/Oneflow-Inc/oneflow#install-with-pip-package)
```
如果你想使用 X2OneFlow(X 代表 TensorFlow/Pytorch/PaddlePaddle)则需要安装对应的深度学习框架。依赖如下:
```sh
pytorch>=1.7.0
paddlepaddle>=2.0.0
paddle2onnx>=0.6
tensorflow>=2.0.0
tf2onnx>=1.8.4
oneflow>=0.5.0
```
#### 安装
......@@ -70,35 +54,9 @@ python3 setup.py install
#### 相关文档
- [OneFlow2ONNX模型列表](docs/oneflow2onnx/oneflow2onnx_model_zoo.md)
- [X2OneFlow模型列表](docs/x2oneflow/x2oneflow_model_zoo.md)
- [OneFlow2ONNX算子列表](docs/oneflow2onnx/op_list.md)
- [X2OneFlow算子列表](docs/x2oneflow/op_list.md)
- [使用示例](examples/README.md)
### nchw2nhwc_tool
#### 简介
本工具的功能是将 OneFlow 训练的 NCHW 排布的权重转换为 NHWC 排布,使用方法[在这里](nchw2nhwc_tool/README.md)
### save_serving_tool
#### 简介
本工具的目的是将 OneFlow 训练的模型转换为 Serving 端可用的模型,使用方法[在这里](save_serving_tool/README.md)
### 项目进展
- 2021/4/13 支持ResNet18代码自动生成,量化OP转换失败暂时移除c测试脚本,发布0.2.2 wheel包。
- 2021/4/14 修复CI错误,支持X2OneFlow的所有模型自动代码生成功能,发布0.2.3 whell包。
- 2020/4/15 完成X2OneFlow所有模型的自动代码生成功能,发布0.3.0 whell包。
- 2020/4/16 将Expand OP并入主分支,并修复导入oneflow_api报错的bug,发布0.3.1 whell包。
- 2020/4/16 解决自动代码生成遗留问题,并将自动代码生成的测试加入CI,发布0.3.2 whell包。
- 2020/6/21 导出ONNX新增PreLU/LeakyReLU OP,修复自动代码生成bug,发布0.3.3 whell包。
- 2020/6/23 导出ONNX新增Constant OP,修复BN只有NC两个维度(InsightFace)导出的bug以及禁用导出ONNX时默认开启的global function,发布0.3.3.20210623 whell包。
- 2020/6/24 导出ONNX新增Flatten OP,发布0.3.4 whell包。
......@@ -4,39 +4,5 @@ python3 -m pip install --user --upgrade pip
python3 -m pip install -r test-requirements.txt --user --extra-index-url https://pypi.ngc.nvidia.com
if [ -f requirements.txt ]; then python3 -m pip install -r requirements.txt --user; fi
python3 -m pip install oneflow --user -U -f https://staging.oneflow.info/branch/master/cu110
python3 -m pip install gast==0.3.3 --user
python3 setup.py install
python3 examples/tensorrt_qat/test_lenet_qat_train.py
python3 -m pytest -s examples/tensorrt_qat/test_lenet_qat.py
python3 examples/tensorrt_qat/test_mobilenet_qat_train.py
python3 -m pytest -s examples/tensorrt_qat/test_mobilenet_qat.py
python3 -m pytest examples/oneflow2onnx
python3 -m pytest examples/x2oneflow/pytorch2oneflow/nodes
python3 -m pytest examples/x2oneflow/pytorch2oneflow/models
python3 -m pytest examples/x2oneflow/tensorflow2oneflow/nodes
python3 -m pytest examples/x2oneflow/tensorflow2oneflow/models
python3 -m pytest examples/x2oneflow/paddle2oneflow/nodes
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_alexnet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_darknet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_densenet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_dpn.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_efficientnet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_ghostnet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_googlenet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_inceptionv3.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_inceptionv4.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_mobilenetv1.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_mobilenetv2.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_mobilenetv3.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_regnet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_repvgg.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_res2net.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_resnet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_resnext.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_se_resnext.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_shufflenet_v2.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_squeezenet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_vggnet.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_vision_transformer.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_xception_deeplab.py
python3 -m pytest examples/x2oneflow/paddle2oneflow/models/test_xception.py
......@@ -4,10 +4,3 @@
1. op_list.md: supported OP in the transformation process from OneFlow to ONNX.
2. oneflow2onnx_model_zoo.md: tested model list.
# x2oneflow
1. op_list.md: transform OP from other frameworks into OneFlow's format
2. x2oneflow_model_zoo.md: tested model list.
3. code_gen.md: supported model list for OneFlow code generation .
# X2OneFlow 代码生成
> 这里记录在X2OneFlow中生成OneFlow代码的支持情况
## Pytorch
| 模型 | 是否支持 |
| ------------ | -------- |
| LeNet | Yes |
| AlexNet | Yes |
| VGGNet | Yes |
| GoogleNet | Yes |
| ResNet | Yes |
| PreActResNet | Yes |
| ResNext | Yes |
| SENet | Yes |
| MobileNetV1 | Yes |
| MobileNetV2 | Yes |
| MobileNetV3 | Yes |
| RegNet | Yes |
| DenseNet | Yes |
| EfficientNet | Yes |
| InceptionNet | Yes |
| ShuffleNetV1 | Yes |
| ShuffleNetV2 | Yes |
| SqueezeNet | Yes |
| DPN | Yes |
| PNASNet | Yes |
| DLANet | Yes |
## TensorFlow
| 模型 | 是否支持 |
| ------------ | -------- |
| VGGNet | Yes |
| ResNet | Yes |
| ResNetV2 | Yes |
| XceptionNet | Yes |
| MobileNetV1 | Yes |
| MobileNetV2 | Yes |
| MobileNetV3 | Yes |
| DenseNet | Yes |
| EfficientNet | Yes |
| InceptionNet | Yes |
## PaddlePaddle
| 模型 | 是否支持 |
| ------------------ | -------- |
| AlexNet | Yes |
| VGGNet | Yes |
| GoogleNet | Yes |
| ResNet | Yes |
| ResNext | Yes |
| SE_ResNext | Yes |
| SENet | Yes |
| MobileNetV1 | Yes |
| MobileNetV2 | Yes |
| MobileNetV3 | Yes |
| RegNet | Yes |
| DenseNet | Yes |
| EfficientNet | Yes |
| InceptionNet | Yes |
| ShuffleNetV2 | Yes |
| SqueezeNet | Yes |
| DPNNet | Yes |
| DarkNet | Yes |
| GhostNet | Yes |
| RepVGG | Yes |
| XceptionNet | Yes |
| Xception_DeepLab | Yes |
| Vision_Transformer | Yes |
| Res2Net | Yes |
\ No newline at end of file
# X2OneFlow 支持的OP列表
> 目前X2OneFlow 支持80个ONNX OP,50+个TensorFlow OP,80+个Pytorch OP,50+个PaddlePaddle OP,覆盖了大部分CV分类模型常用的操作。注意我们支持的OP和模型均为动态图API下的OP和模型,要求PaddlePaddle的版本>=2.0.0,TensorFlow>=2.0.0,Pytorch无明确版本要求。
**注:** 目前,部分OP暂未支持,如您在转换过程中出现OP不支持的情况,可自行添加或反馈给我们。欢迎通过[ISSUE](https://github.com/Oneflow-Inc/oneflow_convert_tools/issues/new)反馈的方式告知我们(模型名,代码实现或模型获取方式),我们会及时跟进:)
## ONNX
| 序号 | OP | 序号 | OP | 序号 | OP | 序号 | OP |
|------|------|------|------|------|------|------|------|
| 1 | Conv | 2 |BatchNormalization| 3 | MaxPool | 4 | AveragePool|
| 5 | Concat | 6 | ReLU | 7 |AdaptiveMaxPool| 8 | Softmax |
| 9 | Unsqueeze | 10 | Transpose | 11 | Clip | 12 | Gather |
| 13 | Slice | 14 | Split | 15 | Flatten | 16 | Add |
| 17 | Sub | 18 | Mul | 19 | Div | 20 |Sqrt |
| 21 |Pow | 22 | Tanh | 23 | Sigmoid | 24 | Cast |
| 25 | Pad | 26 | ReduceMean | 27 | Reshape | 28 | AdaptiveAvgPool|
|29 | Squeeze | 30 | Expand | 31 | Gather | 32 | Slice |
|33 | Split | 34 | Min | 35 | Max | 36 | Constant |
|37 | HardSigmoid| 38 | Gemm | 39 | MatMul | 40 | Erf |
|41 | ~~Cast~~ | 42 | GlobalMaxPool | 43 | GlobalAveragePool |44|ReduceMax|
|45 | Identity | 46 | Rsqrt | 47 | LeakyRelu | 48 | Abs |
|49 | Exp | 50 | Reciprocal | 51 | Floor | 52 | ArgMax |
|53 | Range | 54 | Greator | 55 | Less | 56 | Softplus |
|57 | Neg | 58 | Ceil | 59 | Where | 60 | Equal |
|61 | Sign | 62 | NonZero | 63 | Acos | 64 | Acosh |
|65 | ArgMin | 66 | Asin | 67 | Atan | 68 | Cos |
|69 | Elu | 70 | Exp | 71 | Log | 72 | LogSoftmax|
|73 |ReduceLogSumExp|74| ReduceMin | 75 | ReduceProd | 76 | Round |
|77 | Sin | 78 | Tanh | 79 |Tan | 80 | PReLU |
## TensorFlow
| 序号 | OP | 序号 | OP | 序号 | OP | 序号 | OP |
|------|------|------|------|------|------|------|------|
| 1 | Relu | 2 | Relu6 | 3 | Shape | 4 | Abs |
| 5 | Sigmoid | 6 | Exp | 7 | Rsqrt | 8 | Swish |
| 9 | Tanh | 10 | LeakyRelu | 11 | Add | 12 | Greater |
| 13 | Sub | 14 | Maximum | 15 | Mul | 16 | FloorDiv |
| 17 | Pow | 18 | Const | 19 | Transpose | 20 | BatchNormalization |
| 21 | Conv2D | 22 | BiasAdd | 23 | MaxPool | 24 | DepthwiseConv2D |
| 25 | Reshape | 26 | AvgPool | 27 | Where | 28 | SquaredDifference |
| 29 | Neg | 30 | Ceil | 31 | Pad | 32 | ~~ResizeBilinear~~ |
| 33 | ReduceMean | 34 | MatMul | 35 | ArgMax | 36 | ExpandDims |
| 37 | Slice | 38 | Sum | 39 | Max | 40 | ~~LessEqual~~ |
| 41 | ~~Cast~~ | 42 | Split | 43 | Squeeze | 44 | ~~ResizeNearestNeighbor~~ |
| 45 | Softmax | 46 | Range | 47 | Size | 48 | Sqrt |
| 49 | Identity | 50 |~~GreaterEqual~~| 51 | Equal | 52 | Minimum |
| 53 | | 54 | Fill | 55 | Floor | 56 | |
| 57 | Sqrt | 58 | Softplus | 59 | Erf | 60 | |
## Pytorch
| 序号 | OP | 序号 | OP | 序号 | OP | 序号 | OP |
|------|------|------|------|------|------|------|------|
| 1 | BatchNorm | 2 | ConstantPad2d | 3 | Conv2D | 4 | Dropout |
| 5 | MaxPool2d | 6 | adaptive_avg_pool2d | 7 | adaptive_max_pool2d | 8 | AvgPool2d |
| 9 | abs | 10| absolute | 11| acos | 12 | add |
| 13| addmm | 14| arange | 15| argmax | 16 | argmin |
| 17| asin | 18| atan | 19| baddbmm | 20 | cat |
| 21| ceil | 22| ~~celu~~ | 23| clamp | 24 | clamp_max|
| 25| clamp_min | 26| concat | 27| cos | 28 | ~~cumsum~~|
| 29| div | 30| elu | 31| eq | 32 | erf |
| 33| exp | 34| ~~expand~~ | 35| flatten | 36 | floor |
| 37|floor_divide|38| full | 39| full_like | 40 | gather |
| 41| ~~ge~~ | 42| gelu | 43| ~~GroupNorm~~ | 44 |~~hardswish~~|
| 45| hardtanh | 46| ~~instance_norm~~ | 47| ~~interpolate~~ | 48 | ~~layer_norm~~|
| 49| leaky_relu| 50| log | 51| log1p | 52 | log2 |
| 53| log_softmax|54| logsumexp | 55| max | 56 | min |
| 57| mean |58 | mm | 59| mul | 60 | neg |
| 61| ~~norm~~ | 62| ~~pixel_shuffle~~ | 63| pow | 64 | permute |
| 65| ~~prelu~~ | 66| relu | 67| reshape | 68 | relu6 |
| 69| softmax | 70| slice | 71| sub | 72 | sqrt |
| 73| sigmoid | 74| prod | 75| reshape_as | 76 | round |
| 77| rsqrt | 78| ~~selu~~ | 79| sign | 80 | sin |
| 81| softplus | 82| split | 83| squeeze | 84 | sum |
| 85| tan | 86| tanh | 87 | transpose | 88 | unsqueeze|
| 89| ~~upsample_nearest2d~~ | 90| prelu |
- hardswish pytorch导出存在bug
- interpolate oneflow和pytorch的参数列表未完全对齐,只能转nearest和align_corners=False的情况,working
## PaddlePaddle
| 序号 | OP | 序号 | OP | 序号 | OP | 序号 | OP |
|------|------|------|------|------|------|------|------|
| 1 | abs | 2 | acos | 3 | add | 4 | argmax |
| 5 | batch_norm| 6 | ~~bilinear_interp~~| 7 | bmm | 8 | ~~cast~~ |
| 9 | clip | 10| concat | 11| conv2d | 12| ~~conv2d_transpose~~|
| 13| ~~cumsum~~| 14| depthwise_conv2d | 15| dropout | 16| elementwise_add|
| 17| elementwise_div| 18| elementwise_mul | 19| elementwise_min | 20| elementwise_max|
| 21| elementwise_pow| 22| elementwise_sub | 23| exp | 24| expand_as |
| 25| expand_dims|26| flatten | 27| floor | 28| gather |
| 29| hardsigmoid|30| hardswish | 31| leaky_relu| 32| log |
| 33| matmul | 34| mean | 35| mul | 36| ~~nearest_interp~~|
| 37| pad2d | 38| pow | 39| ~~prelu~~ | 40| reduce_mean|
| 41| reduce_max| 42| reduce_min | 43| reduce_prod|44| reduce_sum |
| 45| relu | 46| relu6 | 47| reshape | 48| softmax |
| 49| sigmoid | 50| slice | 51| scale | 52| ~~split~ |
| 53| squeeze | 54| sqrt | 55| square | 56| stack |
| 57| stride_slice|58| sum | 59| swish | 60| tanh |
| 61| transpose | 62| unsqueeze|
相关issue:
- https://github.com/PaddlePaddle/Paddle2ONNX/issues/221
- https://github.com/PaddlePaddle/Paddle2ONNX/issues/220
# X2OneFlow模型测试库
> 目前X2OneFlow 支持80个ONNX OP,50+个TensorFlow OP,80+个Pytorch OP,50+个PaddlePaddle OP,覆盖了大部分CV分类模型常用的操作。注意我们支持的OP和模型均为动态图API下的OP和模型,要求PaddlePaddle的版本>=2.0.0,TensorFlow>=2.0.0,Pytorch无明确版本要求。我们在如下模型列表中测试了X2OneFlow的转换。
## Pytorch
| 模型 | 是否支持 |
| ------------ | -------- |
| LeNet | Yes |
| AlexNet | Yes |
| VGGNet | Yes |
| GoogleNet | Yes |
| ResNet | Yes |
| PreActResNet | Yes |
| ResNext | Yes |
| SENet | Yes |
| MobileNetV1 | Yes |
| MobileNetV2 | Yes |
| MobileNetV3 | Yes |
| RegNet | Yes |
| DenseNet | Yes |
| EfficientNet | Yes |
| InceptionNet | Yes |
| ShuffleNetV1 | Yes |
| ShuffleNetV2 | Yes |
| SqueezeNet | Yes |
| DPN | Yes |
| PNASNet | Yes |
| DLANet | Yes |
## TensorFlow
| 模型 | 是否支持 |
| ------------ | -------- |
| VGGNet | Yes |
| ResNet | Yes |
| ResNetV2 | Yes |
| XceptionNet | Yes |
| MobileNetV1 | Yes |
| MobileNetV2 | Yes |
| MobileNetV3 | Yes |
| DenseNet | Yes |
| EfficientNet | Yes |
| InceptionNet | Yes |
## PaddlePaddle
| 模型 | 是否支持 |
| ------------------ | -------- |
| AlexNet | Yes |
| VGGNet | Yes |
| GoogleNet | Yes |
| ResNet | Yes |
| ResNext | Yes |
| SE_ResNext | Yes |
| SENet | Yes |
| MobileNetV1 | Yes |
| MobileNetV2 | Yes |
| MobileNetV3 | Yes |
| RegNet | Yes |
| DenseNet | Yes |
| EfficientNet | Yes |
| InceptionNet | Yes |
| ShuffleNetV2 | Yes |
| SqueezeNet | Yes |
| DPNNet | Yes |
| DarkNet | Yes |
| GhostNet | Yes |
| RepVGG | Yes |
| XceptionNet | Yes |
| Xception_DeepLab | Yes |
| Vision_Transformer | Yes |
| Res2Net | Yes |
- 模型的测试代码均可以在本工程的examples中找到
\ No newline at end of file
......@@ -2,4 +2,4 @@
## oneflow_onnx samples
At present, oneflow2onnx supports the export of 80 + ONEFLOW OP to onnx Op. X2oneflow supports 80+ onnx OP's, 50 + TensorFlow OP's, 80 + Pytorch OP's and 50 + PaddlePaddle OP's, covering most common operations of CV classification model. Note that the OP and model we support are all OP and model under the dynamic graph API, which requires that the version of PaddlePaddle > = 2.0.0, TensorFlow > = 2.0.0, and there is no explicit version requirement for Pytorch. At present, x2oneflow has successfully converted 50 + TensorFlow/Pytorch/PaddlePaddle official models. We provide all the test examples in the 'oneflow2onnx' and 'x2oneflow' folders respectively. If you want to export onnx or convert the custom TensorFlow/Pytorch/PaddlePaddle network, you can modify it accordingly.
\ No newline at end of file
At present, oneflow2onnx supports the export of 80 + ONEFLOW OP to onnx Op.
\ No newline at end of file
## oneflow_onnx 使用示例
目前OneFlow2ONNX 支持80+的OneFlow OP导出为ONNX OP。X2OneFlow支持80+个ONNX OP,50+个TensorFlow OP,80+个Pytorch OP,50+个PaddlePaddle OP,覆盖了大部分CV分类模型常用的操作。注意我们支持的OP和模型均为动态图API下的OP和模型,要求PaddlePaddle的版本>=2.0.0,TensorFlow>=2.0.0,Pytorch无明确版本要求。目前X2OneFlow已经成功转换了50+个TensorFlow/Pytorch/PaddlePaddle官方模型.我们分别在`oneflow2onnx``x2oneflow`文件夹下提供了所有的测试示例,需导出ONNX或者转换自定义的TensorFlow/Pytorch/PaddlePaddle网络可以对应修改使用。
目前OneFlow2ONNX 支持80+的OneFlow OP导出为ONNX OP。
"""
Copyright 2020 The OneFlow 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 numpy as np
import oneflow as flow
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check
def set_moving_max_min_value():
max_key, min_key = None, None
keys = flow.get_all_variables().keys()
for key in keys:
if max_key is not None and min_key is not None:
break
if key[-3:] == "max":
max_key = key
if key[-3:] == "min":
min_key = key
if max_key is not None and min_key is not None:
flow.load_variables(
{
max_key: np.array([0.5]).astype(np.float32),
min_key: np.array([-0.2]).astype(np.float32),
}
)
def generate_fake_quantization_test(
per_layer: bool = True, scheme: str = "symmetric", device_type: str = "cpu",
):
@flow.global_function()
def fake_quantization():
with flow.scope.placement(device_type, "0:0"):
x = flow.get_variable(
name="x1",
shape=(2, 3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(-10, 10),
)
return flow.quantization.fake_quantization(
x,
*flow.quantization.min_max_observer(
x, per_layer_quantization=per_layer, quantization_scheme=scheme,
),
quantization_scheme=scheme,
)
convert_to_onnx_and_check(fake_quantization, opset=10 if per_layer else 13)
def generate_fake_quantization_test_moving_average(
scheme: str = "symmetric", device_type: str = "cpu",
):
@flow.global_function()
def fake_quantization_moving_average():
with flow.scope.placement(device_type, "0:0"):
x = flow.get_variable(
name="x1",
shape=(2, 3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(-10, 10),
)
return flow.quantization.fake_quantization(
x,
*flow.quantization.moving_average_min_max_observer(
x, quantization_scheme=scheme,
),
quantization_scheme=scheme,
)
set_moving_max_min_value()
convert_to_onnx_and_check(
fake_quantization_moving_average, opset=10, explicit_init=False
)
# min_max_observer
def test_fake_quantization_symmetric():
generate_fake_quantization_test(per_layer=True, scheme="symmetric")
def test_fake_quantization_symmetric_per_channel():
generate_fake_quantization_test(per_layer=False, scheme="symmetric")
def test_fake_quantization_affine():
generate_fake_quantization_test(per_layer=True, scheme="affine")
def test_fake_quantization_affine_per_channel():
generate_fake_quantization_test(per_layer=False, scheme="affine")
def test_fake_quantization_symmetric_gpu():
generate_fake_quantization_test(
per_layer=True, scheme="symmetric", device_type="gpu"
)
def test_fake_quantization_symmetric_per_channel_gpu():
generate_fake_quantization_test(
per_layer=False, scheme="symmetric", device_type="gpu"
)
def test_fake_quantization_affine_gpu():
generate_fake_quantization_test(per_layer=True, scheme="affine", device_type="gpu")
def test_fake_quantization_affine_per_channel_gpu():
generate_fake_quantization_test(per_layer=False, scheme="affine", device_type="gpu")
# moving_average_min_max_observer
def test_fake_quantization_symmetric_moving_average():
generate_fake_quantization_test_moving_average(scheme="symmetric")
def test_fake_quantization_affine_moving_average():
generate_fake_quantization_test_moving_average(scheme="affine")
def test_fake_quantization_symmetric_gpu_moving_average():
generate_fake_quantization_test_moving_average(
scheme="symmetric", device_type="gpu"
)
def test_fake_quantization_affine_gpu_moving_average():
generate_fake_quantization_test_moving_average(scheme="affine", device_type="gpu")
......@@ -13,36 +13,36 @@ 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 tempfile
import oneflow as flow
import oneflow.typing as tp
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check
class Flatten(flow.nn.Module):
def __init__(self) -> None:
super(Flatten, self).__init__()
self.flatten = flow.nn.Flatten()
def test_flatten():
@flow.global_function()
def flatten(x: tp.Numpy.Placeholder((3, 4, 2, 5))):
return flow.flatten(x, start_dim=1, end_dim=-1)
convert_to_onnx_and_check(flatten)
def forward(self, x: flow.Tensor) -> flow.Tensor:
return self.flatten(x)
def test_flatten_aixs_negative():
@flow.global_function()
def flatten(x: tp.Numpy.Placeholder((3, 4, 2, 5))):
return flow.flatten(x, start_dim=0, end_dim=-1)
flatten = Flatten()
class flattenOpGraph(flow.nn.Graph):
def __init__(self):
super().__init__()
self.m = flatten
convert_to_onnx_and_check(flatten)
def build(self, x):
out = self.m(x)
return out
def test_flatten_aixs_default():
@flow.global_function()
def flatten(x: tp.Numpy.Placeholder((3, 4, 2, 5))):
return flow.flatten(x)
convert_to_onnx_and_check(flatten)
def test_flatten():
flatten_graph = flattenOpGraph()
flatten_graph._compile(flow.randn(1, 3, 224, 224))
def test_flatten_dtype_int():
@flow.global_function()
def flatten(x: tp.Numpy.Placeholder((3, 4, 2, 5))):
x = flow.cast(x, flow.int32)
return flow.flatten(x)
with tempfile.TemporaryDirectory() as tmpdirname:
flow.save(flatten.state_dict(), tmpdirname)
convert_to_onnx_and_check(flatten_graph, flow_weight_dir=tmpdirname, onnx_model_path="/tmp")
convert_to_onnx_and_check(flatten, opset=11)
test_flatten()
"""
Copyright 2020 The OneFlow 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 oneflow as flow
import oneflow.typing as tp
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check
def test_gather_nd():
@flow.global_function()
def gather_nd():
x = flow.get_variable(
name="x",
shape=(2, 3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
y = flow.get_variable(
name="y",
shape=(2, 3),
dtype=flow.int64,
initializer=flow.random_uniform_initializer(0, 1, flow.int64),
)
return flow.gather_nd(x, y)
convert_to_onnx_and_check(gather_nd, opset=11)
"""
Copyright 2020 The OneFlow 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 oneflow as flow
import oneflow.typing as tp
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check
def test_large_array():
@flow.global_function()
def add_with_large_array():
large_shape = (256 * 1024 * 1024 + 1,)
x = flow.get_variable(
name="x",
shape=large_shape,
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
y = flow.get_variable(
name="y",
shape=large_shape,
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
return flow.math.add_n([x, y])
# ONNX Runtime optimizers doesn't support external data
convert_to_onnx_and_check(
add_with_large_array, external_data=True, ort_optimize=False
)
......@@ -13,105 +13,36 @@ 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 tempfile
import oneflow as flow
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check
class MatMul(flow.nn.Module):
def __init__(self) -> None:
super(MatMul, self).__init__()
self.matmul = flow.nn.Linear(20, 30)
def test_matmul():
@flow.global_function()
def matmul():
a = flow.get_variable(
name="a",
shape=(2, 3),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
b = flow.get_variable(
name="b",
shape=(3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
return flow.matmul(a, b)
convert_to_onnx_and_check(matmul)
def test_matmul_ta():
@flow.global_function()
def matmul():
a = flow.get_variable(
name="a",
shape=(3, 2),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
b = flow.get_variable(
name="b",
shape=(3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
return flow.matmul(a, b, transpose_a=True)
convert_to_onnx_and_check(matmul)
def forward(self, x: flow.Tensor) -> flow.Tensor:
return self.matmul(x)
matmul = MatMul()
class matmulOpGraph(flow.nn.Graph):
def __init__(self):
super().__init__()
self.m = matmul
def test_matmul_tb():
@flow.global_function()
def matmul():
a = flow.get_variable(
name="a",
shape=(2, 3),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
b = flow.get_variable(
name="b",
shape=(4, 3),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
return flow.matmul(a, b, transpose_b=True)
def build(self, x):
out = self.m(x)
return out
convert_to_onnx_and_check(matmul)
def test_matmul_ta_tb():
@flow.global_function()
def matmul():
a = flow.get_variable(
name="a",
shape=(3, 2),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
b = flow.get_variable(
name="b",
shape=(4, 3),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
return flow.matmul(a, b, transpose_a=True, transpose_b=True)
convert_to_onnx_and_check(matmul)
def test_matmul():
matmul_graph = matmulOpGraph()
matmul_graph._compile(flow.randn(1, 20))
def test_batch_matmul():
@flow.global_function()
def matmul():
a = flow.get_variable(
name="a",
shape=(4, 2, 3),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
b = flow.get_variable(
name="b",
shape=(4, 3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(),
)
return flow.matmul(a, b)
with tempfile.TemporaryDirectory() as tmpdirname:
flow.save(matmul.state_dict(), tmpdirname)
convert_to_onnx_and_check(matmul_graph, flow_weight_dir=tmpdirname, onnx_model_path="/tmp")
convert_to_onnx_and_check(matmul)
test_matmul()
"""
Copyright 2020 The OneFlow 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 oneflow as flow
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check
def generate_min_max_observer_test(
out_pos: int,
per_layer: bool,
formula: str,
scheme: str,
device_type: str = "cpu",
opset: int = 10,
):
@flow.global_function()
def min_max_observer():
with flow.scope.placement(device_type, "0:0"):
x = flow.get_variable(
name="x1",
shape=(2, 3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(-10, 10),
)
return flow.quantization.min_max_observer(
x,
per_layer_quantization=per_layer,
quantization_formula=formula,
quantization_scheme=scheme,
)[out_pos]
convert_to_onnx_and_check(min_max_observer, opset=opset)
def test_min_max_observer_symmetric():
generate_min_max_observer_test(0, True, "google", "symmetric")
def test_min_max_observer_symmetric_zero_point():
generate_min_max_observer_test(1, True, "google", "symmetric")
def test_min_max_observer_affine():
generate_min_max_observer_test(0, True, "google", "affine")
def test_min_max_observer_affine_zero_point():
generate_min_max_observer_test(1, True, "google", "affine")
def test_min_max_observer_symmetric_per_channel():
generate_min_max_observer_test(0, False, "google", "symmetric", opset=13)
def test_min_max_observer_symmetric_per_channel_zero_point():
generate_min_max_observer_test(1, False, "google", "symmetric", opset=13)
def test_min_max_observer_affine_per_channel():
generate_min_max_observer_test(0, False, "google", "affine", opset=13)
def test_min_max_observer_affine_per_channel_zero_point():
generate_min_max_observer_test(1, False, "google", "affine", opset=13)
def test_min_max_observer_cambricon():
generate_min_max_observer_test(0, True, "cambricon", "symmetric")
def test_min_max_observer_cambricon_zero_point():
generate_min_max_observer_test(1, True, "cambricon", "symmetric")
def test_min_max_observer_symmetric_gpu():
generate_min_max_observer_test(0, True, "google", "symmetric", device_type="gpu")
def test_min_max_observer_symmetric_zero_point_gpu():
generate_min_max_observer_test(1, True, "google", "symmetric", device_type="gpu")
def test_min_max_observer_affine_gpu():
generate_min_max_observer_test(0, True, "google", "affine", device_type="gpu")
def test_min_max_observer_affine_zero_point_gpu():
generate_min_max_observer_test(1, True, "google", "affine", device_type="gpu")
def test_min_max_observer_symmetric_per_channel_gpu():
generate_min_max_observer_test(
0, False, "google", "symmetric", device_type="gpu", opset=13
)
def test_min_max_observer_symmetric_per_channel_zero_point_gpu():
generate_min_max_observer_test(
1, False, "google", "symmetric", device_type="gpu", opset=13
)
def test_min_max_observer_affine_per_channel_gpu():
generate_min_max_observer_test(
0, False, "google", "affine", device_type="gpu", opset=13
)
def test_min_max_observer_affine_per_channel_zero_point_gpu():
generate_min_max_observer_test(
1, False, "google", "affine", device_type="gpu", opset=13
)
def test_min_max_observer_cambricon_gpu():
generate_min_max_observer_test(0, True, "cambricon", "symmetric", device_type="gpu")
def test_min_max_observer_cambricon_zero_point_gpu():
generate_min_max_observer_test(1, True, "cambricon", "symmetric", device_type="gpu")
"""
Copyright 2020 The OneFlow 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 numpy as np
import oneflow as flow
from oneflow_onnx.oneflow2onnx.util import convert_to_onnx_and_check
def set_moving_max_min_value():
max_key, min_key = "", ""
keys = flow.get_all_variables().keys()
for key in keys:
if max_key != "" and min_key != "":
break
if key[-3:] == "max":
max_key = key
if key[-3:] == "min":
min_key = key
flow.load_variables(
{
max_key: np.array([0.5]).astype(np.float32),
min_key: np.array([-0.2]).astype(np.float32),
}
)
def generate_moving_average_min_max_observer_test(
out_pos: int, formula: str, scheme: str = "symmetric", device_type: str = "cpu",
):
flow.clear_default_session()
@flow.global_function()
def moving_average_min_max_observer():
with flow.scope.placement(device_type, "0:0"):
x = flow.get_variable(
name="x1",
shape=(2, 3, 4),
dtype=flow.float,
initializer=flow.random_uniform_initializer(-10, 10),
)
return flow.quantization.moving_average_min_max_observer(
x, quantization_formula=formula, quantization_scheme=scheme
)[out_pos]
set_moving_max_min_value()
convert_to_onnx_and_check(
moving_average_min_max_observer, opset=10, explicit_init=False
)
def test_moving_average_min_max_observer_symmetric():
generate_moving_average_min_max_observer_test(0, "google", "symmetric")
def test_moving_average_min_max_observer_symmetric_zero_point():
generate_moving_average_min_max_observer_test(1, "google", "symmetric")
def test_moving_average_min_max_observer_affine():
generate_moving_average_min_max_observer_test(0, "google", "affine")
def test_moving_average_min_max_observer_affine_zero_point():
generate_moving_average_min_max_observer_test(1, "google", "affine")
def test_moving_average_min_max_observer_cambricon():
generate_moving_average_min_max_observer_test(0, "cambricon")
def test_moving_average_min_max_observer_cambricon_zero_point():
generate_moving_average_min_max_observer_test(1, "cambricon")
def test_moving_average_min_max_observer_symmetric_gpu():
generate_moving_average_min_max_observer_test(
0, "google", "symmetric", device_type="gpu"
)
def test_moving_average_min_max_observer_symmetric_zero_point_gpu():
generate_moving_average_min_max_observer_test(
1, "google", "symmetric", device_type="gpu"
)
def test_moving_average_min_max_observer_affine_gpu():
generate_moving_average_min_max_observer_test(
0, "google", "affine", device_type="gpu"
)
def test_moving_average_min_max_observer_affine_zero_point_gpu():
generate_moving_average_min_max_observer_test(
1, "google", "affine", device_type="gpu"
)
def test_moving_average_min_max_observer_cambricon_gpu():
generate_moving_average_min_max_observer_test(0, "cambricon", device_type="gpu")
def test_moving_average_min_max_observer_cambricon_zero_point_gpu():
generate_moving_average_min_max_observer_test(1, "cambricon", device_type="gpu")
#!/bin/bash
read_dir(){
for file in `ls -a $1`
do
if [ -d $1"/"$file ]
then
if [[ $file != '.' && $file != '..' ]]
then
read_dir $1"/"$file
fi
else
check_suffix $1"/"$file
fi
done
}
check_suffix()
{
file=$1
if [ "${file##*.}"x = "py"x ];then
python3 -m pytest $file -v -s
python3 /tmp/oneflow_code.py
fi
}
path="examples/x2oneflow/pytorch2oneflow/code_gen"
read_dir $path
path="examples/x2oneflow/tensorflow2oneflow/code_gen"
read_dir $path
path="examples/x2oneflow/paddle2oneflow/code_gen"
read_dir $path
\ No newline at end of file
"""
Copyright 2020 The OneFlow 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.
"""
# https://github.com/PaddlePaddle/PaddleClas/blob/develop/ppcls/modeling/architectures/alexnet.py
import paddle
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout, ReLU
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
import math
_all__ = ["AlexNet"]
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
class ConvPoolLayer(nn.Layer):
def __init__(
self,
input_channels,
output_channels,
filter_size,
stride,
padding,
stdv,
groups=1,
act=None,
name=None,
):
super(ConvPoolLayer, self).__init__()
self.relu = ReLU() if act == "relu" else None
self._conv = Conv2D(
in_channels=input_channels,
out_channels=output_channels,
kernel_size=filter_size,
stride=stride,
padding=padding,
groups=groups,
weight_attr=ParamAttr(
name=name + "_weights", initializer=Uniform(-stdv, stdv)
),
bias_attr=ParamAttr(
name=name + "_offset", initializer=Uniform(-stdv, stdv)
),
)
self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
def forward(self, inputs):
x = self._conv(inputs)
if self.relu is not None:
x = self.relu(x)
x = self._pool(x)
return x
class AlexNetDY(nn.Layer):
def __init__(self, class_dim=1000):
super(AlexNetDY, self).__init__()
stdv = 1.0 / math.sqrt(3 * 11 * 11)
self._conv1 = ConvPoolLayer(3, 64, 11, 4, 2, stdv, act="relu", name="conv1")
stdv = 1.0 / math.sqrt(64 * 5 * 5)
self._conv2 = ConvPoolLayer(64, 192, 5, 1, 2, stdv, act="relu", name="conv2")
stdv = 1.0 / math.sqrt(192 * 3 * 3)
self._conv3 = Conv2D(
192,
384,
3,
stride=1,
padding=1,
weight_attr=ParamAttr(
name="conv3_weights", initializer=Uniform(-stdv, stdv)
),
bias_attr=ParamAttr(name="conv3_offset", initializer=Uniform(-stdv, stdv)),
)
stdv = 1.0 / math.sqrt(384 * 3 * 3)
self._conv4 = Conv2D(
384,
256,
3,
stride=1,
padding=1,
weight_attr=ParamAttr(
name="conv4_weights", initializer=Uniform(-stdv, stdv)
),
bias_attr=ParamAttr(name="conv4_offset", initializer=Uniform(-stdv, stdv)),
)
stdv = 1.0 / math.sqrt(256 * 3 * 3)
self._conv5 = ConvPoolLayer(256, 256, 3, 1, 1, stdv, act="relu", name="conv5")
stdv = 1.0 / math.sqrt(256 * 6 * 6)
self._drop1 = Dropout(p=0.5, mode="downscale_in_infer")
self._fc6 = Linear(
in_features=256 * 6 * 6,
out_features=4096,
weight_attr=ParamAttr(name="fc6_weights", initializer=Uniform(-stdv, stdv)),
bias_attr=ParamAttr(name="fc6_offset", initializer=Uniform(-stdv, stdv)),
)
self._drop2 = Dropout(p=0.5, mode="downscale_in_infer")
self._fc7 = Linear(
in_features=4096,
out_features=4096,
weight_attr=ParamAttr(name="fc7_weights", initializer=Uniform(-stdv, stdv)),
bias_attr=ParamAttr(name="fc7_offset", initializer=Uniform(-stdv, stdv)),
)
self._fc8 = Linear(
in_features=4096,
out_features=class_dim,
weight_attr=ParamAttr(name="fc8_weights", initializer=Uniform(-stdv, stdv)),
bias_attr=ParamAttr(name="fc8_offset", initializer=Uniform(-stdv, stdv)),
)
def forward(self, inputs):
x = self._conv1(inputs)
x = self._conv2(x)
x = self._conv3(x)
x = F.relu(x)
x = self._conv4(x)
x = F.relu(x)
x = self._conv5(x)
x = paddle.flatten(x, start_axis=1, stop_axis=-1)
x = self._drop1(x)
x = self._fc6(x)
x = F.relu(x)
x = self._drop2(x)
x = self._fc7(x)
x = F.relu(x)
x = self._fc8(x)
return x
def AlexNet(**args):
model = AlexNetDY(**args)
return model
def test_alexnet():
load_paddle_module_and_check(
AlexNet, input_size=(1, 3, 224, 224), train_flag=False, flow_weight_dir="/tmp/oneflow", oneflow_code_gen_flag=True
)
"""
Copyright 2020 The OneFlow 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.
"""
# https://github.com/PaddlePaddle/PaddleClas/blob/develop/ppcls/modeling/architectures/darknet.py
import paddle
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
import math
__all__ = ["DarkNet53"]
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
class ConvBNLayer(nn.Layer):
def __init__(
self, input_channels, output_channels, filter_size, stride, padding, name=None
):
super(ConvBNLayer, self).__init__()
self._conv = Conv2D(
in_channels=input_channels,
out_channels=output_channels,
kernel_size=filter_size,
stride=stride,
padding=padding,
weight_attr=ParamAttr(name=name + ".conv.weights"),
bias_attr=False,
)
bn_name = name + ".bn"
self._bn = BatchNorm(
num_channels=output_channels,
act="relu",
param_attr=ParamAttr(name=bn_name + ".scale"),
bias_attr=ParamAttr(name=bn_name + ".offset"),
moving_mean_name=bn_name + ".mean",
moving_variance_name=bn_name + ".var",
)
def forward(self, inputs):
x = self._conv(inputs)
x = self._bn(x)
return x
class BasicBlock(nn.Layer):
def __init__(self, input_channels, output_channels, name=None):
super(BasicBlock, self).__init__()
self._conv1 = ConvBNLayer(
input_channels, output_channels, 1, 1, 0, name=name + ".0"
)
self._conv2 = ConvBNLayer(
output_channels, output_channels * 2, 3, 1, 1, name=name + ".1"
)
def forward(self, inputs):
x = self._conv1(inputs)
x = self._conv2(x)
return paddle.add(x=inputs, y=x)
class DarkNet(nn.Layer):
def __init__(self, class_dim=1000):
super(DarkNet, self).__init__()
self.stages = [1, 2, 8, 8, 4]
self._conv1 = ConvBNLayer(3, 32, 3, 1, 1, name="yolo_input")
self._conv2 = ConvBNLayer(32, 64, 3, 2, 1, name="yolo_input.downsample")
self._basic_block_01 = BasicBlock(64, 32, name="stage.0.0")
self._downsample_0 = ConvBNLayer(64, 128, 3, 2, 1, name="stage.0.downsample")
self._basic_block_11 = BasicBlock(128, 64, name="stage.1.0")
self._basic_block_12 = BasicBlock(128, 64, name="stage.1.1")
self._downsample_1 = ConvBNLayer(128, 256, 3, 2, 1, name="stage.1.downsample")
self._basic_block_21 = BasicBlock(256, 128, name="stage.2.0")
self._basic_block_22 = BasicBlock(256, 128, name="stage.2.1")
self._basic_block_23 = BasicBlock(256, 128, name="stage.2.2")
self._basic_block_24 = BasicBlock(256, 128, name="stage.2.3")
self._basic_block_25 = BasicBlock(256, 128, name="stage.2.4")
self._basic_block_26 = BasicBlock(256, 128, name="stage.2.5")
self._basic_block_27 = BasicBlock(256, 128, name="stage.2.6")
self._basic_block_28 = BasicBlock(256, 128, name="stage.2.7")
self._downsample_2 = ConvBNLayer(256, 512, 3, 2, 1, name="stage.2.downsample")
self._basic_block_31 = BasicBlock(512, 256, name="stage.3.0")
self._basic_block_32 = BasicBlock(512, 256, name="stage.3.1")
self._basic_block_33 = BasicBlock(512, 256, name="stage.3.2")
self._basic_block_34 = BasicBlock(512, 256, name="stage.3.3")
self._basic_block_35 = BasicBlock(512, 256, name="stage.3.4")
self._basic_block_36 = BasicBlock(512, 256, name="stage.3.5")
self._basic_block_37 = BasicBlock(512, 256, name="stage.3.6")
self._basic_block_38 = BasicBlock(512, 256, name="stage.3.7")
self._downsample_3 = ConvBNLayer(512, 1024, 3, 2, 1, name="stage.3.downsample")
self._basic_block_41 = BasicBlock(1024, 512, name="stage.4.0")
self._basic_block_42 = BasicBlock(1024, 512, name="stage.4.1")
self._basic_block_43 = BasicBlock(1024, 512, name="stage.4.2")
self._basic_block_44 = BasicBlock(1024, 512, name="stage.4.3")
self._pool = AdaptiveAvgPool2D(1)
stdv = 1.0 / math.sqrt(1024.0)
self._out = Linear(
1024,
class_dim,
weight_attr=ParamAttr(name="fc_weights", initializer=Uniform(-stdv, stdv)),
bias_attr=ParamAttr(name="fc_offset"),
)
def forward(self, inputs):
x = self._conv1(inputs)
x = self._conv2(x)
x = self._basic_block_01(x)
x = self._downsample_0(x)
x = self._basic_block_11(x)
x = self._basic_block_12(x)
x = self._downsample_1(x)
x = self._basic_block_21(x)
x = self._basic_block_22(x)
x = self._basic_block_23(x)
x = self._basic_block_24(x)
x = self._basic_block_25(x)
x = self._basic_block_26(x)
x = self._basic_block_27(x)
x = self._basic_block_28(x)
x = self._downsample_2(x)
x = self._basic_block_31(x)
x = self._basic_block_32(x)
x = self._basic_block_33(x)
x = self._basic_block_34(x)
x = self._basic_block_35(x)
x = self._basic_block_36(x)
x = self._basic_block_37(x)
x = self._basic_block_38(x)
x = self._downsample_3(x)
x = self._basic_block_41(x)
x = self._basic_block_42(x)
x = self._basic_block_43(x)
x = self._basic_block_44(x)
x = self._pool(x)
x = paddle.squeeze(x, axis=[2, 3])
x = self._out(x)
return x
def DarkNet53(**args):
model = DarkNet(**args)
return model
def test_darknet():
load_paddle_module_and_check(
DarkNet53, input_size=(1, 3, 224, 224), train_flag=False, flow_weight_dir="/tmp/oneflow", oneflow_code_gen_flag=True
)
"""
Copyright 2020 The OneFlow 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 numpy as np
import paddle
from paddle import ParamAttr
import paddle.nn as nn
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
import math
__all__ = ["DenseNet121", "DenseNet161", "DenseNet169", "DenseNet201", "DenseNet264"]
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
class BNACConvLayer(nn.Layer):
def __init__(
self,
num_channels,
num_filters,
filter_size,
stride=1,
pad=0,
groups=1,
act="relu",
name=None,
):
super(BNACConvLayer, self).__init__()
self._batch_norm = BatchNorm(
num_channels,
act=act,
param_attr=ParamAttr(name=name + "_bn_scale"),
bias_attr=ParamAttr(name + "_bn_offset"),
moving_mean_name=name + "_bn_mean",
moving_variance_name=name + "_bn_variance",
)
self._conv = Conv2D(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=filter_size,
stride=stride,
padding=pad,
groups=groups,
weight_attr=ParamAttr(name=name + "_weights"),
bias_attr=False,
)
def forward(self, input):
y = self._batch_norm(input)
y = self._conv(y)
return y
class DenseLayer(nn.Layer):
def __init__(self, num_channels, growth_rate, bn_size, dropout, name=None):
super(DenseLayer, self).__init__()
self.dropout = dropout
self.bn_ac_func1 = BNACConvLayer(
num_channels=num_channels,
num_filters=bn_size * growth_rate,
filter_size=1,
pad=0,
stride=1,
name=name + "_x1",
)
self.bn_ac_func2 = BNACConvLayer(
num_channels=bn_size * growth_rate,
num_filters=growth_rate,
filter_size=3,
pad=1,
stride=1,
name=name + "_x2",
)
if dropout:
self.dropout_func = Dropout(p=dropout, mode="downscale_in_infer")
def forward(self, input):
conv = self.bn_ac_func1(input)
conv = self.bn_ac_func2(conv)
if self.dropout:
conv = self.dropout_func(conv)
conv = paddle.concat([input, conv], axis=1)
return conv
class DenseBlock(nn.Layer):
def __init__(
self, num_channels, num_layers, bn_size, growth_rate, dropout, name=None
):
super(DenseBlock, self).__init__()
self.dropout = dropout
self.dense_layer_func = []
pre_channel = num_channels
for layer in range(num_layers):
self.dense_layer_func.append(
self.add_sublayer(
"{}_{}".format(name, layer + 1),
DenseLayer(
num_channels=pre_channel,
growth_rate=growth_rate,
bn_size=bn_size,
dropout=dropout,
name=name + "_" + str(layer + 1),
),
)
)
pre_channel = pre_channel + growth_rate
def forward(self, input):
conv = input
for func in self.dense_layer_func:
conv = func(conv)
return conv
class TransitionLayer(nn.Layer):
def __init__(self, num_channels, num_output_features, name=None):
super(TransitionLayer, self).__init__()
self.conv_ac_func = BNACConvLayer(
num_channels=num_channels,
num_filters=num_output_features,
filter_size=1,
pad=0,
stride=1,
name=name,
)
self.pool2d_avg = AvgPool2D(kernel_size=2, stride=2, padding=0)
def forward(self, input):
y = self.conv_ac_func(input)
y = self.pool2d_avg(y)
return y
class ConvBNLayer(nn.Layer):
def __init__(
self,
num_channels,
num_filters,
filter_size,
stride=1,
pad=0,
groups=1,
act="relu",
name=None,
):
super(ConvBNLayer, self).__init__()
self._conv = Conv2D(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=filter_size,
stride=stride,
padding=pad,
groups=groups,
weight_attr=ParamAttr(name=name + "_weights"),
bias_attr=False,
)
self._batch_norm = BatchNorm(
num_filters,
act=act,
param_attr=ParamAttr(name=name + "_bn_scale"),
bias_attr=ParamAttr(name + "_bn_offset"),
moving_mean_name=name + "_bn_mean",
moving_variance_name=name + "_bn_variance",
)
def forward(self, input):
y = self._conv(input)
y = self._batch_norm(y)
return y
class DenseNet(nn.Layer):
def __init__(self, layers=60, bn_size=4, dropout=0, class_dim=1000):
super(DenseNet, self).__init__()
supported_layers = [121, 161, 169, 201, 264]
assert (
layers in supported_layers
), "supported layers are {} but input layer is {}".format(
supported_layers, layers
)
densenet_spec = {
121: (64, 32, [6, 12, 24, 16]),
161: (96, 48, [6, 12, 36, 24]),
169: (64, 32, [6, 12, 32, 32]),
201: (64, 32, [6, 12, 48, 32]),
264: (64, 32, [6, 12, 64, 48]),
}
num_init_features, growth_rate, block_config = densenet_spec[layers]
self.conv1_func = ConvBNLayer(
num_channels=3,
num_filters=num_init_features,
filter_size=7,
stride=2,
pad=3,
act="relu",
name="conv1",
)
self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1)
self.block_config = block_config
self.dense_block_func_list = []
self.transition_func_list = []
pre_num_channels = num_init_features
num_features = num_init_features
for i, num_layers in enumerate(block_config):
self.dense_block_func_list.append(
self.add_sublayer(
"db_conv_{}".format(i + 2),
DenseBlock(
num_channels=pre_num_channels,
num_layers=num_layers,
bn_size=bn_size,
growth_rate=growth_rate,
dropout=dropout,
name="conv" + str(i + 2),
),
)
)
num_features = num_features + num_layers * growth_rate
pre_num_channels = num_features
if i != len(block_config) - 1:
self.transition_func_list.append(
self.add_sublayer(
"tr_conv{}_blk".format(i + 2),
TransitionLayer(
num_channels=pre_num_channels,
num_output_features=num_features // 2,
name="conv" + str(i + 2) + "_blk",
),
)
)
pre_num_channels = num_features // 2
num_features = num_features // 2
self.batch_norm = BatchNorm(
num_features,
act="relu",
param_attr=ParamAttr(name="conv5_blk_bn_scale"),
bias_attr=ParamAttr(name="conv5_blk_bn_offset"),
moving_mean_name="conv5_blk_bn_mean",
moving_variance_name="conv5_blk_bn_variance",
)
self.pool2d_avg = AdaptiveAvgPool2D(1)
stdv = 1.0 / math.sqrt(num_features * 1.0)
self.out = Linear(
num_features,
class_dim,
weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name="fc_weights"),
bias_attr=ParamAttr(name="fc_offset"),
)
def forward(self, input):
conv = self.conv1_func(input)
conv = self.pool2d_max(conv)
for i, num_layers in enumerate(self.block_config):
conv = self.dense_block_func_list[i](conv)
if i != len(self.block_config) - 1:
conv = self.transition_func_list[i](conv)
conv = self.batch_norm(conv)
y = self.pool2d_avg(conv)
y = paddle.flatten(y, start_axis=1, stop_axis=-1)
# y = paddle.reshape(y, [y.shape[0], -1])
y = self.out(y)
return y
def DenseNet121(**args):
model = DenseNet(layers=121, **args)
return model
def DenseNet161(**args):
model = DenseNet(layers=161, **args)
return model
def DenseNet169(**args):
model = DenseNet(layers=169, **args)
return model
def DenseNet201(**args):
model = DenseNet(layers=201, **args)
return model
def DenseNet264(**args):
model = DenseNet(layers=264, **args)
return model
def test_densenet121():
load_paddle_module_and_check(
DenseNet121, input_size=(1, 3, 224, 224), train_flag=False, flow_weight_dir="/tmp/oneflow", oneflow_code_gen_flag=True
)
"""
Copyright 2020 The OneFlow 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 numpy as np
import sys
import paddle
from paddle import ParamAttr
import paddle.nn as nn
from paddle.nn import Conv2D, BatchNorm, Linear
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
import math
__all__ = [
"DPN",
"DPN68",
"DPN92",
"DPN98",
"DPN107",
"DPN131",
]
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
class ConvBNLayer(nn.Layer):
def __init__(
self,
num_channels,
num_filters,
filter_size,
stride=1,
pad=0,
groups=1,
act="relu",
name=None,
):
super(ConvBNLayer, self).__init__()
self._conv = Conv2D(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=filter_size,
stride=stride,
padding=pad,
groups=groups,
weight_attr=ParamAttr(name=name + "_weights"),
bias_attr=False,
)
self._batch_norm = BatchNorm(
num_filters,
act=act,
param_attr=ParamAttr(name=name + "_bn_scale"),
bias_attr=ParamAttr(name + "_bn_offset"),
moving_mean_name=name + "_bn_mean",
moving_variance_name=name + "_bn_variance",
)
def forward(self, input):
y = self._conv(input)
y = self._batch_norm(y)
return y
class BNACConvLayer(nn.Layer):
def __init__(
self,
num_channels,
num_filters,
filter_size,
stride=1,
pad=0,
groups=1,
act="relu",
name=None,
):
super(BNACConvLayer, self).__init__()
self.num_channels = num_channels
self._batch_norm = BatchNorm(
num_channels,
act=act,
param_attr=ParamAttr(name=name + "_bn_scale"),
bias_attr=ParamAttr(name + "_bn_offset"),
moving_mean_name=name + "_bn_mean",
moving_variance_name=name + "_bn_variance",
)
self._conv = Conv2D(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=filter_size,
stride=stride,
padding=pad,
groups=groups,
weight_attr=ParamAttr(name=name + "_weights"),
bias_attr=False,
)
def forward(self, input):
y = self._batch_norm(input)
y = self._conv(y)
return y
class DualPathFactory(nn.Layer):
def __init__(
self,
num_channels,
num_1x1_a,
num_3x3_b,
num_1x1_c,
inc,
G,
_type="normal",
name=None,
):
super(DualPathFactory, self).__init__()
self.num_1x1_c = num_1x1_c
self.inc = inc
self.name = name
kw = 3
kh = 3
pw = (kw - 1) // 2
ph = (kh - 1) // 2
# type
if _type == "proj":
key_stride = 1
self.has_proj = True
elif _type == "down":
key_stride = 2
self.has_proj = True
elif _type == "normal":
key_stride = 1
self.has_proj = False
else:
print("not implemented now!!!")
sys.exit(1)
data_in_ch = (
sum(num_channels) if isinstance(num_channels, list) else num_channels
)
if self.has_proj:
self.c1x1_w_func = BNACConvLayer(
num_channels=data_in_ch,
num_filters=num_1x1_c + 2 * inc,
filter_size=(1, 1),
pad=(0, 0),
stride=(key_stride, key_stride),
name=name + "_match",
)
self.c1x1_a_func = BNACConvLayer(
num_channels=data_in_ch,
num_filters=num_1x1_a,
filter_size=(1, 1),
pad=(0, 0),
name=name + "_conv1",
)
self.c3x3_b_func = BNACConvLayer(
num_channels=num_1x1_a,
num_filters=num_3x3_b,
filter_size=(kw, kh),
pad=(pw, ph),
stride=(key_stride, key_stride),
groups=G,
name=name + "_conv2",
)
self.c1x1_c_func = BNACConvLayer(
num_channels=num_3x3_b,
num_filters=num_1x1_c + inc,
filter_size=(1, 1),
pad=(0, 0),
name=name + "_conv3",
)
def forward(self, input):
# PROJ
if isinstance(input, list):
data_in = paddle.concat([input[0], input[1]], axis=1)
else:
data_in = input
if self.has_proj:
c1x1_w = self.c1x1_w_func(data_in)
data_o1, data_o2 = paddle.split(
c1x1_w, num_or_sections=[self.num_1x1_c, 2 * self.inc], axis=1
)
else:
data_o1 = input[0]
data_o2 = input[1]
c1x1_a = self.c1x1_a_func(data_in)
c3x3_b = self.c3x3_b_func(c1x1_a)
c1x1_c = self.c1x1_c_func(c3x3_b)
c1x1_c1, c1x1_c2 = paddle.split(
c1x1_c, num_or_sections=[self.num_1x1_c, self.inc], axis=1
)
# OUTPUTS
summ = paddle.add(x=data_o1, y=c1x1_c1)
dense = paddle.concat([data_o2, c1x1_c2], axis=1)
# tensor, channels
return [summ, dense]
class DPN(nn.Layer):
def __init__(self, layers=68, class_dim=1000):
super(DPN, self).__init__()
self._class_dim = class_dim
args = self.get_net_args(layers)
bws = args["bw"]
inc_sec = args["inc_sec"]
rs = args["r"]
k_r = args["k_r"]
k_sec = args["k_sec"]
G = args["G"]
init_num_filter = args["init_num_filter"]
init_filter_size = args["init_filter_size"]
init_padding = args["init_padding"]
self.k_sec = k_sec
self.conv1_x_1_func = ConvBNLayer(
num_channels=3,
num_filters=init_num_filter,
filter_size=init_filter_size,
stride=2,
pad=init_padding,
act="relu",
name="conv1",
)
self.pool2d_max = MaxPool2D(kernel_size=3, stride=2, padding=1)
num_channel_dpn = init_num_filter
self.dpn_func_list = []
# conv2 - conv5
match_list, num = [], 0
for gc in range(4):
bw = bws[gc]
inc = inc_sec[gc]
R = (k_r * bw) // rs[gc]
if gc == 0:
_type1 = "proj"
_type2 = "normal"
match = 1
else:
_type1 = "down"
_type2 = "normal"
match = match + k_sec[gc - 1]
match_list.append(match)
self.dpn_func_list.append(
self.add_sublayer(
"dpn{}".format(match),
DualPathFactory(
num_channels=num_channel_dpn,
num_1x1_a=R,
num_3x3_b=R,
num_1x1_c=bw,
inc=inc,
G=G,
_type=_type1,
name="dpn" + str(match),
),
)
)
num_channel_dpn = [bw, 3 * inc]
for i_ly in range(2, k_sec[gc] + 1):
num += 1
if num in match_list:
num += 1
self.dpn_func_list.append(
self.add_sublayer(
"dpn{}".format(num),
DualPathFactory(
num_channels=num_channel_dpn,
num_1x1_a=R,
num_3x3_b=R,
num_1x1_c=bw,
inc=inc,
G=G,
_type=_type2,
name="dpn" + str(num),
),
)
)
num_channel_dpn = [num_channel_dpn[0], num_channel_dpn[1] + inc]
out_channel = sum(num_channel_dpn)
self.conv5_x_x_bn = BatchNorm(
num_channels=sum(num_channel_dpn),
act="relu",
param_attr=ParamAttr(name="final_concat_bn_scale"),
bias_attr=ParamAttr("final_concat_bn_offset"),
moving_mean_name="final_concat_bn_mean",
moving_variance_name="final_concat_bn_variance",
)
self.pool2d_avg = AdaptiveAvgPool2D(1)
stdv = 0.01
self.out = Linear(
out_channel,
class_dim,
weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name="fc_weights"),
bias_attr=ParamAttr(name="fc_offset"),
)
def forward(self, input):
conv1_x_1 = self.conv1_x_1_func(input)
convX_x_x = self.pool2d_max(conv1_x_1)
dpn_idx = 0
for gc in range(4):
convX_x_x = self.dpn_func_list[dpn_idx](convX_x_x)
dpn_idx += 1
for i_ly in range(2, self.k_sec[gc] + 1):
convX_x_x = self.dpn_func_list[dpn_idx](convX_x_x)
dpn_idx += 1
conv5_x_x = paddle.concat(convX_x_x, axis=1)
conv5_x_x = self.conv5_x_x_bn(conv5_x_x)
y = self.pool2d_avg(conv5_x_x)
y = paddle.flatten(y, start_axis=1, stop_axis=-1)
y = self.out(y)
return y
def get_net_args(self, layers):
if layers == 68:
k_r = 128
G = 32
k_sec = [3, 4, 12, 3]
inc_sec = [16, 32, 32, 64]
bw = [64, 128, 256, 512]
r = [64, 64, 64, 64]
init_num_filter = 10
init_filter_size = 3
init_padding = 1
elif layers == 92:
k_r = 96
G = 32
k_sec = [3, 4, 20, 3]
inc_sec = [16, 32, 24, 128]
bw = [256, 512, 1024, 2048]
r = [256, 256, 256, 256]
init_num_filter = 64
init_filter_size = 7
init_padding = 3
elif layers == 98:
k_r = 160
G = 40
k_sec = [3, 6, 20, 3]
inc_sec = [16, 32, 32, 128]
bw = [256, 512, 1024, 2048]
r = [256, 256, 256, 256]
init_num_filter = 96
init_filter_size = 7
init_padding = 3
elif layers == 107:
k_r = 200
G = 50
k_sec = [4, 8, 20, 3]
inc_sec = [20, 64, 64, 128]
bw = [256, 512, 1024, 2048]
r = [256, 256, 256, 256]
init_num_filter = 128
init_filter_size = 7
init_padding = 3
elif layers == 131:
k_r = 160
G = 40
k_sec = [4, 8, 28, 3]
inc_sec = [16, 32, 32, 128]
bw = [256, 512, 1024, 2048]
r = [256, 256, 256, 256]
init_num_filter = 128
init_filter_size = 7
init_padding = 3
else:
raise NotImplementedError
net_arg = {
"k_r": k_r,
"G": G,
"k_sec": k_sec,
"inc_sec": inc_sec,
"bw": bw,
"r": r,
}
net_arg["init_num_filter"] = init_num_filter
net_arg["init_filter_size"] = init_filter_size
net_arg["init_padding"] = init_padding
return net_arg
def DPN68(**args):
model = DPN(layers=68, **args)
return model
def DPN92(**args):
model = DPN(layers=92, **args)
return model
def DPN98(**args):
model = DPN(layers=98, **args)
return model
def DPN107(**args):
model = DPN(layers=107, **args)
return model
def DPN131(**args):
model = DPN(layers=131, **args)
return model
def test_dpn68():
load_paddle_module_and_check(
DPN68, input_size=(1, 3, 224, 224), train_flag=False, flow_weight_dir="/tmp/oneflow", oneflow_code_gen_flag=True
)
"""
Copyright 2020 The OneFlow 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 math
import paddle
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2D, BatchNorm, AdaptiveAvgPool2D, Linear
from paddle.regularizer import L2Decay
from paddle.nn.initializer import Uniform, KaimingNormal
__all__ = ["GhostNet_x0_5", "GhostNet_x1_0", "GhostNet_x1_3"]
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
class ConvBNLayer(nn.Layer):
def __init__(
self,
in_channels,
out_channels,
kernel_size,
stride=1,
groups=1,
act="relu",
name=None,
):
super(ConvBNLayer, self).__init__()
self._conv = Conv2D(
in_channels=in_channels,
out_channels=out_channels,
kernel_size=kernel_size,
stride=stride,
padding=(kernel_size - 1) // 2,
groups=groups,
weight_attr=ParamAttr(initializer=KaimingNormal(), name=name + "_weights"),
bias_attr=False,
)
bn_name = name + "_bn"
self._batch_norm = BatchNorm(
num_channels=out_channels,
act=act,
param_attr=ParamAttr(name=bn_name + "_scale", regularizer=L2Decay(0.0)),
bias_attr=ParamAttr(name=bn_name + "_offset", regularizer=L2Decay(0.0)),
moving_mean_name=bn_name + "_mean",
moving_variance_name=bn_name + "_variance",
)
def forward(self, inputs):
y = self._conv(inputs)
y = self._batch_norm(y)
return y
class SEBlock(nn.Layer):
def __init__(self, num_channels, reduction_ratio=4, name=None):
super(SEBlock, self).__init__()
self.pool2d_gap = AdaptiveAvgPool2D(1)
self._num_channels = num_channels
stdv = 1.0 / math.sqrt(num_channels * 1.0)
med_ch = num_channels // reduction_ratio
self.squeeze = Linear(
num_channels,
med_ch,
weight_attr=ParamAttr(
initializer=Uniform(-stdv, stdv), name=name + "_1_weights"
),
bias_attr=ParamAttr(name=name + "_1_offset"),
)
stdv = 1.0 / math.sqrt(med_ch * 1.0)
self.excitation = Linear(
med_ch,
num_channels,
weight_attr=ParamAttr(
initializer=Uniform(-stdv, stdv), name=name + "_2_weights"
),
bias_attr=ParamAttr(name=name + "_2_offset"),
)
def forward(self, inputs):
pool = self.pool2d_gap(inputs)
pool = paddle.squeeze(pool, axis=[2, 3])
squeeze = self.squeeze(pool)
squeeze = F.relu(squeeze)
excitation = self.excitation(squeeze)
excitation = paddle.clip(x=excitation, min=0, max=1)
excitation = paddle.unsqueeze(excitation, axis=[2, 3])
out = paddle.multiply(inputs, excitation)
return out
class GhostModule(nn.Layer):
def __init__(
self,
in_channels,
output_channels,
kernel_size=1,
ratio=2,
dw_size=3,
stride=1,
relu=True,
name=None,
):
super(GhostModule, self).__init__()
init_channels = int(math.ceil(output_channels / ratio))
new_channels = int(init_channels * (ratio - 1))
self.primary_conv = ConvBNLayer(
in_channels=in_channels,
out_channels=init_channels,
kernel_size=kernel_size,
stride=stride,
groups=1,
act="relu" if relu else None,
name=name + "_primary_conv",
)
self.cheap_operation = ConvBNLayer(
in_channels=init_channels,
out_channels=new_channels,
kernel_size=dw_size,
stride=1,
groups=init_channels,
act="relu" if relu else None,
name=name + "_cheap_operation",
)
def forward(self, inputs):
x = self.primary_conv(inputs)
y = self.cheap_operation(x)
out = paddle.concat([x, y], axis=1)
return out
class GhostBottleneck(nn.Layer):
def __init__(
self,
in_channels,
hidden_dim,
output_channels,
kernel_size,
stride,
use_se,
name=None,
):
super(GhostBottleneck, self).__init__()
self._stride = stride
self._use_se = use_se
self._num_channels = in_channels
self._output_channels = output_channels
self.ghost_module_1 = GhostModule(
in_channels=in_channels,
output_channels=hidden_dim,
kernel_size=1,
stride=1,
relu=True,
name=name + "_ghost_module_1",
)
if stride == 2:
self.depthwise_conv = ConvBNLayer(
in_channels=hidden_dim,
out_channels=hidden_dim,
kernel_size=kernel_size,
stride=stride,
groups=hidden_dim,
act=None,
name=name
+ "_depthwise_depthwise", # looks strange due to an old typo, will be fixed later.
)
if use_se:
self.se_block = SEBlock(num_channels=hidden_dim, name=name + "_se")
self.ghost_module_2 = GhostModule(
in_channels=hidden_dim,
output_channels=output_channels,
kernel_size=1,
relu=False,
name=name + "_ghost_module_2",
)
if stride != 1 or in_channels != output_channels:
self.shortcut_depthwise = ConvBNLayer(
in_channels=in_channels,
out_channels=in_channels,
kernel_size=kernel_size,
stride=stride,
groups=in_channels,
act=None,
name=name
+ "_shortcut_depthwise_depthwise", # looks strange due to an old typo, will be fixed later.
)
self.shortcut_conv = ConvBNLayer(
in_channels=in_channels,
out_channels=output_channels,
kernel_size=1,
stride=1,
groups=1,
act=None,
name=name + "_shortcut_conv",
)
def forward(self, inputs):
x = self.ghost_module_1(inputs)
if self._stride == 2:
x = self.depthwise_conv(x)
if self._use_se:
x = self.se_block(x)
x = self.ghost_module_2(x)
if self._stride == 1 and self._num_channels == self._output_channels:
shortcut = inputs
else:
shortcut = self.shortcut_depthwise(inputs)
shortcut = self.shortcut_conv(shortcut)
return paddle.add(x=x, y=shortcut)
class GhostNet(nn.Layer):
def __init__(self, scale, class_dim=1000):
super(GhostNet, self).__init__()
self.cfgs = [
# k, t, c, SE, s
[3, 16, 16, 0, 1],
[3, 48, 24, 0, 2],
[3, 72, 24, 0, 1],
[5, 72, 40, 1, 2],
[5, 120, 40, 1, 1],
[3, 240, 80, 0, 2],
[3, 200, 80, 0, 1],
[3, 184, 80, 0, 1],
[3, 184, 80, 0, 1],
[3, 480, 112, 1, 1],
[3, 672, 112, 1, 1],
[5, 672, 160, 1, 2],
[5, 960, 160, 0, 1],
[5, 960, 160, 1, 1],
[5, 960, 160, 0, 1],
[5, 960, 160, 1, 1],
]
self.scale = scale
output_channels = int(self._make_divisible(16 * self.scale, 4))
self.conv1 = ConvBNLayer(
in_channels=3,
out_channels=output_channels,
kernel_size=3,
stride=2,
groups=1,
act="relu",
name="conv1",
)
# build inverted residual blocks
idx = 0
self.ghost_bottleneck_list = []
for k, exp_size, c, use_se, s in self.cfgs:
in_channels = output_channels
output_channels = int(self._make_divisible(c * self.scale, 4))
hidden_dim = int(self._make_divisible(exp_size * self.scale, 4))
ghost_bottleneck = self.add_sublayer(
name="_ghostbottleneck_" + str(idx),
sublayer=GhostBottleneck(
in_channels=in_channels,
hidden_dim=hidden_dim,
output_channels=output_channels,
kernel_size=k,
stride=s,
use_se=use_se,
name="_ghostbottleneck_" + str(idx),
),
)
self.ghost_bottleneck_list.append(ghost_bottleneck)
idx += 1
# build last several layers
in_channels = output_channels
output_channels = int(self._make_divisible(exp_size * self.scale, 4))
self.conv_last = ConvBNLayer(
in_channels=in_channels,
out_channels=output_channels,
kernel_size=1,
stride=1,
groups=1,
act="relu",
name="conv_last",
)
self.pool2d_gap = AdaptiveAvgPool2D(1)
in_channels = output_channels
self._fc0_output_channels = 1280
self.fc_0 = ConvBNLayer(
in_channels=in_channels,
out_channels=self._fc0_output_channels,
kernel_size=1,
stride=1,
act="relu",
name="fc_0",
)
self.dropout = nn.Dropout(p=0.2)
stdv = 1.0 / math.sqrt(self._fc0_output_channels * 1.0)
self.fc_1 = Linear(
self._fc0_output_channels,
class_dim,
weight_attr=ParamAttr(
name="fc_1_weights", initializer=Uniform(-stdv, stdv)
),
bias_attr=ParamAttr(name="fc_1_offset"),
)
def forward(self, inputs):
x = self.conv1(inputs)
for ghost_bottleneck in self.ghost_bottleneck_list:
x = ghost_bottleneck(x)
x = self.conv_last(x)
x = self.pool2d_gap(x)
x = self.fc_0(x)
x = self.dropout(x)
x = paddle.reshape(x, shape=[-1, self._fc0_output_channels])
x = self.fc_1(x)
return x
def _make_divisible(self, v, divisor, min_value=None):
"""
This function is taken from the original tf repo.
It ensures that all layers have a channel number that is divisible by 8
It can be seen here:
https://github.com/tensorflow/models/blob/master/research/slim/nets/mobilenet/mobilenet.py
"""
if min_value is None:
min_value = divisor
new_v = max(min_value, int(v + divisor / 2) // divisor * divisor)
# Make sure that round down does not go down by more than 10%.
if new_v < 0.9 * v:
new_v += divisor
return new_v
def GhostNet_x0_5(**args):
model = GhostNet(scale=0.5)
return model
def GhostNet_x1_0(**args):
model = GhostNet(scale=1.0)
return model
def GhostNet_x1_3(**args):
model = GhostNet(scale=1.3)
return model
def test_GhostNet_x0_5():
load_paddle_module_and_check(
GhostNet_x0_5, input_size=(1, 3, 224, 224), train_flag=False, flow_weight_dir="/tmp/oneflow", oneflow_code_gen_flag=True
)
"""
Copyright 2020 The OneFlow 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
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
import math
__all__ = ["GoogLeNet"]
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
def xavier(channels, filter_size, name):
stdv = (3.0 / (filter_size ** 2 * channels)) ** 0.5
param_attr = ParamAttr(initializer=Uniform(-stdv, stdv), name=name + "_weights")
return param_attr
class ConvLayer(nn.Layer):
def __init__(
self,
num_channels,
num_filters,
filter_size,
stride=1,
groups=1,
act=None,
name=None,
):
super(ConvLayer, self).__init__()
self._conv = Conv2D(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=filter_size,
stride=stride,
padding=(filter_size - 1) // 2,
groups=groups,
weight_attr=ParamAttr(name=name + "_weights"),
bias_attr=False,
)
def forward(self, inputs):
y = self._conv(inputs)
return y
class Inception(nn.Layer):
def __init__(
self,
input_channels,
output_channels,
filter1,
filter3R,
filter3,
filter5R,
filter5,
proj,
name=None,
):
super(Inception, self).__init__()
self._conv1 = ConvLayer(
input_channels, filter1, 1, name="inception_" + name + "_1x1"
)
self._conv3r = ConvLayer(
input_channels, filter3R, 1, name="inception_" + name + "_3x3_reduce"
)
self._conv3 = ConvLayer(filter3R, filter3, 3, name="inception_" + name + "_3x3")
self._conv5r = ConvLayer(
input_channels, filter5R, 1, name="inception_" + name + "_5x5_reduce"
)
self._conv5 = ConvLayer(filter5R, filter5, 5, name="inception_" + name + "_5x5")
self._pool = MaxPool2D(kernel_size=3, stride=1, padding=1)
self._convprj = ConvLayer(
input_channels, proj, 1, name="inception_" + name + "_3x3_proj"
)
def forward(self, inputs):
conv1 = self._conv1(inputs)
conv3r = self._conv3r(inputs)
conv3 = self._conv3(conv3r)
conv5r = self._conv5r(inputs)
conv5 = self._conv5(conv5r)
pool = self._pool(inputs)
convprj = self._convprj(pool)
cat = paddle.concat([conv1, conv3, conv5, convprj], axis=1)
cat = F.relu(cat)
return cat
class GoogLeNetDY(nn.Layer):
def __init__(self, class_dim=1000):
super(GoogLeNetDY, self).__init__()
self._conv = ConvLayer(3, 64, 7, 2, name="conv1")
self._pool = MaxPool2D(kernel_size=3, stride=2)
self._conv_1 = ConvLayer(64, 64, 1, name="conv2_1x1")
self._conv_2 = ConvLayer(64, 192, 3, name="conv2_3x3")
self._ince3a = Inception(192, 192, 64, 96, 128, 16, 32, 32, name="ince3a")
self._ince3b = Inception(256, 256, 128, 128, 192, 32, 96, 64, name="ince3b")
self._ince4a = Inception(480, 480, 192, 96, 208, 16, 48, 64, name="ince4a")
self._ince4b = Inception(512, 512, 160, 112, 224, 24, 64, 64, name="ince4b")
self._ince4c = Inception(512, 512, 128, 128, 256, 24, 64, 64, name="ince4c")
self._ince4d = Inception(512, 512, 112, 144, 288, 32, 64, 64, name="ince4d")
self._ince4e = Inception(528, 528, 256, 160, 320, 32, 128, 128, name="ince4e")
self._ince5a = Inception(832, 832, 256, 160, 320, 32, 128, 128, name="ince5a")
self._ince5b = Inception(832, 832, 384, 192, 384, 48, 128, 128, name="ince5b")
self._pool_5 = AvgPool2D(kernel_size=7, stride=7)
self._drop = Dropout(p=0.4, mode="downscale_in_infer")
self._fc_out = Linear(
1024,
class_dim,
weight_attr=xavier(1024, 1, "out"),
bias_attr=ParamAttr(name="out_offset"),
)
self._pool_o1 = AvgPool2D(kernel_size=5, stride=3)
self._conv_o1 = ConvLayer(512, 128, 1, name="conv_o1")
self._fc_o1 = Linear(
1152,
1024,
weight_attr=xavier(2048, 1, "fc_o1"),
bias_attr=ParamAttr(name="fc_o1_offset"),
)
self._drop_o1 = Dropout(p=0.7, mode="downscale_in_infer")
self._out1 = Linear(
1024,
class_dim,
weight_attr=xavier(1024, 1, "out1"),
bias_attr=ParamAttr(name="out1_offset"),
)
self._pool_o2 = AvgPool2D(kernel_size=5, stride=3)
self._conv_o2 = ConvLayer(528, 128, 1, name="conv_o2")
self._fc_o2 = Linear(
1152,
1024,
weight_attr=xavier(2048, 1, "fc_o2"),
bias_attr=ParamAttr(name="fc_o2_offset"),
)
self._drop_o2 = Dropout(p=0.7, mode="downscale_in_infer")
self._out2 = Linear(
1024,
class_dim,
weight_attr=xavier(1024, 1, "out2"),
bias_attr=ParamAttr(name="out2_offset"),
)
def forward(self, inputs):
x = self._conv(inputs)
x = self._pool(x)
x = self._conv_1(x)
x = self._conv_2(x)
x = self._pool(x)
x = self._ince3a(x)
x = self._ince3b(x)
x = self._pool(x)
ince4a = self._ince4a(x)
x = self._ince4b(ince4a)
x = self._ince4c(x)
ince4d = self._ince4d(x)
x = self._ince4e(ince4d)
x = self._pool(x)
x = self._ince5a(x)
ince5b = self._ince5b(x)
x = self._pool_5(ince5b)
x = self._drop(x)
x = paddle.squeeze(x, axis=[2, 3])
out = self._fc_out(x)
# x = self._pool_o1(ince4a)
# x = self._conv_o1(x)
# x = paddle.flatten(x, start_axis=1, stop_axis=-1)
# x = self._fc_o1(x)
# x = F.relu(x)
# x = self._drop_o1(x)
# out1 = self._out1(x)
# x = self._pool_o2(ince4d)
# x = self._conv_o2(x)
# x = paddle.flatten(x, start_axis=1, stop_axis=-1)
# x = self._fc_o2(x)
# x = self._drop_o2(x)
# out2 = self._out2(x)
return out
def GoogLeNet(**args):
model = GoogLeNetDY(**args)
return model
def test_GoogLeNet():
load_paddle_module_and_check(
GoogLeNet, input_size=(1, 3, 224, 224), train_flag=False, flow_weight_dir="/tmp/oneflow", oneflow_code_gen_flag=True
)
"""
Copyright 2020 The OneFlow 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 __future__ import absolute_import
from __future__ import division
from __future__ import print_function
import paddle
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
import math
__all__ = ["InceptionV3"]
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
class ConvBNLayer(nn.Layer):
def __init__(
self,
num_channels,
num_filters,
filter_size,
stride=1,
padding=0,
groups=1,
act="relu",
name=None,
):
super(ConvBNLayer, self).__init__()
self.conv = Conv2D(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=filter_size,
stride=stride,
padding=padding,
groups=groups,
weight_attr=ParamAttr(name=name + "_weights"),
bias_attr=False,
)
self.batch_norm = BatchNorm(
num_filters,
act=act,
param_attr=ParamAttr(name=name + "_bn_scale"),
bias_attr=ParamAttr(name=name + "_bn_offset"),
moving_mean_name=name + "_bn_mean",
moving_variance_name=name + "_bn_variance",
)
def forward(self, inputs):
y = self.conv(inputs)
y = self.batch_norm(y)
return y
class InceptionStem(nn.Layer):
def __init__(self):
super(InceptionStem, self).__init__()
self.conv_1a_3x3 = ConvBNLayer(
num_channels=3,
num_filters=32,
filter_size=3,
stride=2,
act="relu",
name="conv_1a_3x3",
)
self.conv_2a_3x3 = ConvBNLayer(
num_channels=32,
num_filters=32,
filter_size=3,
stride=1,
act="relu",
name="conv_2a_3x3",
)
self.conv_2b_3x3 = ConvBNLayer(
num_channels=32,
num_filters=64,
filter_size=3,
padding=1,
act="relu",
name="conv_2b_3x3",
)
self.maxpool = MaxPool2D(kernel_size=3, stride=2, padding=0)
self.conv_3b_1x1 = ConvBNLayer(
num_channels=64,
num_filters=80,
filter_size=1,
act="relu",
name="conv_3b_1x1",
)
self.conv_4a_3x3 = ConvBNLayer(
num_channels=80,
num_filters=192,
filter_size=3,
act="relu",
name="conv_4a_3x3",
)
def forward(self, x):
y = self.conv_1a_3x3(x)
y = self.conv_2a_3x3(y)
y = self.conv_2b_3x3(y)
y = self.maxpool(y)
y = self.conv_3b_1x1(y)
y = self.conv_4a_3x3(y)
y = self.maxpool(y)
return y
class InceptionA(nn.Layer):
def __init__(self, num_channels, pool_features, name=None):
super(InceptionA, self).__init__()
self.branch1x1 = ConvBNLayer(
num_channels=num_channels,
num_filters=64,
filter_size=1,
act="relu",
name="inception_a_branch1x1_" + name,
)
self.branch5x5_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=48,
filter_size=1,
act="relu",
name="inception_a_branch5x5_1_" + name,
)
self.branch5x5_2 = ConvBNLayer(
num_channels=48,
num_filters=64,
filter_size=5,
padding=2,
act="relu",
name="inception_a_branch5x5_2_" + name,
)
self.branch3x3dbl_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=64,
filter_size=1,
act="relu",
name="inception_a_branch3x3dbl_1_" + name,
)
self.branch3x3dbl_2 = ConvBNLayer(
num_channels=64,
num_filters=96,
filter_size=3,
padding=1,
act="relu",
name="inception_a_branch3x3dbl_2_" + name,
)
self.branch3x3dbl_3 = ConvBNLayer(
num_channels=96,
num_filters=96,
filter_size=3,
padding=1,
act="relu",
name="inception_a_branch3x3dbl_3_" + name,
)
self.branch_pool = AvgPool2D(
kernel_size=3, stride=1, padding=1, exclusive=False
)
self.branch_pool_conv = ConvBNLayer(
num_channels=num_channels,
num_filters=pool_features,
filter_size=1,
act="relu",
name="inception_a_branch_pool_" + name,
)
def forward(self, x):
branch1x1 = self.branch1x1(x)
branch5x5 = self.branch5x5_1(x)
branch5x5 = self.branch5x5_2(branch5x5)
branch3x3dbl = self.branch3x3dbl_1(x)
branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)
branch_pool = self.branch_pool(x)
branch_pool = self.branch_pool_conv(branch_pool)
outputs = paddle.concat(
[branch1x1, branch5x5, branch3x3dbl, branch_pool], axis=1
)
return outputs
class InceptionB(nn.Layer):
def __init__(self, num_channels, name=None):
super(InceptionB, self).__init__()
self.branch3x3 = ConvBNLayer(
num_channels=num_channels,
num_filters=384,
filter_size=3,
stride=2,
act="relu",
name="inception_b_branch3x3_" + name,
)
self.branch3x3dbl_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=64,
filter_size=1,
act="relu",
name="inception_b_branch3x3dbl_1_" + name,
)
self.branch3x3dbl_2 = ConvBNLayer(
num_channels=64,
num_filters=96,
filter_size=3,
padding=1,
act="relu",
name="inception_b_branch3x3dbl_2_" + name,
)
self.branch3x3dbl_3 = ConvBNLayer(
num_channels=96,
num_filters=96,
filter_size=3,
stride=2,
act="relu",
name="inception_b_branch3x3dbl_3_" + name,
)
self.branch_pool = MaxPool2D(kernel_size=3, stride=2)
def forward(self, x):
branch3x3 = self.branch3x3(x)
branch3x3dbl = self.branch3x3dbl_1(x)
branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
branch3x3dbl = self.branch3x3dbl_3(branch3x3dbl)
branch_pool = self.branch_pool(x)
outputs = paddle.concat([branch3x3, branch3x3dbl, branch_pool], axis=1)
return outputs
class InceptionC(nn.Layer):
def __init__(self, num_channels, channels_7x7, name=None):
super(InceptionC, self).__init__()
self.branch1x1 = ConvBNLayer(
num_channels=num_channels,
num_filters=192,
filter_size=1,
act="relu",
name="inception_c_branch1x1_" + name,
)
self.branch7x7_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=channels_7x7,
filter_size=1,
stride=1,
act="relu",
name="inception_c_branch7x7_1_" + name,
)
self.branch7x7_2 = ConvBNLayer(
num_channels=channels_7x7,
num_filters=channels_7x7,
filter_size=(1, 7),
stride=1,
padding=(0, 3),
act="relu",
name="inception_c_branch7x7_2_" + name,
)
self.branch7x7_3 = ConvBNLayer(
num_channels=channels_7x7,
num_filters=192,
filter_size=(7, 1),
stride=1,
padding=(3, 0),
act="relu",
name="inception_c_branch7x7_3_" + name,
)
self.branch7x7dbl_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=channels_7x7,
filter_size=1,
act="relu",
name="inception_c_branch7x7dbl_1_" + name,
)
self.branch7x7dbl_2 = ConvBNLayer(
num_channels=channels_7x7,
num_filters=channels_7x7,
filter_size=(7, 1),
padding=(3, 0),
act="relu",
name="inception_c_branch7x7dbl_2_" + name,
)
self.branch7x7dbl_3 = ConvBNLayer(
num_channels=channels_7x7,
num_filters=channels_7x7,
filter_size=(1, 7),
padding=(0, 3),
act="relu",
name="inception_c_branch7x7dbl_3_" + name,
)
self.branch7x7dbl_4 = ConvBNLayer(
num_channels=channels_7x7,
num_filters=channels_7x7,
filter_size=(7, 1),
padding=(3, 0),
act="relu",
name="inception_c_branch7x7dbl_4_" + name,
)
self.branch7x7dbl_5 = ConvBNLayer(
num_channels=channels_7x7,
num_filters=192,
filter_size=(1, 7),
padding=(0, 3),
act="relu",
name="inception_c_branch7x7dbl_5_" + name,
)
self.branch_pool = AvgPool2D(
kernel_size=3, stride=1, padding=1, exclusive=False
)
self.branch_pool_conv = ConvBNLayer(
num_channels=num_channels,
num_filters=192,
filter_size=1,
act="relu",
name="inception_c_branch_pool_" + name,
)
def forward(self, x):
branch1x1 = self.branch1x1(x)
branch7x7 = self.branch7x7_1(x)
branch7x7 = self.branch7x7_2(branch7x7)
branch7x7 = self.branch7x7_3(branch7x7)
branch7x7dbl = self.branch7x7dbl_1(x)
branch7x7dbl = self.branch7x7dbl_2(branch7x7dbl)
branch7x7dbl = self.branch7x7dbl_3(branch7x7dbl)
branch7x7dbl = self.branch7x7dbl_4(branch7x7dbl)
branch7x7dbl = self.branch7x7dbl_5(branch7x7dbl)
branch_pool = self.branch_pool(x)
branch_pool = self.branch_pool_conv(branch_pool)
outputs = paddle.concat(
[branch1x1, branch7x7, branch7x7dbl, branch_pool], axis=1
)
return outputs
class InceptionD(nn.Layer):
def __init__(self, num_channels, name=None):
super(InceptionD, self).__init__()
self.branch3x3_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=192,
filter_size=1,
act="relu",
name="inception_d_branch3x3_1_" + name,
)
self.branch3x3_2 = ConvBNLayer(
num_channels=192,
num_filters=320,
filter_size=3,
stride=2,
act="relu",
name="inception_d_branch3x3_2_" + name,
)
self.branch7x7x3_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=192,
filter_size=1,
act="relu",
name="inception_d_branch7x7x3_1_" + name,
)
self.branch7x7x3_2 = ConvBNLayer(
num_channels=192,
num_filters=192,
filter_size=(1, 7),
padding=(0, 3),
act="relu",
name="inception_d_branch7x7x3_2_" + name,
)
self.branch7x7x3_3 = ConvBNLayer(
num_channels=192,
num_filters=192,
filter_size=(7, 1),
padding=(3, 0),
act="relu",
name="inception_d_branch7x7x3_3_" + name,
)
self.branch7x7x3_4 = ConvBNLayer(
num_channels=192,
num_filters=192,
filter_size=3,
stride=2,
act="relu",
name="inception_d_branch7x7x3_4_" + name,
)
self.branch_pool = MaxPool2D(kernel_size=3, stride=2)
def forward(self, x):
branch3x3 = self.branch3x3_1(x)
branch3x3 = self.branch3x3_2(branch3x3)
branch7x7x3 = self.branch7x7x3_1(x)
branch7x7x3 = self.branch7x7x3_2(branch7x7x3)
branch7x7x3 = self.branch7x7x3_3(branch7x7x3)
branch7x7x3 = self.branch7x7x3_4(branch7x7x3)
branch_pool = self.branch_pool(x)
outputs = paddle.concat([branch3x3, branch7x7x3, branch_pool], axis=1)
return outputs
class InceptionE(nn.Layer):
def __init__(self, num_channels, name=None):
super(InceptionE, self).__init__()
self.branch1x1 = ConvBNLayer(
num_channels=num_channels,
num_filters=320,
filter_size=1,
act="relu",
name="inception_e_branch1x1_" + name,
)
self.branch3x3_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=384,
filter_size=1,
act="relu",
name="inception_e_branch3x3_1_" + name,
)
self.branch3x3_2a = ConvBNLayer(
num_channels=384,
num_filters=384,
filter_size=(1, 3),
padding=(0, 1),
act="relu",
name="inception_e_branch3x3_2a_" + name,
)
self.branch3x3_2b = ConvBNLayer(
num_channels=384,
num_filters=384,
filter_size=(3, 1),
padding=(1, 0),
act="relu",
name="inception_e_branch3x3_2b_" + name,
)
self.branch3x3dbl_1 = ConvBNLayer(
num_channels=num_channels,
num_filters=448,
filter_size=1,
act="relu",
name="inception_e_branch3x3dbl_1_" + name,
)
self.branch3x3dbl_2 = ConvBNLayer(
num_channels=448,
num_filters=384,
filter_size=3,
padding=1,
act="relu",
name="inception_e_branch3x3dbl_2_" + name,
)
self.branch3x3dbl_3a = ConvBNLayer(
num_channels=384,
num_filters=384,
filter_size=(1, 3),
padding=(0, 1),
act="relu",
name="inception_e_branch3x3dbl_3a_" + name,
)
self.branch3x3dbl_3b = ConvBNLayer(
num_channels=384,
num_filters=384,
filter_size=(3, 1),
padding=(1, 0),
act="relu",
name="inception_e_branch3x3dbl_3b_" + name,
)
self.branch_pool = AvgPool2D(
kernel_size=3, stride=1, padding=1, exclusive=False
)
self.branch_pool_conv = ConvBNLayer(
num_channels=num_channels,
num_filters=192,
filter_size=1,
act="relu",
name="inception_e_branch_pool_" + name,
)
def forward(self, x):
branch1x1 = self.branch1x1(x)
branch3x3 = self.branch3x3_1(x)
branch3x3 = [
self.branch3x3_2a(branch3x3),
self.branch3x3_2b(branch3x3),
]
branch3x3 = paddle.concat(branch3x3, axis=1)
branch3x3dbl = self.branch3x3dbl_1(x)
branch3x3dbl = self.branch3x3dbl_2(branch3x3dbl)
branch3x3dbl = [
self.branch3x3dbl_3a(branch3x3dbl),
self.branch3x3dbl_3b(branch3x3dbl),
]
branch3x3dbl = paddle.concat(branch3x3dbl, axis=1)
branch_pool = self.branch_pool(x)
branch_pool = self.branch_pool_conv(branch_pool)
outputs = paddle.concat(
[branch1x1, branch3x3, branch3x3dbl, branch_pool], axis=1
)
return outputs
class InceptionV3(nn.Layer):
def __init__(self, class_dim=1000):
super(InceptionV3, self).__init__()
self.inception_a_list = [[192, 256, 288], [32, 64, 64]]
self.inception_c_list = [[768, 768, 768, 768], [128, 160, 160, 192]]
self.inception_stem = InceptionStem()
self.inception_block_list = []
for i in range(len(self.inception_a_list[0])):
inception_a = self.add_sublayer(
"inception_a_" + str(i + 1),
InceptionA(
self.inception_a_list[0][i],
self.inception_a_list[1][i],
name=str(i + 1),
),
)
self.inception_block_list.append(inception_a)
inception_b = self.add_sublayer("nception_b_1", InceptionB(288, name="1"))
self.inception_block_list.append(inception_b)
for i in range(len(self.inception_c_list[0])):
inception_c = self.add_sublayer(
"inception_c_" + str(i + 1),
InceptionC(
self.inception_c_list[0][i],
self.inception_c_list[1][i],
name=str(i + 1),
),
)
self.inception_block_list.append(inception_c)
inception_d = self.add_sublayer("inception_d_1", InceptionD(768, name="1"))
self.inception_block_list.append(inception_d)
inception_e = self.add_sublayer("inception_e_1", InceptionE(1280, name="1"))
self.inception_block_list.append(inception_e)
inception_e = self.add_sublayer("inception_e_2", InceptionE(2048, name="2"))
self.inception_block_list.append(inception_e)
self.gap = AdaptiveAvgPool2D(1)
self.drop = Dropout(p=0.2, mode="downscale_in_infer")
stdv = 1.0 / math.sqrt(2048 * 1.0)
self.out = Linear(
2048,
class_dim,
weight_attr=ParamAttr(initializer=Uniform(-stdv, stdv), name="fc_weights"),
bias_attr=ParamAttr(name="fc_offset"),
)
def forward(self, x):
y = self.inception_stem(x)
for inception_block in self.inception_block_list:
y = inception_block(y)
y = self.gap(y)
y = paddle.reshape(y, shape=[-1, 2048])
y = self.drop(y)
y = self.out(y)
return y
def test_InceptionV3():
load_paddle_module_and_check(
InceptionV3, input_size=(1, 3, 224, 224), train_flag=False, flow_weight_dir="/tmp/oneflow", oneflow_code_gen_flag=True
)
"""
Copyright 2020 The OneFlow 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
from paddle import ParamAttr
import paddle.nn as nn
import paddle.nn.functional as F
from paddle.nn import Conv2D, BatchNorm, Linear, Dropout
from paddle.nn import AdaptiveAvgPool2D, MaxPool2D, AvgPool2D
from paddle.nn.initializer import Uniform
import math
__all__ = ["InceptionV4"]
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
class ConvBNLayer(nn.Layer):
def __init__(
self,
num_channels,
num_filters,
filter_size,
stride=1,
padding=0,
groups=1,
act="relu",
name=None,
):
super(ConvBNLayer, self).__init__()
self._conv = Conv2D(
in_channels=num_channels,
out_channels=num_filters,
kernel_size=filter_size,
stride=stride,
padding=padding,
groups=groups,
weight_attr=ParamAttr(name=name + "_weights"),
bias_attr=False,
)
bn_name = name + "_bn"
self._batch_norm = BatchNorm(
num_filters,
act=act,
param_attr=ParamAttr(name=bn_name + "_scale"),
bias_attr=ParamAttr(name=bn_name + "_offset"),
moving_mean_name=bn_name + "_mean",
moving_variance_name=bn_name + "_variance",
)
def forward(self, inputs):
y = self._conv(inputs)
y = self._batch_norm(y)
return y
class InceptionStem(nn.Layer):
def __init__(self):
super(InceptionStem, self).__init__()
self._conv_1 = ConvBNLayer(3, 32, 3, stride=2, act="relu", name="conv1_3x3_s2")
self._conv_2 = ConvBNLayer(32, 32, 3, act="relu", name="conv2_3x3_s1")
self._conv_3 = ConvBNLayer(
32, 64, 3, padding=1, act="relu", name="conv3_3x3_s1"
)
self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
self._conv2 = ConvBNLayer(
64, 96, 3, stride=2, act="relu", name="inception_stem1_3x3_s2"
)
self._conv1_1 = ConvBNLayer(
160, 64, 1, act="relu", name="inception_stem2_3x3_reduce"
)
self._conv1_2 = ConvBNLayer(64, 96, 3, act="relu", name="inception_stem2_3x3")
self._conv2_1 = ConvBNLayer(
160, 64, 1, act="relu", name="inception_stem2_1x7_reduce"
)
self._conv2_2 = ConvBNLayer(
64, 64, (7, 1), padding=(3, 0), act="relu", name="inception_stem2_1x7"
)
self._conv2_3 = ConvBNLayer(
64, 64, (1, 7), padding=(0, 3), act="relu", name="inception_stem2_7x1"
)
self._conv2_4 = ConvBNLayer(64, 96, 3, act="relu", name="inception_stem2_3x3_2")
self._conv3 = ConvBNLayer(
192, 192, 3, stride=2, act="relu", name="inception_stem3_3x3_s2"
)
def forward(self, inputs):
conv = self._conv_1(inputs)
conv = self._conv_2(conv)
conv = self._conv_3(conv)
pool1 = self._pool(conv)
conv2 = self._conv2(conv)
concat = paddle.concat([pool1, conv2], axis=1)
conv1 = self._conv1_1(concat)
conv1 = self._conv1_2(conv1)
conv2 = self._conv2_1(concat)
conv2 = self._conv2_2(conv2)
conv2 = self._conv2_3(conv2)
conv2 = self._conv2_4(conv2)
concat = paddle.concat([conv1, conv2], axis=1)
conv1 = self._conv3(concat)
pool1 = self._pool(concat)
concat = paddle.concat([conv1, pool1], axis=1)
return concat
class InceptionA(nn.Layer):
def __init__(self, name):
super(InceptionA, self).__init__()
self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
self._conv1 = ConvBNLayer(
384, 96, 1, act="relu", name="inception_a" + name + "_1x1"
)
self._conv2 = ConvBNLayer(
384, 96, 1, act="relu", name="inception_a" + name + "_1x1_2"
)
self._conv3_1 = ConvBNLayer(
384, 64, 1, act="relu", name="inception_a" + name + "_3x3_reduce"
)
self._conv3_2 = ConvBNLayer(
64, 96, 3, padding=1, act="relu", name="inception_a" + name + "_3x3"
)
self._conv4_1 = ConvBNLayer(
384, 64, 1, act="relu", name="inception_a" + name + "_3x3_2_reduce"
)
self._conv4_2 = ConvBNLayer(
64, 96, 3, padding=1, act="relu", name="inception_a" + name + "_3x3_2"
)
self._conv4_3 = ConvBNLayer(
96, 96, 3, padding=1, act="relu", name="inception_a" + name + "_3x3_3"
)
def forward(self, inputs):
pool1 = self._pool(inputs)
conv1 = self._conv1(pool1)
conv2 = self._conv2(inputs)
conv3 = self._conv3_1(inputs)
conv3 = self._conv3_2(conv3)
conv4 = self._conv4_1(inputs)
conv4 = self._conv4_2(conv4)
conv4 = self._conv4_3(conv4)
concat = paddle.concat([conv1, conv2, conv3, conv4], axis=1)
return concat
class ReductionA(nn.Layer):
def __init__(self):
super(ReductionA, self).__init__()
self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
self._conv2 = ConvBNLayer(
384, 384, 3, stride=2, act="relu", name="reduction_a_3x3"
)
self._conv3_1 = ConvBNLayer(
384, 192, 1, act="relu", name="reduction_a_3x3_2_reduce"
)
self._conv3_2 = ConvBNLayer(
192, 224, 3, padding=1, act="relu", name="reduction_a_3x3_2"
)
self._conv3_3 = ConvBNLayer(
224, 256, 3, stride=2, act="relu", name="reduction_a_3x3_3"
)
def forward(self, inputs):
pool1 = self._pool(inputs)
conv2 = self._conv2(inputs)
conv3 = self._conv3_1(inputs)
conv3 = self._conv3_2(conv3)
conv3 = self._conv3_3(conv3)
concat = paddle.concat([pool1, conv2, conv3], axis=1)
return concat
class InceptionB(nn.Layer):
def __init__(self, name=None):
super(InceptionB, self).__init__()
self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
self._conv1 = ConvBNLayer(
1024, 128, 1, act="relu", name="inception_b" + name + "_1x1"
)
self._conv2 = ConvBNLayer(
1024, 384, 1, act="relu", name="inception_b" + name + "_1x1_2"
)
self._conv3_1 = ConvBNLayer(
1024, 192, 1, act="relu", name="inception_b" + name + "_1x7_reduce"
)
self._conv3_2 = ConvBNLayer(
192,
224,
(1, 7),
padding=(0, 3),
act="relu",
name="inception_b" + name + "_1x7",
)
self._conv3_3 = ConvBNLayer(
224,
256,
(7, 1),
padding=(3, 0),
act="relu",
name="inception_b" + name + "_7x1",
)
self._conv4_1 = ConvBNLayer(
1024, 192, 1, act="relu", name="inception_b" + name + "_7x1_2_reduce"
)
self._conv4_2 = ConvBNLayer(
192,
192,
(1, 7),
padding=(0, 3),
act="relu",
name="inception_b" + name + "_1x7_2",
)
self._conv4_3 = ConvBNLayer(
192,
224,
(7, 1),
padding=(3, 0),
act="relu",
name="inception_b" + name + "_7x1_2",
)
self._conv4_4 = ConvBNLayer(
224,
224,
(1, 7),
padding=(0, 3),
act="relu",
name="inception_b" + name + "_1x7_3",
)
self._conv4_5 = ConvBNLayer(
224,
256,
(7, 1),
padding=(3, 0),
act="relu",
name="inception_b" + name + "_7x1_3",
)
def forward(self, inputs):
pool1 = self._pool(inputs)
conv1 = self._conv1(pool1)
conv2 = self._conv2(inputs)
conv3 = self._conv3_1(inputs)
conv3 = self._conv3_2(conv3)
conv3 = self._conv3_3(conv3)
conv4 = self._conv4_1(inputs)
conv4 = self._conv4_2(conv4)
conv4 = self._conv4_3(conv4)
conv4 = self._conv4_4(conv4)
conv4 = self._conv4_5(conv4)
concat = paddle.concat([conv1, conv2, conv3, conv4], axis=1)
return concat
class ReductionB(nn.Layer):
def __init__(self):
super(ReductionB, self).__init__()
self._pool = MaxPool2D(kernel_size=3, stride=2, padding=0)
self._conv2_1 = ConvBNLayer(
1024, 192, 1, act="relu", name="reduction_b_3x3_reduce"
)
self._conv2_2 = ConvBNLayer(
192, 192, 3, stride=2, act="relu", name="reduction_b_3x3"
)
self._conv3_1 = ConvBNLayer(
1024, 256, 1, act="relu", name="reduction_b_1x7_reduce"
)
self._conv3_2 = ConvBNLayer(
256, 256, (1, 7), padding=(0, 3), act="relu", name="reduction_b_1x7"
)
self._conv3_3 = ConvBNLayer(
256, 320, (7, 1), padding=(3, 0), act="relu", name="reduction_b_7x1"
)
self._conv3_4 = ConvBNLayer(
320, 320, 3, stride=2, act="relu", name="reduction_b_3x3_2"
)
def forward(self, inputs):
pool1 = self._pool(inputs)
conv2 = self._conv2_1(inputs)
conv2 = self._conv2_2(conv2)
conv3 = self._conv3_1(inputs)
conv3 = self._conv3_2(conv3)
conv3 = self._conv3_3(conv3)
conv3 = self._conv3_4(conv3)
concat = paddle.concat([pool1, conv2, conv3], axis=1)
return concat
class InceptionC(nn.Layer):
def __init__(self, name=None):
super(InceptionC, self).__init__()
self._pool = AvgPool2D(kernel_size=3, stride=1, padding=1)
self._conv1 = ConvBNLayer(
1536, 256, 1, act="relu", name="inception_c" + name + "_1x1"
)
self._conv2 = ConvBNLayer(
1536, 256, 1, act="relu", name="inception_c" + name + "_1x1_2"
)
self._conv3_0 = ConvBNLayer(
1536, 384, 1, act="relu", name="inception_c" + name + "_1x1_3"
)
self._conv3_1 = ConvBNLayer(
384,
256,
(1, 3),
padding=(0, 1),
act="relu",
name="inception_c" + name + "_1x3",
)
self._conv3_2 = ConvBNLayer(
384,
256,
(3, 1),
padding=(1, 0),
act="relu",
name="inception_c" + name + "_3x1",
)
self._conv4_0 = ConvBNLayer(
1536, 384, 1, act="relu", name="inception_c" + name + "_1x1_4"
)
self._conv4_00 = ConvBNLayer(
384,
448,
(1, 3),
padding=(0, 1),
act="relu",
name="inception_c" + name + "_1x3_2",
)
self._conv4_000 = ConvBNLayer(
448,
512,
(3, 1),
padding=(1, 0),
act="relu",
name="inception_c" + name + "_3x1_2",
)
self._conv4_1 = ConvBNLayer(
512,
256,
(1, 3),
padding=(0, 1),
act="relu",
name="inception_c" + name + "_1x3_3",
)
self._conv4_2 = ConvBNLayer(
512,
256,
(3, 1),
padding=(1, 0),
act="relu",
name="inception_c" + name + "_3x1_3",
)
def forward(self, inputs):
pool1 = self._pool(inputs)
conv1 = self._conv1(pool1)
conv2 = self._conv2(inputs)
conv3 = self._conv3_0(inputs)
conv3_1 = self._conv3_1(conv3)
conv3_2 = self._conv3_2(conv3)
conv4 = self._conv4_0(inputs)
conv4 = self._conv4_00(conv4)
conv4 = self._conv4_000(conv4)
conv4_1 = self._conv4_1(conv4)
conv4_2 = self._conv4_2(conv4)
concat = paddle.concat(
[conv1, conv2, conv3_1, conv3_2, conv4_1, conv4_2], axis=1
)
return concat
class InceptionV4DY(nn.Layer):
def __init__(self, class_dim=1000):
super(InceptionV4DY, self).__init__()
self._inception_stem = InceptionStem()
self._inceptionA_1 = InceptionA(name="1")
self._inceptionA_2 = InceptionA(name="2")
self._inceptionA_3 = InceptionA(name="3")
self._inceptionA_4 = InceptionA(name="4")
self._reductionA = ReductionA()
self._inceptionB_1 = InceptionB(name="1")
self._inceptionB_2 = InceptionB(name="2")
self._inceptionB_3 = InceptionB(name="3")
self._inceptionB_4 = InceptionB(name="4")
self._inceptionB_5 = InceptionB(name="5")
self._inceptionB_6 = InceptionB(name="6")
self._inceptionB_7 = InceptionB(name="7")
self._reductionB = ReductionB()
self._inceptionC_1 = InceptionC(name="1")
self._inceptionC_2 = InceptionC(name="2")
self._inceptionC_3 = InceptionC(name="3")
self.avg_pool = AdaptiveAvgPool2D(1)
self._drop = Dropout(p=0.2, mode="downscale_in_infer")
stdv = 1.0 / math.sqrt(1536 * 1.0)
self.out = Linear(
1536,
class_dim,
weight_attr=ParamAttr(
initializer=Uniform(-stdv, stdv), name="final_fc_weights"
),
bias_attr=ParamAttr(name="final_fc_offset"),
)
def forward(self, inputs):
x = self._inception_stem(inputs)
x = self._inceptionA_1(x)
x = self._inceptionA_2(x)
x = self._inceptionA_3(x)
x = self._inceptionA_4(x)
x = self._reductionA(x)
x = self._inceptionB_1(x)
x = self._inceptionB_2(x)
x = self._inceptionB_3(x)
x = self._inceptionB_4(x)
x = self._inceptionB_5(x)
x = self._inceptionB_6(x)
x = self._inceptionB_7(x)
x = self._reductionB(x)
x = self._inceptionC_1(x)
x = self._inceptionC_2(x)
x = self._inceptionC_3(x)
x = self.avg_pool(x)
x = paddle.squeeze(x, axis=[2, 3])
x = self._drop(x)
x = self.out(x)
return x
def InceptionV4(**args):
model = InceptionV4DY(**args)
return model
def test_InceptionV4():
load_paddle_module_and_check(
InceptionV4, input_size=(1, 3, 224, 224), train_flag=False, flow_weight_dir="/tmp/oneflow", oneflow_code_gen_flag=True
)
"""
Copyright 2020 The OneFlow 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
import paddle.nn as nn
import paddle.nn.functional as F
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
def test_relu():
class Net(nn.Layer):
def forward(self, x):
x = F.relu(x)
return x
load_paddle_module_and_check(Net)
"""
Copyright 2020 The OneFlow 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
import paddle.nn as nn
import paddle.nn.functional as F
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
def test_concat():
class Net(nn.Layer):
def forward(self, x):
y = x * 3
return paddle.concat((x, y))
load_paddle_module_and_check(Net)
def test_concat_with_axis():
class Net(nn.Layer):
def forward(self, x):
y = x * 3
return paddle.concat((x, y), axis=1)
load_paddle_module_and_check(Net)
def test_unsqueeze():
class Net(nn.Layer):
def forward(self, x):
return paddle.unsqueeze(x, 2)
load_paddle_module_and_check(Net)
def test_transpose():
class Net(nn.Layer):
def forward(self, x):
# shape = x.shape
return paddle.transpose(x, perm=(0, 3, 1, 2))
load_paddle_module_and_check(Net)
def test_gather():
class Net(nn.Layer):
def forward(self, x):
return x[1]
load_paddle_module_and_check(Net)
def test_tensor_index():
class Net(nn.Layer):
def forward(self, x):
return x[0, 1:3, :1, 2:4]
load_paddle_module_and_check(Net)
def test_split():
class Net(nn.Layer):
def forward(self, x):
x1, y1 = paddle.split(x, num_or_sections=[3, 3], axis=1)
return x1
load_paddle_module_and_check(Net, input_size=(1, 6, 3, 3))
"""
Copyright 2020 The OneFlow 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
import paddle.nn as nn
import paddle.nn.functional as F
from oneflow_onnx.x2oneflow.util import load_paddle_module_and_check
def test_bn():
class Net(nn.Layer):
def __init__(self):
super(Net, self).__init__()
self.bn = nn.BatchNorm2D(4)
def forward(self, x):
x = self.bn(x)
return x
load_paddle_module_and_check(Net)
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
此差异已折叠。
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册