Hsyshasuzh's picture
download
raw
3.63 kB
import unittest
from types import SimpleNamespace
from unittest.mock import patch
import numpy as np
from inference.api import Model
from inference.preprocess import load_rgb
from inference.tiling import iter_tiles, stitch
class Array:
def __init__(self,value):self.value=np.asarray(value)
def cpu(self):return self
def numpy(self):return self.value
class InferenceTests(unittest.TestCase):
def test_source_coordinates_and_duplicate_at_tile_boundary(self):
class Detector:
def __init__(self):self.calls=0
def predict(self,*args,**kwargs):
x=900-self.calls*768;self.calls+=1
return [SimpleNamespace(obb=SimpleNamespace(xyxyxyxy=Array([[[x,100],[x+50,100],[x+50,140],[x,140]]]),
cls=Array([0]),conf=Array([.9])))]
class Segmenter:
def predict(self,*args,**kwargs):return [np.zeros((1024,1024),np.uint8)]
model=Model.__new__(Model)
model.models={'detection':Detector(),'segmentation':Segmenter()}
model.mapping={0:4};model.names={4:'vehicle'};model.thresholds={'4':.5}
model.config={'tiling':{'nms_iou':.5},'segmentation_class_ids':[1,2,12,13,14,15,16]}
with patch('inference.api.semantic_array',side_effect=lambda x:x):
result=model.predict(np.full((1024,1792,3),100,np.uint8))
self.assertEqual(len(result['objects']),1)
self.assertEqual(result['objects'][0]['bbox'],[900.,100.,50.,40.])
self.assertTrue(np.all(result['regions']==1))
self.assertEqual(result['regions'].shape,(1024,1792))
model.models['detection']=Detector()
model.profile={'name':'precision_first'};model.abstained={4};model.thresholds={}
with patch('inference.api.semantic_array',side_effect=lambda x:x):
abstained=model.predict(np.full((1024,1792,3),100,np.uint8))
self.assertEqual(abstained['objects'],[])
self.assertEqual(abstained['detection_policy']['abstained_class_ids'],[4])
self.assertFalse(abstained['detection_policy']['certified_99_precision'])
def test_precision_profile_cannot_be_overridden_silently(self):
model=Model.__new__(Model)
model.profile={'name':'precision_first'}
with self.assertRaises(ValueError):
model.predict(np.zeros((10,10,3),np.uint8),confidence=.01)
def test_same_tile_predictions_are_not_suppressed(self):
from inference.fusion import fuse
obj={'category_id':4,'confidence':.9,'obb_polygon':[0,0,10,0,10,10,0,10],'_tile':(0,0)}
self.assertEqual(len(fuse([obj,{**obj,'confidence':.8}])),2)
self.assertNotIn('_tile',fuse([obj])[0])
def test_padding_and_categorical_stitch_preserve_invalid_pixels(self):
image=np.ones((100,200,3),np.uint8)
tile=next(iter_tiles(image))
self.assertEqual(tile.image.shape,(1024,1024,3))
self.assertFalse(tile.image[100:].any())
mask=np.full((100,200),255,np.uint8);weights=np.zeros_like(mask,dtype=np.uint16)
valid=np.ones((1024,1024),bool);valid[:10]=False
stitch(mask,weights,tile,np.full((1024,1024),12,np.uint8),valid)
self.assertTrue(np.all(mask[:10]==255));self.assertTrue(np.all(mask[10:]==12))
with self.assertRaises(ValueError):list(iter_tiles(image,overlap=.9))
def test_rgb_validation_and_black_nodata(self):
with self.assertRaises(ValueError):load_rgb(np.zeros((4,4),np.uint8))
with self.assertRaises(ValueError):load_rgb(np.ones((4,4,3),np.float32))
_,valid=load_rgb(np.zeros((4,4,3),np.uint8))
self.assertFalse(valid.any())

Xet Storage Details

Size:
3.63 kB
·
Xet hash:
dbc4a55183e52fe69877b42fb561c76c8612155dc08d05c6827d2c18caaf8dfa

Xet efficiently stores files, intelligently splitting them into unique chunks and accelerating uploads and downloads. More info.