tensorflow / models

Models and examples built with TensorFlow
Other
77.24k stars 45.75k forks source link

how to reuse Variables if I am adding node to the graph of deeplab? #8897

Open zheyuanWang opened 4 years ago

zheyuanWang commented 4 years ago

Also regarding the modification: since you are creating a the first layer (which outputs feature_2) under different variable scope, my bet is it would not be shared with the layer that creates feature_1 ;)

Originally posted by @YknZhu in https://github.com/tensorflow/models/issues/8864#issuecomment-659563101


Hi,

I want to reuse some layers of the pre-defined mobilenet-v3 backbone, so I made some modification in research/slim/nets/mobilenet/mobilenet.py

basically like this:

with tf.variable_scope('parallel_layer',reuse=tf.compat.v1.AUTO_REUSE) as scope:
   try:
        net1 = opdef.op(net1, **params)
        net2 = opdef.op(net2, **params)
   except Exception:
        print('Failed to create op %i: %r params: %r' % (i, opdef, params))

Ps. I also tried without the extra variable_scope. I.e. "MobilenetV3" instead of "MobilenetV3/parallel_layer".

According to the graph in tensorboard, I do have two nodes _(Conv & Conv1) instead of only one node (Conv) now. I can't load pre-trained checkpoint anyway since the graph was changed.

How to reuse the layers correctly? And how to know weather I am correct?

zheyuanWang commented 4 years ago

for example, how could I reuse the whole feature extractor of mobilenet_v3 for 2 inputs? Without reusing the variables, the Network would be too big to fit in my GPU.

I want to combine the extracted feature maps afterwards, before they are sent to decoder.