current_time = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
log_dir = 'logs/' + current_time
summary_writer = tf.summary.create_file_writer(log_dir)
# 获取图像数据,假设已经是 (28, 28)
sample_img = next(iter(db))[0] # 假设这里 db 是数据迭代器
sample_img = sample_img[0] # 获取第一张图像
# 重新整形为 [1, 28, 28, 1] 以满足 tf.summary.image 的要求
sample_img = tf.reshape(sample_img, [1,28, 28, 1])
with summary_writer.as_default():
tf.summary.image("Training sample", sample_img, step=0)
报错:
---------------------------------------------------------------------------
InvalidArgumentError Traceback (most recent call last)
Cell In[26], line 13
10 sample_img = tf.reshape(sample_img, [1,28, 28, 1])
12 with summary_writer.as_default():
---> 13 tf.summary.image("Training sample", sample_img, step=0)
File h:\modeltrain\anaconda3\envs\tf\lib\site-packages\tensorboard\plugins\image\summary_v2.py:129, in image(name, data, step, max_outputs, description)
125 return tf.concat([dimensions, encoded_images], axis=0)
127 # To ensure that image encoding logic is only executed when summaries
128 # are written, we pass callable to `tensor` parameter.
--> 129 return tf.summary.write(
130 tag=tag, tensor=lazy_tensor, step=step, metadata=summary_metadata
131 )
File h:\modeltrain\anaconda3\envs\tf\lib\site-packages\tensorflow\python\ops\summary_ops_v2.py:762, in write(tag, tensor, step, metadata, name)
759 with ops.control_dependencies([write_summary_op]):
760 return constant_op.constant(True)
--> 762 op = smart_cond.smart_cond(
763 should_record_summaries(), record, _nothing, name="summary_cond")
764 if not context.executing_eagerly():
765 ops.add_to_collection(ops.GraphKeys._SUMMARY_COLLECTION, op) # pylint: disable=protected-access
File h:\modeltrain\anaconda3\envs\tf\lib\site-packages\tensorflow\python\framework\smart_cond.py:56, in smart_cond(pred, true_fn, false_fn, name)
54 if pred_value is not None:
...
-> 6941 six.raise_from(core._status_to_exception(e.code, message), None)
File <string>:3, in raise_from(value, from_value)
InvalidArgumentError: image must be 3-dimensional[1,28,28,1] [Op:EncodePng]