utils.py 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. # copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
  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 six
  15. import types
  16. from difflib import SequenceMatcher
  17. from . import architectures
  18. def get_architectures():
  19. """
  20. get all of model architectures
  21. """
  22. names = []
  23. for k, v in architectures.__dict__.items():
  24. if isinstance(v, (types.FunctionType, six.class_types)):
  25. names.append(k)
  26. return names
  27. def get_blacklist_model_in_static_mode():
  28. from paddlex.ppcls.modeling.architectures import distilled_vision_transformer
  29. from paddlex.ppcls.modeling.architectures import vision_transformer
  30. blacklist = distilled_vision_transformer.__all__ + vision_transformer.__all__
  31. return blacklist
  32. def similar_architectures(name='', names=[], thresh=0.1, topk=10):
  33. """
  34. inferred similar architectures
  35. """
  36. scores = []
  37. for idx, n in enumerate(names):
  38. if n.startswith('__'):
  39. continue
  40. score = SequenceMatcher(None, n.lower(), name.lower()).quick_ratio()
  41. if score > thresh:
  42. scores.append((idx, score))
  43. scores.sort(key=lambda x: x[1], reverse=True)
  44. similar_names = [names[s[0]] for s in scores[:min(topk, len(scores))]]
  45. return similar_names