網(wǎng)站首頁(yè) 編程語(yǔ)言 正文
1.配置環(huán)境
操作系統(tǒng):Ubuntu20.04
CUDA版本:11.4
Pytorch版本:1.9.0
TorchVision版本:0.7.0
IDE:PyCharm
硬件:RTX2070S*2
2.問題描述
在訓(xùn)練YOLOv5時(shí)由于數(shù)據(jù)集很大導(dǎo)致訓(xùn)練時(shí)間十分漫長(zhǎng),這期間Python、主機(jī)等可能遇到死機(jī)的情況,如果需要訓(xùn)練300個(gè)epoch但是訓(xùn)練一晚后發(fā)現(xiàn)在200epoch時(shí)停下是十分崩潰了,好在博主摸索到在yolov5中接續(xù)訓(xùn)練的方法了。
3.解決方法
首先直接上方法
3.1設(shè)置需要接續(xù)訓(xùn)練的結(jié)果
如果你想從上一次訓(xùn)練結(jié)果中回復(fù)訓(xùn)練,那么首先保證你的訓(xùn)練結(jié)果(一般都存放在/runs/train目錄下)在保存目錄中代號(hào)為最大的。
如上圖所示,在train文件夾下一共有14個(gè)訓(xùn)練結(jié)果,假設(shè)我的第12次訓(xùn)練中斷了,想接著第12次的結(jié)果繼續(xù)訓(xùn)練,那么只需要將比12更大的:exp13、exp14這兩個(gè)文件夾刪除或者移動(dòng)到其他地方,這樣便設(shè)置好了需要接續(xù)訓(xùn)練的結(jié)果。
3.2設(shè)置訓(xùn)練代碼
代碼見yolov5代碼中的train.py
if __name__ == '__main__': os.environ["KMP_DUPLICATE_LIB_OK"] = "TRUE" parser = argparse.ArgumentParser() parser.add_argument('--weights', type=str, default='../weights/yolov5s.pt', help='initial weights path') parser.add_argument('--cfg', type=str, default='./models/yolov5s.yaml', help='model.yaml path') parser.add_argument('--data', type=str, default='data/car.yaml', help='data.yaml path') parser.add_argument('--hyp', type=str, default='data/hyp.scratch.yaml', help='hyperparameters path') parser.add_argument('--epochs', type=int, default=300) parser.add_argument('--batch-size', type=int, default=32, help='total batch size for all GPUs') parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='[train, test] image sizes') parser.add_argument('--rect', action='store_true', help='rectangular training') parser.add_argument('--resume', nargs='?', const=True, default=False, help='resume most recent training') parser.add_argument('--nosave', action='store_true', help='only save final checkpoint') parser.add_argument('--notest', action='store_true', help='only test final epoch') parser.add_argument('--noautoanchor', action='store_true', help='disable autoanchor check') parser.add_argument('--evolve', action='store_true', help='evolve hyperparameters') parser.add_argument('--bucket', type=str, default='', help='gsutil bucket') parser.add_argument('--cache-images', action='store_true', help='cache images for faster training') parser.add_argument('--image-weights', action='store_true', help='use weighted image selection for training') parser.add_argument('--device', default='1', help='cuda device, i.e. 0 or 0,1,2,3 or cpu') parser.add_argument('--multi-scale', action='store_true', help='vary img-size +/- 50%%') parser.add_argument('--single-cls', action='store_true', help='train multi-class data as single-class') parser.add_argument('--adam', action='store_true', help='use torch.optim.Adam() optimizer') parser.add_argument('--sync-bn', action='store_true', help='use SyncBatchNorm, only available in DDP mode') parser.add_argument('--local_rank', type=int, default=-1, help='DDP parameter, do not modify') parser.add_argument('--workers', type=int, default=8, help='maximum number of dataloader workers') parser.add_argument('--project', default='runs/train', help='save to project/name') parser.add_argument('--entity', default=None, help='W&B entity') parser.add_argument('--name', default='exp', help='save to project/name') parser.add_argument('--exist-ok', action='store_true', help='existing project/name ok, do not increment') parser.add_argument('--quad', action='store_true', help='quad dataloader') parser.add_argument('--linear-lr', action='store_true', help='linear LR') parser.add_argument('--label-smoothing', type=float, default=0.0, help='Label smoothing epsilon') parser.add_argument('--upload_dataset', action='store_true', help='Upload dataset as W&B artifact table') parser.add_argument('--bbox_interval', type=int, default=-1, help='Set bounding-box image logging interval for W&B') parser.add_argument('--save_period', type=int, default=-1, help='Log model after every "save_period" epoch') parser.add_argument('--artifact_alias', type=str, default="latest", help='version of dataset artifact to be used') opt = parser.parse_args()
注意上面patser中第9個(gè)參數(shù)resume,將其設(shè)置為default=True
即可,也就是那一行代碼改變?yōu)?/p>
parser.add_argument('--resume', nargs='?', const=True, default=True, help='resume most recent training')
接下來運(yùn)行python train.py邊不會(huì)產(chǎn)生新的exp而是在最新的exp上接續(xù)訓(xùn)練
如下圖所示:
博主運(yùn)行完python train.py
后便是接著上一次訓(xùn)練完139個(gè)epoch繼續(xù)訓(xùn)練
4.原理
其實(shí)接續(xù)訓(xùn)練不是什么深?yuàn)W內(nèi)容 ,博主在訓(xùn)練自己模型的時(shí)候也早會(huì)使用。
我們?cè)谑褂脃olov5提供的權(quán)重,也就是像yolov5s.pt之類的文件時(shí)就是使用了官方提供的模型接續(xù)訓(xùn)練的。
我們每次訓(xùn)練模型時(shí)都會(huì)生成新的模型結(jié)果,存放在/runs/train/expxxx/weights下,接續(xù)訓(xùn)練就是將上次訓(xùn)練一半得到的結(jié)果拿來和模型結(jié)合進(jìn)行訓(xùn)練。具體來說:如果最終訓(xùn)練目標(biāo)是300個(gè)epoch,上次訓(xùn)練完了139個(gè)epoch,那么就是將第139個(gè)epoch得到的權(quán)重載入到模型中再訓(xùn)練161個(gè)epoch便可等效為訓(xùn)練了300個(gè)epoch
5.結(jié)束語(yǔ)
原文鏈接:https://blog.csdn.net/ShakalakaPHD/article/details/120635894
相關(guān)推薦
- 2021-12-08 el-cascader 級(jí)聯(lián)選擇器回顯數(shù)據(jù) (帶有子級(jí)children)
- 2022-07-08 Python數(shù)據(jù)分析之使用matplotlib繪制折線圖、柱狀圖和柱線混合圖_python
- 2022-07-14 React?Native采用Hermes熱更新打包方案詳解_React
- 2022-08-21 Python命令行庫(kù)click的具體使用_python
- 2022-05-01 Python字符串和其常用函數(shù)合集_python
- 2022-09-23 flutter實(shí)現(xiàn)切換頁(yè)面緩存_Android
- 2022-03-23 如何使用Redis實(shí)現(xiàn)電商系統(tǒng)的庫(kù)存扣減_Redis
- 2022-06-18 SpringBoot打包docker鏡像發(fā)布的詳細(xì)步驟_docker
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細(xì)win安裝深度學(xué)習(xí)環(huán)境2025年最新版(
- Linux 中運(yùn)行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲(chǔ)小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運(yùn)算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認(rèn)證信息的處理
- Spring Security之認(rèn)證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯(cuò)誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實(shí)現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡(jiǎn)單動(dòng)態(tài)字符串(SD
- arthas操作spring被代理目標(biāo)對(duì)象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊(duì)列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠(yuǎn)程分支