Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Paddle-Lite
提交
dd4a10a5
P
Paddle-Lite
项目概览
PaddlePaddle
/
Paddle-Lite
通知
337
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看板
提交
dd4a10a5
编写于
12月 22, 2018
作者:
L
liuruilong
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
modify ios interface
上级
c203ecac
变更
8
展开全部
隐藏空白更改
内联
并排
Showing
8 changed file
with
219 addition
and
1083 deletion
+219
-1083
metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj
...-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj
+61
-1003
metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift
...addle-mobile-demo/paddle-mobile-demo/ViewController.swift
+6
-8
metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj
metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj
+6
-6
metal/paddle-mobile/paddle-mobile/Operators/Kernels/metal/FetchKernel.metal
...e/paddle-mobile/Operators/Kernels/metal/FetchKernel.metal
+4
-4
src/io/ios_io/PaddleMobileCPU.h
src/io/ios_io/PaddleMobileCPU.h
+91
-31
src/io/ios_io/PaddleMobileCPU.mm
src/io/ios_io/PaddleMobileCPU.mm
+43
-23
src/io/paddle_mobile.cpp
src/io/paddle_mobile.cpp
+6
-7
src/io/paddle_mobile.h
src/io/paddle_mobile.h
+2
-1
未找到文件。
metal/paddle-mobile-demo/paddle-mobile-demo.xcodeproj/project.pbxproj
浏览文件 @
dd4a10a5
此差异已折叠。
点击以展开。
metal/paddle-mobile-demo/paddle-mobile-demo/ViewController.swift
浏览文件 @
dd4a10a5
...
...
@@ -175,19 +175,17 @@ class ViewController: UIViewController {
override
func
viewDidLoad
()
{
super
.
viewDidLoad
()
// if runner.load() {
// print(" load success ! ")
// } else {
// print(" load error ! ")
// }
//
modelPickerView
.
delegate
=
self
modelPickerView
.
dataSource
=
self
threadPickerView
.
delegate
=
self
threadPickerView
.
dataSource
=
self
if
let
image
=
UIImage
.
init
(
named
:
"test.jpg"
)
{
selectImage
=
image
selectImageView
.
image
=
image
}
else
{
print
(
"请添加测试图片"
)
}
selectImage
=
UIImage
.
init
(
named
:
"hand.jpg"
)
selectImageView
.
image
=
selectImage
// if platform == .CPU {
// inputPointer = runner.preproccess(image: selectImage!.cgImage!)
...
...
metal/paddle-mobile/paddle-mobile.xcodeproj/project.pbxproj
浏览文件 @
dd4a10a5
...
...
@@ -902,8 +902,8 @@
baseConfigurationReference
=
CDF58151D902A1CBAE56A0C2
/* Pods-paddle-mobile.debug.xcconfig */
;
buildSettings
=
{
CLANG_ENABLE_MODULES
=
YES
;
CODE_SIGN_IDENTITY
=
""
;
CODE_SIGN_STYLE
=
Manual
;
CODE_SIGN_IDENTITY
=
"
iPhone Developer
"
;
CODE_SIGN_STYLE
=
Automatic
;
DEFINES_MODULE
=
YES
;
DEVELOPMENT_TEAM
=
""
;
DYLIB_COMPATIBILITY_VERSION
=
1
;
...
...
@@ -922,7 +922,7 @@
"$(inherited)"
,
"$(PROJECT_DIR)/paddle-mobile/CPU"
,
);
MACH_O_TYPE
=
static
lib
;
MACH_O_TYPE
=
mh_dy
lib
;
MTL_LANGUAGE_REVISION
=
UseDeploymentTarget
;
PRODUCT_BUNDLE_IDENTIFIER
=
"orange.paddle-mobile"
;
PRODUCT_NAME
=
"$(TARGET_NAME:c99extidentifier)"
;
...
...
@@ -939,8 +939,8 @@
baseConfigurationReference
=
E2A7957C92EDA5C3BEC0FFC2
/* Pods-paddle-mobile.release.xcconfig */
;
buildSettings
=
{
CLANG_ENABLE_MODULES
=
YES
;
CODE_SIGN_IDENTITY
=
""
;
CODE_SIGN_STYLE
=
Manual
;
CODE_SIGN_IDENTITY
=
"
iPhone Developer
"
;
CODE_SIGN_STYLE
=
Automatic
;
DEFINES_MODULE
=
YES
;
DEVELOPMENT_TEAM
=
""
;
DYLIB_COMPATIBILITY_VERSION
=
1
;
...
...
@@ -959,7 +959,7 @@
"$(inherited)"
,
"$(PROJECT_DIR)/paddle-mobile/CPU"
,
);
MACH_O_TYPE
=
static
lib
;
MACH_O_TYPE
=
mh_dy
lib
;
MTL_LANGUAGE_REVISION
=
UseDeploymentTarget
;
PRODUCT_BUNDLE_IDENTIFIER
=
"orange.paddle-mobile"
;
PRODUCT_NAME
=
"$(TARGET_NAME:c99extidentifier)"
;
...
...
metal/paddle-mobile/paddle-mobile/Operators/Kernels/metal/FetchKernel.metal
浏览文件 @
dd4a10a5
...
...
@@ -31,8 +31,8 @@ kernel void fetch(texture2d_array<float, access::read> inTexture [[texture(0)]],
int output_to = 4 * input_width * input_height;
output[gid.z * output_to + 0 * input_width * input_height + gid.y * input_width + gid.x] = input.x;
output[gid.z * output_to + 1 * input_width * input_height + gid.y * input_width + gid.x] = input.y;
//
output[gid.z * output_to + 2 * input_width * input_height + gid.y * input_width + gid.x] = input.z;
//
output[gid.z * output_to + 3 * input_width * input_height + gid.y * input_width + gid.x] = input.w;
output[gid.z * output_to + 2 * input_width * input_height + gid.y * input_width + gid.x] = input.z;
output[gid.z * output_to + 3 * input_width * input_height + gid.y * input_width + gid.x] = input.w;
}
...
...
@@ -52,8 +52,8 @@ kernel void fetch_half(texture2d_array<half, access::read> inTexture [[texture(0
int output_to = 4 * input_width * input_height;
output[gid.z * output_to + 0 * input_width * input_height + gid.y * input_width + gid.x] = input.x;
output[gid.z * output_to + 1 * input_width * input_height + gid.y * input_width + gid.x] = input.y;
//
output[gid.z * output_to + 2 * input_width * input_height + gid.y * input_width + gid.x] = input.z;
//
output[gid.z * output_to + 3 * input_width * input_height + gid.y * input_width + gid.x] = input.w;
output[gid.z * output_to + 2 * input_width * input_height + gid.y * input_width + gid.x] = input.z;
output[gid.z * output_to + 3 * input_width * input_height + gid.y * input_width + gid.x] = input.w;
}
...
...
src/io/ios_io/PaddleMobileCPU.h
浏览文件 @
dd4a10a5
...
...
@@ -27,59 +27,119 @@
@end
@interface
PaddleMobileCPUConfig
:
NSObject
/**
@b 默认为 1, 多线程时, 建议设置为 2
*/
@property
(
assign
,
nonatomic
)
int
threadNum
;
/**
@b 是否开启运行时 infershape
*/
@property
(
assign
,
nonatomic
)
BOOL
loddable
;
/**
@b 是否开启模型 op 融合优化
*/
@property
(
assign
,
nonatomic
)
BOOL
optimize
;
@end
@interface
PaddleMobileCPU
:
NSObject
/*
创建对象
*/
-
(
instancetype
)
init
;
/**
@b 创建对象
/*
load 模型, 开辟内存
*/
-
(
BOOL
)
load
:(
NSString
*
)
modelPath
andWeightsPath
:(
NSString
*
)
weighsPath
;
@param config 配置
@return paddlemobile CPU 对象
*/
-
(
instancetype
)
initWithConfig
:(
PaddleMobileCPUConfig
*
)
config
;
/*
加载散开形式的模型, 需传入模型的目录
*/
/**
@b 加载模型
@param modelPath 模型路径
@param weighsPath 权重路径
@return 是否加载成功
*/
-
(
BOOL
)
loadModel
:(
NSString
*
)
modelPath
andWeightsPath
:(
NSString
*
)
weighsPath
;
/**
@b 加载散开形式的模型, 需传入模型的目录
@param modelAndWeightPath 模型和权重的路径
@return 是否加载成功
*/
-
(
BOOL
)
load
:(
NSString
*
)
modelAndWeightPath
;
/*
* 从内存中加载模型
* */
/**
@b 从内存中加载模型
@param modelLen 模型大小(字节数)
@param modelBuf 模型在内存中的位置
@param combinedParamsLen 权重大小(字节数)
@param combinedParamsBuf 权重在内存中的位置
@return 是否加载成功
*/
-
(
BOOL
)
LoadCombinedMemory
:(
size_t
)
modelLen
andModelBuf
:(
const
uint8_t
*
)
modelBuf
andModelParamsLen
:(
size_t
)
combinedParamsLen
andCombinedParamsBuf
:(
const
uint8_t
*
)
combinedParamsBuf
;
/*
*
对图像进行预处理, 需要外部开辟 output 内存, 外部释放 output 内存
*
* */
/**
@b 对图像进行预处理, 需要外部开辟 output 内存, 外部释放 output 内存, 每一个像素经过这样的预处理 (x + means) * scale, 其中 x 为像素值
@param image 输入的图像
@param output 预处理后的输出
@param means 预处理中 means
@param scale 预处理中的 scale
@param dim 预处理后的维度
*/
-
(
void
)
preprocess
:(
CGImageRef
)
image
output
:(
float
*
)
output
means
:(
NSArray
<
NSNumber
*>
*
)
means
scale
:(
float
)
scale
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
;
/*
* 预测预处理后的数据, 返回结果使用结束需要调用其 realseOutput 函数进行释放
* */
/**
进行预测
@param input 输入
@param dim 输入维度
@return 输出结果
*/
-
(
PaddleMobileCPUResult
*
)
predictInput
:(
float
*
)
input
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
;
/*
进行预测, means 和 scale 为训练模型时的预处理参数, 如训练时没有做这些预处理则直接使用 predict
*/
-
(
NSArray
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
means
:(
NSArray
<
NSNumber
*>
*
)
means
scale
:(
float
)
scale
;
/*
进行预测, 默认 means 为 0, scale 为 1.0
*/
-
(
NSArray
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
;
/*
清理内存
*/
/**
@b 进行预测, means 和 scale 为训练模型时的预处理参数, 如训练时没有做这些预处理则直接使用 predict, 每一个像素经过这样的预处理 (x + means) * scale, 其中 x 为像素值
@param image 输入图像
@param dim 输入维度
@param means 预处理中 means
@param scale 预处理中 scale
@return 预测结果
*/
-
(
PaddleMobileCPUResult
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
means
:(
NSArray
<
NSNumber
*>
*
)
means
scale
:(
float
)
scale
;
/**
进行预测, 预处理 means 值为 0, scale 值为 1
@param image 输入图像
@param dim 输入维度
@return 预测结果
*/
-
(
PaddleMobileCPUResult
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
;
/**
@b 清理内存
*/
-
(
void
)
clear
;
@end
src/io/ios_io/PaddleMobileCPU.mm
浏览文件 @
dd4a10a5
...
...
@@ -45,21 +45,43 @@
@end
@implementation
PaddleMobileCPUConfig
-
(
instancetype
)
init
{
if
(
self
=
[
super
init
])
{
self
.
threadNum
=
1
;
self
.
optimize
=
YES
;
}
return
self
;
}
@end
@interface
PaddleMobileCPU
()
{
paddle_mobile
::
PaddleMobile
<
paddle_mobile
::
CPU
,
paddle_mobile
::
Precision
::
FP32
>
*
pam_
;
BOOL
loaded_
;
}
@property
(
strong
,
nonatomic
)
PaddleMobileCPUConfig
*
config
;
@end
@implementation
PaddleMobileCPU
static
std
::
mutex
shared_mutex
;
-
(
instancetype
)
init
{
-
(
instancetype
)
init
WithConfig
:(
PaddleMobileCPUConfig
*
)
config
{
if
(
self
=
[
super
init
])
{
pam_
=
new
paddle_mobile
::
PaddleMobile
<
paddle_mobile
::
CPU
,
paddle_mobile
::
Precision
::
FP32
>
();
_config
=
config
;
}
return
self
;
}
-
(
instancetype
)
init
{
if
(
self
=
[
super
init
])
{
_config
=
[[
PaddleMobileCPUConfig
alloc
]
init
];
}
return
self
;
}
...
...
@@ -79,11 +101,11 @@ static std::mutex shared_mutex;
return
sharedManager
;
}
-
(
BOOL
)
load
:(
NSString
*
)
modelPath
andWeightsPath
:(
NSString
*
)
weighsPath
{
-
(
BOOL
)
load
Model
:(
NSString
*
)
modelPath
andWeightsPath
:(
NSString
*
)
weighsPath
{
std
::
string
model_path_str
=
std
::
string
([
modelPath
UTF8String
]);
std
::
string
weights_path_str
=
std
::
string
([
weighsPath
UTF8String
]);
pam_
->
SetThreadNum
(
2
);
if
(
loaded_
=
pam_
->
Load
(
model_path_str
,
weights_path_str
,
tru
e
))
{
pam_
->
SetThreadNum
(
self
.
config
.
threadNum
);
if
(
loaded_
=
pam_
->
Load
(
model_path_str
,
weights_path_str
,
self
.
config
.
optimize
,
false
,
1
,
self
.
config
.
loddabl
e
))
{
return
YES
;
}
else
{
return
NO
;
...
...
@@ -94,14 +116,14 @@ static std::mutex shared_mutex;
andModelBuf
:(
const
uint8_t
*
)
modelBuf
andModelParamsLen
:(
size_t
)
combinedParamsLen
andCombinedParamsBuf
:(
const
uint8_t
*
)
combinedParamsBuf
{
pam_
->
SetThreadNum
(
2
);
pam_
->
SetThreadNum
(
self
.
config
.
threadNum
);
return
loaded_
=
pam_
->
LoadCombinedMemory
(
modelLen
,
modelBuf
,
combinedParamsLen
,
const_cast
<
uint8_t
*>
(
combinedParamsBuf
));
const_cast
<
uint8_t
*>
(
combinedParamsBuf
)
,
self
.
config
.
optimize
,
false
,
1
,
self
.
config
.
loddable
);
}
-
(
BOOL
)
load
:(
NSString
*
)
modelAndWeightPath
{
std
::
string
model_path_str
=
std
::
string
([
modelAndWeightPath
UTF8String
]);
if
(
loaded_
=
pam_
->
Load
(
model_path_str
))
{
if
(
loaded_
=
pam_
->
Load
(
model_path_str
,
self
.
config
.
optimize
,
false
,
1
,
self
.
config
.
loddable
))
{
return
YES
;
}
else
{
return
NO
;
...
...
@@ -116,6 +138,10 @@ static std::mutex shared_mutex;
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
{
std
::
lock_guard
<
std
::
mutex
>
lock
(
shared_mutex
);
if
(
means
==
nil
)
{
means
=
@[
@0
,
@0
,
@0
];
}
// dim to c++ vector, get numel
std
::
vector
<
int64_t
>
dim_vec
;
int
numel
=
1
;
...
...
@@ -234,7 +260,7 @@ static std::mutex shared_mutex;
return
cpuResult
;
}
-
(
NSArray
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
means
:(
NSArray
<
NSNumber
*>
*
)
means
scale
:(
float
)
scale
{
-
(
PaddleMobileCPUResult
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
means
:(
NSArray
<
NSNumber
*>
*
)
means
scale
:(
float
)
scale
{
// printf(" predict one ");
std
::
lock_guard
<
std
::
mutex
>
lock
(
shared_mutex
);
if
(
!
loaded_
)
{
...
...
@@ -283,28 +309,22 @@ static std::mutex shared_mutex;
// predict
std
::
vector
<
float
>
cpp_result
=
pam_
->
Predict
(
predict_input
,
dim_vec
);
// result
long
count
=
0
;
count
=
cpp_result
.
size
();
NSMutableArray
*
result
=
[[
NSMutableArray
alloc
]
init
];
for
(
int
i
=
0
;
i
<
count
;
i
++
)
{
[
result
addObject
:[
NSNumber
numberWithFloat
:
cpp_result
[
i
]]];
}
float
*
output_pointer
=
new
float
[
cpp_result
.
size
()];
memcpy
(
output_pointer
,
cpp_result
.
data
(),
cpp_result
.
size
()
*
sizeof
(
float
));
PaddleMobileCPUResult
*
cpuResult
=
[[
PaddleMobileCPUResult
alloc
]
init
];
[
cpuResult
toSetOutput
:
output_pointer
];
[
cpuResult
toSetOutputSize
:
cpp_result
.
size
()];
free
(
output
);
// 待验证
// if ([UIDevice currentDevice].systemVersion.doubleValue < 11.0) {
CFRelease
(
cfData
);
cfData
=
NULL
;
// }
return
r
esult
;
return
cpuR
esult
;
}
-
(
NSArray
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
{
[
self
predict
:
image
dim
:
dim
means
:
nil
scale
:
1
];
-
(
PaddleMobileCPUResult
*
)
predict
:(
CGImageRef
)
image
dim
:(
NSArray
<
NSNumber
*>
*
)
dim
{
return
[
self
predict
:
image
dim
:
dim
means
:
nil
scale
:
1
];
}
-
(
void
)
clear
{
...
...
src/io/paddle_mobile.cpp
浏览文件 @
dd4a10a5
...
...
@@ -75,11 +75,10 @@ template <typename Dtype, Precision P>
bool
PaddleMobile
<
Dtype
,
P
>::
LoadCombinedMemory
(
size_t
model_len
,
const
uint8_t
*
model_buf
,
size_t
combined_params_len
,
uint8_t
*
combined_params_buf
)
{
int
batch_size
=
1
;
bool
optimise
=
true
;
bool
quantification
=
false
;
uint8_t
*
combined_params_buf
,
bool
optimize
,
bool
quantification
,
int
batch_size
,
bool
loddable
)
{
if
(
loader_
.
get
()
==
nullptr
)
{
loader_
=
std
::
make_shared
<
framework
::
Loader
<
Dtype
,
P
>>
();
}
else
{
...
...
@@ -89,9 +88,9 @@ bool PaddleMobile<Dtype, P>::LoadCombinedMemory(size_t model_len,
if
(
executor_
.
get
()
==
nullptr
)
{
executor_
=
std
::
make_shared
<
framework
::
Executor
<
Dtype
,
P
>>
(
loader_
->
LoadCombinedMemory
(
model_len
,
model_buf
,
combined_params_len
,
combined_params_buf
,
optimi
s
e
,
combined_params_buf
,
optimi
z
e
,
quantification
),
batch_size
,
optimi
s
e
);
batch_size
,
optimi
ze
,
loddabl
e
);
}
else
{
LOG
(
kLOG_INFO
)
<<
"executor inited"
;
}
...
...
src/io/paddle_mobile.h
浏览文件 @
dd4a10a5
...
...
@@ -61,7 +61,8 @@ class PaddleMobile {
bool
LoadCombinedMemory
(
size_t
model_len
,
const
uint8_t
*
model_buf
,
size_t
combined_params_len
,
uint8_t
*
combined_params_buf
);
uint8_t
*
combined_params_buf
,
bool
optimize
=
false
,
bool
quantification
=
false
,
int
batch_size
=
1
,
bool
loddable
=
false
);
void
SetThreadNum
(
int
num
);
void
Clear
();
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录