Java 后端面试题库 / 全部题解免费阅读理解原理,有条理地回答

一批图像怎么写,怎么控制展示数量?

查看完整题解

参考回答

直接回答

TensorFlow 传 [N,H,W,C] 给 tf.summary.image,用 max_outputs 限制展示数量。PyTorch 可用 writer.add_images 传 NCHW 等注明格式的批数据,或者先挑样本做拼图。

讲清原理

图像数量多会增加日志和页面开销。每次用固定几张用于比较,或者明确按类别挑错例。想让对照结果可复查,就记录样本 ID 或固定抽样种子。

例子与易错点

下面给 TensorFlow 三张小图,但只允许摘要保存前两张,再从事件读回实际图像数量。

from pathlib import Path
from tempfile import TemporaryDirectory

directory = TemporaryDirectory()
root = Path(directory.name)
import tensorflow as tf
from tensorboard.backend.event_processing.event_accumulator import EventAccumulator

images = tf.zeros([3, 8, 10, 3], dtype=tf.uint8)
writer = tf.summary.create_file_writer(str(root))
with writer.as_default():
    tf.summary.image("samples/batch", images, step=1, max_outputs=2)
writer.close()

entry, = EventAccumulator(str(root)).Reload().Tensors("samples/batch")
encoded = tf.make_ndarray(entry.tensor_proto)
# For the image summary tensor, the first two values hold display dimensions.
assert len(encoded[2:]) == 2
assert all(image.startswith(bytes.fromhex("89504e47")) for image in encoded[2:])
print("the summary contains 2 PNG images from 3 inputs")

相关题:第 26 题、第 27 题、第 28 题、第 29 题。

这道题,你掌握得怎么样?

掌握程度由你主动标记,阅读和收藏不会自动改变状态。