PaddleClas/ppcls/modeling/utils.py

43 lines
1.4 KiB
Python
Raw Normal View History

2020-04-09 02:16:30 +08:00
#copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
#
#Licensed under the Apache License, Version 2.0 (the "License");
#you may not use this file except in compliance with the License.
#You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
#Unless required by applicable law or agreed to in writing, software
#distributed under the License is distributed on an "AS IS" BASIS,
#WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
#See the License for the specific language governing permissions and
#limitations under the License.
import architectures
2020-04-13 18:53:03 +08:00
import types
2020-04-09 02:16:30 +08:00
from difflib import SequenceMatcher
def get_architectures():
"""
get all of model architectures
"""
names = []
for k, v in architectures.__dict__.items():
if isinstance(v, (types.FunctionType, types.ClassType)):
names.append(k)
return names
2020-04-13 18:53:03 +08:00
def similar_architectures(name='', names=[], thresh=0.1, topk=10):
2020-04-09 02:16:30 +08:00
"""
inferred similar architectures
"""
scores = []
for idx, n in enumerate(names):
if n[:2] == '__': continue
score = SequenceMatcher(None, n.lower(), name.lower()).quick_ratio()
if score > thresh: scores.append((idx, score))
scores.sort(key=lambda x: x[1], reverse=True)
similar_names = [names[s[0]] for s in scores[:min(topk, len(scores))]]
return similar_names