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.

inference.py 2.7 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. # Copyright 2021 The KubeEdge Authors.
  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. import glob
  15. from PIL import Image
  16. from sedna.common.config import Context
  17. from sedna.core.incremental_learning import IncrementalLearning
  18. from interface import Estimator
  19. import shutil
  20. import mindspore as ms
  21. from mobilenet_v2 import mobilenet_v2_fine_tune
  22. he_saved_url = Context.get_parameters("HE_SAVED_URL", './tmp')
  23. def output_deal(is_hard_example, infer_image_path):
  24. img_name=infer_image_path.split(r"/")[-1]
  25. img_category = infer_image_path.split(r"/")[-2]
  26. if is_hard_example:
  27. shutil.copy(infer_image_path,f"{he_saved_url}/{img_category}_{img_name}")
  28. def main():
  29. hard_example_mining = IncrementalLearning.get_hem_algorithm_from_config(
  30. random_ratio=0.3
  31. )
  32. incremental_instance = IncrementalLearning(estimator=Estimator, hard_example_mining=hard_example_mining)
  33. class_names=Context.get_parameters("class_name")
  34. #read parameters from deployment config
  35. input_shape=int(Context.get_parameters("input_shape"))
  36. # load ckpt
  37. model_url=Context.get_parameters("model_url")
  38. print("model_url=" + model_url)
  39. # load model ckpt here
  40. network = mobilenet_v2_fine_tune(base_model_url=model_url).get_eval_network()
  41. #ms.load_checkpoint(model_url, network)
  42. model = ms.Model(network)
  43. # load dataset
  44. #train_dataset_url = BaseConfig.train_dataset_url
  45. infer_dataset_url=Context.get_parameters("infer_url")
  46. print(infer_dataset_url)
  47. # get each image unber infer_dataset_url with wildcard
  48. while True:
  49. for each_img in glob.glob(infer_dataset_url+"/*/*"):
  50. infer_data=Image.open(each_img)
  51. results, _, is_hard_example = incremental_instance.inference(data=infer_data,
  52. model=model,
  53. class_names=class_names,
  54. input_shape=input_shape)
  55. hard_example="is hard example" if is_hard_example else "is not hard example"
  56. print(f"{each_img}--->{results}-->{hard_example}")
  57. output_deal(is_hard_example, each_img)
  58. if __name__ == "__main__":
  59. main()