Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
S
seminar-breakout
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Analytics
Analytics
CI / CD
Repository
Value Stream
Wiki
Wiki
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
Shashank Suhas
seminar-breakout
Commits
a988fc18
Commit
a988fc18
authored
Nov 29, 2017
by
Yuxin Wu
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Update the use of exportmodel
parent
2e2bbcac
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
with
61 additions
and
54 deletions
+61
-54
docs/tutorial/inference.md
docs/tutorial/inference.md
+8
-1
docs/tutorial/symbolic.md
docs/tutorial/symbolic.md
+2
-1
tensorpack/tfutils/export.py
tensorpack/tfutils/export.py
+51
-52
No files found.
docs/tutorial/inference.md
View file @
a988fc18
...
@@ -23,8 +23,15 @@ It saves models to standard checkpoint format, plus a metagraph protobuf file.
...
@@ -23,8 +23,15 @@ It saves models to standard checkpoint format, plus a metagraph protobuf file.
They are sufficient to use with whatever deployment methods TensorFlow supports.
They are sufficient to use with whatever deployment methods TensorFlow supports.
But you'll need to read TF docs and do it on your own.
But you'll need to read TF docs and do it on your own.
Please note that, the metagraph saved during training is the training graph.
But you may need a different one for inference.
For example, you may need a different data layout for CPU inference,
or you may need placeholders in the inference graph, or the training graph contains multi-GPU replication
which you want to remove.
In this case, you can always create a new graph by yourself with TF symbolic functions.
The only thing tensorpack has is
`OfflinePredictor`
,
The only thing tensorpack has is
`OfflinePredictor`
,
a simple function to build the graph and a callable for you.
a simple function to build the graph and a callable for you.
It is mainly for quick demo purpose.
It is mainly for quick demo purpose.
It only runs inference on Python data, therefore may not be the most efficient way.
It only runs inference on Python data, therefore may not be the most efficient way.
Check out some examples for
the
usage.
Check out some examples for
its
usage.
docs/tutorial/symbolic.md
View file @
a988fc18
...
@@ -66,7 +66,8 @@ with TowerContext('', is_training=True):
...
@@ -66,7 +66,8 @@ with TowerContext('', is_training=True):
Some layers (in particular
``BatchNorm``
) has different train/test time behavior which is controlled
Some layers (in particular
``BatchNorm``
) has different train/test time behavior which is controlled
by
``TowerContext``
. If you need to use the tensorpack version of them in test time, you'll need to create the ops for them under another context.
by
``TowerContext``
. If you need to use the tensorpack version of them in test time, you'll need to create the ops for them under another context.
```
python
```
python
with
tf
.
variable_scope
(
tf
.
get_variable_scope
(),
reuse
=
True
),
TowerContext
(
'predict'
,
is_training
=
False
):
# Open a `reuse=True` variable scope here if you're sharing variables, then:
with
TowerContext
(
'some_name_or_empty_string'
,
is_training
=
False
):
# build the graph again
# build the graph again
```
```
...
...
tensorpack/tfutils/export.py
View file @
a988fc18
...
@@ -58,25 +58,22 @@ class ModelExport(object):
...
@@ -58,25 +58,22 @@ class ModelExport(object):
assert
isinstance
(
input_names
,
list
)
assert
isinstance
(
input_names
,
list
)
assert
isinstance
(
output_names
,
list
)
assert
isinstance
(
output_names
,
list
)
assert
isinstance
(
model
,
ModelDescBase
)
assert
isinstance
(
model
,
ModelDescBase
)
logger
.
info
(
'[export] prepare new model export'
)
super
(
ModelExport
,
self
)
.
__init__
()
self
.
model
=
model
self
.
model
=
model
self
.
input
=
PlaceholderInput
()
self
.
input
.
setup
(
self
.
model
.
get_inputs_desc
())
self
.
output_names
=
output_names
self
.
output_names
=
output_names
self
.
input_names
=
input_names
self
.
input_names
=
input_names
def
export
(
self
,
checkpoint
,
export_path
,
version
=
1
,
tags
=
[
tf
.
saved_model
.
tag_constants
.
SERVING
],
def
export
(
self
,
checkpoint
,
export_path
,
tags
=
[
tf
.
saved_model
.
tag_constants
.
SERVING
],
signature_name
=
'prediction_pipeline'
):
signature_name
=
'prediction_pipeline'
):
"""Use SavedModelBuilder to export a trained model without TensorPack depency.
"""
Use SavedModelBuilder to export a trained model without tensorpack depency.
Remarks:
Remarks:
This produces
This produces
variables/ # output from the vanilla Saver
variables/ # output from the vanilla Saver
variables.data-?????-of-?????
variables.data-?????-of-?????
variables.index
variables.index
saved_model.pb #
saved model in protcol buffer format
saved_model.pb #
a `SavedModel` protobuf
Currently, we only support a single signature, which is the general PredictSignatureDef:
Currently, we only support a single signature, which is the general PredictSignatureDef:
https://github.com/tensorflow/serving/blob/master/tensorflow_serving/g3doc/signature_defs.md
https://github.com/tensorflow/serving/blob/master/tensorflow_serving/g3doc/signature_defs.md
...
@@ -89,47 +86,49 @@ class ModelExport(object):
...
@@ -89,47 +86,49 @@ class ModelExport(object):
"""
"""
logger
.
info
(
'[export] build model for
%
s'
%
checkpoint
)
logger
.
info
(
'[export] build model for
%
s'
%
checkpoint
)
with
TowerContext
(
''
,
is_training
=
False
):
with
TowerContext
(
''
,
is_training
=
False
):
self
.
model
.
build_graph
(
*
self
.
input
.
get_input_tensors
())
input
=
PlaceholderInput
()
input
.
setup
(
self
.
model
.
get_inputs_desc
())
self
.
sess
=
tf
.
Session
(
config
=
tf
.
ConfigProto
(
allow_soft_placement
=
True
))
self
.
model
.
build_graph
(
*
input
.
get_input_tensors
())
# load values from latest checkpoint
init
=
sessinit
.
SaverRestore
(
checkpoint
)
self
.
sess
=
tf
.
Session
(
config
=
tf
.
ConfigProto
(
allow_soft_placement
=
True
))
self
.
sess
.
run
(
tf
.
global_variables_initializer
())
# load values from latest checkpoint
init
.
init
(
self
.
sess
)
init
=
sessinit
.
SaverRestore
(
checkpoint
)
self
.
sess
.
run
(
tf
.
global_variables_initializer
())
self
.
inputs
=
[]
init
.
init
(
self
.
sess
)
for
n
in
self
.
input_names
:
tensor
=
tf
.
get_default_graph
()
.
get_tensor_by_name
(
'
%
s:0'
%
n
)
self
.
inputs
=
[]
logger
.
info
(
'[export] add input-tensor "
%
s"'
%
tensor
.
name
)
for
n
in
self
.
input_names
:
self
.
inputs
.
append
(
tensor
)
tensor
=
tf
.
get_default_graph
()
.
get_tensor_by_name
(
'
%
s:0'
%
n
)
logger
.
info
(
'[export] add input-tensor "
%
s"'
%
tensor
.
name
)
self
.
outputs
=
[]
self
.
inputs
.
append
(
tensor
)
for
n
in
self
.
output_names
:
tensor
=
tf
.
get_default_graph
()
.
get_tensor_by_name
(
'
%
s:0'
%
n
)
self
.
outputs
=
[]
logger
.
info
(
'[export] add output-tensor "
%
s"'
%
tensor
.
name
)
for
n
in
self
.
output_names
:
self
.
outputs
.
append
(
tensor
)
tensor
=
tf
.
get_default_graph
()
.
get_tensor_by_name
(
'
%
s:0'
%
n
)
logger
.
info
(
'[export] add output-tensor "
%
s"'
%
tensor
.
name
)
logger
.
info
(
'[export] exporting trained model to
%
s'
%
export_path
)
self
.
outputs
.
append
(
tensor
)
builder
=
tf
.
saved_model
.
builder
.
SavedModelBuilder
(
export_path
)
logger
.
info
(
'[export] exporting trained model to
%
s'
%
export_path
)
logger
.
info
(
'[export] build signatures'
)
builder
=
tf
.
saved_model
.
builder
.
SavedModelBuilder
(
export_path
)
# build inputs
inputs_signature
=
dict
()
logger
.
info
(
'[export] build signatures'
)
for
n
,
v
in
zip
(
self
.
input_names
,
self
.
inputs
):
# build inputs
logger
.
info
(
'[export] add input signature:
%
s'
%
v
)
inputs_signature
=
dict
()
inputs_signature
[
n
]
=
tf
.
saved_model
.
utils
.
build_tensor_info
(
v
)
for
n
,
v
in
zip
(
self
.
input_names
,
self
.
inputs
):
logger
.
info
(
'[export] add input signature:
%
s'
%
v
)
outputs_signature
=
dict
()
inputs_signature
[
n
]
=
tf
.
saved_model
.
utils
.
build_tensor_info
(
v
)
for
n
,
v
in
zip
(
self
.
output_names
,
self
.
outputs
):
logger
.
info
(
'[export] add output signature:
%
s'
%
v
)
outputs_signature
=
dict
()
outputs_signature
[
n
]
=
tf
.
saved_model
.
utils
.
build_tensor_info
(
v
)
for
n
,
v
in
zip
(
self
.
output_names
,
self
.
outputs
):
logger
.
info
(
'[export] add output signature:
%
s'
%
v
)
prediction_signature
=
tf
.
saved_model
.
signature_def_utils
.
build_signature_def
(
outputs_signature
[
n
]
=
tf
.
saved_model
.
utils
.
build_tensor_info
(
v
)
inputs
=
inputs_signature
,
outputs
=
outputs_signature
,
prediction_signature
=
tf
.
saved_model
.
signature_def_utils
.
build_signature_def
(
method_name
=
tf
.
saved_model
.
signature_constants
.
PREDICT_METHOD_NAME
)
inputs
=
inputs_signature
,
outputs
=
outputs_signature
,
builder
.
add_meta_graph_and_variables
(
method_name
=
tf
.
saved_model
.
signature_constants
.
PREDICT_METHOD_NAME
)
self
.
sess
,
tags
,
signature_def_map
=
{
signature_name
:
prediction_signature
})
builder
.
add_meta_graph_and_variables
(
builder
.
save
()
self
.
sess
,
tags
,
signature_def_map
=
{
signature_name
:
prediction_signature
})
builder
.
save
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment