创建共享的完整连接层时遇到的问题

时间:2018-12-06 02:27:24

标签: tensorflow python-2.x

我有一个FCN来获取三个特征图([batch,6,6,128]),然后我想创建一个共享的完整连接层来映射三个特征图([batch,6,6,128] )到[batch,128],这是我的代码:

def bulit_triplet(self):
    #Buliding the postive, achor, negative

    #self.anchor = tf.nn.l2_normalize(self.templates, 1, 1e-10, name='embeddings')
    #self.postive = tf.nn.l2_normalize(self.templates1, 1, 1e-10, name='embeddings1')
    #self.negative = tf.nn.l2_normalize(self.instance_embeds[:,0:6,0:6,:], 1, 1e-10, name='embeddings2')
    self.anchor = self.templates
    self.positive = self.templates1
    self.negative = self.instance_embeds[:, 0:6, 0:6, :]

    self.anchor_embeds = logit(self.anchor, reuse=False)
    self.positive_embeds = logit(self.positive, reuse=True)
    self.negative_embeds = logit(self.negative, reuse=True) 
def logit(inputs, reuse=None, is_training=True, dropout_keep_prob=0.8, bottleneck_layer_size=128,
  scope='logit_alexnet_rt'):
    end_points_l = {}
    with tf.variable_scope(scope, 'logit_alexnet_rt', [inputs], reuse=reuse):
      with slim.arg_scope([slim.batch_norm, slim.dropout], is_training=is_training):
        with slim.arg_scope([slim.conv2d, slim.max_pool2d, slim.avg_pool2d],stride=1, padding='SAME'):
            net = inputs
            end_points_l['pre'] = net
            net = slim.flatten(net)
            net = slim.dropout(net, dropout_keep_prob, is_training=True, scope='dropout')
            end_points_l['PreLogitsFlatten'] = net
            net = slim.fully_connected(net, 128, activation_fn=None, scope='bottleneck')
            end_points_l['Fc'] = net
    return net, end_points_l

但是它不起作用:

Traceback (most recent call last):
  File "experiments/SiamFC-3s-color-scratch.py", line 29, in <module>
    '--enforce_clean': False,
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/sacred/experiment.py", line 209, in run
    run()
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/sacred/run.py", line 221, in __call__
    self.result = self.main_function(*args)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/sacred/config/captured_function.py", line 46, in captured_function
    result = wrapped(*args, **kwargs)
  File "experiments/../train_siamese_model.py", line 106, in main
    model_va.build(reuse=True)
  File "experiments/../siamese_model.py", line 277, in build
    self.bulit_triplet()
  File "experiments/../siamese_model.py", line 131, in bulit_triplet
    self.anchor_embeds = logit(self.anchor, reuse=False)
  File "experiments/../embeddings/convolutional_alexnet.py", line 271, in logit
    net = slim.fully_connected(net, 128, activation_fn=None, scope='bottleneck')
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
    return func(*args, **current_args)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1639, in fully_connected
    outputs = layer.apply(inputs)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 671, in apply
    return self.__call__(inputs, *args, **kwargs)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 559, in __call__
    self.build(input_shapes[0])
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/layers/core.py", line 137, in build
    trainable=True)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/layers/base.py", line 458, in add_variable
    trainable=trainable and self.trainable)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1203, in get_variable
    constraint=constraint)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 1092, in get_variable
    constraint=constraint)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 417, in get_variable
    return custom_getter(**custom_getter_kwargs)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1539, in layer_variable_getter
    return _model_variable_getter(getter, *args, **kwargs)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/layers/python/layers/layers.py", line 1531, in _model_variable_getter
    custom_getter=getter, use_resource=use_resource)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
    return func(*args, **current_args)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/variables.py", line 262, in model_variable
    use_resource=use_resource)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
    return func(*args, **current_args)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/variables.py", line 217, in variable
    use_resource=use_resource)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 394, in _true_getter
    use_resource=use_resource, constraint=constraint)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/python/ops/variable_scope.py", line 742, in _get_single_variable
    name, "".join(traceback.format_list(tb))))
ValueError: Variable logit_alexnet_rt/bottleneck/weights already exists, disallowed. Did you mean to set reuse=True or reuse=tf.AUTO_REUSE in VarScope? Originally defined at:

  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/variables.py", line 217, in variable
    use_resource=use_resource)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/arg_scope.py", line 181, in func_with_args
    return func(*args, **current_args)
  File "/home/xieyangyang/anaconda3/envs/tensorflow1/lib/python2.7/site-packages/tensorflow/contrib/framework/python/ops/variables.py", line 262, in model_variable
    use_resource=use_resource)

这是怎么回事?我该怎么办,我的tensorflow版本:(1.4.0-gpu),我的python版本是2.7,我的系统是Ubuntu16.04

0 个答案:

没有答案