python - TF : how to solve ValueError: Variable . ..重量已经存在,不允许。您是要设置 reuse=True

标签 python tensorflow

我构建了一个反向组合 CNN,但它报告错误如下:

ValueError: Variable left_src_tgt_warp/ICSTN/icnv1/weight already exists, disallowed. Did you mean to set reuse=True in VarScope? Originally defined at:

我发现使用tf.reset_default_graph() 可以解决这个问题。但是我不知道应该在哪里添加它。

for l in range(opt.warpN):
    with tf.variable_scope("ICSTN", reuse=l > 0) as sc:
    end_points_collection = sc.original_name_scope + '_end_points'
    with slim.arg_scope([slim.conv2d, slim.conv2d_transpose],
                       normalizer_fn=slim.batch_norm,
                      weights_regularizer=slim.l2_regularizer(0.05),
                            normalizer_params=batch_norm_params,
                            activation_fn=tf.nn.relu,
                          outputs_collections=end_points_collection):
            imageWarp = inverse_warp(
                inputImage,
                depth,
                pM,
                intrinsics,
                intrinsics_inv)
            imageWarpAll.append(imageWarp)
            feat = tf.reshape(imageWarp, [batch_size, H, W, C])
            print('feat shape:', feat.get_shape())
            print('pM_ini:', pM.get_shape())
            with tf.variable_scope("icnv1"):
                feat = conv2Layer(opt, feat, 4)
                feat = tf.nn.relu(feat)
            with tf.variable_scope("icnv2"):
                feat = conv2Layer(opt, feat, 8)
                feat = tf.nn.relu(feat)
                feat = tf.nn.max_pool(feat, ksize=[1, 2, 2, 1], strides=[1, 2, 2, 1], padding="VALID")
            feat = tf.reshape(feat, [opt.batch_size, -1])
            with tf.variable_scope("fc3"):
                feat = linearLayer(opt, feat, 48)
                feat = tf.nn.relu(feat)
            with tf.variable_scope("fc4"):
                feat = linearLayer(opt, feat, 6, final=True)
            dp = tf.reshape(feat, [-1, 6])
            print('dp: ', dp.get_shape())
        dpM = pose_vec2mat(dp)
        pM = tf.matmul(dpM, pM)
    imageWarp = inverse_warp(
        inputImage,
        depth,
        pM,
        intrinsics,
        intrinsics_inv)
    imageWarpAll.append(imageWarp)
    return imageWarpAll, pM
def build_train_graph():
    with tf.name_scope("cnn1"):...
    with tf.name_scope("cnn2"):...
    with tf.name_scope("Inverse Compositional CNN"):...
def train(self, opt):
    with tf.variable_scope(tf.get_variable_scope()):
            for i in range(opt.num_gpus):
                print('gpu:', i)
                with tf.device('/gpu:%d' % i):
                    self.build_train_graph(L_img_splits[i], R_img_splits[i], L_cam2pix_splits[i], L_pix2cam_splits[i],
                                       R_cam2pix_splits[i], R_pix2cam_splits[i], L_sca_splits[i], R_sca_splits[i],
                                       reuse_variables)
                    self.collect_summaries(i)
                    tower_losses.append(self.total_loss)
                    reuse_variables = True
                    grads = opt_step.compute_gradients(self.total_loss)
                    tower_grads.append(grads)
        grads = average_gradients(tower_grads)
        apply_gradient_op = opt_step.apply_gr`enter code here`adients(grads, global_step=global_step)
        incr_global_step = tf.assign(global_step, global_step + 1)
        total_loss = tf.reduce_mean(tower_losses)

        tf.summary.scalar('learning_rate', learning_rate, ['model_0'])
        tf.summary.scalar('total_loss', total_loss, ['model_0'])
        summary_op = tf.summary.merge_all('model_0')
        # self.collect_summaries()
        # SESSION
        config = tf.ConfigProto(allow_soft_placement=True)
        config.gpu_options.allow_growth = True
        sess = tf.Session(config=config)

        # SAVER
        summary_writer = tf.summary.FileWriter(
            opt.checkpoint_dir + '/s%.1d_%.3d/' % (opt.seq_length, opt.img_height) + opt.model_name, sess.graph)
        self.saver = tf.train.Saver()
        # COUNT PARAM
        total_num_parameters = 0
        for variable in tf.trainable_variables():
            total_num_parameters += np.array(variable.get_shape().as_list()).prod()
         print("number of trainable parameters: {}".format(total_num_parameters))
        # INIT
        sess.run(tf.global_variables_initializer())
        sess.run(tf.local_variables_initializer())
        coordinator = tf.train.Coordinator()
        threads = tf.train.start_queue_runners(sess=sess, coord=coordinator)
        # LOAD CHECKPOINT IF SET
        if opt.continue_train:
             print("Resume training from previous checkpoint")
             checkpoint = tf.train.latest_checkpoint(
                 os.path.join(opt.checkpoint_dir, 's%.1d_%.3d' % (opt.seq_length, opt.img_height), opt.model_name))
             self.saver.restore(sess, checkpoint)
        if opt.re_train:
            sess.run(global_step.assign(0))

最佳答案

这是因为代码第一部分的 for 循环可能缺少函数名。

循环尝试创建 left_src_tgt_warp/ICSTN/icnv1/weight(与 icnv2 等相同):

def foo(num_layers):
    opt = tf.placeholder(tf.float32, [None, 64])
    for i in range(num_layers):
        with tf.variable_scope("icnv1"):
            feat = tf.layers.dense(opt, units=1, activation=tf.nn.relu)

foo(5)
ValueError: Variable icnv1/dense/kernel already exists, disallowed. Did you mean to set reuse=True

您需要为变量命名。实现这一目标的一种方法是这样的:

def foo(num_layers):
    opt = tf.placeholder(tf.float32, [None, 64])
    for i in range(num_layers):
        with tf.variable_scope("icnv1_layer_{}".format(i)):
            feat = tf.layers.dense(opt, units=1, activation=tf.nn.relu)

我们现在为每一个都有不同的名称,icnv1_layer_1icnv1_layer_2 等。具体取决于深度。

当然,除非您想要共享权重(例如,它是同一层,更新为一个)。在那种情况下只需设置:

with tf.variable_scope("icnv1", reuse=tf.AUTO_REUSE):

关于python - TF : how to solve ValueError: Variable . ..重量已经存在,不允许。您是要设置 reuse=True,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50210785/

相关文章:

python - 将数字和字母的字符串转换为 pandas 数据框中的 int/float

python - 如何使输出图像的大小与原始图像的大小相同以计算CNN中的损失?

python - 抓取多个帐户,也就是多次登录

python - 使用 getattr [ python ] 在实例上调用方法

python - 在 GAE (GCP) 上的 cron.yaml 中编写工作日日常工作脚本

python - 与重复索引合并 - 行数大于预期

python - Keras:在不同的模型中使用相同的层(共享权重)

tensorflow - 模块 'keras.backend' 没有属性 'tensorflow_backend'

python - 为什么对 TensorFlow 函数的输入数据使用占位符

Tensorflow - 数据集 API 中的字符串处理