Skip to content

Commit

Permalink
fix numpy version compatibility (#327)
Browse files Browse the repository at this point in the history
* fix numpy version compatibility
  • Loading branch information
Cathy0908 authored Aug 18, 2023
1 parent 915bb73 commit db33ced
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 44 deletions.
4 changes: 2 additions & 2 deletions easycv/core/evaluation/custom_cocotools/cocoeval.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,8 +467,8 @@ def accumulate(self, p=None):
fps = np.logical_and(
np.logical_not(dtm), np.logical_not(dtIg))

tp_sum = np.cumsum(tps, axis=1).astype(dtype=np.float)
fp_sum = np.cumsum(fps, axis=1).astype(dtype=np.float)
tp_sum = np.cumsum(tps, axis=1).astype(dtype=np.float32)
fp_sum = np.cumsum(fps, axis=1).astype(dtype=np.float32)
for t, (tp, fp) in enumerate(zip(tp_sum, fp_sum)):
tp = np.array(tp)
fp = np.array(fp)
Expand Down
2 changes: 1 addition & 1 deletion easycv/datasets/face/pipelines/face_keypoint_transform.py
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,7 @@ def aug_clr_noise_blur(self, img):
skin_factor_list = [0.6, 0.8, 1.0, 1.2, 1.4]
skin_factor = np.random.choice(skin_factor_list)
img_ycrcb_raw[:, :, 0:1] = np.clip(
img_ycrcb_raw[:, :, 0:1].astype(np.float) * skin_factor, 0,
img_ycrcb_raw[:, :, 0:1].astype(np.float32) * skin_factor, 0,
255).astype(np.uint8)
img = cv2.cvtColor(img_ycrcb_raw, cv2.COLOR_YCR_CB2BGR)

Expand Down
2 changes: 1 addition & 1 deletion easycv/models/utils/pos_embed.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def get_1d_sincos_pos_embed_from_grid(embed_dim, pos):
out: (M, D)
"""
assert embed_dim % 2 == 0
omega = np.arange(embed_dim // 2, dtype=np.float)
omega = np.arange(embed_dim // 2, dtype=np.float32)
omega /= embed_dim / 2.
omega = 1. / 10000**omega # (D/2,)

Expand Down
2 changes: 1 addition & 1 deletion easycv/thirdparty/mot/bytetrack/byte_tracker.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class STrack(BaseTrack):
def __init__(self, tlwh, score):

# wait activate
self._tlwh = np.asarray(tlwh, dtype=np.float)
self._tlwh = np.asarray(tlwh, dtype=np.float32)
self.kalman_filter = None
self.mean, self.covariance = None, None
self.is_activated = False
Expand Down
12 changes: 6 additions & 6 deletions easycv/thirdparty/mot/bytetrack/matching.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,15 +86,15 @@ def ious(atlbrs, btlbrs):
:rtype ious np.ndarray
"""
ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float)
ious = np.zeros((len(atlbrs), len(btlbrs)), dtype=np.float32)
if ious.size == 0:
return ious

from cython_bbox import bbox_overlaps as bbox_ious

ious = bbox_ious(
np.ascontiguousarray(atlbrs, dtype=np.float),
np.ascontiguousarray(btlbrs, dtype=np.float))
np.ascontiguousarray(atlbrs, dtype=np.float32),
np.ascontiguousarray(btlbrs, dtype=np.float32))

return ious

Expand Down Expand Up @@ -151,15 +151,15 @@ def embedding_distance(tracks, detections, metric='cosine'):
:return: cost_matrix np.ndarray
"""

cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float)
cost_matrix = np.zeros((len(tracks), len(detections)), dtype=np.float32)
if cost_matrix.size == 0:
return cost_matrix
det_features = np.asarray([track.curr_feat for track in detections],
dtype=np.float)
dtype=np.float32)
#for i, track in enumerate(tracks):
#cost_matrix[i, :] = np.maximum(0.0, cdist(track.smooth_feat.reshape(1,-1), det_features, metric))
track_features = np.asarray([track.smooth_feat for track in tracks],
dtype=np.float)
dtype=np.float32)
cost_matrix = np.maximum(0.0, cdist(track_features, det_features,
metric)) # Nomalized features
return cost_matrix
Expand Down
4 changes: 2 additions & 2 deletions easycv/version.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,5 +2,5 @@
# GENERATED VERSION FILE
# TIME: Thu Nov 5 14:17:50 2020

__version__ = '0.11.3'
short_version = '0.11.3'
__version__ = '0.11.4'
short_version = '0.11.4'
52 changes: 39 additions & 13 deletions tests/test_core/evaluation/test_coco_tools.py
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ def testCocoWrappers(self):
def testExportGroundtruthToCOCO(self):
image_ids = ['first', 'second']
groundtruth_boxes = [
np.array([[100, 100, 200, 200]], np.float),
np.array([[50, 50, 100, 100]], np.float)
np.array([[100, 100, 200, 200]], np.float32),
np.array([[50, 50, 100, 100]], np.float32)
]
groundtruth_classes = [
np.array([1], np.int32),
Expand Down Expand Up @@ -126,12 +126,12 @@ def testExportGroundtruthToCOCO(self):
def testExportDetectionsToCOCO(self):
image_ids = ['first', 'second']
detections_boxes = [
np.array([[100, 100, 200, 200]], np.float),
np.array([[50, 50, 100, 100]], np.float)
np.array([[100, 100, 200, 200]], np.float32),
np.array([[50, 50, 100, 100]], np.float32)
]
detections_scores = [
np.array([.8], np.float),
np.array([.7], np.float)
np.array([.8], np.float32),
np.array([.7], np.float32)
]
detections_classes = [np.array([1], np.int32), np.array([1], np.int32)]
categories = [{
Expand All @@ -152,15 +152,28 @@ def testExportDetectionsToCOCO(self):
detections_classes,
categories,
output_path=output_path)
self.assertListEqual(result, self._detections_list)

self.assertEqual(len(result), len(detections_boxes))
self.assertEqual(len(detections_boxes), len(detections_boxes))

score_list = []
for i in range(len(detections_boxes)):
score = self._detections_list[i].pop('score')
score_list.append(score)
self.assertAlmostEqual(result[i].pop('score'), score)
self.assertDictEqual(result[i], self._detections_list[i])

with io.open(output_path, 'r') as f:
written_result = f.read()
# The json output should have floats written to 4 digits of precision.
matcher = re.compile(r'"bbox":\s+\[\n\s+\d+.\d\d\d\d,',
re.MULTILINE)
self.assertTrue(matcher.findall(written_result))
written_result = json.loads(written_result)
self.assertAlmostEqual(result, written_result)
for i in range(len(result)):
self.assertAlmostEqual(written_result[i].pop('score'),
score_list[i])
self.assertDictEqual(result[i], written_result[i])

def testExportSegmentsToCOCO(self):
image_ids = ['first', 'second']
Expand All @@ -176,7 +189,10 @@ def testExportSegmentsToCOCO(self):
for i, detection_mask in enumerate(detection_masks):
detection_masks[i] = detection_mask[:, :, :, None]

detection_scores = [np.array([.8], np.float), np.array([.7], np.float)]
detection_scores = [
np.array([.8], np.float32),
np.array([.7], np.float32)
]
detection_classes = [np.array([1], np.int32), np.array([1], np.int32)]

categories = [{
Expand All @@ -202,7 +218,12 @@ def testExportSegmentsToCOCO(self):
written_result = json.loads(written_result)
mask_load = mask.decode([written_result[0]['segmentation']])
self.assertTrue(np.allclose(mask_load, detection_masks[0]))
self.assertAlmostEqual(result, written_result)
self.assertEqual(len(result), len(detection_masks))
self.assertEqual(len(written_result), len(detection_masks))
for i in range(len(detection_masks)):
self.assertAlmostEqual(result[i].pop('score'),
written_result[i].pop('score'))
self.assertDictEqual(result[i], written_result[i])

def testExportKeypointsToCOCO(self):
image_ids = ['first', 'second']
Expand All @@ -216,8 +237,8 @@ def testExportKeypointsToCOCO(self):
]

detection_scores = [
np.array([.8, 0.2], np.float),
np.array([.7, 0.3], np.float)
np.array([.8, 0.2], np.float32),
np.array([.7, 0.3], np.float32)
]
detection_classes = [
np.array([1, 1], np.int32),
Expand Down Expand Up @@ -248,7 +269,12 @@ def testExportKeypointsToCOCO(self):
with io.open(output_path, 'r') as f:
written_result = f.read()
written_result = json.loads(written_result)
self.assertAlmostEqual(result, written_result)
self.assertEqual(len(result), 4)
self.assertEqual(len(written_result), 4)
for i in range(4):
self.assertAlmostEqual(result[i].pop('score'),
written_result[i].pop('score'))
self.assertDictEqual(result[i], written_result[i])

def testSingleImageDetectionBoxesExport(self):
boxes = np.array([[0, 0, 1, 1], [0, 0, .5, .5], [.5, .5, 1, 1]],
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models/detection/yolox/test_yolox.py
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,9 @@ def test_yolox(self):
}
output = model(imgs, mode='train', **kwargs)
self.assertEqual(output['img_h'].cpu().numpy(),
np.array(640, dtype=np.float))
np.array(640, dtype=np.float32))
self.assertEqual(output['img_w'].cpu().numpy(),
np.array(640, dtype=np.float))
np.array(640, dtype=np.float32))
self.assertEqual(output['total_loss'].shape, torch.Size([]))
self.assertEqual(output['iou_l'].shape, torch.Size([]))
self.assertEqual(output['conf_l'].shape, torch.Size([]))
Expand Down
4 changes: 2 additions & 2 deletions tests/test_models/detection/yolox_edge/test_yolox_edge.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,9 @@ def test_yolox_edge(self):
}
output = model(imgs, mode='train', **kwargs)
self.assertEqual(output['img_h'].cpu().numpy(),
np.array(640, dtype=np.float))
np.array(640, dtype=np.float32))
self.assertEqual(output['img_w'].cpu().numpy(),
np.array(640, dtype=np.float))
np.array(640, dtype=np.float32))
self.assertEqual(output['total_loss'].shape, torch.Size([]))
self.assertEqual(output['iou_l'].shape, torch.Size([]))
self.assertEqual(output['conf_l'].shape, torch.Size([]))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,11 @@
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.cv.image_utils import panoptic_seg_masks_to_image
from modelscope.utils.demo_utils import DemoCompatibilityCheck
from modelscope.utils.test_utils import test_level
from tests.ut_config import BASE_LOCAL_PATH


class EasyCVPanopticSegmentationPipelineTest(unittest.TestCase,
DemoCompatibilityCheck):
class EasyCVPanopticSegmentationPipelineTest(unittest.TestCase):
img_path = os.path.join(
BASE_LOCAL_PATH, 'data/test_images/image_semantic_segmentation.jpg')

Expand All @@ -32,10 +30,6 @@ def test_r50(self):
cv2.imwrite(tmp_save_path, draw_img)
print('print ' + self.model_id + ' success')

@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_demo_compatibility(self):
self.compatibility_check()


if __name__ == '__main__':
unittest.main()
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,12 @@
from modelscope.pipelines import pipeline
from modelscope.utils.constant import Tasks
from modelscope.utils.cv.image_utils import semantic_seg_masks_to_image
from modelscope.utils.demo_utils import DemoCompatibilityCheck
from modelscope.utils.test_utils import test_level
from PIL import Image
from tests.ut_config import BASE_LOCAL_PATH


class EasyCVSegmentationPipelineTest(unittest.TestCase,
DemoCompatibilityCheck):
class EasyCVSegmentationPipelineTest(unittest.TestCase):
img_path = os.path.join(BASE_LOCAL_PATH,
'data/test_images/image_segmentation.jpg')

Expand Down Expand Up @@ -82,10 +80,6 @@ def test_segformer_b5(self):
model_id = 'damo/cv_segformer-b5_image_semantic-segmentation_coco-stuff164k'
self._internal_test_(model_id)

@unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
def test_demo_compatibility(self):
self.compatibility_check()


if __name__ == '__main__':
unittest.main()

0 comments on commit db33ced

Please sign in to comment.