|
|
|
|
|
|
|
|
import torch |
|
|
import cv2 |
|
|
import numpy as np |
|
|
from detectron2 import model_zoo |
|
|
from detectron2.config import get_cfg |
|
|
from detectron2.engine import DefaultPredictor |
|
|
|
|
|
def load_ensemble_models(inception_path, resnet_path, class_names): |
|
|
"""Load both ensemble models for inference""" |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
cfg_inception = get_cfg() |
|
|
cfg_inception.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")) |
|
|
cfg_inception.MODEL.BACKBONE.NAME = "InceptionBackboneWrapper" |
|
|
cfg_inception.MODEL.ROI_HEADS.NUM_CLASSES = len(class_names) |
|
|
cfg_inception.MODEL.WEIGHTS = inception_path |
|
|
cfg_inception.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 |
|
|
|
|
|
cfg_resnet = get_cfg() |
|
|
cfg_resnet.merge_from_file(model_zoo.get_config_file("COCO-Detection/faster_rcnn_R_50_FPN_3x.yaml")) |
|
|
cfg_resnet.MODEL.BACKBONE.NAME = "ResNetBackboneWrapper" |
|
|
cfg_resnet.MODEL.ROI_HEADS.NUM_CLASSES = len(class_names) |
|
|
cfg_resnet.MODEL.WEIGHTS = resnet_path |
|
|
cfg_resnet.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5 |
|
|
|
|
|
predictor_inception = DefaultPredictor(cfg_inception) |
|
|
predictor_resnet = DefaultPredictor(cfg_resnet) |
|
|
|
|
|
return predictor_inception, predictor_resnet |
|
|
|
|
|
def predict_ensemble(image_path, predictor_inception, predictor_resnet, class_names): |
|
|
"""Run ensemble inference on an image""" |
|
|
|
|
|
img = cv2.imread(image_path) |
|
|
|
|
|
|
|
|
outputs_inc = predictor_inception(img) |
|
|
outputs_res = predictor_resnet(img) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
return outputs_inc, outputs_res |
|
|
|