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
0201f2df
Commit
0201f2df
authored
May 10, 2018
by
Bohumír Zámečník
Committed by
Yuxin Wu
May 09, 2018
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Refactor KerasModelCaller to prevent using an unassigned variable. (#758)
fix #756
parent
9bcf561c
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
with
22 additions
and
17 deletions
+22
-17
tensorpack/contrib/keras.py
tensorpack/contrib/keras.py
+22
-17
No files found.
tensorpack/contrib/keras.py
View file @
0201f2df
...
...
@@ -56,26 +56,12 @@ class KerasModelCaller(object):
old_trainable_names
=
set
([
x
.
name
for
x
in
tf
.
trainable_variables
()])
trainable_backup
=
backup_collection
([
tf
.
GraphKeys
.
TRAINABLE_VARIABLES
])
try
:
if
self
.
cached_model
is
None
:
assert
not
reuse
M
=
self
.
cached_model
=
self
.
get_model
(
*
input_tensors
)
return
M
.
outputs
elif
reuse
:
# use the cached Keras model to mimic reuse
# NOTE: ctx.is_training won't be useful inside model,
# because inference will always use the cached Keras model
M
=
self
.
cached_model
return
M
.
call
(
input_tensors
)
else
:
# create new Keras model if not reuse
M
=
self
.
get_model
(
*
input_tensors
)
return
M
.
outputs
finally
:
def
post_process_model
(
model
):
added_trainable_names
=
set
([
x
.
name
for
x
in
tf
.
trainable_variables
()])
restore_collection
(
trainable_backup
)
for
v
in
M
.
weights
:
for
v
in
model
.
weights
:
# In Keras, the collection is not respected and could contain non-trainable vars.
# We put M.weights into the collection instead.
if
v
.
name
not
in
old_trainable_names
and
v
.
name
in
added_trainable_names
:
...
...
@@ -87,6 +73,25 @@ class KerasModelCaller(object):
logger
.
warn
(
"Keras created trainable variable '{}' which is actually not trainable. "
"This was automatically corrected by tensorpack."
.
format
(
n
))
if
self
.
cached_model
is
None
:
assert
not
reuse
model
=
self
.
cached_model
=
self
.
get_model
(
*
input_tensors
)
outputs
=
model
.
outputs
elif
reuse
:
# use the cached Keras model to mimic reuse
# NOTE: ctx.is_training won't be useful inside model,
# because inference will always use the cached Keras model
model
=
self
.
cached_model
outputs
=
model
.
call
(
input_tensors
)
else
:
# create new Keras model if not reuse
model
=
self
.
get_model
(
*
input_tensors
)
outputs
=
model
.
outputs
post_process_model
(
model
)
return
outputs
# Keras needs an extra input if learning_phase is used by the model
# This cb will be used by
...
...
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