Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

The output problem of sharing weights. #28

Closed
LynnHo opened this issue Oct 14, 2018 · 4 comments
Closed

The output problem of sharing weights. #28

LynnHo opened this issue Oct 14, 2018 · 4 comments

Comments

@LynnHo
Copy link

LynnHo commented Oct 14, 2018

I use the code like below,

...
resnet = partial(tensornets.ResNet50, reuse=tf.AUTO_REUSE)
outputs_1 = resnet(x_1).get_outputs()
outputs_2 = resnet(x_2).get_outputs() 
...

outputs_2 appends the outputs of x_2 to outputs_1.

@LynnHo
Copy link
Author

LynnHo commented Oct 25, 2018

@taehoonlee Do you have any idea? Is it a bug?

@taehoonlee
Copy link
Owner

@LynnHo, Sorry for the late reply. This is because the two models have the same prefix for the output tensors. Currently, the following is one of the workarounds:

import tensorflow as tf
import tensornets as nets
from functools import partial

x_1 = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x1')
x_2 = tf.placeholder(tf.float32, [None, 224, 224, 3], name='x2')

resnet = partial(nets.ResNet50, scope='resnet50', reuse=tf.AUTO_REUSE)
model1 = resnet(x_1)
with tf.name_scope('xx'):
    model2 = resnet(x_2)

assert len(model1.get_outputs()) == 161
assert len(model2.get_outputs()) == 161
assert len(model1.get_weights()) == 320
assert len(model2.get_weights()) == 320

If I find the elegant methods or make weight sharing more seamless, I'll let you know. Thank you for the reports!

@taehoonlee
Copy link
Owner

The recent updates address the issue you reported. Currently, I recommend the following pattern:

with tf.name_scope('clone0'):
    model1 = nets.ResNet50(x1, reuse=tf.AUTO_REUSE)
with tf.name_scope('clone1'):
    model2 = nets.ResNet50(x2, reuse=tf.AUTO_REUSE)

These are the materials you might be interested in:

  1. Fix: ae8e431
  2. Notes: Notes for variable_scope, name_scope, and weight sharing #43
  3. Test codes: b1c2533

If you want to use the latest version, you can try pip install -U tensornets. Please feel free to open it again at any time if you have additional comments :)

@LynnHo
Copy link
Author

LynnHo commented Mar 4, 2019

@taehoonlee Thanks for your effort! Tensornets really helps in my works!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants