普通网友 2024-05-07 21:37 采纳率: 0%
浏览 8

按照教程来的 怎么报错了呢?


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]

  • 写回答

4条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-05-07 21:38
    关注

    引自免费微信小程序:皆我百晓生

    看起来你在尝试使用 TensorFlow 来记录训练样本的图像日志,但遇到了一个错误。这个错误信息表明,tf.summary.image 需要一个三维数组作为输入,而你提供的 sample_img 是四维的。

    在你提供的代码中,sample_img = tf.reshape(sample_img, [1,28, 28, 1]) 这一行将图片转换为了形状 [1, 28, 28, 1]。然而,对于 tf.summary.image 函数来说,它期望的是 [height, width, channels] 格式的三维数组,所以你需要去掉额外的维度。将这一行修改为:

    sample_img = tf.reshape(sample_img, [28, 28, 1])
    

    然后这段代码应该可以正常运行。下面是修改后的完整代码:

    import tensorflow as tf
    from datetime import datetime
    
    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)
    
    # 假设 db 是数据迭代器
    for _, sample_img in enumerate(db):
        break  # 只获取第一张图像
    
    sample_img = sample_img[0]
    
    # 重新整形为 [28, 28, 1] 以满足 tf.summary.image 的要求
    sample_img = tf.reshape(sample_img, [28, 28, 1])
    
    with summary_writer.as_default():
        tf.summary.image("Training sample", sample_img, step=0)
    

    这段代码首先创建了一个新的 TensorBoard 日志目录,并从数据迭代器 db 中获取了第一张图像。接着,我们调整了图像的形状,然后将其写入到 TensorBoard 中。注意,这里的 enumerate(db) 是用来遍历数据迭代器并获取第一个元素的。如果你的数据迭代器已经处理过这些细节,那么可以直接使用 next(iter(db))[0]

    评论

报告相同问题?

问题事件

  • 创建了问题 5月7日

悬赏问题

  • ¥15 from seleniumwire import webdriver 在抓取http://链接的时候会自动转https://这个怎么解决
  • ¥15 hive直连数据库模式插入mysql表数据失败(相关搜索:数据库)
  • ¥30 不会,学习,有偿解答
  • ¥15 SQL查询语句报错(检查)
  • ¥15 此表中公式应该怎么写
  • ¥15 求HI-TECH PICC 9.50 PL3安装包
  • ¥15 下载ctorch报错,求解
  • ¥15 如何入门学习c语言,单片机
  • ¥15 idea 编辑语言的选择
  • ¥15 Windows下部署Asmjit