Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
372759f7
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
331
Star
4
Fork
1
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
271
列表
看板
标记
里程碑
合并请求
78
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
Paddle-Lite
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
271
Issue
271
列表
看板
标记
里程碑
合并请求
78
合并请求
78
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
372759f7
编写于
9月 15, 2018
作者:
R
Ruilong Liu
提交者:
GitHub
9月 15, 2018
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #979 from codeWorm2015/metal
correct pribox
上级
70bc0ed0
13486489
变更
3
显示空白变更内容
内联
并排
Showing
3 changed file
with
237 addition
and
10 deletion
+237
-10
metal/paddle-mobile/paddle-mobile/Operators/Kernels/PriorBoxKernel.swift
...bile/paddle-mobile/Operators/Kernels/PriorBoxKernel.swift
+16
-2
metal/paddle-mobile/paddle-mobile/Operators/Kernels/metal/PriorBoxKernel.metal
...addle-mobile/Operators/Kernels/metal/PriorBoxKernel.metal
+204
-0
metal/paddle-mobile/paddle-mobile/Operators/PriorBoxOp.swift
metal/paddle-mobile/paddle-mobile/Operators/PriorBoxOp.swift
+17
-8
未找到文件。
metal/paddle-mobile/paddle-mobile/Operators/Kernels/PriorBoxKernel.swift
浏览文件 @
372759f7
...
@@ -38,13 +38,27 @@ class PriorBoxKernel<P: PrecisionType>: Kernel, Computable{
...
@@ -38,13 +38,27 @@ class PriorBoxKernel<P: PrecisionType>: Kernel, Computable{
param
.
outputVariances
.
initTexture
(
device
:
device
,
inTranspose
:
[
2
,
0
,
1
,
3
],
computePrecision
:
computePrecision
)
param
.
outputVariances
.
initTexture
(
device
:
device
,
inTranspose
:
[
2
,
0
,
1
,
3
],
computePrecision
:
computePrecision
)
if
computePrecision
==
.
Float32
{
if
computePrecision
==
.
Float32
{
if
param
.
min_max_aspect_ratios_order
{
super
.
init
(
device
:
device
,
inFunctionName
:
"prior_box_MinMaxAspectRatiosOrder"
)
}
else
{
super
.
init
(
device
:
device
,
inFunctionName
:
"prior_box"
)
super
.
init
(
device
:
device
,
inFunctionName
:
"prior_box"
)
}
}
else
if
computePrecision
==
.
Float16
{
}
else
if
computePrecision
==
.
Float16
{
if
param
.
min_max_aspect_ratios_order
{
super
.
init
(
device
:
device
,
inFunctionName
:
"prior_box_MinMaxAspectRatiosOrder_half"
)
}
else
{
super
.
init
(
device
:
device
,
inFunctionName
:
"prior_box_half"
)
super
.
init
(
device
:
device
,
inFunctionName
:
"prior_box_half"
)
}
}
else
{
}
else
{
fatalError
()
fatalError
()
}
}
guard
param
.
minSizes
.
count
==
1
else
{
fatalError
(
" need implement "
)
}
let
n
=
1
let
n
=
1
let
h
=
param
.
output
.
dim
[
1
]
let
h
=
param
.
output
.
dim
[
1
]
let
w
=
param
.
output
.
dim
[
2
]
let
w
=
param
.
output
.
dim
[
2
]
...
...
metal/paddle-mobile/paddle-mobile/Operators/Kernels/metal/PriorBoxKernel.metal
浏览文件 @
372759f7
...
@@ -161,3 +161,207 @@ kernel void prior_box_half(texture2d_array<half, access::read> inTexture [[textu
...
@@ -161,3 +161,207 @@ kernel void prior_box_half(texture2d_array<half, access::read> inTexture [[textu
}
}
}
}
kernel void prior_box_MinMaxAspectRatiosOrder(texture2d_array<float, access::read> inTexture [[texture(0)]],
texture2d_array<float, access::write> outBoxTexture [[texture(1)]],
texture2d_array<float, access::write> varianceTexture [[texture(2)]],
const device float *aspect_ratios [[buffer(0)]],
constant PriorBoxMetalParam ¶m [[buffer(1)]],
const device float4 *variances [[buffer(2)]],
uint3 gid [[thread_position_in_grid]]) {
if (gid.x >= outBoxTexture.get_width() ||
gid.y >= outBoxTexture.get_height() ||
gid.z >= outBoxTexture.get_array_size()) return;
float center_x = (gid.x + param.offset) * param.stepWidth;
float center_y = (gid.y + param.offset) * param.stepHeight;
float box_width, box_height;
if (gid.z == 0) {
box_width = box_height = param.minSize / 2;
float4 box;
box.x = (center_x - box_width) / param.imageWidth;
box.y = (center_y - box_height) / param.imageHeight;
box.z = (center_x + box_width) / param.imageWidth;
box.w = (center_y + box_height) / param.imageHeight;
float4 res;
if (param.clip) {
res = fmin(fmax(box, 0.0), 1.0);
} else {
res = box;
}
outBoxTexture.write(res, gid.xy, gid.z);
}
if (gid.z == 1 && param.maxSizeSize > 0) {
box_width = box_height = sqrt(param.minSize * param.maxSize) / 2;
float4 max_box;
max_box.x = (center_x - box_width) / param.imageWidth;
max_box.y = (center_y - box_height) / param.imageHeight;
max_box.z = (center_x + box_width) / param.imageWidth;
max_box.w = (center_y + box_height) / param.imageHeight;
float4 res;
if (param.clip) {
res = min(max(max_box, 0.0), 1.0);
} else {
res = max_box;
}
outBoxTexture.write(res, gid.xy, gid.z);
}
int aspect_to = 0;
if (param.maxSizeSize > 0) {
aspect_to = gid.z - 2;
} else {
aspect_to = gid.z - 1;
}
if (aspect_to >= 0 && aspect_to < int(param.aspecRatiosSize)) {
int skip = 0;
for (int i = 0; i < aspect_to + 1; ++i) {
if (fabs(aspect_ratios[i] - 1.) < 1e-6) {
skip += 1;
}
}
aspect_to += skip;
float ar = aspect_ratios[aspect_to];
box_width = param.minSize * sqrt(ar) / 2;
box_height = param.minSize / sqrt(ar) / 2;
float4 box;
box.x = (center_x - box_width) / param.imageWidth;
box.y = (center_y - box_height) / param.imageHeight;
box.z = (center_x + box_width) / param.imageWidth;
box.w = (center_y + box_height) / param.imageHeight;
float4 res;
if (param.clip) {
res = fmin(fmax(box, 0.0), 1.0);
} else {
res = box;
}
outBoxTexture.write(res, gid.xy, gid.z);
}
float4 variance = variances[0];
if (gid.z < param.numPriors) {
float4 variances_output;
variances_output.x = variance.x;
variances_output.y = variance.y;
variances_output.z = variance.z;
variances_output.w = variance.w;
varianceTexture.write(variances_output, gid.xy, gid.z);
}
}
kernel void prior_box_MinMaxAspectRatiosOrder_half(texture2d_array<half, access::read> inTexture [[texture(0)]],
texture2d_array<half, access::write> outBoxTexture [[texture(1)]],
texture2d_array<half, access::write> varianceTexture [[texture(2)]],
const device half *aspect_ratios [[buffer(0)]],
constant PriorBoxMetalParam ¶m [[buffer(1)]],
const device float4 *variances [[buffer(2)]],
uint3 gid [[thread_position_in_grid]]) {
if (gid.x >= outBoxTexture.get_width() ||
gid.y >= outBoxTexture.get_height() ||
gid.z >= outBoxTexture.get_array_size()) return;
float center_x = (gid.x + param.offset) * param.stepWidth;
float center_y = (gid.y + param.offset) * param.stepHeight;
float box_width, box_height;
if (gid.z == 0) {
box_width = box_height = param.minSize / 2;
float4 box;
box.x = (center_x - box_width) / param.imageWidth;
box.y = (center_y - box_height) / param.imageHeight;
box.z = (center_x + box_width) / param.imageWidth;
box.w = (center_y + box_height) / param.imageHeight;
float4 res;
if (param.clip) {
res = fmin(fmax(box, 0.0), 1.0);
} else {
res = box;
}
outBoxTexture.write(half4(res), gid.xy, gid.z);
}
if (gid.z == 1 && param.maxSizeSize > 0) {
box_width = box_height = sqrt(param.minSize * param.maxSize) / 2;
float4 max_box;
max_box.x = (center_x - box_width) / param.imageWidth;
max_box.y = (center_y - box_height) / param.imageHeight;
max_box.z = (center_x + box_width) / param.imageWidth;
max_box.w = (center_y + box_height) / param.imageHeight;
float4 res;
if (param.clip) {
res = min(max(max_box, 0.0), 1.0);
} else {
res = max_box;
}
outBoxTexture.write(half4(res), gid.xy, gid.z);
}
int aspect_to = 0;
if (param.maxSizeSize > 0) {
aspect_to = gid.z - 2;
} else {
aspect_to = gid.z - 1;
}
if (aspect_to > 0 && aspect_to < int(param.aspecRatiosSize) && fabs(aspect_ratios[aspect_to] - 1.) > 1e-6) {
float ar = aspect_ratios[aspect_to];
box_width = param.minSize * sqrt(ar) / 2;
box_height = param.minSize / sqrt(ar) / 2;
float4 box;
box.x = (center_x - box_width) / param.imageWidth;
box.y = (center_y - box_height) / param.imageHeight;
box.z = (center_x + box_width) / param.imageWidth;
box.w = (center_y + box_height) / param.imageHeight;
float4 res;
if (param.clip) {
res = fmin(fmax(box, 0.0), 1.0);
} else {
res = box;
}
outBoxTexture.write(half4(res), gid.xy, gid.z);
}
float4 variance = variances[0];
if (gid.z < param.numPriors) {
float4 variances_output;
variances_output.x = variance.x;
variances_output.y = variance.y;
variances_output.z = variance.z;
variances_output.w = variance.w;
varianceTexture.write(half4(variances_output), gid.xy, gid.z);
}
}
metal/paddle-mobile/paddle-mobile/Operators/PriorBoxOp.swift
浏览文件 @
372759f7
...
@@ -17,6 +17,11 @@ import Foundation
...
@@ -17,6 +17,11 @@ import Foundation
class
PriorBoxParam
<
P
:
PrecisionType
>
:
OpParam
{
class
PriorBoxParam
<
P
:
PrecisionType
>
:
OpParam
{
typealias
ParamPrecisionType
=
P
typealias
ParamPrecisionType
=
P
required
init
(
opDesc
:
OpDesc
,
inScope
:
Scope
)
throws
{
required
init
(
opDesc
:
OpDesc
,
inScope
:
Scope
)
throws
{
do
{
min_max_aspect_ratios_order
=
try
PriorBoxParam
.
getAttr
(
key
:
"min_max_aspect_ratios_order"
,
attrs
:
opDesc
.
attrs
)
}
catch
_
{
}
do
{
do
{
input
=
try
PriorBoxParam
.
input
(
inputs
:
opDesc
.
inputs
,
from
:
inScope
)
input
=
try
PriorBoxParam
.
input
(
inputs
:
opDesc
.
inputs
,
from
:
inScope
)
output
=
try
PriorBoxParam
.
outputBoxes
(
outputs
:
opDesc
.
outputs
,
from
:
inScope
)
output
=
try
PriorBoxParam
.
outputBoxes
(
outputs
:
opDesc
.
outputs
,
from
:
inScope
)
...
@@ -36,6 +41,7 @@ class PriorBoxParam<P: PrecisionType>: OpParam {
...
@@ -36,6 +41,7 @@ class PriorBoxParam<P: PrecisionType>: OpParam {
}
}
}
}
var
min_max_aspect_ratios_order
:
Bool
=
false
let
minSizes
:
[
Float32
]
let
minSizes
:
[
Float32
]
let
maxSizes
:
[
Float32
]
let
maxSizes
:
[
Float32
]
let
aspectRatios
:
[
Float32
]
let
aspectRatios
:
[
Float32
]
...
@@ -74,17 +80,20 @@ class PriorBoxOp<P: PrecisionType>: Operator<PriorBoxKernel<P>, PriorBoxParam<P>
...
@@ -74,17 +80,20 @@ class PriorBoxOp<P: PrecisionType>: Operator<PriorBoxKernel<P>, PriorBoxParam<P>
// output
// output
// let outputArray = para.output.metalTexture.float32Array()
// let outputArray = para.output.metalTexture.float32Array()
// print(outputArray.strideArray())
// print(outputArray.strideArray())
let
device
=
para
.
input
.
metalTexture
!.
device
//
let device = para.input.metalTexture!.device
let
boxes
:[
Float32
]
=
device
.
texture2tensor
(
texture
:
para
.
output
.
metalTexture
!
,
dim
:
para
.
output
.
tensorDim
.
dims
,
transpose
:
[
2
,
0
,
1
,
3
])
//
let boxes:[Float32] = device.texture2tensor(texture: para.output.metalTexture!, dim: para.output.tensorDim.dims, transpose: [2,0,1,3])
let
variances
:[
Float32
]
=
device
.
texture2tensor
(
texture
:
para
.
outputVariances
.
metalTexture
!
,
dim
:
para
.
outputVariances
.
tensorDim
.
dims
,
transpose
:
[
2
,
0
,
1
,
3
])
//
let variances:[Float32] = device.texture2tensor(texture: para.outputVariances.metalTexture!, dim: para.outputVariances.tensorDim.dims, transpose: [2,0,1,3])
print
(
"boxes: "
)
//
print("boxes: ")
print
(
boxes
.
strideArray
())
//
print(boxes.strideArray())
print
(
"variances: "
)
//
print("variances: ")
print
(
variances
.
strideArray
())
//
print(variances.strideArray())
// output
// output
print
(
"
\(
type
)
output: "
)
print
(
"
\(
type
)
output: "
)
print
(
para
.
output
.
metalTexture
.
realNHWC
(
dim
:
(
para
.
output
.
dim
[
0
],
para
.
output
.
dim
[
1
],
para
.
output
.
dim
[
2
],
para
.
output
.
dim
[
3
]))
.
strideArray
())
let
box
=
para
.
output
.
metalTexture
.
realNHWC
(
dim
:
(
para
.
output
.
dim
[
0
],
para
.
output
.
dim
[
1
],
para
.
output
.
dim
[
2
],
para
.
output
.
dim
[
3
]))
print
(
" dim:
\(
para
.
output
.
dim
)
"
)
print
(
box
.
strideArray
())
// print((0..<box.count).map { (index: $0, value: box[$0])})
// print(para.output.realNHWC().strideArray())
// print(para.output.realNHWC().strideArray())
// let padToFourDim = para.output.padToFourDim
// let padToFourDim = para.output.padToFourDim
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录