首页 > 解决方案 > Tensorflow - 无法获得损失和输入图像之间的梯度

问题描述

我正在尝试为语义分割分类器生成对抗性示例,其中涉及使用损失相对于输入图像变量的梯度优化图像(其中损失在当前和目标网络输出之间)。但是,无论我尝试了什么,我似乎都无法以允许计算这些梯度的方式创建图形。我需要确保为图像的每次迭代计算的网络输出不会与损失断开连接。这是代码。我没有完全包括所有内容,因为它会长得像噩梦一样。模型构建器是我正在尝试适应的代码套件中的一种方法。我敢肯定,这一定是我的某种微不足道的误解。

    #From elsewhere - x is the processed input image and yg is calculated using argmin on the output 
    #of a previous run through the network.
    x = self.xclean
    self.get_ygoal()
    yg = self.ygoal
    yg = tf.convert_to_tensor(yg)

    tf.reset_default_graph()
    config = tf.ConfigProto()
    config.gpu_options.allow_growth = True
    with tf.Session(config=config) as sess:

        #sess.run(tf.global_variables_initializer())
        net_input = tf.placeholder(tf.float32,shape=[None,None,None,3])
        net_output = tf.placeholder(tf.float32,shape=[None,None,None,self.num_classes]) 

        network, _ = model_builder.build_model(self.model, net_input=net_input,
                                            num_classes=self.num_classes,
                                            crop_width=self.dims[0],
                                            crop_height=self.dims[1],
                                            is_training=True)
        print('Loading model checkpoint weights')
        checkpoint_path = 'checkpoints/latest_model_'+self.model+'_'+self.dataset+'.ckpt'
        saver=tf.train.Saver(max_to_keep=1000)
        saver.restore(sess, checkpoint_path)


        img = tf.Variable(tf.zeros(shape=(1,self.dims[0], self.dims[1], 3)),name='img')
        assign = tf.assign(img,net_input)
        learning_rate = tf.constant(lr,dtype=tf.float32)

        loss = tf.nn.softmax_cross_entropy_with_logits_v2(logits=network, labels=net_output)
        optim_step = tf.compat.v1.train.GradientDescentOptimizer(learning_rate).minimize(loss, var_list=[img])
        epsilon_ph = tf.placeholder(tf.float32, ())
        below = net_input - epsilon_ph
        above = net_input + epsilon_ph
        projected = tf.clip_by_value(tf.clip_by_value(img, below, above), 0, 1)
        with tf.control_dependencies([projected]):
            project_step = tf.assign(img, projected)


        sess.run(assign, feed_dict={net_input: x})

        for i in range(steps):      
            print('Starting...')    
            # gradient descent step
            _, loss_value = sess.run([optim_step], feed_dict={net_input:x,net_output:yg})

            # project step
            sess.run(project_step, feed_dict={net_input: x, epsilon_ph: epsilon})

            if (i+1) % 10 == 0:
                print('step %d, loss=%g' % (i+1, loss_value))

        adv = img.eval() # retrieve the adversarial example

这是我收到的错误消息:

ValueError: No gradients provided for any variable, check your graph for ops that do not support gradients, between variables ["<tf.Variable 'img:0' shape=(1, 512, 512, 3) dtype=float32_ref>"] and loss Tensor("softmax_cross_entropy_with_logits/Reshape_2:0", shape=(?, ?, ?), dtype=float32).

我应该提到这是使用 Tensorflow 1.14 - 因为代码套件是围绕它构建的。提前致谢。

标签: pythontensorflowgradientloss

解决方案


推荐阅读