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_image_denoise.py 2.9 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. # Copyright (c) Alibaba, Inc. and its affiliates.
  2. import unittest
  3. from PIL import Image
  4. from modelscope.hub.snapshot_download import snapshot_download
  5. from modelscope.models import Model
  6. from modelscope.outputs import OutputKeys
  7. from modelscope.pipelines import pipeline
  8. from modelscope.pipelines.cv import ImageDenoisePipeline
  9. from modelscope.utils.constant import Tasks
  10. from modelscope.utils.demo_utils import DemoCompatibilityCheck
  11. from modelscope.utils.test_utils import test_level
  12. class ImageDenoiseTest(unittest.TestCase, DemoCompatibilityCheck):
  13. def setUp(self) -> None:
  14. self.task = Tasks.image_denoising
  15. self.model_id = 'damo/cv_nafnet_image-denoise_sidd'
  16. demo_image_path = 'data/test/images/noisy-demo-1.png'
  17. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  18. def test_run_by_direct_model_download(self):
  19. cache_path = snapshot_download(self.model_id)
  20. pipeline = ImageDenoisePipeline(cache_path)
  21. denoise_img = pipeline(
  22. input=self.demo_image_path)[OutputKeys.OUTPUT_IMG]
  23. denoise_img = Image.fromarray(denoise_img)
  24. w, h = denoise_img.size
  25. print('pipeline: the shape of output_img is {}x{}'.format(h, w))
  26. @unittest.skipUnless(test_level() >= 0, 'skip test in current test level')
  27. def test_run_with_model_from_modelhub(self):
  28. model = Model.from_pretrained(self.model_id)
  29. pipeline_ins = pipeline(task=Tasks.image_denoising, model=model)
  30. denoise_img = pipeline_ins(
  31. input=self.demo_image_path)[OutputKeys.OUTPUT_IMG]
  32. denoise_img = Image.fromarray(denoise_img)
  33. w, h = denoise_img.size
  34. print('pipeline: the shape of output_img is {}x{}'.format(h, w))
  35. @unittest.skipUnless(test_level() >= 1, 'skip test in current test level')
  36. def test_run_with_model_name(self):
  37. pipeline_ins = pipeline(
  38. task=Tasks.image_denoising, model=self.model_id)
  39. denoise_img = pipeline_ins(
  40. input=self.demo_image_path)[OutputKeys.OUTPUT_IMG]
  41. denoise_img = Image.fromarray(denoise_img)
  42. w, h = denoise_img.size
  43. print('pipeline: the shape of output_img is {}x{}'.format(h, w))
  44. @unittest.skipUnless(test_level() >= 2, 'skip test in current test level')
  45. def test_run_with_default_model(self):
  46. pipeline_ins = pipeline(task=Tasks.image_denoising)
  47. denoise_img = pipeline_ins(
  48. input=self.demo_image_path)[OutputKeys.OUTPUT_IMG]
  49. denoise_img = Image.fromarray(denoise_img)
  50. w, h = denoise_img.size
  51. print('pipeline: the shape of output_img is {}x{}'.format(h, w))
  52. @unittest.skip('demo compatibility test is only enabled on a needed-basis')
  53. def test_demo_compatibility(self):
  54. self.compatibility_check()
  55. if __name__ == '__main__':
  56. unittest.main()