0

我有一个使用 Tensorflow 对象检测 API 获得的冻结图。它是在 Tensorflow 1.X 上训练的 mobilnet SSD 模型的图表。

我想在此图上使用快速梯度符号法生成对抗性示例。冻结图的输入数据类型是 uint8 但由于梯度磁带无法观看 int 我将输入节点的数据类型更改为浮点数。我是新手,不确定我是否做的一切正确,我正在按照以下步骤操作,

  1. 在 TF2 中加载冻结图

  2. 将输入节点的数据类型更改为浮动,如下所示

    for node in graph_def.node:
     if 'image' in node.name:            
         node.attr["dtype"].type = 1 
    
  3. 将冻结图包装到 ConcreteFunctions

  4. 创建一个梯度磁带,让梯度磁带在使用冻结图进行推理时观看输入图像

    with tf.GradientTape(persistent=True) as tape:
         tape.watch(image)
         boxes, scores, classes, num_detections = 
                          frozen_func(image_tensor=image) 
    

有了这个,我得到了错误“AttributeError:'NoneType'对象没有属性'outer_context'”这将出现在下面的tensorflow函数中的节点'Postprocessor/BatchMultiClassNonMaxSuppression/map/while/Exit_2'

def AddWhileContext(self, op, between_op_list, between_ops):
    """Add the grad state for the while loop that op belongs to.
    
    Note that op is an Exit, and this method must be called in
    the control flow context where gradients() is called.
    
    Note that this method modifies `between_op_list` and `between_ops`.
    """
    forward_ctxt = util.GetWhileContext(op) # forward_ctxt is coming as None
    grad_state = self._map.get(forward_ctxt)
    if grad_state is None:
      # This is a new while loop so create a grad state for it.
      outer_forward_ctxt = forward_ctxt.outer_context 
      if outer_forward_ctxt:
        outer_forward_ctxt = outer_forward_ctxt.GetWhileContext()
      outer_grad_state = None
      if outer_forward_ctxt:
        outer_grad_state = self._map.get(outer_forward_ctxt)
      grad_state = _GradLoopState(forward_ctxt, outer_grad_state)
      self._map[forward_ctxt] = grad_state
    
      # We need to include all exits of a loop for backprop.
      for loop_exit in grad_state.forward_loop_exits:
        if loop_exit.op not in between_ops:
          between_ops.add(loop_exit.op)
          between_op_list.append(loop_exit.op)

此操作所属的 while 上下文报告为 None。冻结图工作正常,如果我只是做简单的推理并要求梯度磁带不看任何东西,那么我会从图中得到输出。如果我在这里做错了什么,请帮助我。

我的最终目标是计算冻结图的输出与地面实况之间的损失,然后计算损失相对于实现 FGSM 所需的输入图像的梯度。

4

0 回答 0