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

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  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. net = ncnn.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. ex = net.create_extractor()
  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_custom_layer():
  31. class CustomLayer(ncnn.Layer):
  32. customLayers = []
  33. def __init__(self):
  34. ncnn.Layer.__init__(self)
  35. self.one_blob_only = True
  36. self.customLayers.append(self)
  37. def forward(self, bottom_blob, top_blob, opt):
  38. x = np.array(bottom_blob)
  39. x += 1
  40. top_blob.clone_from(ncnn.Mat(x), opt.blob_allocator)
  41. if top_blob.empty():
  42. return -100
  43. return 0
  44. def CustomLayer_layer_creator():
  45. return CustomLayer()
  46. def CustomLayer_layer_destroyer(layer):
  47. for i in range(len(CustomLayer.customLayers)):
  48. if CustomLayer.customLayers[i] == layer:
  49. del CustomLayer.customLayers[i]
  50. break
  51. dr = ncnn.DataReaderFromEmpty()
  52. net = ncnn.Net()
  53. net.register_custom_layer(
  54. "CustomLayer", CustomLayer_layer_creator, CustomLayer_layer_destroyer
  55. )
  56. ret = net.load_param("tests/custom_layer.param")
  57. net.load_model(dr)
  58. assert ret == 0 and len(net.blobs()) == 2 and len(net.layers()) == 2
  59. in_mat = ncnn.Mat(1)
  60. in_mat.fill(1.0)
  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 and out_mat[0] == 2.0
  65. net.clear()
  66. assert len(net.blobs()) == 0 and len(net.layers()) == 0