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_extractor.py 2.4 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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 pytest
  15. import ncnn
  16. alloctor = ncnn.PoolAllocator()
  17. def test_extractor():
  18. with pytest.raises(TypeError, match="No constructor"):
  19. ex = ncnn.Extractor()
  20. dr = ncnn.DataReaderFromEmpty()
  21. net = ncnn.Net()
  22. net.load_param("tests/test.param")
  23. net.load_model(dr)
  24. in_mat = ncnn.Mat((227, 227, 3))
  25. with net.create_extractor() as ex:
  26. ex.set_light_mode(True)
  27. ex.set_num_threads(2)
  28. ex.set_blob_allocator(alloctor)
  29. ex.set_workspace_allocator(alloctor)
  30. ex.input("data", in_mat)
  31. ret, out_mat = ex.extract("conv0_fwd")
  32. assert (
  33. ret == 0
  34. and out_mat.dims == 3
  35. and out_mat.w == 225
  36. and out_mat.h == 225
  37. and out_mat.c == 3
  38. )
  39. ret, out_mat = ex.extract("output")
  40. assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
  41. def test_extractor_index():
  42. with pytest.raises(TypeError, match="No constructor"):
  43. ex = ncnn.Extractor()
  44. dr = ncnn.DataReaderFromEmpty()
  45. net = ncnn.Net()
  46. net.load_param("tests/test.param")
  47. net.load_model(dr)
  48. in_mat = ncnn.Mat((227, 227, 3))
  49. ex = net.create_extractor()
  50. ex.set_light_mode(True)
  51. ex.set_num_threads(2)
  52. ex.set_blob_allocator(alloctor)
  53. ex.set_workspace_allocator(alloctor)
  54. ex.input(0, in_mat)
  55. ret, out_mat = ex.extract(1)
  56. assert (
  57. ret == 0
  58. and out_mat.dims == 3
  59. and out_mat.w == 225
  60. and out_mat.h == 225
  61. and out_mat.c == 3
  62. )
  63. ret, out_mat = ex.extract(2)
  64. assert ret == 0 and out_mat.dims == 1 and out_mat.w == 1
  65. # not use with sentence, call clear manually to ensure ex destruct before net
  66. ex.clear()