You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
TensorNets provides a seamless integration with regular TensorFlow APIs. You can define any models under tf.variable_scope and tf.name_scope to couple the model with your established scripts. This document shows basic examples for tf.variable_scope, tf.name_scope, and weight sharing. First, import two libraries:
importtensorflowastfimporttensornetsasnets
Let's get started with basic TensorFlow APIs. You can manage a prefix of variable names with tf.variable_scope and tf.name_scope. The difference is that tf.Variable will be affected by only tf.name_scope, while tf.Tensor by both tf.variable_scope and tf.name_scope. Also, the second tf.get_variable('w', [1]) will try to create the same variable if tf.variable_scope(reuse=None), or return the pointer of the existing variable otherwise (reuse=True, reuse=tf.AUTO_REUSE). Here is an example:
withtf.name_scope('foo'):
withtf.variable_scope('goo'):
withtf.name_scope('hoo'):
# `tf.Variable` will be affected by only `tf.name_scope`.w=tf.get_variable('w', [1])
assertw.name=='goo/w:0'# `tf.Tensor` will be affected by both `tf.variable_scope` and `tf.name_scope`.s=tf.constant(-1.0)
y=s*wasserts.name=='foo/goo/hoo/Const:0'asserty.name=='foo/goo/hoo/mul:0'# `tf.get_variable` will try to create the same variable again# if `tf.variable_scope(reuse=None)` (default).try:
w2=tf.get_variable('w', [1])
exceptValueErrorase:
print(e) # Variable goo/w already exists, disallowed.
The principle is easily extended to TensorNets. The weights returned by get_weights are tf.Variable, and the outputs from get_outputs and get_middles are tf.Tensor. Thus, the weights will be affected by only tf.name_scope, while the outputs and the middles by both tf.variable_scope and tf.name_scope. Surely, the model function call can't be performed without reuse=True or reuse=tf.AUTO_REUSE because the function will try to create the same variable again.
withtf.name_scope('xoo'):
withtf.variable_scope('yoo'):
withtf.name_scope('zoo'):
# The weights returned by `get_weights` are `tf.Variable`,# and the outputs from `get_outputs` and `get_middles` are `tf.Tensor`x1=tf.placeholder(tf.float32, [None, 224, 224, 3], name='x1')
model1=nets.ResNet50(x1)
# `tf.Variable` will be affected by only `tf.name_scope`.assertmodel1.get_weights()[-1].name=='yoo/resnet50/logits/biases:0'# `tf.Tensor` will be affected by both `tf.variable_scope` and `tf.name_scope`.assertmodel1.name=='xoo/yoo/zoo/resnet50/probs:0'assertmodel1.get_outputs()[-1].name=='xoo/yoo/zoo/resnet50/probs:0'assertmodel1.get_middles()[-1].name=='xoo/yoo/zoo/resnet50/conv5/block3/out:0'# `tf.get_variable` will try to create the same variable again# if `tf.variable_scope(reuse=None)` (default).try:
x2=tf.placeholder(tf.float32, [None, 224, 224, 3], name='x2')
model2=nets.ResNet50(x2)
exceptValueErrorase:
print(e) # Variable yoo/resnet50/conv1/conv/weights already exists, disallowed.
And we can easily implement the concept of weight sharing by using tf.variable_scope(reuse=tf.AUTO_REUSE). An example is as follows:
And I recommend the following pattern used in deploying multiple clones in tf.slim:
withtf.name_scope('clone0'):
model1=nets.ResNet50(x1, reuse=tf.AUTO_REUSE)
withtf.name_scope('clone1'):
model2=nets.ResNet50(x2, reuse=tf.AUTO_REUSE)
for (a, b) inzip(model1.get_weights(), model2.get_weights()):
asserta==bassertmodel1.name=='clone0/resnet50/probs:0'assertmodel2.name=='clone1/resnet50/probs:0'
Without tf.name_scope, tf.Tensor will be automatically named with a postfix-style (resnet50, resnet50_1, ...). I think that it may be difficult to manage tensor names in such cases.
The text was updated successfully, but these errors were encountered:
TensorNets provides a seamless integration with regular TensorFlow APIs. You can define any models under
tf.variable_scope
andtf.name_scope
to couple the model with your established scripts. This document shows basic examples fortf.variable_scope
,tf.name_scope
, and weight sharing. First, import two libraries:Let's get started with basic TensorFlow APIs. You can manage a prefix of variable names with
tf.variable_scope
andtf.name_scope
. The difference is thattf.Variable
will be affected by onlytf.name_scope
, whiletf.Tensor
by bothtf.variable_scope
andtf.name_scope
. Also, the secondtf.get_variable('w', [1])
will try to create the same variable iftf.variable_scope(reuse=None)
, or return the pointer of the existing variable otherwise (reuse=True
,reuse=tf.AUTO_REUSE
). Here is an example:The principle is easily extended to TensorNets. The weights returned by
get_weights
aretf.Variable
, and the outputs fromget_outputs
andget_middles
aretf.Tensor
. Thus, the weights will be affected by onlytf.name_scope
, while the outputs and the middles by bothtf.variable_scope
andtf.name_scope
. Surely, the model function call can't be performed withoutreuse=True
orreuse=tf.AUTO_REUSE
because the function will try to create the same variable again.And we can easily implement the concept of weight sharing by using
tf.variable_scope(reuse=tf.AUTO_REUSE)
. An example is as follows:TensorNets can be also easily integrated with
tf.variable_scope
:Summary
I'd like to say that there are two patterns to implement weight sharing:
tf.variable_scope
:variable_scope
:And I recommend the following pattern used in deploying multiple clones in
tf.slim
:Without
tf.name_scope
,tf.Tensor
will be automatically named with a postfix-style (resnet50
,resnet50_1
, ...). I think that it may be difficult to manage tensor names in such cases.The text was updated successfully, but these errors were encountered: