Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleHub
提交
cb817fd9
P
PaddleHub
项目概览
PaddlePaddle
/
PaddleHub
大约 2 年 前同步成功
通知
285
Star
12117
Fork
2091
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
200
列表
看板
标记
里程碑
合并请求
4
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleHub
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
200
Issue
200
列表
看板
标记
里程碑
合并请求
4
合并请求
4
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
cb817fd9
编写于
11月 04, 2022
作者:
jm_12138
提交者:
GitHub
11月 04, 2022
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update fix_resnext101_32x48d_wsl_imagenet (#2042)
上级
57d97730
变更
7
隐藏空白更改
内联
并排
Showing
7 changed file
with
115 addition
and
252 deletion
+115
-252
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/README.md
...assification/fix_resnext101_32x48d_wsl_imagenet/README.md
+6
-1
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/README_en.md
...ification/fix_resnext101_32x48d_wsl_imagenet/README_en.md
+6
-1
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/data_feed.py
...ification/fix_resnext101_32x48d_wsl_imagenet/data_feed.py
+0
-2
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/module.py
...assification/fix_resnext101_32x48d_wsl_imagenet/module.py
+39
-97
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/processor.py
...ification/fix_resnext101_32x48d_wsl_imagenet/processor.py
+1
-4
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/resnext101_wsl.py
...tion/fix_resnext101_32x48d_wsl_imagenet/resnext101_wsl.py
+0
-147
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/test.py
...classification/fix_resnext101_32x48d_wsl_imagenet/test.py
+63
-0
未找到文件。
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/README.md
浏览文件 @
cb817fd9
...
@@ -129,6 +129,11 @@
...
@@ -129,6 +129,11 @@
*
1.0.0
*
1.0.0
初始发布
初始发布
*
1.1.0
移除 Fluid API
-
```shell
-
```shell
$ hub install fix_resnext101_32x48d_wsl_imagenet==1.
0
.0
$ hub install fix_resnext101_32x48d_wsl_imagenet==1.
1
.0
```
```
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/README_en.md
浏览文件 @
cb817fd9
...
@@ -129,6 +129,11 @@
...
@@ -129,6 +129,11 @@
*
1.0.0
*
1.0.0
First release
First release
*
1.1.0
Remove Fluid API
-
```shell
-
```shell
$ hub install fix_resnext101_32x48d_wsl_imagenet==1.
0
.0
$ hub install fix_resnext101_32x48d_wsl_imagenet==1.
1
.0
```
```
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/data_feed.py
浏览文件 @
cb817fd9
# coding=utf-8
import
os
import
os
import
time
import
time
from
collections
import
OrderedDict
from
collections
import
OrderedDict
import
cv2
import
numpy
as
np
import
numpy
as
np
from
PIL
import
Image
from
PIL
import
Image
...
...
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/module.py
浏览文件 @
cb817fd9
# coding=utf-8
from
__future__
import
absolute_import
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
division
import
ast
import
argparse
import
argparse
import
ast
import
os
import
os
import
numpy
as
np
import
numpy
as
np
import
paddle.fluid
as
fluid
from
paddle.inference
import
Config
import
paddlehub
as
hub
from
paddle.inference
import
create_predictor
from
paddle.fluid.core
import
PaddleTensor
,
AnalysisConfig
,
create_paddle_predictor
from
paddlehub.module.module
import
moduleinfo
,
runnable
,
serving
from
paddlehub.common.paddle_helper
import
add_vars_prefix
from
fix_resnext101_32x48d_wsl_imagenet.processor
import
postprocess
,
base64_to_cv2
from
.data_feed
import
reader
from
fix_resnext101_32x48d_wsl_imagenet.data_feed
import
reader
from
.processor
import
base64_to_cv2
from
fix_resnext101_32x48d_wsl_imagenet.resnext101_wsl
import
Fix_ResNeXt101_32x48d_wsl
from
.processor
import
postprocess
from
paddlehub.module.module
import
moduleinfo
from
paddlehub.module.module
import
runnable
from
paddlehub.module.module
import
serving
@
moduleinfo
(
@
moduleinfo
(
...
@@ -24,10 +23,11 @@ from fix_resnext101_32x48d_wsl_imagenet.resnext101_wsl import Fix_ResNeXt101_32x
...
@@ -24,10 +23,11 @@ from fix_resnext101_32x48d_wsl_imagenet.resnext101_wsl import Fix_ResNeXt101_32x
author
=
"paddlepaddle"
,
author
=
"paddlepaddle"
,
author_email
=
"paddle-dev@baidu.com"
,
author_email
=
"paddle-dev@baidu.com"
,
summary
=
"fix_resnext101_32x48d_wsl is a image classfication model, this module is trained with imagenet datasets."
,
summary
=
"fix_resnext101_32x48d_wsl is a image classfication model, this module is trained with imagenet datasets."
,
version
=
"1.0.0"
)
version
=
"1.1.0"
)
class
FixResnext10132x48dwslImagenet
(
hub
.
Module
):
class
FixResnext10132x48dwslImagenet
:
def
_initialize
(
self
):
self
.
default_pretrained_model_path
=
os
.
path
.
join
(
self
.
directory
,
"model"
)
def
__init__
(
self
):
self
.
default_pretrained_model_path
=
os
.
path
.
join
(
self
.
directory
,
"model"
,
"model"
)
label_file
=
os
.
path
.
join
(
self
.
directory
,
"label_list.txt"
)
label_file
=
os
.
path
.
join
(
self
.
directory
,
"label_list.txt"
)
with
open
(
label_file
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
with
open
(
label_file
,
'r'
,
encoding
=
'utf-8'
)
as
file
:
self
.
label_list
=
file
.
read
().
split
(
"
\n
"
)[:
-
1
]
self
.
label_list
=
file
.
read
().
split
(
"
\n
"
)[:
-
1
]
...
@@ -51,10 +51,12 @@ class FixResnext10132x48dwslImagenet(hub.Module):
...
@@ -51,10 +51,12 @@ class FixResnext10132x48dwslImagenet(hub.Module):
"""
"""
predictor config setting
predictor config setting
"""
"""
cpu_config
=
AnalysisConfig
(
self
.
default_pretrained_model_path
)
model
=
self
.
default_pretrained_model_path
+
'.pdmodel'
params
=
self
.
default_pretrained_model_path
+
'.pdiparams'
cpu_config
=
Config
(
model
,
params
)
cpu_config
.
disable_glog_info
()
cpu_config
.
disable_glog_info
()
cpu_config
.
disable_gpu
()
cpu_config
.
disable_gpu
()
self
.
cpu_predictor
=
create_p
addle_p
redictor
(
cpu_config
)
self
.
cpu_predictor
=
create_predictor
(
cpu_config
)
try
:
try
:
_places
=
os
.
environ
[
"CUDA_VISIBLE_DEVICES"
]
_places
=
os
.
environ
[
"CUDA_VISIBLE_DEVICES"
]
...
@@ -63,58 +65,10 @@ class FixResnext10132x48dwslImagenet(hub.Module):
...
@@ -63,58 +65,10 @@ class FixResnext10132x48dwslImagenet(hub.Module):
except
:
except
:
use_gpu
=
False
use_gpu
=
False
if
use_gpu
:
if
use_gpu
:
gpu_config
=
AnalysisConfig
(
self
.
default_pretrained_model_path
)
gpu_config
=
Config
(
model
,
params
)
gpu_config
.
disable_glog_info
()
gpu_config
.
disable_glog_info
()
gpu_config
.
enable_use_gpu
(
memory_pool_init_size_mb
=
1000
,
device_id
=
0
)
gpu_config
.
enable_use_gpu
(
memory_pool_init_size_mb
=
1000
,
device_id
=
0
)
self
.
gpu_predictor
=
create_paddle_predictor
(
gpu_config
)
self
.
gpu_predictor
=
create_predictor
(
gpu_config
)
def
context
(
self
,
trainable
=
True
,
pretrained
=
True
):
"""context for transfer learning.
Args:
trainable (bool): Set parameters in program to be trainable.
pretrained (bool) : Whether to load pretrained model.
Returns:
inputs (dict): key is 'image', corresponding vaule is image tensor.
outputs (dict): key is :
'classification', corresponding value is the result of classification.
'feature_map', corresponding value is the result of the layer before the fully connected layer.
context_prog (fluid.Program): program for transfer learning.
"""
context_prog
=
fluid
.
Program
()
startup_prog
=
fluid
.
Program
()
with
fluid
.
program_guard
(
context_prog
,
startup_prog
):
with
fluid
.
unique_name
.
guard
():
image
=
fluid
.
layers
.
data
(
name
=
"image"
,
shape
=
[
3
,
224
,
224
],
dtype
=
"float32"
)
resnet_vd
=
Fix_ResNeXt101_32x48d_wsl
()
output
,
feature_map
=
resnet_vd
.
net
(
input
=
image
,
class_dim
=
len
(
self
.
label_list
))
name_prefix
=
'@HUB_{}@'
.
format
(
self
.
name
)
inputs
=
{
'image'
:
name_prefix
+
image
.
name
}
outputs
=
{
'classification'
:
name_prefix
+
output
.
name
,
'feature_map'
:
name_prefix
+
feature_map
.
name
}
add_vars_prefix
(
context_prog
,
name_prefix
)
add_vars_prefix
(
startup_prog
,
name_prefix
)
global_vars
=
context_prog
.
global_block
().
vars
inputs
=
{
key
:
global_vars
[
value
]
for
key
,
value
in
inputs
.
items
()}
outputs
=
{
key
:
global_vars
[
value
]
for
key
,
value
in
outputs
.
items
()}
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
# pretrained
if
pretrained
:
def
_if_exist
(
var
):
b
=
os
.
path
.
exists
(
os
.
path
.
join
(
self
.
default_pretrained_model_path
,
var
.
name
))
return
b
fluid
.
io
.
load_vars
(
exe
,
self
.
default_pretrained_model_path
,
context_prog
,
predicate
=
_if_exist
)
else
:
exe
.
run
(
startup_prog
)
# trainable
for
param
in
context_prog
.
global_block
().
iter_parameters
():
param
.
trainable
=
trainable
return
inputs
,
outputs
,
context_prog
def
classification
(
self
,
images
=
None
,
paths
=
None
,
batch_size
=
1
,
use_gpu
=
False
,
top_k
=
1
):
def
classification
(
self
,
images
=
None
,
paths
=
None
,
batch_size
=
1
,
use_gpu
=
False
,
top_k
=
1
):
"""
"""
...
@@ -136,7 +90,7 @@ class FixResnext10132x48dwslImagenet(hub.Module):
...
@@ -136,7 +90,7 @@ class FixResnext10132x48dwslImagenet(hub.Module):
int
(
_places
[
0
])
int
(
_places
[
0
])
except
:
except
:
raise
RuntimeError
(
raise
RuntimeError
(
"
Environment Variable CUDA_VISIBLE_DEVICES is not set correctly. If you wanna use gpu, please set CUDA_VISIBLE_DEVICES as cuda_device_id
."
"
Attempt to use GPU for prediction, but environment variable CUDA_VISIBLE_DEVICES was not set correctly
."
)
)
if
not
self
.
predictor_set
:
if
not
self
.
predictor_set
:
...
@@ -161,32 +115,19 @@ class FixResnext10132x48dwslImagenet(hub.Module):
...
@@ -161,32 +115,19 @@ class FixResnext10132x48dwslImagenet(hub.Module):
pass
pass
# feed batch image
# feed batch image
batch_image
=
np
.
array
([
data
[
'image'
]
for
data
in
batch_data
])
batch_image
=
np
.
array
([
data
[
'image'
]
for
data
in
batch_data
])
batch_image
=
PaddleTensor
(
batch_image
.
copy
())
predictor_output
=
self
.
gpu_predictor
.
run
([
batch_image
])
if
use_gpu
else
self
.
cpu_predictor
.
run
(
predictor
=
self
.
gpu_predictor
if
use_gpu
else
self
.
cpu_predictor
[
batch_image
])
input_names
=
predictor
.
get_input_names
()
out
=
postprocess
(
data_out
=
predictor_output
[
0
].
as_ndarray
(),
label_list
=
self
.
label_list
,
top_k
=
top_k
)
input_handle
=
predictor
.
get_input_handle
(
input_names
[
0
])
input_handle
.
copy_from_cpu
(
batch_image
.
copy
())
predictor
.
run
()
output_names
=
predictor
.
get_output_names
()
output_handle
=
predictor
.
get_output_handle
(
output_names
[
0
])
out
=
postprocess
(
data_out
=
output_handle
.
copy_to_cpu
(),
label_list
=
self
.
label_list
,
top_k
=
top_k
)
res
+=
out
res
+=
out
return
res
return
res
def
save_inference_model
(
self
,
dirname
,
model_filename
=
None
,
params_filename
=
None
,
combined
=
True
):
if
combined
:
model_filename
=
"__model__"
if
not
model_filename
else
model_filename
params_filename
=
"__params__"
if
not
params_filename
else
params_filename
place
=
fluid
.
CPUPlace
()
exe
=
fluid
.
Executor
(
place
)
program
,
feeded_var_names
,
target_vars
=
fluid
.
io
.
load_inference_model
(
dirname
=
self
.
default_pretrained_model_path
,
executor
=
exe
)
fluid
.
io
.
save_inference_model
(
dirname
=
dirname
,
main_program
=
program
,
executor
=
exe
,
feeded_var_names
=
feeded_var_names
,
target_vars
=
target_vars
,
model_filename
=
model_filename
,
params_filename
=
params_filename
)
@
serving
@
serving
def
serving_method
(
self
,
images
,
**
kwargs
):
def
serving_method
(
self
,
images
,
**
kwargs
):
"""
"""
...
@@ -201,11 +142,10 @@ class FixResnext10132x48dwslImagenet(hub.Module):
...
@@ -201,11 +142,10 @@ class FixResnext10132x48dwslImagenet(hub.Module):
"""
"""
Run as a command.
Run as a command.
"""
"""
self
.
parser
=
argparse
.
ArgumentParser
(
self
.
parser
=
argparse
.
ArgumentParser
(
description
=
"Run the {} module."
.
format
(
self
.
name
),
description
=
"Run the {} module."
.
format
(
self
.
name
),
prog
=
'hub run {}'
.
format
(
self
.
name
),
prog
=
'hub run {}'
.
format
(
self
.
name
),
usage
=
'%(prog)s'
,
usage
=
'%(prog)s'
,
add_help
=
True
)
add_help
=
True
)
self
.
arg_input_group
=
self
.
parser
.
add_argument_group
(
title
=
"Input options"
,
description
=
"Input data. Required"
)
self
.
arg_input_group
=
self
.
parser
.
add_argument_group
(
title
=
"Input options"
,
description
=
"Input data. Required"
)
self
.
arg_config_group
=
self
.
parser
.
add_argument_group
(
self
.
arg_config_group
=
self
.
parser
.
add_argument_group
(
title
=
"Config options"
,
description
=
"Run configuration for controlling module behavior, not required."
)
title
=
"Config options"
,
description
=
"Run configuration for controlling module behavior, not required."
)
...
@@ -219,8 +159,10 @@ class FixResnext10132x48dwslImagenet(hub.Module):
...
@@ -219,8 +159,10 @@ class FixResnext10132x48dwslImagenet(hub.Module):
"""
"""
Add the command config options.
Add the command config options.
"""
"""
self
.
arg_config_group
.
add_argument
(
self
.
arg_config_group
.
add_argument
(
'--use_gpu'
,
'--use_gpu'
,
type
=
ast
.
literal_eval
,
default
=
False
,
help
=
"whether use GPU or not."
)
type
=
ast
.
literal_eval
,
default
=
False
,
help
=
"whether use GPU or not."
)
self
.
arg_config_group
.
add_argument
(
'--batch_size'
,
type
=
ast
.
literal_eval
,
default
=
1
,
help
=
"batch size."
)
self
.
arg_config_group
.
add_argument
(
'--batch_size'
,
type
=
ast
.
literal_eval
,
default
=
1
,
help
=
"batch size."
)
self
.
arg_config_group
.
add_argument
(
'--top_k'
,
type
=
ast
.
literal_eval
,
default
=
1
,
help
=
"Return top k results."
)
self
.
arg_config_group
.
add_argument
(
'--top_k'
,
type
=
ast
.
literal_eval
,
default
=
1
,
help
=
"Return top k results."
)
...
...
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/processor.py
浏览文件 @
cb817fd9
# coding=utf-8
from
__future__
import
absolute_import
from
__future__
import
absolute_import
from
__future__
import
division
from
__future__
import
division
from
__future__
import
print_function
from
__future__
import
print_function
import
base64
import
base64
import
cv2
import
os
import
cv2
import
numpy
as
np
import
numpy
as
np
...
@@ -18,7 +16,6 @@ def base64_to_cv2(b64str):
...
@@ -18,7 +16,6 @@ def base64_to_cv2(b64str):
def
softmax
(
x
):
def
softmax
(
x
):
orig_shape
=
x
.
shape
if
len
(
x
.
shape
)
>
1
:
if
len
(
x
.
shape
)
>
1
:
tmp
=
np
.
max
(
x
,
axis
=
1
)
tmp
=
np
.
max
(
x
,
axis
=
1
)
x
-=
tmp
.
reshape
((
x
.
shape
[
0
],
1
))
x
-=
tmp
.
reshape
((
x
.
shape
[
0
],
1
))
...
...
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/resnext101_wsl.py
已删除
100644 → 0
浏览文件 @
57d97730
#copyright (c) 2019 PaddlePaddle Authors. All Rights Reserve.
#
#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
import
paddle.fluid
as
fluid
import
math
from
paddle.fluid.param_attr
import
ParamAttr
__all__
=
[
"ResNeXt101_32x8d_wsl"
,
"ResNeXt101_32x16d_wsl"
,
"ResNeXt101_32x32d_wsl"
,
"ResNeXt101_32x48d_wsl"
,
"Fix_ResNeXt101_32x48d_wsl"
]
class
ResNeXt101_wsl
():
def
__init__
(
self
,
layers
=
101
,
cardinality
=
32
,
width
=
48
):
self
.
layers
=
layers
self
.
cardinality
=
cardinality
self
.
width
=
width
def
net
(
self
,
input
,
class_dim
=
1000
):
layers
=
self
.
layers
cardinality
=
self
.
cardinality
width
=
self
.
width
depth
=
[
3
,
4
,
23
,
3
]
base_width
=
cardinality
*
width
num_filters
=
[
base_width
*
i
for
i
in
[
1
,
2
,
4
,
8
]]
conv
=
self
.
conv_bn_layer
(
input
=
input
,
num_filters
=
64
,
filter_size
=
7
,
stride
=
2
,
act
=
'relu'
,
name
=
"conv1"
)
#debug
conv
=
fluid
.
layers
.
pool2d
(
input
=
conv
,
pool_size
=
3
,
pool_stride
=
2
,
pool_padding
=
1
,
pool_type
=
'max'
)
for
block
in
range
(
len
(
depth
)):
for
i
in
range
(
depth
[
block
]):
conv_name
=
'layer'
+
str
(
block
+
1
)
+
"."
+
str
(
i
)
conv
=
self
.
bottleneck_block
(
input
=
conv
,
num_filters
=
num_filters
[
block
],
stride
=
2
if
i
==
0
and
block
!=
0
else
1
,
cardinality
=
cardinality
,
name
=
conv_name
)
pool
=
fluid
.
layers
.
pool2d
(
input
=
conv
,
pool_type
=
'avg'
,
global_pooling
=
True
)
stdv
=
1.0
/
math
.
sqrt
(
pool
.
shape
[
1
]
*
1.0
)
out
=
fluid
.
layers
.
fc
(
input
=
pool
,
size
=
class_dim
,
param_attr
=
fluid
.
param_attr
.
ParamAttr
(
initializer
=
fluid
.
initializer
.
Uniform
(
-
stdv
,
stdv
),
name
=
'fc.weight'
),
bias_attr
=
fluid
.
param_attr
.
ParamAttr
(
name
=
'fc.bias'
))
return
out
,
pool
def
conv_bn_layer
(
self
,
input
,
num_filters
,
filter_size
,
stride
=
1
,
groups
=
1
,
act
=
None
,
name
=
None
):
if
"downsample"
in
name
:
conv_name
=
name
+
'.0'
else
:
conv_name
=
name
conv
=
fluid
.
layers
.
conv2d
(
input
=
input
,
num_filters
=
num_filters
,
filter_size
=
filter_size
,
stride
=
stride
,
padding
=
(
filter_size
-
1
)
//
2
,
groups
=
groups
,
act
=
None
,
param_attr
=
ParamAttr
(
name
=
conv_name
+
".weight"
),
bias_attr
=
False
)
if
"downsample"
in
name
:
bn_name
=
name
[:
9
]
+
'downsample'
+
'.1'
else
:
if
"conv1"
==
name
:
bn_name
=
'bn'
+
name
[
-
1
]
else
:
bn_name
=
(
name
[:
10
]
if
name
[
7
:
9
].
isdigit
()
else
name
[:
9
])
+
'bn'
+
name
[
-
1
]
return
fluid
.
layers
.
batch_norm
(
input
=
conv
,
act
=
act
,
param_attr
=
ParamAttr
(
name
=
bn_name
+
'.weight'
),
bias_attr
=
ParamAttr
(
bn_name
+
'.bias'
),
moving_mean_name
=
bn_name
+
'.running_mean'
,
moving_variance_name
=
bn_name
+
'.running_var'
,
)
def
shortcut
(
self
,
input
,
ch_out
,
stride
,
name
):
ch_in
=
input
.
shape
[
1
]
if
ch_in
!=
ch_out
or
stride
!=
1
:
return
self
.
conv_bn_layer
(
input
,
ch_out
,
1
,
stride
,
name
=
name
)
else
:
return
input
def
bottleneck_block
(
self
,
input
,
num_filters
,
stride
,
cardinality
,
name
):
cardinality
=
self
.
cardinality
width
=
self
.
width
conv0
=
self
.
conv_bn_layer
(
input
=
input
,
num_filters
=
num_filters
,
filter_size
=
1
,
act
=
'relu'
,
name
=
name
+
".conv1"
)
conv1
=
self
.
conv_bn_layer
(
input
=
conv0
,
num_filters
=
num_filters
,
filter_size
=
3
,
stride
=
stride
,
groups
=
cardinality
,
act
=
'relu'
,
name
=
name
+
".conv2"
)
conv2
=
self
.
conv_bn_layer
(
input
=
conv1
,
num_filters
=
num_filters
//
(
width
//
8
),
filter_size
=
1
,
act
=
None
,
name
=
name
+
".conv3"
)
short
=
self
.
shortcut
(
input
,
num_filters
//
(
width
//
8
),
stride
,
name
=
name
+
".downsample"
)
return
fluid
.
layers
.
elementwise_add
(
x
=
short
,
y
=
conv2
,
act
=
'relu'
)
def
ResNeXt101_32x8d_wsl
():
model
=
ResNeXt101_wsl
(
cardinality
=
32
,
width
=
8
)
return
model
def
ResNeXt101_32x16d_wsl
():
model
=
ResNeXt101_wsl
(
cardinality
=
32
,
width
=
16
)
return
model
def
ResNeXt101_32x32d_wsl
():
model
=
ResNeXt101_wsl
(
cardinality
=
32
,
width
=
32
)
return
model
def
ResNeXt101_32x48d_wsl
():
model
=
ResNeXt101_wsl
(
cardinality
=
32
,
width
=
48
)
return
model
def
Fix_ResNeXt101_32x48d_wsl
():
model
=
ResNeXt101_wsl
(
cardinality
=
32
,
width
=
48
)
return
model
modules/image/classification/fix_resnext101_32x48d_wsl_imagenet/test.py
0 → 100644
浏览文件 @
cb817fd9
import
os
import
shutil
import
unittest
import
cv2
import
requests
import
paddlehub
as
hub
os
.
environ
[
'CUDA_VISIBLE_DEVICES'
]
=
'0'
class
TestHubModule
(
unittest
.
TestCase
):
@
classmethod
def
setUpClass
(
cls
)
->
None
:
img_url
=
'https://unsplash.com/photos/brFsZ7qszSY/download?ixid=MnwxMjA3fDB8MXxzZWFyY2h8OHx8ZG9nfGVufDB8fHx8MTY2MzA1ODQ1MQ&force=true&w=640'
if
not
os
.
path
.
exists
(
'tests'
):
os
.
makedirs
(
'tests'
)
response
=
requests
.
get
(
img_url
)
assert
response
.
status_code
==
200
,
'Network Error.'
with
open
(
'tests/test.jpg'
,
'wb'
)
as
f
:
f
.
write
(
response
.
content
)
cls
.
module
=
hub
.
Module
(
name
=
"fix_resnext101_32x48d_wsl_imagenet"
)
@
classmethod
def
tearDownClass
(
cls
)
->
None
:
shutil
.
rmtree
(
'tests'
)
shutil
.
rmtree
(
'inference'
)
def
test_classification1
(
self
):
results
=
self
.
module
.
classification
(
paths
=
[
'tests/test.jpg'
])
data
=
results
[
0
]
self
.
assertTrue
(
'Pembroke'
in
data
)
self
.
assertTrue
(
data
[
'Pembroke'
]
>
0.5
)
def
test_classification2
(
self
):
results
=
self
.
module
.
classification
(
images
=
[
cv2
.
imread
(
'tests/test.jpg'
)])
data
=
results
[
0
]
self
.
assertTrue
(
'Pembroke'
in
data
)
self
.
assertTrue
(
data
[
'Pembroke'
]
>
0.5
)
def
test_classification3
(
self
):
results
=
self
.
module
.
classification
(
images
=
[
cv2
.
imread
(
'tests/test.jpg'
)],
use_gpu
=
True
)
data
=
results
[
0
]
self
.
assertTrue
(
'Pembroke'
in
data
)
self
.
assertTrue
(
data
[
'Pembroke'
]
>
0.5
)
def
test_classification4
(
self
):
self
.
assertRaises
(
AssertionError
,
self
.
module
.
classification
,
paths
=
[
'no.jpg'
])
def
test_classification5
(
self
):
self
.
assertRaises
(
TypeError
,
self
.
module
.
classification
,
images
=
[
'tests/test.jpg'
])
def
test_save_inference_model
(
self
):
self
.
module
.
save_inference_model
(
'./inference/model'
)
self
.
assertTrue
(
os
.
path
.
exists
(
'./inference/model.pdmodel'
))
self
.
assertTrue
(
os
.
path
.
exists
(
'./inference/model.pdiparams'
))
if
__name__
==
"__main__"
:
unittest
.
main
()
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录