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_net.py 3.8 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. # Tencent is pleased to support the open source community by making ncnn available.
  2. #
  3. # Copyright (C) 2021 THL A29 Limited, a Tencent company. All rights reserved.
  4. #
  5. # Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. # in compliance with the License. You may obtain a copy of the License at
  7. #
  8. # https://opensource.org/licenses/BSD-3-Clause
  9. #
  10. # Unless required by applicable law or agreed to in writing, software distributed
  11. # under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. # CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. # specific language governing permissions and limitations under the License.
  14. import numpy as np
  15. import pytest
  16. import ncnn
  17. def test_net():
  18. dr = ncnn.DataReaderFromEmpty()
  19. with ncnn.Net() as net:
  20. ret = net.load_param("tests/test.param")
  21. net.load_model(dr)
  22. assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3
  23. in_mat = ncnn.Mat((227, 227, 3))
  24. with net.create_extractor() as ex:
  25. ex.input("data", in_mat)
  26. ret, out_mat = ex.extract("output")
  27. assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
  28. net.clear()
  29. assert len(net.blobs()) == 0 and len(net.layers()) == 0
  30. def test_net_vulkan():
  31. if not hasattr(ncnn, "get_gpu_count"):
  32. return
  33. dr = ncnn.DataReaderFromEmpty()
  34. net = ncnn.Net()
  35. net.opt.use_vulkan_compute = True
  36. ret = net.load_param("tests/test.param")
  37. net.load_model(dr)
  38. assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3
  39. in_mat = ncnn.Mat((227, 227, 3))
  40. ex = net.create_extractor()
  41. ex.input("data", in_mat)
  42. ret, out_mat = ex.extract("output")
  43. assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
  44. ex.clear()
  45. net.clear()
  46. assert len(net.blobs()) == 0 and len(net.layers()) == 0
  47. def test_custom_layer():
  48. class CustomLayer(ncnn.Layer):
  49. customLayers = []
  50. def __init__(self):
  51. ncnn.Layer.__init__(self)
  52. self.one_blob_only = True
  53. self.customLayers.append(self)
  54. def forward(self, bottom_blob, top_blob, opt):
  55. x = np.array(bottom_blob)
  56. x += 1
  57. top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator)
  58. if top_blob.empty():
  59. return -100
  60. return 0
  61. def CustomLayer_layer_creator():
  62. return CustomLayer()
  63. def CustomLayer_layer_destroyer(layer):
  64. for i in range(len(CustomLayer.customLayers)):
  65. if CustomLayer.customLayers[i] == layer:
  66. del CustomLayer.customLayers[i]
  67. break
  68. dr = ncnn.DataReaderFromEmpty()
  69. net = ncnn.Net()
  70. net.register_custom_layer(
  71. "CustomLayer", CustomLayer_layer_creator, CustomLayer_layer_destroyer
  72. )
  73. ret = net.load_param("tests/custom_layer.param")
  74. net.load_model(dr)
  75. assert ret == 0 and len(net.blobs()) == 2 and len(net.layers()) == 2
  76. in_mat = ncnn.Mat(1)
  77. in_mat.fill(1.0)
  78. ex = net.create_extractor()
  79. ex.input("data", in_mat)
  80. ret, out_mat = ex.extract("output")
  81. assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 and out_mat[0] == 2.0
  82. ex.clear()
  83. net.clear()
  84. assert len(net.blobs()) == 0 and len(net.layers()) == 0
  85. def test_vulkan_device_index():
  86. if not hasattr(ncnn, "get_gpu_count"):
  87. return
  88. net = ncnn.Net()
  89. assert net.vulkan_device() is None
  90. net.set_vulkan_device(0)
  91. assert net.vulkan_device() is not None
  92. def test_vulkan_device_vkdev():
  93. if not hasattr(ncnn, "get_gpu_count"):
  94. return
  95. net = ncnn.Net()
  96. assert net.vulkan_device() is None
  97. vkdev = ncnn.get_gpu_device(0)
  98. net.set_vulkan_device(vkdev)
  99. assert net.vulkan_device() is not None