Windows - tensorflow.python.framework.errors_impl.UnknownError: Failed to rename

最近在调试tensorflow,环境是win10,IDE pycharm    tensorflow 2.5 python3.6 

在使用tensorflow训练的模型时 ,模型能够训练出来,在保存未checkpoint时却不能被以.ckpt的形式保存,出现了很多问题,在经过几天的查询后,最终也得到了解决。这里感慨一下学习途中真的坎坷和艰难,顺便记录一下,希望帮助到有需要的同学。

保存checkpoint调用的主要代码如下

 checkpoint_file = os.path.join(TRAIN_DIR, 'checkpoint_regression')  

 saver = tf.train.Saver()    

 saver.save(sess, checkpoint_file, global_step=step)
 

问题一 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 164

问题报错如下


Traceback (most recent call last):
  File "D:/tensorflow/class4/class4/MNIST_FC/p16_mnist_train_ex3.py", line 29, in <module>
    callbacks=[cp_callback])
  File "D:\Python37\lib\site-packages\keras\utils\traceback_utils.py", line 67, in error_handler
    raise e.with_traceback(filtered_tb) from None
  File "D:\Python37\lib\site-packages\tensorflow\python\eager\execute.py", line 55, in quick_execute
    inputs, attrs, num_outputs)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb5 in position 137: invalid start byte

对于这个问题,网上有很多解释,大部分都都集中在checkpoint文件保存路径讨论上,参考这些方法后改进,一直没解决。

最好参考了UnicodeDecodeError:‘utf-8‘codec can‘t decode byte 0xd5 in position 0:invalid continuation byte_py~豆腐的博客-CSDN博客

终于解决了问题,有以上烦劳的同学们也可以参考一下这个博客,博主写的已经很详细了,我就不赘述了。

在解决第一个问题后,第二个问题就马上出现了

问题二   tensorflow.python.framework.errors_impl.UnknownError: Failed to rename

报错如下

  File "C:\Users\<redacted>\Miniconda3\envs\<redacted>\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 532, in atomic_write_string_to_file
    rename(temp_pathname, filename, overwrite)
  File "C:\Users\<redacted>\Miniconda3\envs\<redacted>\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 491, in rename
    rename_v2(oldname, newname, overwrite)
  File "C:\Users\<redacted>\Miniconda3\envs\<redacted>\lib\site-packages\tensorflow\python\lib\io\file_io.py", line 508, in rename_v2
    compat.as_bytes(src), compat.as_bytes(dst), overwrite)
tensorflow.python.framework.errors_impl.UnknownError: Failed to rename: tests\files\checkpoints\0000_00_00_00_00_00\checkpoint.tmpc6ee5d6bc5a445c884bba8c3acadf01f to: tests\files\checkpoints\0000_00_00_00_00_00\checkpoint : Access is denied.
; Input/output error

关于这个报错,网上也有很多讨论和方法

比如来自StackOverflow的问题:tensorflow.python.framework.errors_impl.UnknownError:无法重命名:输入/输出错误 - 问答 - 腾讯云开发者社区-腾讯云

大部分都讨论还是集中在文件路径上,但都不能解决问题。直到看到github,终于找到了答案。

具体如下:点击traceback中的最后一项 

 File "C:\Users\<redacted>\Miniconda3\envs\<redacted>\lib\site-packages\tensorflow\python\lib\io\file_io.py", 

打开file_io.py,将其中的代码改成下述所示代码即可

def atomic_write_string_to_file(filename, contents, overwrite=True):
  if not has_atomic_move(filename):
    write_string_to_file(filename, contents)
  else:
    temp_pathname = filename + ".tmp" + uuid.uuid4().hex
    write_string_to_file(temp_pathname, contents)
    try:
      if overwrite and os.path.exists(filename):
        os.remove(filename)
      rename(temp_pathname, filename, overwrite)
    except errors.OpError:
      delete_file(temp_pathname)
      raise

测试效果良好,目前成果解决了问题。

GitHub地址如下:Windows - tensorflow.python.framework.errors_impl.UnknownError: Failed to rename: · Issue #41380 · tensorflow/tensorflow · GitHub

总结:这些问题大部分都跟代码无关,都是在windows下一些环境配置问题,解决起来却要花太多时间。所以,如果可以,还是建议使用linux,少很非必要的麻烦。

  • 2
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值