【问题标题】:python- tf.write_file does ot work in tensorflowpython- tf.write_file 在张量流中不起作用
【发布时间】:2017-10-03 11:58:16
【问题描述】:

我有一个代码可以训练对象坐标以进行对象检测。 我使用了 CNN 网络,输出层是回归层(称为 bound_box_output),它返回图像中对象的 (x0,y0, height, width)。在这一层之后,我尝试在损失步骤之前直接保存图像。

   i = 0
   image_decoded = tf.image.decode_jpeg(tf.read_file('3.jpg'), channels=3)
   cropped = tf.image.crop_to_bounding_box(image = image_decoded,
                                          offset_height = tf.cast(bound_box_output[i,0], tf.int32),
                                          offset_width = tf.cast(bound_box_output[i,1], tf.int32),
                                          target_height = tf.cast(bound_box_output[i,2], tf.int32),
                                          target_width = tf.cast(bound_box_output[i,3], tf.int32))

   enc = tf.image.encode_jpeg(cropped)
   fname = tf.constant('4.jpeg')
   fwrite = tf.write_file(fname, enc)

tf.train.SessionRunHook 我运行它

def begin(self):
        self._step = -1
        self._start_time = time.time()

def before_run(self, run_context):
        self._step += 1
        return tf.train.SessionRunArgs(loss)
def after_run(self, run_context, run_values):
        if self._step % LOG_FREQUENCY == 0:
          current_time = time.time()
          duration = current_time - self._start_time
          self._start_time = current_time

          loss_value = run_values.results
          examples_per_sec = LOG_FREQUENCY * BATCH_SIZE / duration
          sec_per_batch = float(duration / LOG_FREQUENCY)


          format_str = ('%s: step %d, loss = %.2f (%.1f examples/sec; %.3f '
                        'sec/batch)')
          print (format_str % (datetime.now(), self._step, loss_value,
                               examples_per_sec, sec_per_batch))

        if self._step  == MAX_STEPS-1:
          loss_value = run_values.results
          print("The final value of loss is:: ")
          print(loss_value)
          print(fwrite)
          tf.train.SessionRunArgs(fwrite)

问题是它没有将“4.jpeg”图像保存在特定文件夹中

注意:我使用 tensorflow 1.1.3python3.5

【问题讨论】:

  • 程序是否打印了最后的打印语句(“损失的最终值是::”等)?
  • 是的,这是训练的最后一步
  • 好的,很高兴知道这一点。代码似乎是正确的,所以我不确定出了什么问题。您是否尝试过明确说明输出文件的路径? (例如,将/tmp/4.jpeg 用作fname。只是为了确保文件编写器实际上正在保存某些内容。
  • 是的,我这样做了,但没有保存图像

标签: python-3.x tensorflow conv-neural-network


【解决方案1】:

TLDR;将tf.train.SessionRunArgs(fwrite) 替换为run_context.session.run(fwrite)

SessionRunArgs 实际上并没有运行提供的操作。 SessionRunArgsbefore_run() 调用返回。它们的作用是为下一个session.run() 调用添加参数。

if self._step  == MAX_STEPS-1:
  loss_value = run_values.results
  print("The final value of loss is:: ")
  print(loss_value)
  print(fwrite)
  tf.train.SessionRunArgs(fwrite)  # problematic line

您正试图在after_run() 的末尾运行fwrite 操作。但是,它只是实例化了SessionRunArgs 对象。

实现所需行为的一个选项是利用提供给after_run()run_context 参数。 run_context 属于 SessionRunContext 类型,该类型包含 session 引用。

run_context.session.run(fwrite) 应该为您解决问题。

【讨论】:

    猜你喜欢
    • 2021-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-11-20
    • 1970-01-01
    • 2017-10-14
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多