[英]Displaying images on Tensorboard (through Keras)
我的X_test
是 128x128x3 的圖像,我的Y_test
是 512x512x3 的圖像。 我想在每個 epoch 之后展示輸入 (X_test) 的樣子,預期輸出 (Y_test) 的樣子,以及實際輸出的樣子。 到目前為止,我只知道如何在 Tensorboard 中添加前 2 個。 這是調用回調的代碼:
model.fit(X_train,
Y_train,
epochs=epochs,
verbose=2,
shuffle=False,
validation_data=(X_test, Y_test),
batch_size=batch_size,
callbacks=get_callbacks())
這是回調的代碼:
import tensorflow as tf
from keras.callbacks import Callback
from keras.callbacks import TensorBoard
import io
from PIL import Image
from constants import batch_size
def get_callbacks():
tbCallBack = TensorBoard(log_dir='./logs',
histogram_freq=1,
write_graph=True,
write_images=True,
write_grads=True,
batch_size=batch_size)
tbi_callback = TensorBoardImage('Image test')
return [tbCallBack, tbi_callback]
def make_image(tensor):
"""
Convert an numpy representation image to Image protobuf.
Copied from https://github.com/lanpa/tensorboard-pytorch/
"""
height, width, channel = tensor.shape
print(tensor)
image = Image.fromarray(tensor.astype('uint8')) # TODO: maybe float ?
output = io.BytesIO()
image.save(output, format='JPEG')
image_string = output.getvalue()
output.close()
return tf.Summary.Image(height=height,
width=width,
colorspace=channel,
encoded_image_string=image_string)
class TensorBoardImage(Callback):
def __init__(self, tag):
super().__init__()
self.tag = tag
def on_epoch_end(self, epoch, logs={}):
# Load image
img_input = self.validation_data[0][0] # X_train
img_valid = self.validation_data[1][0] # Y_train
print(self.validation_data[0].shape) # (8, 128, 128, 3)
print(self.validation_data[1].shape) # (8, 512, 512, 3)
image = make_image(img_input)
summary = tf.Summary(value=[tf.Summary.Value(tag=self.tag, image=image)])
writer = tf.summary.FileWriter('./logs')
writer.add_summary(summary, epoch)
writer.close()
image = make_image(img_valid)
summary = tf.Summary(value=[tf.Summary.Value(tag=self.tag, image=image)])
writer = tf.summary.FileWriter('./logs')
writer.add_summary(summary, epoch)
writer.close()
return
我想知道在哪里/如何獲得網絡的實際輸出。
我遇到的另一個問題是,這里有一個被移植到 TensorBoard 的圖像示例:
[[[0.10909907 0.09341043 0.08224604]
[0.11599099 0.09922747 0.09138277]
[0.15596421 0.13087936 0.11472746]
...
[0.87589591 0.72773653 0.69428956]
[0.87006552 0.7218123 0.68836991]
[0.87054225 0.72794635 0.6967475 ]]
...
[[0.26142332 0.16216267 0.10314116]
[0.31526875 0.18743924 0.12351286]
[0.5499796 0.35461449 0.24772873]
...
[0.80937942 0.62956016 0.53784871]
[0.80906054 0.62843601 0.5368183 ]
[0.81046278 0.62453899 0.53849678]]]
這就是為什么我的image = Image.fromarray(tensor.astype('uint8'))
行可能會生成看起來與實際輸出完全不同的圖像的原因嗎? 這是來自 TensorBoard 的示例:
我確實嘗試過.astype('float64')
但它啟動了一個錯誤,因為它顯然不是受支持的類型。
無論如何,我不確定這真的是問題所在,因為我在 TensorBoard 中顯示的其余圖像都只是白色/灰色/黑色方塊(就在那里, conv2D_7
,實際上是我網絡的最后一層,因此應該顯示輸出的實際圖像,不是嗎?):
最終,我想要這樣的東西,我已經通過 matplot 訓練后顯示了它:
最后,我想說明這個回調需要很長時間來處理的事實。 有沒有更有效的方法來做到這一點? 它幾乎使我的訓練時間加倍(可能是因為它需要將 numpy 轉換為圖像,然后再將它們保存在 TensorBoard 日志文件中)。
下面的代碼接受模型的輸入、模型的輸出和地面實況並保存到 Tensorboard。 該模型是分割的,因此每個樣本有 3 個圖像。
代碼非常簡單明了。 但仍有一些解釋:-
make_image_tensor
- 該方法轉換 numpy 圖像並創建一個張量以保存在 tensorboard 摘要中。
TensorBoardWriter
- 不是必需的,但可以很好地將 Tensorboard 功能與其他模塊分開。 允許可重用性。
ModelDiagonoser
- 采用生成器並通過 self.model 進行預測的類(由 Keras 設置為所有回調)。 ModelDiagonoser 獲取輸入、輸出和地面實況並傳遞給 Tensorboard 以保存圖像。
import os
import io
import numpy as np
import tensorflow as tf
from PIL import Image
from tensorflow.keras.callbacks import Callback
# Depending on your keras version use one of the following:
# from tensorflow.keras.engine.training import GeneratorEnqueuer, Sequence, OrderedEnqueuer
from tensorflow.keras.utils import GeneratorEnqueuer, Sequence, OrderedEnqueuer
def make_image_tensor(tensor):
"""
Convert an numpy representation image to Image protobuf.
Adapted from https://github.com/lanpa/tensorboard-pytorch/
"""
if len(tensor.shape) == 3:
height, width, channel = tensor.shape
else:
height, width = tensor.shape
channel = 1
tensor = tensor.astype(np.uint8)
image = Image.fromarray(tensor)
output = io.BytesIO()
image.save(output, format='PNG')
image_string = output.getvalue()
output.close()
return tf.Summary.Image(height=height,
width=width,
colorspace=channel,
encoded_image_string=image_string)
class TensorBoardWriter:
def __init__(self, outdir):
assert (os.path.isdir(outdir))
self.outdir = outdir
self.writer = tf.summary.FileWriter(self.outdir,
flush_secs=10)
def save_image(self, tag, image, global_step=None):
image_tensor = make_image_tensor(image)
self.writer.add_summary(tf.Summary(value=[tf.Summary.Value(tag=tag, image=image_tensor)]),
global_step)
def close(self):
"""
To be called in the end
"""
self.writer.close()
class ModelDiagonoser(Callback):
def __init__(self,
data_generator,
batch_size,
num_samples,
output_dir,
normalization_mean):
self.batch_size = batch_size
self.num_samples = num_samples
self.tensorboard_writer = TensorBoardWriter(output_dir)
self.normalization_mean = normalization_mean
is_sequence = isinstance(data_generator, Sequence)
if is_sequence:
self.enqueuer = OrderedEnqueuer(data_generator,
use_multiprocessing=True,
shuffle=False)
else:
self.enqueuer = GeneratorEnqueuer(data_generator,
use_multiprocessing=True)
self.enqueuer.start(workers=4, max_queue_size=4)
def on_epoch_end(self, epoch, logs=None):
output_generator = self.enqueuer.get()
steps_done = 0
total_steps = int(np.ceil(np.divide(self.num_samples, self.batch_size)))
sample_index = 0
while steps_done < total_steps:
generator_output = next(output_generator)
x, y = generator_output[:2]
y_pred = self.model.predict(x)
y_pred = np.argmax(y_pred, axis=-1)
y_true = np.argmax(y, axis=-1)
for i in range(0, len(y_pred)):
n = steps_done * self.batch_size + i
if n >= self.num_samples:
return
img = np.squeeze(x[i, :, :, :])
img = 255. * (img + self.normalization_mean) # mean is the training images normalization mean
img = img[:, :, [2, 1, 0]] # reordering of channels
pred = y_pred[i]
pred = pred.reshape(img.shape[0:2])
ground_truth = y_true[i]
ground_truth = ground_truth.reshape(img.shape[0:2])
self.tensorboard_writer.save_image("Epoch-{}/{}/x"
.format(epoch, sample_index), img)
self.tensorboard_writer.save_image("Epoch-{}/{}/y"
.format(epoch, sample_index), ground_truth)
self.tensorboard_writer.save_image("Epoch-{}/{}/y_pred"
.format(epoch, sample_index), pred)
sample_index += 1
steps_done += 1
def on_train_end(self, logs=None):
self.enqueuer.stop()
self.tensorboard_writer.close()
img_input 和 img_valid 可能在 0 到 1 的范圍內。將它們轉換為 uint8 類型將解決問題。
img_input = self.validation_data[0][0]
# img_input = img_input / np.max(img_input) # if img_input is not in (0,1), rescale it.
img_input = (255*img_input).astype(np.uint8)
img_valid = self.validation_data[1][0] # Y_train
img_valid = (255*img_valid ).astype(np.uint8)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.