TShopping

 找回密碼
 註冊
搜索
查看: 264|回復: 0
打印 上一主題 下一主題

[教學] Yolov8 PyTorch出現 An attempt has been made to start a new process before t...

[複製鏈接]
跳轉到指定樓層
1#
發表於 2023-9-28 17:32:29 | 只看該作者 |只看大圖 回帖獎勵 |倒序瀏覽 |閱讀模式
 
Push to Facebook
最近從新調試一段pytorch 程式碼,以前的伺服器上完全沒問題,但換了一台機器,重新安裝了新版本的cuda, anaconda ,pytorch 等,以前的程式碼出現各種版本不適合的問題。
問題:
現在說說這個問題。運行pytorch 時出現的情況如下:

  1. RuntimeError:
  2.         An attempt has been made to start a new process before the
  3.         current process has finished its bootstrapping phase.

  4.         This probably means that you are not using fork to start your
  5.         child processes and you have forgotten to use the proper idiom
  6.         in the main module:

  7.             if __name__ == '__main__':
  8.                 freeze_support()
  9.                 ...

  10.         The "freeze_support()" line can be omitted if the program
  11.         is not going to be frozen to produce an executable.
複製代碼






原因:
在網上查找結果,原因是多進程的原因。具體可參考博文:Python 中的if __name__ == '__main__' 該如何理解。


解決方法:
既然是多執行緒的原因,那麼可以從兩個角度解決問題:

1. 程式碼在運行 epoch 之前,加上    if __name__=='__main__'

  1. from ultralytics import YOLO

  2. # Load a model
  3. model = YOLO("yolov8n.yaml")  # build a new model from scratch
  4. model = YOLO("yolov8n.pt")  # load a pretrained model (recommended for training)

  5. if __name__ == '__main__':
  6.     # Use the model
  7.     model.train(data="coco128.yaml", epochs=3)  # train the model
  8.     metrics = model.val()  # evaluate model performance on the validation set
  9.     results = model("https://ultralytics.com/images/bus.jpg")  # predict on an image
  10.     path = model.export(format="onnx")  # export the model to ONNX format
複製代碼

試過,有效,一次通過。

2. 不使用多線程,也就是去掉num_workers 參數,或設定 num_workers=0。

(目前還沒試過,因為我還是想用多執行緒。)


文章出處: NetYea 網頁設計

參考文章
https://blog.csdn.net/u014546828/article/details/109235539

 

臉書網友討論
*滑块验证:
您需要登錄後才可以回帖 登錄 | 註冊 |

本版積分規則



Archiver|手機版|小黑屋|免責聲明|TShopping

GMT+8, 2024-4-28 13:00 , Processed in 0.047170 second(s), 25 queries .

本論壇言論純屬發表者個人意見,與 TShopping綜合論壇 立場無關 如有意見侵犯了您的權益 請寫信聯絡我們。

Powered by Discuz! X3.2

© 2001-2013 Comsenz Inc.

快速回復 返回頂部 返回列表