sandbox338's picture
Upload inference.py with huggingface_hub
e803eba verified
# Simple inference script for wildlife ensemble detector
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"""
# You'll need to copy the model_architecture.py content here
# and register the backbones before loading
# Setup configs (simplified)
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)
# Get predictions from both models
outputs_inc = predictor_inception(img)
outputs_res = predictor_resnet(img)
# Combine predictions (simplified)
# Add your ensemble logic here
return outputs_inc, outputs_res