model_zoo.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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 os
  15. import os.path as osp
  16. import glob
  17. import pkg_resources
  18. try:
  19. from collections.abc import Sequence
  20. except:
  21. from collections import Sequence
  22. from paddlex.ppdet.core.workspace import load_config, create
  23. from paddlex.ppdet.utils.checkpoint import load_weight
  24. from paddlex.ppdet.utils.download import get_config_path
  25. from paddlex.ppdet.utils.logger import setup_logger
  26. logger = setup_logger(__name__)
  27. __all__ = [
  28. 'list_model', 'get_config_file', 'get_weights_url', 'get_model',
  29. 'MODEL_ZOO_FILENAME'
  30. ]
  31. MODEL_ZOO_FILENAME = 'MODEL_ZOO'
  32. def list_model(filters=[]):
  33. model_zoo_file = pkg_resources.resource_filename('ppdet.model_zoo',
  34. MODEL_ZOO_FILENAME)
  35. with open(model_zoo_file) as f:
  36. model_names = f.read().splitlines()
  37. # filter model_name
  38. def filt(name):
  39. for f in filters:
  40. if name.find(f) < 0:
  41. return False
  42. return True
  43. if isinstance(filters, str) or not isinstance(filters, Sequence):
  44. filters = [filters]
  45. model_names = [name for name in model_names if filt(name)]
  46. if len(model_names) == 0 and len(filters) > 0:
  47. raise ValueError("no model found, please check filters seeting, "
  48. "filters can be set as following kinds:\n"
  49. "\tDataset: coco, voc ...\n"
  50. "\tArchitecture: yolo, rcnn, ssd ...\n"
  51. "\tBackbone: resnet, vgg, darknet ...\n")
  52. model_str = "Available Models:\n"
  53. for model_name in model_names:
  54. model_str += "\t{}\n".format(model_name)
  55. logger.info(model_str)
  56. # models and configs save on bcebos under dygraph directory
  57. def get_config_file(model_name):
  58. return get_config_path("ppdet://configs/{}.yml".format(model_name))
  59. def get_weights_url(model_name):
  60. return "ppdet://models/{}.pdparams".format(osp.split(model_name)[-1])
  61. def get_model(model_name, pretrained=True):
  62. cfg_file = get_config_file(model_name)
  63. cfg = load_config(cfg_file)
  64. model = create(cfg.architecture)
  65. if pretrained:
  66. load_weight(model, get_weights_url(model_name))
  67. return model