Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
H
hcl-netadapt
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Raul Marichal
hcl-netadapt
Commits
f1a5506c
Commit
f1a5506c
authored
1 year ago
by
Raul Marichal
Browse files
Options
Downloads
Patches
Plain Diff
Actualizo master con el long() y separo en dos simplify_network_def_based_on_constraint
parent
e51ff779
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
.gitignore
+1
-1
1 addition, 1 deletion
.gitignore
functions.py
+55
-53
55 additions, 53 deletions
functions.py
with
56 additions
and
54 deletions
.gitignore
+
1
−
1
View file @
f1a5506c
...
...
@@ -2,4 +2,4 @@ data/*
models/*
**/__pycache__/
outs/
**.out
This diff is collapsed.
Click to expand it.
functions.py
+
55
−
53
View file @
f1a5506c
...
...
@@ -524,6 +524,58 @@ def build_latency_lookup_table(network_def_full, lookup_table_path, min_conv_fea
pickle
.
dump
(
lookup_table
,
file_id
)
return
def
compose_simplified_network_def
(
simplified_network_def
,
network_def
,
current_num_out_channels
,
target_layer_indices
,
max_num_out_channels
):
for
target_layer_index
in
target_layer_indices
:
update_num_out_channels
=
True
current_num_out_channels_after_pixel_shuffle
=
current_num_out_channels
for
layer_idx
,
(
layer_name
,
layer_properties
)
in
enumerate
(
simplified_network_def
.
items
()):
if
layer_idx
<
target_layer_index
:
continue
# for the block to be simplified (# of output channels is simplified)
if
update_num_out_channels
:
if
not
layer_properties
[
KEY_IS_DEPTHWISE
]:
layer_properties
[
KEY_NUM_OUT_CHANNELS
]
=
current_num_out_channels
update_num_out_channels
=
False
print
(
'
simplify_def> layer {}: num of output channel changed to {}
'
.
format
(
layer_name
,
str
(
current_num_out_channels
)))
else
:
raise
ValueError
(
'
Expected a non-depthwise layer but got a depthwise layer.
'
)
# for blocks following the target blocks (# of input channels is simplified)
else
:
if
current_num_out_channels_after_pixel_shuffle
%
layer_properties
[
KEY_BEFORE_SQUARED_PIXEL_SHUFFLE_FACTOR
]
!=
0
:
raise
ValueError
(
'
current_num_out_channels or current_num_out_channels_after_pixel_shuffle is
'
'
not divisible by the scaling factor of pixel shuffling.
'
)
current_num_out_channels_after_pixel_shuffle
=
(
current_num_out_channels_after_pixel_shuffle
/
layer_properties
[
KEY_BEFORE_SQUARED_PIXEL_SHUFFLE_FACTOR
])
layer_properties
[
KEY_NUM_IN_CHANNELS
]
=
current_num_out_channels_after_pixel_shuffle
print
(
'
simplify_def> layer {}: num of input channel changed to {}
'
.
format
(
layer_name
,
str
(
current_num_out_channels_after_pixel_shuffle
)))
'''
Consider the case that a FC layer is placed after a Conv and Flatten:
FC: input feature size: Cin
output feature size: Cout
Conv: output feature map size: H x W x C
So Cin = H x W x C.
If C -> C
'
based on constraints, then Cin -> H x W x C
'
'''
if
layer_properties
[
KEY_BEFORE_SQUARED_PIXEL_SHUFFLE_FACTOR
]
==
1
:
if
network_def
[
layer_name
][
KEY_NUM_IN_CHANNELS
]
>
max_num_out_channels
:
assert
network_def
[
layer_name
][
KEY_NUM_IN_CHANNELS
]
%
max_num_out_channels
==
0
# H x W here
spatial_factor
=
network_def
[
layer_name
][
KEY_NUM_IN_CHANNELS
]
//
max_num_out_channels
layer_properties
[
KEY_NUM_IN_CHANNELS
]
=
spatial_factor
*
current_num_out_channels
print
(
'
simplify_def> [Update] layer {}: num of input channel changed to {}
'
.
format
(
layer_name
,
str
(
spatial_factor
*
current_num_out_channels
)))
if
not
layer_properties
[
KEY_IS_DEPTHWISE
]:
break
else
:
layer_properties
[
KEY_NUM_OUT_CHANNELS
]
=
current_num_out_channels_after_pixel_shuffle
layer_properties
[
KEY_GROUPS
]
=
current_num_out_channels_after_pixel_shuffle
print
(
'
simplify_def> depthwise layer {}: num of output channel changed to {}
'
.
format
(
layer_name
,
str
(
current_num_out_channels_after_pixel_shuffle
)))
return
simplified_network_def
def
simplify_network_def_based_on_constraint
(
network_def
,
block
,
constraint
,
resource_type
,
lookup_table_path
=
None
,
skip_connection_block_sets
=
[],
...
...
@@ -613,58 +665,8 @@ def simplify_network_def_based_on_constraint(network_def, block, constraint, res
Update # of input channels of one non-depthwise layer following the target layers.
'''
for
current_num_out_channels
in
num_out_channels_try
:
# Only allow multiple of '_MIN_FEATURE_SIZE'.
for
target_layer_index
in
target_layer_indices
:
update_num_out_channels
=
True
current_num_out_channels_after_pixel_shuffle
=
current_num_out_channels
for
layer_idx
,
(
layer_name
,
layer_properties
)
in
enumerate
(
simplified_network_def
.
items
()):
if
layer_idx
<
target_layer_index
:
continue
# for the block to be simplified (# of output channels is simplified)
if
update_num_out_channels
:
if
not
layer_properties
[
KEY_IS_DEPTHWISE
]:
layer_properties
[
KEY_NUM_OUT_CHANNELS
]
=
current_num_out_channels
update_num_out_channels
=
False
print
(
'
simplify_def> layer {}: num of output channel changed to {}
'
.
format
(
layer_name
,
str
(
current_num_out_channels
)))
else
:
raise
ValueError
(
'
Expected a non-depthwise layer but got a depthwise layer.
'
)
# for blocks following the target blocks (# of input channels is simplified)
else
:
if
current_num_out_channels_after_pixel_shuffle
%
layer_properties
[
KEY_BEFORE_SQUARED_PIXEL_SHUFFLE_FACTOR
]
!=
0
:
raise
ValueError
(
'
current_num_out_channels or current_num_out_channels_after_pixel_shuffle is
'
'
not divisible by the scaling factor of pixel shuffling.
'
)
current_num_out_channels_after_pixel_shuffle
=
(
current_num_out_channels_after_pixel_shuffle
/
layer_properties
[
KEY_BEFORE_SQUARED_PIXEL_SHUFFLE_FACTOR
])
layer_properties
[
KEY_NUM_IN_CHANNELS
]
=
current_num_out_channels_after_pixel_shuffle
print
(
'
simplify_def> layer {}: num of input channel changed to {}
'
.
format
(
layer_name
,
str
(
current_num_out_channels_after_pixel_shuffle
)))
'''
Consider the case that a FC layer is placed after a Conv and Flatten:
FC: input feature size: Cin
output feature size: Cout
Conv: output feature map size: H x W x C
So Cin = H x W x C.
If C -> C
'
based on constraints, then Cin -> H x W x C
'
'''
if
layer_properties
[
KEY_BEFORE_SQUARED_PIXEL_SHUFFLE_FACTOR
]
==
1
:
if
network_def
[
layer_name
][
KEY_NUM_IN_CHANNELS
]
>
max_num_out_channels
:
assert
network_def
[
layer_name
][
KEY_NUM_IN_CHANNELS
]
%
max_num_out_channels
==
0
# H x W here
spatial_factor
=
network_def
[
layer_name
][
KEY_NUM_IN_CHANNELS
]
//
max_num_out_channels
layer_properties
[
KEY_NUM_IN_CHANNELS
]
=
spatial_factor
*
current_num_out_channels
print
(
'
simplify_def> [Update] layer {}: num of input channel changed to {}
'
.
format
(
layer_name
,
str
(
spatial_factor
*
current_num_out_channels
)))
if
not
layer_properties
[
KEY_IS_DEPTHWISE
]:
break
else
:
layer_properties
[
KEY_NUM_OUT_CHANNELS
]
=
current_num_out_channels_after_pixel_shuffle
layer_properties
[
KEY_GROUPS
]
=
current_num_out_channels_after_pixel_shuffle
print
(
'
simplify_def> depthwise layer {}: num of output channel changed to {}
'
.
format
(
layer_name
,
str
(
current_num_out_channels_after_pixel_shuffle
)))
# Get the simplified network def compatible with current_num_out_channels
simplified_network_def
=
compose_simplified_network_def
(
simplified_network_def
,
network_def
,
current_num_out_channels
,
target_layer_indices
,
max_num_out_channels
)
# Get the current resource consumption
simplified_resource
=
compute_resource
(
simplified_network_def
,
resource_type
,
lookup_table_path
)
...
...
@@ -719,7 +721,7 @@ def simplify_model_based_on_network_def(simplified_network_def, model):
KEY_BEFORE_SQUARED_PIXEL_SHUFFLE_FACTOR
]
kept_filter_idx
=
(
kept_filter_idx
[::
before_squared_pixel_shuffle_factor
]
/
before_squared_pixel_shuffle_factor
)
kept_filter_idx
=
kept_filter_idx
.
long
()
if
layer_param_name
==
WEIGHTSTRING
:
#WEIGHTSTRING == layer_param_name:
if
layer
.
groups
==
1
:
# Pointwise layer or depthwise layer with only one filter.
setattr
(
layer
,
layer_param_name
,
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment