我正在尝试复制this tutorial,但是使用tf.gradients函数时出现错误。
我的代码与本教程中链接的代码完全相同,除了我使用的是来自tensorflow对象检测API的模型,该模型称为 faster_rcnn_resnet50_coco ,并且我已对笔记本进行了以下修改:
我的t_input张量定义如下:
t_input = tf.placeholder(np.uint8, name='import/image_tensor') # define the input tensor
我在这里假设image_tensor是NN的输入。
和我要查看的图层在这里定义:
layer = 'FirstStageFeatureExtractor/resnet_v1_50/resnet_v1_50/block1/unit_2/bottleneck_v1/conv3/Conv2D'
张量板图形可视化器显示image_tensor像这样: image_tensor node 以及image_tensor的属性,如下所示: properties of image_tensor
以及我正在检查的图层 conv2D
以下是发生我的错误的函数的代码:
def render_naive(t_obj, img0=img_noise, iter_n=20, step=1.0):
print(t_obj, t_input)
t_score = tf.reduce_mean(t_obj) # defining the optimization objective
t_grad = tf.gradients(t_score, t_input)[0] # ERROR HERE
img = img0.copy()
for i in range(iter_n):
g, score = sess.run([t_grad, t_score], {t_input:img})
# normalizing the gradient, so the same step size should work
g /= g.std()+1e-8 # for different layers and networks
img += g*step
print(score, end = ' ')
clear_output()
showarray(visstd(img))
这是错误:
<code>
AttributeError Traceback (most recent call last)
<ipython-input-70-e4cf1a66c81d> in <module>()
37 showarray(visstd(img))
38
---> 39 render_naive(T(layer)[:,:,:,channel])
<ipython-input-70-e4cf1a66c81d> in render_naive(t_obj, img0, iter_n, step)
25 print(t_obj, t_input)
26 t_score = tf.reduce_mean(t_obj) # defining the optimization objective
---> 27 t_grad = tf.gradients(t_score, t_input)[0] # behold the power of automatic differentiation!
28
29 img = img0.copy()
/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gradients_impl.py in gradients(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients)
486 with ops.get_default_graph()._lock: # pylint: disable=protected-access
487 return _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops,
--> 488 gate_gradients, aggregation_method, stop_gradients)
489
490
/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gradients_impl.py in _GradientsHelper(ys, xs, grad_ys, name, colocate_gradients_with_ops, gate_gradients, aggregation_method, stop_gradients)
531 stop_gradient_ops = [t.op for t in stop_gradients]
532 pending_count, loop_state = _PendingCount(
--> 533 ops.get_default_graph(), to_ops, from_ops, colocate_gradients_with_ops)
534
535 # Iterate over the collected ops.
/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/gradients_impl.py in _PendingCount(graph, to_ops, from_ops, colocate_gradients_with_ops)
193 # 'loop_state' is None if there are no while loops.
194 loop_state = control_flow_ops.MaybeCreateControlFlowState(
--> 195 between_op_list, between_ops, colocate_gradients_with_ops)
196
197 # Initialize pending count for between ops.
/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/control_flow_ops.py in MaybeCreateControlFlowState(between_op_list, between_ops, colocate_gradients_with_ops)
1451 loop_state.AddWhileContext(op, between_op_list, between_ops)
1452 else:
-> 1453 loop_state.AddWhileContext(op, between_op_list, between_ops)
1454 return loop_state
1455
/usr/local/lib/python3.5/dist-packages/tensorflow/python/ops/control_flow_ops.py in AddWhileContext(self, op, between_op_list, between_ops)
1257 if grad_state is None:
1258 # This is a new while loop so create a grad state for it.
-> 1259 outer_forward_ctxt = forward_ctxt.outer_context
1260 if outer_forward_ctxt:
1261 outer_forward_ctxt = outer_forward_ctxt.GetWhileContext()
AttributeError: 'NoneType' object has no attribute 'outer_context'
我对该错误的含义或解决方法一无所知。
原始代码和我的代码之间的显着差异:
t_input的原始类型为float32,并且没有形状(shape = {})。我的t_input具有uint8类型,并且形状为[-1,-1,-1、3]
两个代码中的conv2D尺寸完全相同。
这里可能是什么问题?我觉得我已经尝试了一切。