2 Faster R-CNN 模型具有不同的主干,在推理过程中产生相同的结果

问题描述 投票:0回答:1

我正在使用 本教程 中的 python 程序。我按原样复制了它,并对目录和标签列表进行了更改,以便程序接收我的数据集。第一个 Faster R-CNN 模型是使用 ResNet50 主干训练的。我训练了它,进行了推理,一切都很好。接下来我做的是将程序复制到另一个目录。我将主干更改为 MobileNet_v3_large_320_fpn 以及路径。训练运行良好,它产生了与 ResNet50 主干不同的训练损失和验证损失值。然后我再次运行推理,令我惊讶的是我得到了与 ResNet50 主干相同的结果。

我怀疑这两个模型在推理过程中会产生相同的结果。我不确定是什么导致了这个问题。

我用的是torch版本1.13.1+cu116和torchvision版本0.14.1+cu116和python 3.10.4。也赢 10.

我的第一个想法是我忘记更改一些目录路径并检查了它,但一切都有正确的路径。

我的第二个想法是,python 以某种方式缓存了使用 ResNet50 主干进行的推理运行的结果,并只复制了使用 MobileNet 的模型的结果。我尝试了“pip cache purge”,但又一次,它什么也没做。

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()

这是代码

python pytorch torchvision faster-rcnn
1个回答
0
投票

“相同的结果”是什么意思?置信度分数是否完全相等?如果没有一些代码,其他人也很难查明问题出在哪里,如果有的话。

© www.soinside.com 2019 - 2024. All rights reserved.