FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead.
FutureWarning是一个Python库(比如PyTorch中的警告)提示用户,某个函数在未来版本可能会发生变化或者停止使用。在这个例子中,torch.cuda.amp.autocast()
函数被标记为过时,并推荐使用torch.amp.autocast('cuda', args...)
代替。autocast()
是自动混合精度训练(Automatic Mixed Precision, AMP)的一部分,它允许你在保持高性能的同时,使用半精度浮点数(FP16)来节省计算资源。旧版建议是为了向兼容更高版本的行为并避免潜在的问题。
y:362: FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead.
FutureWarning
是 Python 中的一种警告类型,它通常用于告知开发者某个功能在未来某个版本中将会被修改或删除。在你提供的例子中,FutureWarning
警告用户 torch.cuda.amp.autocast(args...)
已经被弃用。
具体到这个警告,它告诉用户在 PyTorch 中使用自动混合精度(Automatic Mixed Precision, AMP)的 autocast
功能时,应该使用新的调用方式。原来的方式 torch.cuda.amp.autocast(args...)
是在 CUDA 上启用自动混合精度的函数,但现在已经不推荐使用了。开发者应该将代码更新为使用新的方法 torch.amp.autocast('cuda', args...)
,这里的 'cuda'
指定了计算在 CUDA 设备上执行,而 args
是传入的其他参数。
更新代码并使用新的 autocast
函数可以确保在 PyTorch 更新后代码的兼容性,并且可以继续利用 PyTorch 的自动混合精度功能来提高计算性能,同时减少内存使用。
关键点数量: 146, 描述符数量: 146 /home/dxinl/slam+yolo/./models/common.py:894: FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead. with amp.autocast(autocast): BOW计算错误: OpenCV(4.11.0) /io/opencv/modules/features2d/src/bagofwords.cpp:205: error: (-215:Assertion failed) queryIdx == (int)i in function 'compute' 关键点数量: 140, 描述符数量: 140 /home/dxinl/slam+yolo/./models/common.py:894: FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead. with amp.autocast(autocast): BOW计算错误: OpenCV(4.11.0) /io/opencv/modules/features2d/src/bagofwords.cpp:205: error: (-215:Assertion failed) queryIdx == (int)i in function 'compute' 关键点数量: 127, 描述符数量: 127 /home/dxinl/slam+yolo/./models/common.py:894: FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead. with amp.autocast(autocast): BOW计算错误: OpenCV(4.11.0) /io/opencv/modules/features2d/src/bagofwords.cpp:205: error: (-215:Assertion failed) queryIdx == (int)i in function 'compute' 关键点数量: 134, 描述符数量: 134 /home/dxinl/slam+yolo/./models/common.py:894: FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead. with amp.autocast(autocast): BOW计算错误: OpenCV(4.11.0) /io/opencv/modules/features2d/src/bagofwords.cpp:205: error: (-215:Assertion failed) queryIdx == (int)i in function 'compute' 关键点数量: 140, 描述符数量: 140 当前地图包含 4132 个点 /home/dxinl/slam+yolo/./models/common.py:894: FutureWarning: `torch.cuda.amp.autocast(args...)` is deprecated. Please use `torch.amp.autocast('cuda', args...)` instead. with amp.autocast(autocast): BOW计算错误: OpenCV(4.11.0) /io/opencv/modules/features2d/src/bagofwords.cpp:205: error: (-215:Assertion failed) queryIdx == (int)i in function 'compute' 关键点数量: 142, 描述符数量: 142 /home/dxinl/slam+yolo/./models/common.py:894: FutureWarning: `torch.cuda.amp.autocast(args..
首先,关于OpenCV的BagOfWords在调用compute方法时出现的断言错误,错误信息是“queryIdx == (int)i”。根据经验,这可能与特征点的索引不匹配有关。在BOW训练过程中,每个描述符应该对应正确的图像索引。用户可能在准备训练数据时,没有正确地将每个描述符与其对应的图像ID关联起来。例如,如果使用cv2.BOWTrainer.add方法时,应该确保每个描述符数组都来自同一图像,并且多次调用add来添加不同图像的数据。如果一次性添加了所有描述符而没有正确分组,就会导致queryIdx不匹配。我需要确认用户是否正确构建了训练数据,每个描述符数组对应一个图像,并且图像索引正确。
接下来是PyTorch中的autocast弃用警告。用户可能在使用torch.cuda.amp.autocast时启用了async参数,而该参数在较新版本中已被弃用。例如,旧代码可能是这样写的:with torch.cuda.amp.autocast(async=True):。从PyTorch 1.10开始,async参数改为non_blocking,所以需要将async=True改为non_blocking=True。或者直接移除该参数,因为默认行为可能已经改变。需要检查用户的代码中是否有设置async=True的地方,并修改为正确的参数。此外,还要确认用户安装的PyTorch版本是否过旧,建议升级到稳定版本,如1.10或更高,以确保兼容性。
然后,我需要验证这些解决方案是否正确。对于OpenCV的问题,确保每个图像的特征描述符单独添加,并正确关联image_id。例如,使用循环遍历每个图像的特征,逐个添加到BOW训练器中。对于PyTorch的问题,检查autocast上下文管理器中的参数,并更新代码。同时,建议用户检查CUDA和PyTorch的版本兼容性,可能需要更新或重新安装正确的版本。
最后,需要确保回答中引用了用户提供的参考内容。例如,在解决PyTorch安装问题时,引用用户提供的conda安装命令,但可能需要调整版本号。同时,如果用户需要编译OpenCV,可以参考提供的指南链接。但用户当前的问题更多是代码层面的调整,而不是安装或编译问题,所以引用可能有限,但需要适当提及。### OpenCV BOW计算错误解决方案
问题分析:
cv::BOWTrainer::compute
出现queryIdx == (int)i
断言错误,通常由特征描述符与图像索引不匹配引起。在BOW训练过程中,每个描述符必须正确关联其所属图像的索引[^1]。
解决步骤:
检查特征提取逻辑
确保每个图像的描述符单独存储,使用循环结构逐个添加:bow_trainer = cv2.BOWKMeansTrainer(cluster_count) for img in image_set: kp, des = feature_detector.detectAndCompute(img, None) if des is not None: bow_trainer.add(des) # 每个图像的描述符单独添加
验证聚类过程
使用标准化方法获取词汇表:vocabulary = bow_trainer.cluster()
检查特征匹配器配置
确保cv2.BOWImgDescriptorExtractor
正确配置:bow_extractor = cv2.BOWImgDescriptorExtractor(feature_extractor, descriptor_matcher) bow_extractor.setVocabulary(vocabulary)
PyTorch autocast弃用警告解决方案
问题分析:
torch.cuda.amp.autocast
的async
参数在PyTorch 1.10+版本中已弃用,需改为non_blocking
参数[^2]。
修改方案:
参数更新
# 旧版写法 (已弃用) with torch.cuda.amp.autocast(async=True): # 前向计算 # 新版写法 with torch.cuda.amp.autocast(enabled=True): # 移除async参数 # 前向计算
环境升级建议
建议使用conda更新PyTorch环境:conda install pytorch=1.13.0 torchvision cudatoolkit=11.6 -c pytorch
CUDA兼容性验证
检查CUDA与PyTorch版本对应关系:print(torch.__version__) print(torch.cuda.is_available())
相关推荐















