python - 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 - 因为代码套件是围绕它构建的。提前致谢。
解决方案
推荐阅读
- javascript - For循环不迭代更正
- c - 为什么我应该使用归约而不是原子变量?
- php - 加入同一个表和列两次
- wpf - 组合框中每个项目的工具提示
- r - 在 R 中使用 Seq 函数
- python - Discord bot 无法使用 Python 在 Heroku 上运行
- ssl - 为什么浏览器有 3 级 SSL 证书?
- javascript - 用于从样式中删除高度/宽度的 Javascript 正则表达式
- python - 尝试使用 Python 3.7.2 和 IMAPClient 批量删除电子邮件 - imaplib.IMAP4.error: UID command error: BAD [b'Command line too large']
- python - LSTM ValueError:无法为测试数据重塑大小数组