You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

不同Backbone的Faster R-CNN模型推理结果一致问题求助

问题:更换Faster R-CNN Backbone后推理结果与原模型完全一致

我基于自定义目标检测的Python程序,仅修改目录路径和标签列表适配自有数据集。先训练了ResNet50 Backbone的Faster R-CNN模型,推理结果正常。随后复制程序到新目录,将Backbone改为MobileNet_v3_large_320_fpn并更新对应路径,训练过程正常且损失值与ResNet50版本不同,但推理结果却与前者完全一致。

不同Backbone的模型理论上不应输出完全相同的推理结果,目前未找到问题原因。

环境配置

  • torch 1.13.1+cu116
  • torchvision 0.14.1+cu116
  • Python 3.10.4
  • Windows 10

已排查步骤

  • 确认所有目录路径配置正确
  • 执行pip cache purge清理Python缓存,问题仍未解决

推理代码

import numpy as np
import cv2
import torch
import glob as glob

from model import create_model

# set the computation device
device = torch.device('cuda') if torch.cuda.is_available() else torch.device('cpu')
# load the model and the trained weights
model = create_model(num_classes=6).to(device)
model.load_state_dict(torch.load(
    'E:\\magisterka_part_2\\Faster - RCNN\\outputs\\model100.pth', map_location=device 
))
model.eval()

# directory where all the images are present
DIR_TEST = 'E:/magisterka_part_2/Faster - RCNN/valid'
test_images = glob.glob(f"{DIR_TEST}/*")
print(f"Test instances: {len(test_images)}")

# classes: 0 index is reserved for background
CLASSES = [
    'background', 'healthy', 'Black_spot', 'Canker', 'Greening', 'Scab'
]

# define the detection threshold...
# ... any detection having score below this will be discarded
detection_threshold = 0.7

for i in range(len(test_images)):
    # get the image file name for saving output later on
    image_name = test_images[i].split('/')[-1].split('.')[0]
    image = cv2.imread(test_images[i])
    orig_image = image.copy()
    # BGR to RGB
    image = cv2.cvtColor(orig_image, cv2.COLOR_BGR2RGB).astype(np.float32)
    # make the pixel range between 0 and 1
    image /= 255.0
    # bring color channels to front
    image = np.transpose(image, (2, 0, 1)).astype(np.cfloat)
    # convert to tensor
    image = torch.tensor(image, dtype=torch.float).cuda()
    # add batch dimension
    image = torch.unsqueeze(image, 0)
    with torch.no_grad():
        outputs = model(image)
    
    # load all detection to CPU for further operations
    outputs = [{k: v.to('cpu') for k, v in t.items()} for t in outputs]
    # carry further only if there are detected boxes
    if len(outputs[0]['boxes']) != 0:
        boxes = outputs[0]['boxes'].data.numpy()
        scores = outputs[0]['scores'].data.numpy()
        # filter out boxes according to `detection_threshold`
        boxes = boxes[scores >= detection_threshold].astype(np.int32)
        draw_boxes = boxes.copy()
        # get all the predicited class names
        pred_classes = [CLASSES[i] for i in outputs[0]['labels'].cpu().numpy()]
        
        # draw the bounding boxes and write the class name on top of it
        for j, box in enumerate(draw_boxes):
            cv2.rectangle(orig_image,
                        (int(box[0]), int(box[1])),
                        (int(box[2]), int(box[3])),
                        (0, 0, 255), 2)
            cv2.putText(orig_image , pred_classes[j] + str(scores[j]), 
                        (int(box[0]), int(box[1]+15)),
                        cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 
                        2, lineType=cv2.LINE_AA)

        cv2.imshow('Prediction', orig_image)
        cv2.waitKey(1)
        #path = '../test_predictions/'.join(f'{image_name}.jpg',)
        cv2.imwrite('../test_predictions/' + image_name + '.jpg', orig_image)
        #    raise Exception("Could not write image")
    print(f"Image {i+1} done...")
    print('-'*50)

print('TEST PREDICTIONS COMPLETE')
cv2.destroyAllWindows()

内容的提问来源于stack exchange,提问作者Rudy Robot

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.22 10:35:39