iKrishneel / efficient_net_v2

Pytorch implementation of efficientnet v2 backbone with detectron2 for object detection (Just for fun)
https://arxiv.org/pdf/2104.00298.pdf
11 stars 3 forks source link

How to perform testing on COCO dataset? #2

Closed Alex123Pr closed 3 years ago

Alex123Pr commented 3 years ago

Hey! Firstly Great work! I was able to successfully train on the COCO dataset. But I am currently puzzled on how to test my model. I was trying to do it like a basic detectron2 model but was facing errors so I was hoping if you could shed some light.

akashAD98 commented 3 years ago

Pass the weight & cfg file

import numpy as np
import os, cv2
from detectron2.engine import DefaultPredictor
from detectron2.config import get_cfg
from detectron2.utils.visualizer import Visualizer
from detectron2.utils.visualizer import ColorMode
from detectron2.data import MetadataCatalog
os.environ['DISPLAY'] = ':0'

from efficient_net_v2.model.backbone import build_effnet_backbone

cfg = get_cfg()
cfg.merge_from_file("D:/TFOD_efficientdet/efficient_net_v2/efficient_net_v2/op_DIRmodel_weights_first_try/config.yaml")
cfg.MODEL.ROI_HEADS.SCORE_THRESH_TEST = 0.5  # set threshold for this model
cfg.MODEL.WEIGHTS = (r"D:\TFOD_efficientdet\efficient_net_v2\efficient_net_v2\op_DIRmodel_weights_first_try\model_final.pth")

img_directory=r"D:\TFOD_efficientdet\efficient_net_v2\efficient_net_v2\datasets\coco\val2017"
img_list=os.listdir(img_directory)

for img in img_list:
    im = cv2.imread(str(img_directory)+'/'+str(img))
    predictor = DefaultPredictor(cfg)
    outputs = predictor(im)
    print(str("Classes:"),outputs["instances"].pred_classes)
    print(str("Bounding Boxes"),outputs["instances"].pred_boxes)

    v = Visualizer(im[:, :, ::-1], MetadataCatalog.get(cfg.DATASETS.TRAIN[0]), scale=0.8, instance_mode=ColorMode.IMAGE)
    out = v.draw_instance_predictions(outputs["instances"].to("cpu"))
    cv2.imwrite('D:/TFOD_efficientdet/efficient_net_v2/efficient_net_v2/datasets/coco/final_weight_test/output_of_{}.jpg'.format(str(img).split('.')[0]),out.get_image()[:, :, ::-1])
cv2.destroyAllWindows()
iKrishneel commented 3 years ago

@Alex123Pr The evaluation function for the coco is #L162. You will need to provide test in config like here.

To visualize you should use the detectron2.utils.visualizer.Visualizer

metadata = MetadataCatalog.get(cfg.DATASETS.TEST[0])
visualizer = Visualizer(image, metadata=metadata, scale=1.0)
viz = visualizer.overlay_instances(labels=labels, boxes=bboxes, masks=masks)

image = viz.get_image()