A simple retinanet_R_101_FPN model to detect (bounding box) and classify insects between these four species:
- Animalia Arthropoda Insecta Coleoptera Curculionidae Cactophagus spinolae
- Animalia Arthropoda Insecta Coleoptera Curculionidae Cyrtepistomus castaneus
- Animalia Arthropoda Insecta Coleoptera Curculionidae Diaprepes abbreviatus
- Animalia Arthropoda Insecta Coleoptera Curculionidae Larinus carlinae
inference example, requires detectron2
from detectron2.config import get_cfg
from detectron2.engine import DefaultPredictor
from detectron2 import model_zoo
import cv2
# edit all of these
image = cv2.imread("../../Datasets/inaturalist2021/custom2/val/imgs/01.jpg")
cfg = get_cfg()
cfg.merge_from_file(model_zoo.get_config_file('COCO-Detection/retinanet_R_101_FPN_3x.yaml'))
cfg.MODEL.WEIGHTS = './out3/model_0002899.pth'
cfg.MODEL.DEVICE = 'cuda:0'
predictor = DefaultPredictor(cfg)
outputs = predictor(image)
threshold = 0.5
classes = ["Curculionidae Cactophagus spinolae", "Curculionidae Cyrtepistomus castaneus", "Curculionidae Diaprepes abbreviatus", "Curculionidae Larinus carlinae"]
# Display predictions
preds = outputs["instances"].pred_classes.tolist()
scores = outputs["instances"].scores.tolist()
bboxes = outputs["instances"].pred_boxes
print(preds,scores,bboxes)
for j, bbox in enumerate(bboxes):
bbox = bbox.tolist()
score = scores[j]
pred = preds[j]
if pred > 3:
continue
text = classes[pred]+ " "+ str(score)[:5]
if score > threshold:
x1, y1, x2, y2 = [int(i) for i in bbox]
cv2.rectangle(image, (x1, y1), (x2, y2), (0, 0, 255), 1)
if y1 < 5:
y1 = 10
image = cv2.putText(image, text, (0, y1), cv2.FONT_HERSHEY_SIMPLEX ,
0.5, (0, 0, 255) , 1, cv2.LINE_AA)
print(text)
cv2.imshow('image', image)
cv2.waitKey(0)