基于Python3.10的YOLO-NAS环境配置

1.创建虚拟环境

conda create -n 环境名称 python=版本名称

2.检查电脑CUDA版本

#输出结果为显卡性能图,右上角可以查看CUDA版本
nvidia-smi

3.安装兼容对应版本CUDA的Pytorch

#官方网站 https://pytorch.org/
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu版本号

4.安装YOLO-NAS所需的其他库 

pip install numpy

pip install sympy

pip install cython

pip install imutils

pip install roboflow

pip install pytube --upgrade

5.安装super-gradients库

#官方网站 https://pypi.org/project/super-gradients/
pip install super-gradients==与Pytorch匹配的版本号

 6.检查CUDA与Pytorch版本兼容性

#版本查询
import torch
torch.__version__

#兼容性查询
import torch
torch.cuda.is_available()

以下是使用PythonYOLO-NAS模型进行水果识别的代码: ```python # 导入相关的库 import cv2 import numpy as np import argparse # 设置命令行参数 ap = argparse.ArgumentParser() ap.add_argument("-i", "--image", required=True, help="path to input image") ap.add_argument("-m", "--model", required=True, help="path to yolo-nas model") ap.add_argument("-c", "--classes", required=True, help="path to text file containing class names") args = vars(ap.parse_args()) # 加载类别名 classes = None with open(args["classes"], 'r') as f: classes = [line.strip() for line in f.readlines()] # 加载模型 net = cv2.dnn.readNet(args["model"]) # 加载输入图像并进行预处理 image = cv2.imread(args["image"]) blob = cv2.dnn.blobFromImage(image, 1/255.0, (416, 416), swapRB=True, crop=False) # 设置模型的输入和输出节点 net.setInput(blob) layerNames = net.getLayerNames() outputLayers = [layerNames[i[0] - 1] for i in net.getUnconnectedOutLayers()] layerOutputs = net.forward(outputLayers) # 初始化输出结果 boxes = [] confidences = [] classIDs = [] # 循环遍历每个输出层,提取检测结果 for output in layerOutputs: for detection in output: scores = detection[5:] classID = np.argmax(scores) confidence = scores[classID] # 过滤掉置信度低的检测结果 if confidence > 0.5: box = detection[0:4] * np.array([image.shape[1], image.shape[0], image.shape[1], image.shape[0]]) (centerX, centerY, width, height) = box.astype("int") # 计算边框的左上角坐标 x = int(centerX - (width / 2)) y = int(centerY - (height / 2)) # 更新输出结果 boxes.append([x, y, int(width), int(height)]) confidences.append(float(confidence)) classIDs.append(classID) # 对输出结果进行NMS处理,去除冗余的检测结果 idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) # 在图像上绘制检测结果 if len(idxs) > 0: for i in idxs.flatten(): (x, y) = (boxes[i][0], boxes[i][1]) (w, h) = (boxes[i][2], boxes[i][3]) color = [int(c) for c in COLORS[classIDs[i]]] cv2.rectangle(image, (x, y), (x + w, y + h), color, 2) text = "{}: {:.4f}".format(classes[classIDs[i]], confidences[i]) cv2.putText(image, text, (x, y - 5), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) # 显示输出图像 cv2.imshow("Image", image) cv2.waitKey(0) cv2.destroyAllWindows() ``` 使用时可以在命令行中运行以下命令: ``` python fruit_detection.py --image input_image.jpg --model yolo-nas.weights --classes classes.txt ``` 其中,`input_image.jpg`是要识别的图像,`yolo-nas.weights`是YOLO-NAS模型的权重文件,`classes.txt`是包含类别名的文本文件。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值