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 4.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  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. input_names = net.input_names()
  24. output_names = net.output_names()
  25. assert len(input_names) > 0 and len(output_names) > 0
  26. in_mat = ncnn.Mat((227, 227, 3))
  27. with net.create_extractor() as ex:
  28. ex.input("data", in_mat)
  29. ret, out_mat = ex.extract("output")
  30. assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
  31. net.clear()
  32. assert len(net.blobs()) == 0 and len(net.layers()) == 0
  33. def test_net_vulkan():
  34. if not hasattr(ncnn, "get_gpu_count"):
  35. return
  36. dr = ncnn.DataReaderFromEmpty()
  37. net = ncnn.Net()
  38. net.opt.use_vulkan_compute = True
  39. ret = net.load_param("tests/test.param")
  40. net.load_model(dr)
  41. assert ret == 0 and len(net.blobs()) == 3 and len(net.layers()) == 3
  42. in_mat = ncnn.Mat((227, 227, 3))
  43. ex = net.create_extractor()
  44. ex.input("data", in_mat)
  45. ret, out_mat = ex.extract("output")
  46. assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
  47. ex.clear()
  48. net.clear()
  49. assert len(net.blobs()) == 0 and len(net.layers()) == 0
  50. def test_custom_layer():
  51. class CustomLayer(ncnn.Layer):
  52. customLayers = []
  53. def __init__(self):
  54. ncnn.Layer.__init__(self)
  55. self.one_blob_only = True
  56. self.customLayers.append(self)
  57. def forward(self, bottom_blob, top_blob, opt):
  58. x = np.array(bottom_blob)
  59. x += 1
  60. top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator)
  61. if top_blob.empty():
  62. return -100
  63. return 0
  64. def CustomLayer_layer_creator():
  65. return CustomLayer()
  66. def CustomLayer_layer_destroyer(layer):
  67. for i in range(len(CustomLayer.customLayers)):
  68. if CustomLayer.customLayers[i] == layer:
  69. del CustomLayer.customLayers[i]
  70. break
  71. dr = ncnn.DataReaderFromEmpty()
  72. net = ncnn.Net()
  73. net.register_custom_layer(
  74. "CustomLayer", CustomLayer_layer_creator, CustomLayer_layer_destroyer
  75. )
  76. ret = net.load_param("tests/custom_layer.param")
  77. net.load_model(dr)
  78. assert ret == 0 and len(net.blobs()) == 2 and len(net.layers()) == 2
  79. in_mat = ncnn.Mat(1)
  80. in_mat.fill(1.0)
  81. ex = net.create_extractor()
  82. ex.input("data", in_mat)
  83. ret, out_mat = ex.extract("output")
  84. assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1 and out_mat[0] == 2.0
  85. ex.clear()
  86. net.clear()
  87. assert len(net.blobs()) == 0 and len(net.layers()) == 0
  88. def test_vulkan_device_index():
  89. if not hasattr(ncnn, "get_gpu_count"):
  90. return
  91. net = ncnn.Net()
  92. assert net.vulkan_device() is None
  93. net.set_vulkan_device(0)
  94. assert net.vulkan_device() is not None
  95. def test_vulkan_device_vkdev():
  96. if not hasattr(ncnn, "get_gpu_count"):
  97. return
  98. net = ncnn.Net()
  99. assert net.vulkan_device() is None
  100. vkdev = ncnn.get_gpu_device(0)
  101. net.set_vulkan_device(vkdev)
  102. assert net.vulkan_device() is not None