Skip to content

Commit

Permalink
Add CI tests for detr-resnet50 inference
Browse files Browse the repository at this point in the history
  • Loading branch information
sandeep-maddipatla committed Sep 17, 2024
1 parent bdd8f54 commit 0237edd
Showing 1 changed file with 59 additions and 6 deletions.
65 changes: 59 additions & 6 deletions tests/test_object_detection.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,21 @@ class GaudiDETRTester(TestCase):
Tests for Object Detection - DETR
"""

def get_expected_loc(self, mode="default"):
expected_location_def = np.array([344.0622, 24.8543, 640.3398, 373.7401])
expected_location_ac = np.array([342, 25.25, 636, 376])
modeCheck = True if mode in ["default", "autocast"] else False
self.assertEqual(modeCheck, True)
if mode == "default":
return expected_location_def
else:
return expected_location_ac

def get_expected_num_boxes(self):
# For image http://images.cocodataset.org/val2017/000000039769.jpg
# the expected result is 5
return 5

def prepare_model_and_processor(self):
model = DetrForObjectDetection.from_pretrained("facebook/detr-resnet-101").to("hpu")
model = model.eval()
Expand All @@ -62,9 +77,9 @@ def test_inference_default(self):
target_sizes = torch.Tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
boxes = results["boxes"]
self.assertEqual(len(boxes), 5)
expected_location = np.array([344.0622, 24.8543, 640.3398, 373.7401])
self.assertLess(np.abs(boxes[0].cpu().detach().numpy() - expected_location).max(), 1)
self.assertEqual(len(boxes), self.get_expected_num_boxes())
expected_loc = self.get_expected_loc()
self.assertLess(np.abs(boxes[0].cpu().detach().numpy() - expected_loc).max(), 1)

def test_inference_autocast(self):
model, processor = self.prepare_model_and_processor()
Expand All @@ -76,8 +91,9 @@ def test_inference_autocast(self):
target_sizes = torch.Tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs, target_sizes=target_sizes, threshold=0.9)[0]
boxes = results["boxes"]
self.assertEqual(len(boxes), 5)
expected_location = np.array([342, 25.25, 636, 376])
self.assertEqual(len(boxes), self.get_expected_num_boxes())
print(f"boxes = {boxes}")
expected_location = self.get_expected_loc(mode="autocast")
self.assertLess(np.abs(boxes[0].to(torch.float32).cpu().detach().numpy() - expected_location).max(), 5)

def test_inference_hpu_graphs(self):
Expand All @@ -91,8 +107,9 @@ def test_inference_hpu_graphs(self):
target_sizes = torch.Tensor([image.size[::-1]])
results = processor.post_process_object_detection(outputs=outputs, target_sizes=target_sizes, threshold=0.1)
boxes = results[0]["boxes"]
print(f"boxes = {boxes}")
self.assertEqual(len(boxes), 5)
expected_location = np.array([344.0622, 24.8543, 640.3398, 373.7401])
expected_location = self.get_expected_loc()
self.assertLess(np.abs(boxes[0].to(torch.float32).cpu().detach().numpy() - expected_location).max(), 1)

def test_no_latency_regression_autocast(self):
Expand Down Expand Up @@ -121,3 +138,39 @@ def test_no_latency_regression_autocast(self):

latency = total_model_time * 1000 / iterations # in terms of ms
self.assertLessEqual(latency, TIME_PERF_FACTOR * LATENCY_DETR_BF16_GRAPH_BASELINE)


class GaudiDetrResnet50_Tester(GaudiDETRTester):
"""
Tests for Custom Configuration of Detr-Resnet50 model
"""

def get_num_labels(self):
# COCO-2017, the dataset the test is based on uses 91 labels
# TODO: automate this from dataset info. For now, just return this number
return 91

def prepare_model_and_processor(self):
model = DetrForObjectDetection.from_pretrained(
"facebook/detr-resnet-50",
revision="no_timm",
num_labels=self.get_num_labels(),
ignore_mismatched_sizes=True,
)
model = model.to("hpu")
model = model.eval()
processor = AutoProcessor.from_pretrained("facebook/detr-resnet-50")
return model, processor

def get_expected_loc(self, mode="default"):
# Reference: first box co-ordinates listed in model card:
# https://huggingface.co/facebook/detr-resnet-50#how-to-use
expected_location = np.array([40.16, 70.81, 175.55, 117.98])
modeCheck = True if mode in ["default", "autocast"] else False
self.assertEqual(modeCheck, True)
return expected_location

def get_expected_num_boxes(self):
# For image http://images.cocodataset.org/val2017/000000039769.jpg
# the expected result is 5
return 5

0 comments on commit 0237edd

Please sign in to comment.