woff 發表於 2023-9-28 17:32:29

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

最近從新調試一段pytorch 程式碼,以前的伺服器上完全沒問題,但換了一台機器,重新安裝了新版本的cuda, anaconda ,pytorch 等,以前的程式碼出現各種版本不適合的問題。
問題:
現在說說這個問題。運行pytorch 時出現的情況如下:
RuntimeError:
      An attempt has been made to start a new process before the
      current process has finished its bootstrapping phase.

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

            if __name__ == '__main__':
                freeze_support()
                ...

      The "freeze_support()" line can be omitted if the program
      is not going to be frozen to produce an executable.





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


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

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

from ultralytics import YOLO

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

if __name__ == '__main__':
    # Use the model
    model.train(data="coco128.yaml", epochs=3)# train the model
    metrics = model.val()# evaluate model performance on the validation set
    results = model("https://ultralytics.com/images/bus.jpg")# predict on an image
    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
頁: [1]
查看完整版本: Yolov8 PyTorch出現 An attempt has been made to start a new process before t...