external_test_image.py 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. #!/usr/bin/env python
  2. import os
  3. import unittest
  4. import numpy as np
  5. if 'IMAGE' not in os.environ:
  6. os.environ['IMAGE'] = '2'
  7. os.environ['GPU'] = '1'
  8. os.environ['OPT'] = '2'
  9. from tinygrad.tensor import Tensor
  10. from tinygrad.nn import Conv2d
  11. Tensor.no_grad = True
  12. class TestImage(unittest.TestCase):
  13. def test_create_image(self):
  14. t = Tensor.ones(128, 128, 1)
  15. t = t.reshape(128, 32, 4) + 3
  16. t.realize()
  17. np.testing.assert_array_equal(t.numpy(), np.ones((128,32,4))*4)
  18. def test_sum_image(self):
  19. t1 = Tensor.ones(16, 16, 1).reshape(16, 4, 4) + 3
  20. t1.realize()
  21. t1 = t1.sum()
  22. t1.realize()
  23. assert t1.numpy() == 16*4*4*4, f"got {t1.numpy()}"
  24. def test_add_image(self):
  25. t1 = Tensor.ones(16, 16, 1).reshape(16, 4, 4) + 3
  26. t2 = Tensor.ones(16, 16, 1).reshape(16, 4, 4) + 4
  27. t1.realize()
  28. t2.realize()
  29. t3 = t1 + t2
  30. t3.realize()
  31. np.testing.assert_array_equal(t3.numpy(), np.ones((16,4,4))*9)
  32. def test_padded_conv(self):
  33. bs, in_chans, out_chans = 1,12,32
  34. tiny_conv = Conv2d(in_chans, out_chans, 3, bias=None, padding=1)
  35. tiny_dat = Tensor.ones(bs, 12, 64, 128)
  36. tiny_conv(tiny_dat).realize()
  37. def test_op_conv(self):
  38. bs, in_chans, out_chans = 1,12,32
  39. tiny_conv = Conv2d(in_chans, out_chans, 3, bias=None, padding=1)
  40. tiny_dconv = Conv2d(out_chans, out_chans, 1, bias=None, padding=0)
  41. tiny_dat = Tensor.ones(bs, 12, 64, 128)
  42. p2 = tiny_conv(tiny_dat).relu()
  43. p2 = tiny_dconv(p2)
  44. p2.realize()
  45. if __name__ == '__main__':
  46. unittest.main()