Tensorbboard write picture

Keywords: Session OpenCV

The default channel for opencv to read pictures is bgr, which is not a common rgb. A conversion is needed to display the pictures normally. Otherwise, there will be color confusion!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

 

Write picture seems to be asynchronous operation

You need to wait for a while to write in. If you don't set the wait, you will find that the log file is much smaller....... Unable to display picture

import tensorflow as tf
import cv2 as cv
import time
import numpy as np

img = cv.imread('fish.jpg')
# img = cv.imread('test.jpg')
# img = cv.imread('img.jpg')

img = cv.cvtColor(img, cv.COLOR_BGR2RGB)
h, w, c = img.shape
print(h, w, c)
hsv = cv.cvtColor(img, cv.COLOR_BGR2HSV)
img = img.reshape([-1, h, w, c])
hsv = hsv.reshape([-1, h, w, c])
# Generate final image
img_cat = tf.cast(
    tf.concat((img, hsv), axis=2), tf.uint8
)
img_summary_op = tf.summary.image('source/test', img_cat)
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    summary_writer = tf.summary.FileWriter('logs/', sess.graph)
    img_summary = sess.run(img_summary_op)
    summary_writer.add_summary(img_summary)
    # time.sleep(1)

 

 

Image processing using tf

family prefix

 

 

Convert the image path array to a queue and read it from the queue

Note that it must be executed in the try, otherwise an error is reported.....................

                new_img = img_data.reshape([-1, *img_data.shape])

The read data is in the form of [h,w,c]. It must be converted into a 4-D tensor, otherwise an error is reported

Each time a write is made, the method created at the time of writing can be used to specify a name at the time of writing

                img_summary = sess.run(
                    tf.summary.image(
                        img_name.decode(),
                        new_img,
                    )
                )
                summary_writer.add_summary(img_summary)

 

import tensorflow as tf

with tf.Session() as sess:
    filename = ['a.jpg', 'b.jpg', 'c.jpg', 'test.jpg']
    filename_queue = tf.train.string_input_producer(filename,
                                                    shuffle=False,
                                                    num_epochs=1)

    reader = tf.WholeFileReader()
    key, value = reader.read(filename_queue)
    tf_img = tf.image.decode_jpeg(value, channels=3)

    tf.local_variables_initializer().run()
    img_hold = tf.placeholder(tf.uint8)
    img_summary_op = tf.summary.image('test/', img_hold)
    summary_writer = tf.summary.FileWriter('logs/', sess.graph)

    # Create a coordinator, launch the queue runner threads.
    coord = tf.train.Coordinator()
    threads = tf.train.start_queue_runners(sess=sess, coord=coord, start=True)
    try:
        while not coord.should_stop():
            while True:
                img_name, img_data = sess.run([key, tf_img])
                print(img_name, img_data.shape)

                new_img = img_data.reshape([-1, *img_data.shape])
                print(new_img.shape)

                img_summary = sess.run(
                    tf.summary.image(
                        img_name.decode(),
                        new_img,
                        # family="family_" + img_name.decode()
                    )
                )
                summary_writer.add_summary(img_summary)
    except tf.errors.OutOfRangeError:
        # When done, ask the threads to stop.
        print('Done training -- epoch limit reached')
    finally:
        coord.request_stop()
        # Wait for threads to finish.
    coord.join(threads)

 

Start to view

tensorboard.exe  --logdir=logs

Posted by Wolfsoap on Sun, 05 Jan 2020 18:14:16 -0800