我已经训练了一个YOLOV8模型来识别路口中的物体(如汽车、道路等)。模型运行正常,我可以将识别出的感兴趣物体以图像的形式输出,并进行分割。
然而,我需要捕获这些物体的原始几何形状(多边形),以便稍后将它们保存到txt文件中。
我尝试了文档中找到的方法(https://docs.ultralytics.com/modes/predict/#key-features-of-predict-mode),但返回的对象与文档描述的不一致。
实际上,结果是一系列tensorflow数字:
这是我的代码:
import argparseimport cv2import numpy as npfrom pathlib import Pathfrom ultralytics.yolo.engine.model import YOLO # Parse command line argumentsparser = argparse.ArgumentParser()parser.add_argument('--source', type=str, required=True, help='Source image directory or file')parser.add_argument('--output', type=str, default='output', help='Output directory')args = parser.parse_args()# Create output directory if it doesn't existPath(args.output).mkdir(parents=True, exist_ok=True)# Model pathmodel_path = r'C:\\_Projects\\best_100img.pt'# Load your model directlymodel = YOLO(model_path)model.fuse()# Load image(s)if Path(args.source).is_dir(): image_paths = list(Path(args.source).rglob('*.tiff'))else: image_paths = [args.source]# Process each imagefor image_path in image_paths: img = cv2.imread(str(image_path)) if img is None: continue # Perform inference predictions = model.predict(image_path, save=True, save_txt=True) print("Processing complete.")
问题在于:返回的对象(predictions变量)中没有boxes, masks, keypoints等属性。
我想问的问题是:
- 为什么结果与文档描述的如此不同?
- 是否需要进行转换步骤?
回答:
根据评论中的信息,你使用的是旧版本的Ultralytics==8.0.0。实际上,它返回的结果是一个torch.Tensor
对象的列表,而不是ultralytics.engine.results.Results
对象,而后者才具有如boxes, masks, keypoints, probs, obb这样的参数。文档符合最新框架版本,现在是8.2.24,而8.0.0版本则是2023年1月的。
解决这个问题最简单的方法是升级Ultralytics版本到最新版本,这样你就可以得到文档中描述的所有结果参数。
如果某些情况不允许你进行更新,你将需要对从8.0.0版本返回的结果格式进行一些数据后处理,并理解返回的结果格式。
对象检测任务结果,版本==8.0.0
# 对于3个检测到的对象[tensor([[2.89000e+02, 7.10000e+01, 1.44000e+03, 5.07000e+02, 8.91113e-01, 2.00000e+00], [1.26700e+03, 6.00000e+01, 1.68200e+03, 3.19000e+02, 8.31055e-01, 2.00000e+00], [6.96000e+02, 0.00000e+00, 1.32200e+03, 1.31000e+02, 2.56836e-01, 7.00000e+00]], device='cuda:0')]# 其中每个数组存储6个值,前4个是像素单位:[x_centre, y_centre, box_width, box_height, confidence, class_id]# 为了便于操作,你可以运行results[0].tolist(),得到以下格式:[[289.0, 71.0, 1440.0, 507.0, 0.89111328125, 2.0], [1267.0, 60.0, 1682.0, 319.0, 0.8310546875, 2.0], [696.0, 0.0, 1322.0, 131.0, 0.2568359375, 7.0]]
对象分割任务结果,版本==8.0.0。与检测任务相同,但会增加第二个torch.Tensor,包含每个对象的分割掩码。
# 对于3个检测到的对象[[tensor([[1.23000e+02, 8.90000e+01, 4.21000e+02, 2.21000e+02, 2.55216e-01, 7.00000e+00], [1.26700e+03, 5.80000e+01, 1.68100e+03, 3.17000e+02, 8.04158e-01, 2.00000e+00], [2.70000e+02, 7.70000e+01, 1.46000e+03, 4.98000e+02, 8.19106e-01, 2.00000e+00]], device='cuda:0'), tensor([[[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]], [[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]], [[0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], ..., [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.], [0., 0., 0., ..., 0., 0., 0.]]], device='cuda:0')]]
了解从这个旧版本的Ultralytics接收到的数据格式后,你可以轻松地访问它们并转换为你需要的格式。但升级Ultralytics到最新版本仍然是获取该框架最有效使用的最佳方式。
安装Ultralytics:https://docs.ultralytics.com/quickstart/