utils.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. from pathlib import Path
  2. import cv2
  3. import numpy as np
  4. import pytest
  5. import torch
  6. from sorawm.iopaint.schema import HDStrategy, InpaintRequest, LDMSampler, SDSampler
  7. current_dir = Path(__file__).parent.absolute().resolve()
  8. save_dir = current_dir / "result"
  9. save_dir.mkdir(exist_ok=True, parents=True)
  10. def check_device(device: str) -> int:
  11. if device == "cuda" and not torch.cuda.is_available():
  12. pytest.skip("CUDA is not available, skip test on cuda")
  13. if device == "mps" and not torch.backends.mps.is_available():
  14. pytest.skip("mps is not available, skip test on mps")
  15. steps = 2 if device == "cpu" else 20
  16. return steps
  17. def assert_equal(
  18. model,
  19. config: InpaintRequest,
  20. gt_name,
  21. fx: float = 1,
  22. fy: float = 1,
  23. img_p=current_dir / "image.png",
  24. mask_p=current_dir / "mask.png",
  25. ):
  26. img, mask = get_data(fx=fx, fy=fy, img_p=img_p, mask_p=mask_p)
  27. print(f"Input image shape: {img.shape}")
  28. res = model(img, mask, config)
  29. ok = cv2.imwrite(
  30. str(save_dir / gt_name),
  31. res,
  32. [int(cv2.IMWRITE_JPEG_QUALITY), 100, int(cv2.IMWRITE_PNG_COMPRESSION), 0],
  33. )
  34. assert ok, save_dir / gt_name
  35. """
  36. Note that JPEG is lossy compression, so even if it is the highest quality 100,
  37. when the saved images is reloaded, a difference occurs with the original pixel value.
  38. If you want to save the original images as it is, save it as PNG or BMP.
  39. """
  40. # gt = cv2.imread(str(current_dir / gt_name), cv2.IMREAD_UNCHANGED)
  41. # assert np.array_equal(res, gt)
  42. def get_data(
  43. fx: float = 1,
  44. fy: float = 1.0,
  45. img_p=current_dir / "image.png",
  46. mask_p=current_dir / "mask.png",
  47. ):
  48. img = cv2.imread(str(img_p))
  49. img = cv2.cvtColor(img, cv2.COLOR_BGRA2RGB)
  50. mask = cv2.imread(str(mask_p), cv2.IMREAD_GRAYSCALE)
  51. img = cv2.resize(img, None, fx=fx, fy=fy, interpolation=cv2.INTER_AREA)
  52. mask = cv2.resize(mask, None, fx=fx, fy=fy, interpolation=cv2.INTER_NEAREST)
  53. return img, mask
  54. def get_config(**kwargs):
  55. data = dict(
  56. sd_sampler=kwargs.get("sd_sampler", SDSampler.uni_pc),
  57. ldm_steps=1,
  58. ldm_sampler=LDMSampler.plms,
  59. hd_strategy=kwargs.get("strategy", HDStrategy.ORIGINAL),
  60. hd_strategy_crop_margin=32,
  61. hd_strategy_crop_trigger_size=200,
  62. hd_strategy_resize_limit=200,
  63. )
  64. data.update(**kwargs)
  65. return InpaintRequest(image="", mask="", **data)