PaddleClas/ppcls/modeling/utils.py

47 lines
1.4 KiB
Python
Raw Normal View History

2020-04-21 00:50:43 +08:00
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
2020-04-09 02:16:30 +08:00
#
2020-04-21 00:50:43 +08:00
# 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
2020-04-09 02:16:30 +08:00
#
# http://www.apache.org/licenses/LICENSE-2.0
#
2020-04-21 00:50:43 +08:00
# 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.
2020-04-09 02:16:30 +08:00
2020-04-21 00:50:43 +08:00
import six
2020-04-13 18:53:03 +08:00
import types
2020-04-09 02:16:30 +08:00
from difflib import SequenceMatcher
2020-04-14 12:25:08 +08:00
from . import architectures
2020-04-09 02:16:30 +08:00
def get_architectures():
"""
get all of model architectures
"""
names = []
for k, v in architectures.__dict__.items():
2020-04-21 00:50:43 +08:00
if isinstance(v, (types.FunctionType, six.class_types)):
2020-04-09 02:16:30 +08:00
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):
2020-04-21 00:50:43 +08:00
if n.startswith('__'):
continue
2020-04-09 02:16:30 +08:00
score = SequenceMatcher(None, n.lower(), name.lower()).quick_ratio()
2020-04-21 00:50:43 +08:00
if score > thresh:
scores.append((idx, score))
2020-04-09 02:16:30 +08:00
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