Created
July 11, 2018 13:29
-
-
Save Sergio0694/aa36c7ed94091ce5503ad908b142aaf0 to your computer and use it in GitHub Desktop.
Reproduces an error when loading multiple shared instances of a Keras model in the same scope
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tensorflow as tf | |
''' | |
This snippet reproduces an error when trying to load multiple shared instances | |
of a Keras model in TensorFlow, when accessing the same scope multiple times. | |
Creating multiple instances repeatedly in a single step works. | |
''' | |
def load_vgg19_multiple(tensors): | |
'''Creates n shared instances of VGG19, in a single run.''' | |
with tf.variable_scope('VGG19_OK', None, [tensors], reuse=tf.AUTO_REUSE): | |
return [ | |
tf.contrib.keras.applications.VGG19(weights='imagenet', include_top=False, input_tensor=x).layers[-1] | |
for x in tensors | |
] | |
def load_vgg19(x): | |
'''Creates a single shared instance of VGG19.''' | |
with tf.variable_scope('VGG19_FAIL', None, [x], reuse=tf.AUTO_REUSE): | |
m = tf.contrib.keras.applications.VGG19(weights='imagenet', include_top=False, input_tensor=x) | |
return m.layers[-1] | |
t = tf.placeholder(tf.float32, [12, 128, 128, 3]) # dummy | |
vgg_multiple = load_vgg19_multiple([t, t, t, t, t]) | |
print('Multiple OK') | |
vgg1 = load_vgg19(t) | |
vgg2 = load_vgg19(t) # fails here: ValueError: You are trying to load a weight file containing 16 layers into a model with 0 layers | |
print('Single OK') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment