Im using a python program from this tutorial. I've copied it as is and made changes to directories and label list so that the program takes in my dataset. The first Faster R-CNN model was trained with the ResNet50 backbone. I have trained it, I have run inference and everything was good. The next thing I did, was copying the program to another direcotrie. I changed the backbone to MobileNet_v3_large_320_fpn and also the paths. Training ran fine, it produced different train loss and validation loss values than the ResNet50 backbone. Then I ran inference again and to my surprise I got the same results as with the ResNet50 backbone.
I doubt that both models would produce the same results during inference. Im not sure on what is causing this problem.
I use torch version 1.13.1+cu116 and torchvision version 0.14.1+cu116 and python 3.10.4. Also Win 10.
My first thought was that I forgot to change some paths to direcories and checked that, but everything had the correct path.
My second thought was that python somehow caches the results from the inference run with the ResNet50 backbone and just copies the results for the model with MobileNet. I tried 'pip cache purge' but again, it did nothing.
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()
This is the code