You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

test_product_retrieval_embedding.py 2.0 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. import numpy as np
  4. from modelscope.models import Model
  5. from modelscope.outputs import OutputKeys
  6. from modelscope.pipelines import pipeline
  7. from modelscope.utils.constant import Tasks
  8. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  9. from modelscope.utils.test_utils import test_level
  10. class ProductRetrievalEmbeddingTest(unittest.TestCase, DemoCompatibilityCheck):
  11. def setUp(self) -> None:
  12. self.task = Tasks.product_retrieval_embedding
  13. self.model_id = 'damo/cv_resnet50_product-bag-embedding-models'
  14. img_input = 'data/test/images/product_embed_bag.jpg'
  15. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  16. def test_run_with_model_name(self):
  17. product_embed = pipeline(Tasks.product_retrieval_embedding,
  18. self.model_id)
  19. result = product_embed(self.img_input)[OutputKeys.IMG_EMBEDDING]
  20. print('abs sum value is: {}'.format(np.sum(np.abs(result))))
  21. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  22. def test_run_with_model_from_modelhub(self):
  23. model = Model.from_pretrained(self.model_id)
  24. product_embed = pipeline(
  25. task=Tasks.product_retrieval_embedding, model=model)
  26. result = product_embed(self.img_input)[OutputKeys.IMG_EMBEDDING]
  27. print('abs sum value is: {}'.format(np.sum(np.abs(result))))
  28. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  29. def test_run_with_default_model(self):
  30. product_embed = pipeline(task=Tasks.product_retrieval_embedding)
  31. result = product_embed(self.img_input)[OutputKeys.IMG_EMBEDDING]
  32. print('abs sum value is: {}'.format(np.sum(np.abs(result))))
  33. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  34. def test_demo_compatibility(self):
  35. self.compatibility_check()
  36. if __name__ == '__main__':
  37. unittest.main()