Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
PaddlePaddle
PaddleSlim
提交
388211f3
P
PaddleSlim
项目概览
PaddlePaddle
/
PaddleSlim
1 年多 前同步成功
通知
51
Star
1434
Fork
344
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
53
列表
看板
标记
里程碑
合并请求
16
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
P
PaddleSlim
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
53
Issue
53
列表
看板
标记
里程碑
合并请求
16
合并请求
16
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
未验证
提交
388211f3
编写于
4月 21, 2020
作者:
B
Bai Yifan
提交者:
GitHub
4月 21, 2020
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Integrates get_genotype in paddleslim (#228)
上级
cdb98e2d
变更
4
显示空白变更内容
内联
并排
Showing
4 changed file
with
82 addition
and
114 deletion
+82
-114
demo/darts/genotypes.py
demo/darts/genotypes.py
+0
-56
demo/darts/model_search.py
demo/darts/model_search.py
+2
-57
paddleslim/nas/darts/get_genotype.py
paddleslim/nas/darts/get_genotype.py
+78
-0
paddleslim/nas/darts/train_search.py
paddleslim/nas/darts/train_search.py
+2
-1
未找到文件。
demo/darts/genotypes.py
浏览文件 @
388211f3
...
@@ -21,62 +21,6 @@ PRIMITIVES = [
...
@@ -21,62 +21,6 @@ PRIMITIVES = [
'sep_conv_5x5'
,
'dil_conv_3x3'
,
'dil_conv_5x5'
'sep_conv_5x5'
,
'dil_conv_3x3'
,
'dil_conv_5x5'
]
]
NASNet
=
Genotype
(
normal
=
[
(
'sep_conv_5x5'
,
1
),
(
'sep_conv_3x3'
,
0
),
(
'sep_conv_5x5'
,
0
),
(
'sep_conv_3x3'
,
0
),
(
'avg_pool_3x3'
,
1
),
(
'skip_connect'
,
0
),
(
'avg_pool_3x3'
,
0
),
(
'avg_pool_3x3'
,
0
),
(
'sep_conv_3x3'
,
1
),
(
'skip_connect'
,
1
),
],
normal_concat
=
[
2
,
3
,
4
,
5
,
6
],
reduce
=
[
(
'sep_conv_5x5'
,
1
),
(
'sep_conv_7x7'
,
0
),
(
'max_pool_3x3'
,
1
),
(
'sep_conv_7x7'
,
0
),
(
'avg_pool_3x3'
,
1
),
(
'sep_conv_5x5'
,
0
),
(
'skip_connect'
,
3
),
(
'avg_pool_3x3'
,
2
),
(
'sep_conv_3x3'
,
2
),
(
'max_pool_3x3'
,
1
),
],
reduce_concat
=
[
4
,
5
,
6
],
)
AmoebaNet
=
Genotype
(
normal
=
[
(
'avg_pool_3x3'
,
0
),
(
'max_pool_3x3'
,
1
),
(
'sep_conv_3x3'
,
0
),
(
'sep_conv_5x5'
,
2
),
(
'sep_conv_3x3'
,
0
),
(
'avg_pool_3x3'
,
3
),
(
'sep_conv_3x3'
,
1
),
(
'skip_connect'
,
1
),
(
'skip_connect'
,
0
),
(
'avg_pool_3x3'
,
1
),
],
normal_concat
=
[
4
,
5
,
6
],
reduce
=
[
(
'avg_pool_3x3'
,
0
),
(
'sep_conv_3x3'
,
1
),
(
'max_pool_3x3'
,
0
),
(
'sep_conv_7x7'
,
2
),
(
'sep_conv_7x7'
,
0
),
(
'avg_pool_3x3'
,
1
),
(
'max_pool_3x3'
,
0
),
(
'max_pool_3x3'
,
1
),
(
'conv_7x1_1x7'
,
0
),
(
'sep_conv_3x3'
,
5
),
],
reduce_concat
=
[
3
,
4
,
6
])
DARTS_V1
=
Genotype
(
DARTS_V1
=
Genotype
(
normal
=
[(
'sep_conv_5x5'
,
0
),
(
'dil_conv_3x3'
,
1
),
(
'sep_conv_3x3'
,
2
),
normal
=
[(
'sep_conv_5x5'
,
0
),
(
'dil_conv_3x3'
,
1
),
(
'sep_conv_3x3'
,
2
),
(
'sep_conv_5x5'
,
0
),
(
'sep_conv_5x5'
,
0
),
(
'dil_conv_3x3'
,
3
),
(
'sep_conv_5x5'
,
0
),
(
'sep_conv_5x5'
,
0
),
(
'dil_conv_3x3'
,
3
),
...
...
demo/darts/model_search.py
浏览文件 @
388211f3
...
@@ -22,7 +22,6 @@ from paddle.fluid.initializer import NormalInitializer, MSRAInitializer, Constan
...
@@ -22,7 +22,6 @@ from paddle.fluid.initializer import NormalInitializer, MSRAInitializer, Constan
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
from
paddle.fluid.dygraph.nn
import
Conv2D
,
Pool2D
,
BatchNorm
,
Linear
from
paddle.fluid.dygraph.base
import
to_variable
from
paddle.fluid.dygraph.base
import
to_variable
from
genotypes
import
PRIMITIVES
from
genotypes
import
PRIMITIVES
from
genotypes
import
Genotype
from
operations
import
*
from
operations
import
*
...
@@ -147,6 +146,7 @@ class Network(fluid.dygraph.Layer):
...
@@ -147,6 +146,7 @@ class Network(fluid.dygraph.Layer):
self
.
_layers
=
layers
self
.
_layers
=
layers
self
.
_steps
=
steps
self
.
_steps
=
steps
self
.
_multiplier
=
multiplier
self
.
_multiplier
=
multiplier
self
.
_primitives
=
PRIMITIVES
self
.
_method
=
method
self
.
_method
=
method
c_cur
=
stem_multiplier
*
c_in
c_cur
=
stem_multiplier
*
c_in
...
@@ -238,7 +238,7 @@ class Network(fluid.dygraph.Layer):
...
@@ -238,7 +238,7 @@ class Network(fluid.dygraph.Layer):
def
_initialize_alphas
(
self
):
def
_initialize_alphas
(
self
):
k
=
sum
(
1
for
i
in
range
(
self
.
_steps
)
for
n
in
range
(
2
+
i
))
k
=
sum
(
1
for
i
in
range
(
self
.
_steps
)
for
n
in
range
(
2
+
i
))
num_ops
=
len
(
PRIMITIVES
)
num_ops
=
len
(
self
.
_primitives
)
self
.
alphas_normal
=
fluid
.
layers
.
create_parameter
(
self
.
alphas_normal
=
fluid
.
layers
.
create_parameter
(
shape
=
[
k
,
num_ops
],
shape
=
[
k
,
num_ops
],
dtype
=
"float32"
,
dtype
=
"float32"
,
...
@@ -268,58 +268,3 @@ class Network(fluid.dygraph.Layer):
...
@@ -268,58 +268,3 @@ class Network(fluid.dygraph.Layer):
def
arch_parameters
(
self
):
def
arch_parameters
(
self
):
return
self
.
_arch_parameters
return
self
.
_arch_parameters
def
genotype
(
self
):
def
_parse
(
weights
,
weights2
=
None
):
gene
=
[]
n
=
2
start
=
0
for
i
in
range
(
self
.
_steps
):
end
=
start
+
n
W
=
weights
[
start
:
end
].
copy
()
if
self
.
_method
==
"PC-DARTS"
:
W2
=
weights2
[
start
:
end
].
copy
()
for
j
in
range
(
n
):
W
[
j
,
:]
=
W
[
j
,
:]
*
W2
[
j
]
edges
=
sorted
(
range
(
i
+
2
),
key
=
lambda
x
:
-
max
(
W
[
x
][
k
]
for
k
in
range
(
len
(
W
[
x
]))
if
k
!=
PRIMITIVES
.
index
(
'none'
)))[:
2
]
for
j
in
edges
:
k_best
=
None
for
k
in
range
(
len
(
W
[
j
])):
if
k
!=
PRIMITIVES
.
index
(
'none'
):
if
k_best
is
None
or
W
[
j
][
k
]
>
W
[
j
][
k_best
]:
k_best
=
k
gene
.
append
((
PRIMITIVES
[
k_best
],
j
))
start
=
end
n
+=
1
return
gene
weightsr2
=
None
weightsn2
=
None
if
self
.
_method
==
"PC-DARTS"
:
n
=
3
start
=
2
weightsr2
=
fluid
.
layers
.
softmax
(
self
.
betas_reduce
[
0
:
2
])
weightsn2
=
fluid
.
layers
.
softmax
(
self
.
betas_normal
[
0
:
2
])
for
i
in
range
(
self
.
_steps
-
1
):
end
=
start
+
n
tw2
=
fluid
.
layers
.
softmax
(
self
.
betas_reduce
[
start
:
end
])
tn2
=
fluid
.
layers
.
softmax
(
self
.
betas_normal
[
start
:
end
])
start
=
end
n
+=
1
weightsr2
=
fluid
.
layers
.
concat
([
weightsr2
,
tw2
])
weightsn2
=
fluid
.
layers
.
concat
([
weightsn2
,
tn2
])
weightsr2
=
weightsr2
.
numpy
()
weightsn2
=
weightsn2
.
numpy
()
gene_normal
=
_parse
(
fluid
.
layers
.
softmax
(
self
.
alphas_normal
).
numpy
(),
weightsn2
)
gene_reduce
=
_parse
(
fluid
.
layers
.
softmax
(
self
.
alphas_reduce
).
numpy
(),
weightsr2
)
concat
=
range
(
2
+
self
.
_steps
-
self
.
_multiplier
,
self
.
_steps
+
2
)
genotype
=
Genotype
(
normal
=
gene_normal
,
normal_concat
=
concat
,
reduce
=
gene_reduce
,
reduce_concat
=
concat
)
return
genotype
paddleslim/nas/darts/get_genotype.py
0 → 100644
浏览文件 @
388211f3
# Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved
#
# 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.fluid
as
fluid
from
collections
import
namedtuple
Genotype
=
namedtuple
(
'Genotype'
,
'normal normal_concat reduce reduce_concat'
)
def
get_genotype
(
model
):
def
_parse
(
weights
,
weights2
=
None
):
gene
=
[]
n
=
2
start
=
0
for
i
in
range
(
model
.
_steps
):
end
=
start
+
n
W
=
weights
[
start
:
end
].
copy
()
if
model
.
_method
==
"PC-DARTS"
:
W2
=
weights2
[
start
:
end
].
copy
()
for
j
in
range
(
n
):
W
[
j
,
:]
=
W
[
j
,
:]
*
W2
[
j
]
edges
=
sorted
(
range
(
i
+
2
),
key
=
lambda
x
:
-
max
(
W
[
x
][
k
]
for
k
in
range
(
len
(
W
[
x
]))
if
k
!=
model
.
_primitives
.
index
(
'none'
)))[:
2
]
for
j
in
edges
:
k_best
=
None
for
k
in
range
(
len
(
W
[
j
])):
if
k
!=
model
.
_primitives
.
index
(
'none'
):
if
k_best
is
None
or
W
[
j
][
k
]
>
W
[
j
][
k_best
]:
k_best
=
k
gene
.
append
((
model
.
_primitives
[
k_best
],
j
))
start
=
end
n
+=
1
return
gene
weightsr2
=
None
weightsn2
=
None
if
model
.
_method
==
"PC-DARTS"
:
n
=
3
start
=
2
weightsr2
=
fluid
.
layers
.
softmax
(
model
.
betas_reduce
[
0
:
2
])
weightsn2
=
fluid
.
layers
.
softmax
(
model
.
betas_normal
[
0
:
2
])
for
i
in
range
(
model
.
_steps
-
1
):
end
=
start
+
n
tw2
=
fluid
.
layers
.
softmax
(
model
.
betas_reduce
[
start
:
end
])
tn2
=
fluid
.
layers
.
softmax
(
model
.
betas_normal
[
start
:
end
])
start
=
end
n
+=
1
weightsr2
=
fluid
.
layers
.
concat
([
weightsr2
,
tw2
])
weightsn2
=
fluid
.
layers
.
concat
([
weightsn2
,
tn2
])
weightsr2
=
weightsr2
.
numpy
()
weightsn2
=
weightsn2
.
numpy
()
gene_normal
=
_parse
(
fluid
.
layers
.
softmax
(
model
.
alphas_normal
).
numpy
(),
weightsn2
)
gene_reduce
=
_parse
(
fluid
.
layers
.
softmax
(
model
.
alphas_reduce
).
numpy
(),
weightsr2
)
concat
=
range
(
2
+
model
.
_steps
-
model
.
_multiplier
,
model
.
_steps
+
2
)
genotype
=
Genotype
(
normal
=
gene_normal
,
normal_concat
=
concat
,
reduce
=
gene_reduce
,
reduce_concat
=
concat
)
return
genotype
paddleslim/nas/darts/train_search.py
浏览文件 @
388211f3
...
@@ -24,6 +24,7 @@ import paddle.fluid as fluid
...
@@ -24,6 +24,7 @@ import paddle.fluid as fluid
from
paddle.fluid.dygraph.base
import
to_variable
from
paddle.fluid.dygraph.base
import
to_variable
from
...common
import
AvgrageMeter
,
get_logger
from
...common
import
AvgrageMeter
,
get_logger
from
.architect
import
Architect
from
.architect
import
Architect
from
.get_genotype
import
get_genotype
logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
logger
=
get_logger
(
__name__
,
level
=
logging
.
INFO
)
...
@@ -201,7 +202,7 @@ class DARTSearch(object):
...
@@ -201,7 +202,7 @@ class DARTSearch(object):
for
epoch
in
range
(
self
.
num_epochs
):
for
epoch
in
range
(
self
.
num_epochs
):
logger
.
info
(
'Epoch {}, lr {:.6f}'
.
format
(
logger
.
info
(
'Epoch {}, lr {:.6f}'
.
format
(
epoch
,
optimizer
.
current_step_lr
()))
epoch
,
optimizer
.
current_step_lr
()))
genotype
=
self
.
model
.
genotype
(
)
genotype
=
get_genotype
(
self
.
model
)
logger
.
info
(
'genotype = %s'
,
genotype
)
logger
.
info
(
'genotype = %s'
,
genotype
)
train_top1
=
self
.
train_one_epoch
(
train_loader
,
valid_loader
,
train_top1
=
self
.
train_one_epoch
(
train_loader
,
valid_loader
,
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录