python- tf.write_file does ot work in tensorflow

Viewed 1624

I have a code that train an object coordinate for object detection. I used CNN network and the output layer is regression layer (called bound_box_output) that return (x0,y0, height, width) for an object in an image. After this layer I try to save the image directly before loss step.

   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)

and in tf.train.SessionRunHook I run it

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)

the problem is that it does not save the '4.jpeg' image in a specific folder

Note: I use tensorflow 1.1.3 and python3.5

1 Answers

TLDR; Substitute tf.train.SessionRunArgs(fwrite) with run_context.session.run(fwrite).

SessionRunArgs doesn't actually run the supplied operation. SessionRunArgs are returned from the before_run() call. Their role is to add arguments to the next session.run() call.

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

You are attempting to run the fwrite operation in the end of after_run(). However, it merely instantiates the SessionRunArgs object.

One option to achieve the desired behavior is to take advantage of the run_context argument supplied to after_run(). run_context is of type SessionRunContext, the type that contains a session reference.

run_context.session.run(fwrite) should do the trick for you.

Related