Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
9e2dc93d
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看板
提交
9e2dc93d
编写于
2月 19, 2019
作者:
R
Ray Liu
提交者:
GitHub
2月 19, 2019
浏览文件
操作
浏览文件
下载
差异文件
Merge pull request #1442 from codeWorm2015/develop
fix
#1441
add oc interface dim info and multi-output
上级
02a5b655
5957475c
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
116 addition
and
22 deletion
+116
-22
src/io/ios_io/PaddleMobileCPU.h
src/io/ios_io/PaddleMobileCPU.h
+28
-6
src/io/ios_io/PaddleMobileCPU.mm
src/io/ios_io/PaddleMobileCPU.mm
+88
-16
未找到文件。
src/io/ios_io/PaddleMobileCPU.h
浏览文件 @
9e2dc93d
...
...
@@ -19,10 +19,21 @@
@interface
PaddleMobileCPUResult
:
NSObject
/**
@b 输出指针
*/
@property
(
assign
,
nonatomic
,
readonly
)
float
*
output
;
/**
@b 输出的 float 数
* */
@property
(
assign
,
nonatomic
,
readonly
)
int
outputSize
;
/**
@b 维度信息, longlongValue
*/
@property
(
strong
,
nonatomic
,
readonly
)
NSArray
<
NSNumber
*>
*
dim
;
-
(
void
)
releaseOutput
;
@end
...
...
@@ -92,11 +103,6 @@
andModelParamsLen
:(
size_t
)
combinedParamsLen
andCombinedParamsBuf
:(
const
uint8_t
*
)
combinedParamsBuf
;
/*
*
* */
/**
@b 对图像进行预处理, 需要外部开辟 output 内存, 外部释放 output 内存, 每一个像素经过这样的预处理 (x + means) * scale, 其中 x 为像素值
...
...
@@ -134,7 +140,7 @@
-
(
PaddleMobileCPUResult
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
means
:(
NSArray
<
NSNumber
*>
*
)
means
scale
:(
float
)
scale
;
/**
进行预测, 预处理 means 值为 0, scale 值为 1
@b
进行预测, 预处理 means 值为 0, scale 值为 1
@param image 输入图像
@param dim 输入维度
...
...
@@ -142,6 +148,22 @@
*/
-
(
PaddleMobileCPUResult
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
;
/**
@b 取出模型描述中 key 为 "fetch" 对应的输出
@return 预测结果
*/
-
(
PaddleMobileCPUResult
*
)
fetchOutput
;
/**
@b 当输出为多个时, 可用此函数取出对应的输出
@param key 模型中输出的key
@return 预测结果
*/
-
(
PaddleMobileCPUResult
*
)
fetchOutputWithKey
:(
NSString
*
)
key
;
/**
@b 清理内存
*/
...
...
src/io/ios_io/PaddleMobileCPU.mm
浏览文件 @
9e2dc93d
...
...
@@ -43,6 +43,10 @@
_outputSize
=
outputSize
;
}
-
(
void
)
toSetDim
:(
NSArray
<
NSNumber
*>
*
)
dim
{
_dim
=
dim
;
}
@end
@implementation
PaddleMobileCPUConfig
...
...
@@ -241,17 +245,22 @@ static std::mutex shared_mutex;
}
paddle_mobile
::
framework
::
Tensor
input_tensor
;
paddle_mobile
::
framework
::
DDim
dims
=
paddle_mobile
::
framework
::
make_ddim
(
dim_vec
);
float
*
input_ptr
=
input_tensor
.
mutable_data
<
float
>
(
dims
);
memcpy
(
input_ptr
,
input
,
numel
*
sizeof
(
float
));
pam_
->
Predict
(
input_tensor
);
std
::
shared_ptr
<
paddle_mobile
::
framework
::
Tensor
>
output
=
pam_
->
Fetch
();
auto
output_dims
=
output
->
dims
();
std
::
vector
<
int64_t
>
output_dim_vec
=
vectorize
(
output_dims
);
NSMutableArray
<
NSNumber
*>
*
ocDim
=
[
NSMutableArray
array
];
for
(
int
i
=
0
;
i
<
output_dim_vec
.
size
();
++
i
)
{
NSNumber
*
num
=
[
NSNumber
numberWithLongLong
:
output_dim_vec
[
i
]];
[
ocDim
addObject
:
num
];
}
float
*
output_pointer
=
new
float
[
output
->
numel
()];
memcpy
(
output_pointer
,
output
->
data
<
float
>
(),
...
...
@@ -259,6 +268,7 @@ static std::mutex shared_mutex;
PaddleMobileCPUResult
*
cpuResult
=
[[
PaddleMobileCPUResult
alloc
]
init
];
[
cpuResult
toSetOutput
:
output_pointer
];
[
cpuResult
toSetDim
:
ocDim
];
[
cpuResult
toSetOutputSize
:
output
->
numel
()];
return
cpuResult
;
...
...
@@ -304,23 +314,31 @@ static std::mutex shared_mutex;
return
nil
;
}
// input
std
::
vector
<
float
>
predict_input
;
f
or
(
int
j
=
0
;
j
<
numel
;
++
j
)
{
predict_input
.
push_back
(
dataPointer
[
j
]);
}
paddle_mobile
::
framework
::
Tensor
input_tensor
;
paddle_mobile
::
framework
::
DDim
dims
=
paddle_mobile
::
framework
::
make_ddim
(
dim_vec
)
;
f
loat
*
input_ptr
=
input_tensor
.
mutable_data
<
float
>
(
dims
);
memcpy
(
input_ptr
,
dataPointer
,
numel
*
sizeof
(
float
));
// predict
std
::
vector
<
float
>
cpp_result
=
pam_
->
Predict
(
predict_input
,
dim_vec
);
pam_
->
Predict
(
input_tensor
);
std
::
shared_ptr
<
paddle_mobile
::
framework
::
Tensor
>
output_tensor
=
pam_
->
Fetch
();
auto
output_dims
=
output_tensor
->
dims
();
std
::
vector
<
int64_t
>
output_dim_vec
=
vectorize
(
output_dims
);
NSMutableArray
<
NSNumber
*>
*
ocDim
=
[
NSMutableArray
array
];
for
(
int
i
=
0
;
i
<
output_dim_vec
.
size
();
++
i
)
{
NSNumber
*
num
=
[
NSNumber
numberWithLongLong
:
output_dim_vec
[
i
]];
[
ocDim
addObject
:
num
];
}
float
*
output_pointer
=
new
float
[
cpp_result
.
size
()];
memcpy
(
output_pointer
,
cpp_result
.
data
(),
cpp_result
.
size
()
*
sizeof
(
float
));
float
*
output_pointer
=
new
float
[
output_tensor
->
numel
()];
memcpy
(
output_pointer
,
output_tensor
->
data
<
float
>
(),
output_tensor
->
numel
()
*
sizeof
(
float
));
PaddleMobileCPUResult
*
cpuResult
=
[[
PaddleMobileCPUResult
alloc
]
init
];
[
cpuResult
toSetOutput
:
output_pointer
];
[
cpuResult
toSetOutputSize
:
cpp_result
.
size
()];
[
cpuResult
toSetDim
:
ocDim
];
[
cpuResult
toSetOutputSize
:
output_tensor
->
numel
()];
free
(
output
);
CFRelease
(
cfData
);
cfData
=
NULL
;
...
...
@@ -331,8 +349,62 @@ static std::mutex shared_mutex;
return
[
self
predict
:
image
dim
:
dim
means
:
nil
scale
:
1
];
}
-
(
PaddleMobileCPUResult
*
)
fetchOutput
{
if
(
pam_
&&
loaded_
)
{
auto
tensorPtr
=
pam_
->
Fetch
();
float
*
output_pointer
=
new
float
[
tensorPtr
->
numel
()];
memcpy
(
output_pointer
,
tensorPtr
->
data
<
float
>
(),
tensorPtr
->
numel
()
*
sizeof
(
float
));
auto
dims
=
tensorPtr
->
dims
();
std
::
vector
<
int64_t
>
dim_vec
=
vectorize
(
dims
);
NSMutableArray
<
NSNumber
*>
*
ocDim
=
[
NSMutableArray
array
];
for
(
int
i
=
0
;
i
<
dim_vec
.
size
();
++
i
)
{
NSNumber
*
num
=
[
NSNumber
numberWithLongLong
:
dim_vec
[
i
]];
[
ocDim
addObject
:
num
];
}
PaddleMobileCPUResult
*
cpuResult
=
[[
PaddleMobileCPUResult
alloc
]
init
];
[
cpuResult
toSetOutput
:
output_pointer
];
[
cpuResult
toSetDim
:
ocDim
];
[
cpuResult
toSetOutputSize
:
tensorPtr
->
numel
()];
return
cpuResult
;
}
return
nil
;
}
-
(
PaddleMobileCPUResult
*
)
fetchOutputWithKey
:(
NSString
*
)
key
{
if
(
pam_
&&
loaded_
&&
key
.
length
)
{
auto
tensorPtr
=
pam_
->
Fetch
(
std
::
string
([
key
cStringUsingEncoding
:
NSUTF8StringEncoding
]));
float
*
output_pointer
=
new
float
[
tensorPtr
->
numel
()];
memcpy
(
output_pointer
,
tensorPtr
->
data
<
float
>
(),
tensorPtr
->
numel
()
*
sizeof
(
float
));
auto
dims
=
tensorPtr
->
dims
();
std
::
vector
<
int64_t
>
dim_vec
=
vectorize
(
dims
);
NSMutableArray
<
NSNumber
*>
*
ocDim
=
[
NSMutableArray
array
];
for
(
int
i
=
0
;
i
<
dim_vec
.
size
();
++
i
)
{
NSNumber
*
num
=
[
NSNumber
numberWithLongLong
:
dim_vec
[
i
]];
[
ocDim
addObject
:
num
];
}
PaddleMobileCPUResult
*
cpuResult
=
[[
PaddleMobileCPUResult
alloc
]
init
];
[
cpuResult
toSetOutput
:
output_pointer
];
[
cpuResult
toSetDim
:
ocDim
];
[
cpuResult
toSetOutputSize
:
tensorPtr
->
numel
()];
return
cpuResult
;
}
return
nil
;
}
-
(
void
)
clear
{
pam_
->
Clear
();
if
(
pam_
)
{
pam_
->
Clear
();
}
}
@end
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录