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.

infer.py 2.6 kB

5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. # Copyright 2020 Huawei Technologies Co., Ltd
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. # ============================================================================
  15. """ infer """
  16. from argparse import ArgumentParser
  17. import numpy as np
  18. from mindspore import Tensor
  19. from ....dataset_mock import MindData
  20. __factory = {
  21. "resnet50": resnet50(),
  22. }
  23. def parse_args():
  24. """ parse_args """
  25. parser = ArgumentParser(description="resnet50 example")
  26. parser.add_argument("--model", type=str, default="resnet50",
  27. help="the network architecture for training or testing")
  28. parser.add_argument("--phase", type=str, default="test",
  29. help="the phase of the model, default is test.")
  30. parser.add_argument("--file_path", type=str, default="/data/file/test1.txt",
  31. help="data directory of training or testing")
  32. parser.add_argument("--batch_size", type=int, default=1,
  33. help="batch size for training or testing ")
  34. return parser.parse_args()
  35. def get_model(name):
  36. """ get_model """
  37. if name not in __factory:
  38. raise KeyError("unknown model:", name)
  39. return __factory[name]
  40. def get_dataset(batch_size=32):
  41. """ get_dataset """
  42. dataset_types = np.float32
  43. dataset_shapes = (batch_size, 3, 224, 224)
  44. dataset = MindData(size=2, batch_size=batch_size,
  45. np_types=dataset_types,
  46. output_shapes=dataset_shapes,
  47. input_indexs=(0, 1))
  48. return dataset
  49. # pylint: disable=unused-argument
  50. def test(name, file_path, batch_size):
  51. """ test """
  52. network = get_model(name)
  53. batch = get_dataset(batch_size=batch_size)
  54. data_list = []
  55. for data in batch:
  56. data_list.append(data.asnumpy())
  57. batch_data = np.concatenate(data_list, axis=0).transpose((0, 3, 1, 2))
  58. input_tensor = Tensor(batch_data)
  59. print(input_tensor.shape)
  60. network(input_tensor)
  61. if __name__ == '__main__':
  62. args = parse_args()
  63. if args.phase == "train":
  64. raise NotImplementedError
  65. test(args.model, args.file_path, args.batch_size)