Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
Serving
提交
ae65c8c0
S
Serving
项目概览
PaddlePaddle
/
Serving
大约 1 年 前同步成功
通知
186
Star
833
Fork
253
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
105
列表
看板
标记
里程碑
合并请求
10
Wiki
2
Wiki
分析
仓库
DevOps
项目成员
Pages
S
Serving
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
105
Issue
105
列表
看板
标记
里程碑
合并请求
10
合并请求
10
Pages
分析
分析
仓库分析
DevOps
Wiki
2
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
提交
ae65c8c0
编写于
5月 02, 2020
作者:
D
dongdaxiang
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
add imagenet preprocessing example and more functions for image_reader
上级
2e03def0
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
110 addition
and
15 deletion
+110
-15
python/examples/imagenet/image_rpc_client.py
python/examples/imagenet/image_rpc_client.py
+9
-7
python/paddle_serving_app/reader/__init__.py
python/paddle_serving_app/reader/__init__.py
+1
-1
python/paddle_serving_app/reader/functional.py
python/paddle_serving_app/reader/functional.py
+6
-4
python/paddle_serving_app/reader/image_reader.py
python/paddle_serving_app/reader/image_reader.py
+94
-3
未找到文件。
python/examples/imagenet/image_rpc_client.py
浏览文件 @
ae65c8c0
...
...
@@ -13,22 +13,24 @@
# limitations under the License.
import
sys
from
image_reader
import
ImageReader
from
paddle_serving_client
import
Client
from
paddle_serving_app.reader
import
Sequential
,
File2Image
,
Resize
,
CenterCrop
,
RGB2BGR
,
Transpose
,
Div
,
Normalize
import
time
client
=
Client
()
client
.
load_client_config
(
sys
.
argv
[
1
])
client
.
connect
([
"127.0.0.1:9393"
])
reader
=
ImageReader
()
seq
=
Sequential
([
File2Image
(),
Resize
(
256
),
CenterCrop
(
224
),
RGB2BGR
(),
Transpose
((
2
,
0
,
1
)),
Div
(
255
),
Normalize
([
0.485
,
0.456
,
0.406
],
[
0.229
,
0.224
,
0.225
])
])
print
(
seq
)
start
=
time
.
time
()
image_file
=
"daisy.jpg"
for
i
in
range
(
1000
):
with
open
(
"./data/n01440764_10026.JPEG"
,
"rb"
)
as
f
:
img
=
f
.
read
()
img
=
reader
.
process_image
(
img
)
img
=
seq
(
image_file
)
fetch_map
=
client
.
predict
(
feed
=
{
"image"
:
img
},
fetch
=
[
"score"
])
end
=
time
.
time
()
print
(
end
-
start
)
#print(fetch_map["score"])
python/paddle_serving_app/reader/__init__.py
浏览文件 @
ae65c8c0
...
...
@@ -11,4 +11,4 @@
# 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
.image_reader
import
ImageReader
,
File2Image
,
URL2Image
,
Sequential
,
Normalize
,
CenterCrop
,
Resize
from
.image_reader
import
ImageReader
,
File2Image
,
URL2Image
,
Sequential
,
Normalize
,
CenterCrop
,
Resize
,
Transpose
,
Div
,
RGB2BGR
,
BGR2RGB
python/paddle_serving_app/reader/functional.py
浏览文件 @
ae65c8c0
...
...
@@ -16,14 +16,17 @@ import cv2
import
numpy
as
np
def
transpose
(
img
,
transpose_target
):
img
=
img
.
transpose
(
transpose_target
)
return
img
def
normalize
(
img
,
mean
,
std
):
# need to optimize here
img
=
img
.
astype
(
'float32'
).
transpose
((
2
,
0
,
1
))
/
255
img_mean
=
np
.
array
(
mean
).
reshape
((
3
,
1
,
1
))
img_std
=
np
.
array
(
std
).
reshape
((
3
,
1
,
1
))
img
-=
img_mean
img
/=
img_std
img
=
img
.
transpose
((
1
,
2
,
0
))
return
img
...
...
@@ -47,7 +50,7 @@ def resize(img, target_size, interpolation):
resized_width
=
target_size
[
0
]
resized_height
=
target_size
[
1
]
else
:
percent
=
float
(
target_size
)
/
min
(
img
.
shape
[
1
],
img
.
shape
[
2
])
percent
=
float
(
target_size
)
/
min
(
img
.
shape
[
0
],
img
.
shape
[
1
])
resized_width
=
int
(
round
(
img
.
shape
[
1
]
*
percent
))
resized_height
=
int
(
round
(
img
.
shape
[
0
]
*
percent
))
if
interpolation
:
...
...
@@ -55,5 +58,4 @@ def resize(img, target_size, interpolation):
img
,
(
resized_width
,
resized_height
),
interpolation
=
interpolation
)
else
:
resized
=
cv2
.
resize
(
img
,
(
resized_width
,
resized_height
))
print
(
resized
.
shape
)
return
resized
python/paddle_serving_app/reader/image_reader.py
浏览文件 @
ae65c8c0
...
...
@@ -17,7 +17,7 @@ import numpy as np
import
base64
import
functional
as
F
_cv2_interpolation_to_str
=
{
cv2
.
INTER_LINEAR
:
"cv2.INTER_LINEAR"
}
_cv2_interpolation_to_str
=
{
cv2
.
INTER_LINEAR
:
"cv2.INTER_LINEAR"
,
None
:
"None"
}
class
Sequential
(
object
):
...
...
@@ -51,6 +51,28 @@ class Sequential(object):
return
format_string_
class
RGB2BGR
(
object
):
def
__init__
(
self
):
pass
def
__call__
(
self
,
img
):
return
img
[:,
:,
::
-
1
]
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
"()"
class
BGR2RGB
(
object
):
def
__init__
(
self
):
pass
def
__call__
(
self
,
img
):
return
img
[:,
:,
::
-
1
]
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
"()"
class
File2Image
(
object
):
def
__init__
(
self
):
pass
...
...
@@ -81,6 +103,40 @@ class URL2Image(object):
return
self
.
__class__
.
__name__
+
"()"
class
Base64ToImage
(
object
):
def
__init__
(
self
):
pass
def
__call__
(
self
,
img_base64
):
img
=
base64
.
b64decode
(
img_base64
)
return
img
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
"()"
class
Div
(
object
):
""" divide by some float number """
def
__init__
(
self
,
value
):
self
.
value
=
value
def
__call__
(
self
,
img
):
"""
Args:
img (numpy array): (int8 numpy array)
Returns:
img (numpy array): (float32 numpy array)
"""
img
=
img
.
astype
(
'float32'
)
/
self
.
value
return
img
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
"({})"
.
format
(
self
.
value
)
class
Normalize
(
object
):
"""Normalize a tensor image with mean and standard deviation.
Given mean: ``(M1,...,Mn)`` and std: ``(S1,..,Sn)`` for ``n`` channels, this transform
...
...
@@ -115,6 +171,27 @@ class Normalize(object):
self
.
std
)
class
Lambda
(
object
):
"""Apply a user-defined lambda as a transform.
Very shame to just copy from
https://github.com/pytorch/vision/blob/master/torchvision/transforms/transforms.py#L301
Args:
lambd (function): Lambda/function to be used for transform.
"""
def
__init__
(
self
,
lambd
):
assert
callable
(
lambd
),
repr
(
type
(
lambd
)
.
__name__
)
+
" object is not callable"
self
.
lambd
=
lambd
def
__call__
(
self
,
img
):
return
self
.
lambd
(
img
)
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
'()'
class
CenterCrop
(
object
):
"""Crops the given Image at the center.
...
...
@@ -154,18 +231,32 @@ class Resize(object):
``PIL.Image.BILINEAR``
"""
def
__init__
(
self
,
size
,
interpolation
=
cv2
.
INTER_LINEAR
):
def
__init__
(
self
,
size
,
interpolation
=
None
):
self
.
size
=
size
self
.
interpolation
=
interpolation
def
__call__
(
self
,
img
):
return
F
.
resize
(
img
,
self
.
size
,
self
.
interpolation
)
def
__repr__
(
self
,
img
):
def
__repr__
(
self
):
return
self
.
__class__
.
__name__
+
'(size={0}, interpolation={1})'
.
format
(
self
.
size
,
_cv2_interpolation_to_str
[
self
.
interpolation
])
class
Transpose
(
object
):
def
__init__
(
self
,
transpose_target
):
self
.
transpose_target
=
transpose_target
def
__call__
(
self
,
img
):
return
F
.
transpose
(
img
,
self
.
transpose_target
)
return
img
def
__repr__
(
self
):
format_string
=
self
.
__class__
.
__name__
+
\
"({})"
.
format
(
self
.
transpose_target
)
return
format_string
class
ImageReader
():
def
__init__
(
self
,
image_shape
=
[
3
,
224
,
224
],
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录