赞
踩
(1). 去官网下载安装包:Free Download | Anaconda
(2). 下载完成,进行安装,建议安装在非系统盘,以后会创建比较多的环境的话,剩下的默认安装即可
具体操作请参考Anaconda 环境变量手动设置(详细)_ananconda环境变量设置-CSDN博客
(4).pycharm中选择conda环境
1.首先创建一个conda环境,打开Anaconda prompt,输入:conda create --name (环境名)python=3.9
删除环境输入: conda remove -n env_name --all
2. 输入conda activate yourname 激活环境
2.pytorch-gpu版本环境的搭建
1. 打开cmd,输入nvidia-smi查看GPU的CUDA版本,可以看到CUDA版本12.5,意味着能安装小于或等于12.5的CUDA版本。
如果觉得版本太低想要安装更高版本(cuda版本低适配不了gpu版的pytorch)可在NVIDIA上更新电脑的CUDA 版本
2.选择合适版本安装cuda,下载路径:CUDA Toolkit Archive | NVIDIA Developer
注意:
1. CUDA版本要小于上面的版本信息
2. 先去pytorch官网Start Locally | PyTorch,看一眼自己需要的pytorch版本对应的CUDA版本
选择自己合适的版本
安装时注意取消Visual Studio Integration的选择否则可能会导致安装失败
(2)安装pytorch的gpu版本(非gpu版本可跳过上一部)
如果是电脑无显卡就选择cpu版
在你yolo的conda环境下安装
打开Anaconda Prompt
进入环境,输入命令 :conda activate (youname)
开始下载安装
将刚才选择的pytorch版本下的命令输入:conda install pytorch torchvision torchaudio pytorch-cuda=11.8 -c pytorch -c nvidia (也可选择pip安装)
因为安装过程会比较慢而且可能会因为网络问题而中断,多试几次就可以了(如果利用“科学上网”的话会好一些)。也可以用清华源安装,但是清华源版本好像无gpu版本的pytorch。
3.yolov8下载配置
(1)下载源码
源码地址:GitHub - ultralytics/ultralytics: NEW - YOLOv8 �� in PyTorch > ONNX > OpenVINO > CoreML > TFLite
下载好解压之后用pycharm进行打开,然后把interpreter设置为刚刚创建的虚拟环境
(2)在pycharm终端中配置conda
关于如何在pycharm终端中配置虚拟环境可以看这篇:
pycharm终端配置,使用Anaconda_pycharm设置terminal打开anaconda的命令行窗口显示找不到本地终端-CSDN博客
具体:把settings-> Tools -> Terminal中的shell path换成你的conda prompt的位置
安装所需要的包
安装代码运行所需的ultralytics和yolo包
pip install ultralytics
pip install yolo
(3) 预训练权重的下载
把权重文件放在根目录处也创建一个文件夹weight,将权重放入
终端输入:yolo predict model=yolov8n.pt source='ultralytics/assets/bus.jpg'
该命令是用下载的yolov8n.pt权重来对选择的照片做测试
成功后是
会将结果保留在该目录下可查看结果
其中train是训练集,valid是测试集,test集用于评估模型的泛化能力,每一个数据集下都有一个images(用于存放照片)labels(用于存放照片对应的标签),标签与照片是一一对应的,是txt格式。
我所用数据共有七个标签。
Yolo标注工具-labelImg的详细使用_yolo labelimg-CSDN博客
具体操作请参考:make-sense | 图像标注工具_makesense标注-CSDN博客
- from ultralytics.data.annotator import auto_annotate
-
- auto_annotate(data=r"D:\data\datas\smake",#数据集路径
- det_model=r"D:\pythonProject\yolov8\ultralytics-main\runs\detect\train4\weights\best.pt",#已有模型
- sam_model=r"D:\pythonProject\yolov8\ultralytics-main\weight\sam_b.pt")#预训练模型
该标注的效果肯定没有自己想要达到的效果,还需要自己手动标注过一遍。
- from ultralytics import YOLO
-
- if __name__ == '__main__':
- # Load a model
- model = YOLO(r'\ultralytics\detection\yolov8n\yolov8n.yaml') # 不使用预训练权重训练
- # model = YOLO(r'yolov8p.yaml').load("yolov8n.pt") # 使用预训练权重训练
- # Trainparameters ----------------------------------------------------------------------------------------------
- model.train(
- data=r'\ultralytics\detection\dataset\appledata.yaml',
- epochs=30, # (int) number of epochs to train for
- patience=50, # (int) epochs to wait for no observable improvement for early stopping of training
- batch=8, # (int) number of images per batch (-1 for AutoBatch)
- imgsz=320, # (int) size of input images as integer or w,h
- save=True, # (bool) save train checkpoints and predict results
- save_period=-1, # (int) Save checkpoint every x epochs (disabled if < 1)
- cache=False, # (bool) True/ram, disk or False. Use cache for data loading
- device=0, # (int | str | list, optional) device to run on, i.e. cuda device=0 or device=0,1,2,3 or device=cpu
- workers=16, # (int) number of worker threads for data loading (per RANK if DDP)
- project='result', # (str, optional) project name
- name='yolov8n', # (str, optional) experiment name, results saved to 'project/name' directory
- exist_ok=False, # (bool) whether to overwrite existing experiment
- pretrained=False, # (bool | str) whether to use a pretrained model (bool) or a model to load weights from (str)
- optimizer='SGD', # (str) optimizer to use, choices=[SGD, Adam, Adamax, AdamW, NAdam, RAdam, RMSProp, auto]
- verbose=True, # (bool) whether to print verbose output
- seed=0, # (int) random seed for reproducibility
- deterministic=True, # (bool) whether to enable deterministic mode
- single_cls=True, # (bool) train multi-class data as single-class
- rect=False, # (bool) rectangular training if mode='train' or rectangular validation if mode='val'
- cos_lr=False, # (bool) use cosine learning rate scheduler
- close_mosaic=0, # (int) disable mosaic augmentation for final epochs
- resume=False, # (bool) resume training from last checkpoint
- amp=False, # (bool) Automatic Mixed Precision (AMP) training, choices=[True, False], True runs AMP check
- fraction=1.0, # (float) dataset fraction to train on (default is 1.0, all images in train set)
- profile=False, # (bool) profile ONNX and TensorRT speeds during training for loggers
- # Segmentation
- overlap_mask=True, # (bool) masks should overlap during training (segment train only)
- mask_ratio=4, # (int) mask downsample ratio (segment train only)
- # Classification
- dropout=0.0, # (float) use dropout regularization (classify train only)
- # Hyperparameters ----------------------------------------------------------------------------------------------
- lr0=0.01, # (float) initial learning rate (i.e. SGD=1E-2, Adam=1E-3)
- lrf=0.01, # (float) final learning rate (lr0 * lrf)
- momentum=0.937, # (float) SGD momentum/Adam beta1
- weight_decay=0.0005, # (float) optimizer weight decay 5e-4
- warmup_epochs=3.0, # (float) warmup epochs (fractions ok)
- warmup_momentum=0.8, # (float) warmup initial momentum
- warmup_bias_lr=0.1, # (float) warmup initial bias lr
- box=7.5, # (float) box loss gain
- cls=0.5, # (float) cls loss gain (scale with pixels)
- dfl=1.5, # (float) dfl loss gain
- pose=12.0, # (float) pose loss gain
- kobj=1.0, # (float) keypoint obj loss gain
- label_smoothing=0.0, # (float) label smoothing (fraction)
- nbs=64, # (int) nominal batch size
- hsv_h=0.015, # (float) image HSV-Hue augmentation (fraction)
- hsv_s=0.7, # (float) image HSV-Saturation augmentation (fraction)
- hsv_v=0.4, # (float) image HSV-Value augmentation (fraction)
- degrees=0.0, # (float) image rotation (+/- deg)
- translate=0.1, # (float) image translation (+/- fraction)
- scale=0.5, # (float) image scale (+/- gain)
- shear=0.0, # (float) image shear (+/- deg)
- perspective=0.0, # (float) image perspective (+/- fraction), range 0-0.001
- flipud=0.0, # (float) image flip up-down (probability)
- fliplr=0.5, # (float) image flip left-right (probability)
- mosaic=1.0, # (float) image mosaic (probability)
- mixup=0.0, # (float) image mixup (probability)
- copy_paste=0.0, # (float) segment copy-paste (probability)
- )
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
yolo detect train data=datasets/smking.yaml model=yolov8n.yaml pretrained=weight/yolov8n.pt epochs=300 batch=4 resume=True device=0
其中data 是数据对应的yaml文件路径;model是预训练权重的yaml文件,如果没下载该yaml会直接帮你下载,下载了就可选择不要;pretrained 权重路径;epochs 训练轮数 ;batch一次训练使用的数据数量;device是否使用gpu训练,如果只使用cpu 修改成device=cpu。
第一次训练会出现各种的问题,可在网络上寻找问题的解决方式
训练成功后模型会保存在其提示的位置
我们还会发现设置了训练300轮但是184轮的时候就停止了,那是因为到184轮的时候训练就没什么训练效果了,就停止训练了。
- from ultralytics import YOLO
-
- if __name__ == '__main__':
- # Load a model
- model = YOLO(r'\ultralytics\detection\yolov8n\result\yolov8n4\weights\best.pt') # build a new model from YAML
- # Validate the model
- model.val(
- val=True, # (bool) validate/test during training
- data=r'\ultralytics\detection\dataset\appledata.yaml',
- split='val', # (str) dataset split to use for validation, i.e. 'val', 'test' or 'train'
- batch=1, # 测试速度时一般设置为 1 ,设置越大速度越快。 (int) number of images per batch (-1 for AutoBatch)
- imgsz=320, # (int) size of input images as integer or w,h
- device=0, # (int | str | list, optional) device to run on, i.e. cuda device=0 or device=0,1,2,3 or device=cpu
- workers=8, # (int) number of worker threads for data loading (per RANK if DDP)
- save_json=False, # (bool) save results to JSON file
- save_hybrid=False, # (bool) save hybrid version of labels (labels + additional predictions)
- conf=0.001, # (float, optional) object confidence threshold for detection (default 0.25 predict, 0.001 val)
- iou=0.7, # (float) intersection over union (IoU) threshold for NMS
- project='val', # (str, optional) project name
- name='', # (str, optional) experiment name, results saved to 'project/name' directory
- max_det=300, # (int) maximum number of detections per image
- half=True, # (bool) use half precision (FP16)
- dnn=False, # (bool) use OpenCV DNN for ONNX inference
- plots=True, # (bool) save plots during train/val
- )
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
命令如:
yolo detect val data=datasets/Prohibited_items/Prohibited_items.yaml model=runs\detect\train12\weights\best.pt
预测的效果如:
- from ultralytics import YOLO
-
- if __name__ == '__main__':
- # Load a model
- model = YOLO(r'\deploy\yolov8n.pt') # pretrained YOLOv8n model
- model.predict(
- source=r'\deploy\output_video.mp4',
- save=False, # save predict results
- imgsz=320, # (int) size of input images as integer or w,h
- conf=0.25, # object confidence threshold for detection (default 0.25 predict, 0.001 val)
- iou=0.7, # # intersection over union (IoU) threshold for NMS
- show=True, # show results if possible
- project='', # (str, optional) project name
- name='', # (str, optional) experiment name, results saved to 'project/name' directory
- save_txt=False, # save results as .txt file
- save_conf=True, # save results with confidence scores
- save_crop=False, # save cropped images with results
- show_labels=True, # show object labels in plots
- show_conf=True, # show object confidence scores in plots
- vid_stride=1, # video frame-rate stride
- line_width=1, # bounding box thickness (pixels)
- visualize=False, # visualize model features
- augment=False, # apply image augmentation to prediction sources
- agnostic_nms=False, # class-agnostic NMS
- retina_masks=False, # use high-resolution segmentation masks
- boxes=True, # Show boxes in segmentation predictions
- )
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- import cv2
-
- from ultralytics import YOLO
-
- # 加载模型
- model = YOLO(model='D:/pythonProject/yolov8/ultralytics-main/runs/detect/train/weights/best.pt')
-
- # 视频文件
- video_path = r"D:\pythonProject\yolov8\ultralytics-main\预测\video.mp4"
-
- # 打开视频
- cap = cv2.VideoCapture(video_path)
-
- while cap.isOpened():
- # 获取图像
- res, frame = cap.read()
- # 如果读取成功
- if res:
- # 正向推理
- results = model(frame)
-
- # 绘制结果
- annotated_frame = results[0].plot()
-
- # 显示图像
- cv2.imshow(winname="YOLOV8", mat=annotated_frame)
-
- # 按ESC退出
- if cv2.waitKey(1) == 27:
- break
-
- else:
- break
-
- # 释放链接
- cap.release()
- # 销毁所有窗口
- cv2.destroyAllWindows()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
- import cv2
-
- from ultralytics import YOLO
-
- # 加载模型
- model = YOLO(model="yolov8n.pt")
-
- # 摄像头编号
- camera_no = 0
-
- # 打开摄像头
- cap = cv2.VideoCapture(camera_no)
-
- while cap.isOpened():
- # 获取图像
- res, frame = cap.read()
- # 如果读取成功
- if res:
- # 正向推理
- results = model(frame)
-
- # 绘制结果
- annotated_frame = results[0].plot()
-
- # 显示图像
- CV2.imshow(winname="YOLOV8", mat=annotated_frame)
-
- # 按ESC退出
- if CV2.waitKey(1) == 27:
- break
-
- else:
- break
-
- # 释放链接
- cap.release()
- # 销毁所有窗口
- cv2.destroyAllWindows()
![](https://csdnimg.cn/release/blogv2/dist/pc/img/newCodeMoreWhite.png)
[1]
[2] 在较新版pycharm中使用conda虚拟环境的两种方法-保姆级教程_pycharm终端如何进入conda虚拟环境-CSDN博客
[3] 超详细||YOLOv8基础教程(环境搭建,训练,测试,部署看一篇就够)(在推理视频中添加FPS信息)_yolov8安装-CSDN博客
[4] make-sense | 图像标注工具_makesense标注-CSDN博客
[5]适合小白的超详细yolov8环境配置+实例运行教程,从零开始教你如何使用yolov8训练自己的数据集(Windows+conda+pycharm)-CSDN博客
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。