Tensorflow報錯:AttributeError: module 'tensorflow._api.v1.io' has no attribute 'gfile'


在使用YOLO v5進行自己數據集的訓練的時候(python3 train.py),報了如下的錯誤:

Traceback (most recent call last):
  File "train.py", line 402, in <module>
    tb_writer = SummaryWriter(log_dir=increment_dir('runs/exp', opt.name))
  File "/home/riddleli/.local/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py", line 225, in __init__
    self._get_file_writer()
  File "/home/riddleli/.local/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py", line 256, in _get_file_writer
    self.flush_secs, self.filename_suffix)
  File "/home/riddleli/.local/lib/python3.6/site-packages/torch/utils/tensorboard/writer.py", line 66, in __init__
    log_dir, max_queue, flush_secs, filename_suffix)
  File "/home/riddleli/.local/lib/python3.6/site-packages/tensorboard/summary/writer/event_file_writer.py", line 76, in __init__
    if not tf.io.gfile.exists(logdir):
AttributeError: module 'tensorflow._api.v1.io' has no attribute 'gfile'

從Traceback我們可以看到這個錯誤是YOLO調用了pytorch,pytorch又調了Tensorflow,最后由Tensorflow報出的錯誤,於是根據報錯,我們前往

/home/riddleli/.local/lib/python3.6/site-packages/tensorboard/summary/writer/event_file_writer.py

這一地址(當然你要修改成你實際報錯的位置),看看這個python文件的76行寫了什么內容

 

這兩行代碼很好理解,意思就是檢查一下有沒有叫做logdir的文件夾,如果沒有的話就創建一個,但是似乎由於tensorflow版本的原因,tf.io.gfile這里出現了報錯,一個最簡單的思路就是使用Python自帶的os.path和os.makedirs來替換掉這兩個函數,將這兩行程序改成如下所示: 

if not os.path.exists(logdir):
  os.makedirs(logdir)

隨后我們還可以看到91行處也有類似的代碼,如果不管它一會也會報錯

self._general_file_writer = tf.io.gfile.GFile(self._file_name, "wb")

這里我們采用另外一種方案,經過查閱資料,我們得知:出現這個問題的根本原因在於tensorflow的新版本與舊版本的不兼容(真是有毒),在這一版tensorflow中,gfile不是tf.io的,而是直接是tf的,所以我們按照如下代碼進行修改即可(當然你仍然可以使用Python自帶的os類來實現這一個功能)

self._general_file_writer = tf.gfile.GFile(self._file_name, "wb")

修改完后保存,重新運行,錯誤消除


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM