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.8 kB

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