utils.py 1.7 KB
Newer Older
W
WuHaobo 已提交
1
# copyright (c) 2020 PaddlePaddle Authors. All Rights Reserve.
W
WuHaobo 已提交
2
#
W
WuHaobo 已提交
3 4 5
# 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
W
WuHaobo 已提交
6 7 8
#
#    http://www.apache.org/licenses/LICENSE-2.0
#
W
WuHaobo 已提交
9 10 11 12 13
# 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.
W
WuHaobo 已提交
14

W
WuHaobo 已提交
15
import six
W
WuHaobo 已提交
16
import types
W
WuHaobo 已提交
17 18
from difflib import SequenceMatcher

W
weishengyu 已提交
19
from . import backbone
W
WuHaobo 已提交
20

W
WuHaobo 已提交
21 22 23 24 25 26

def get_architectures():
    """
    get all of model architectures
    """
    names = []
W
weishengyu 已提交
27
    for k, v in backbone.__dict__.items():
W
WuHaobo 已提交
28
        if isinstance(v, (types.FunctionType, six.class_types)):
W
WuHaobo 已提交
29 30 31 32
            names.append(k)
    return names


33
def get_blacklist_model_in_static_mode():
W
weishengyu 已提交
34 35
    from ppcls.arch.backbone import distilled_vision_transformer
    from ppcls.arch.backbone import vision_transformer
36 37 38 39
    blacklist = distilled_vision_transformer.__all__ + vision_transformer.__all__
    return blacklist


W
WuHaobo 已提交
40
def similar_architectures(name='', names=[], thresh=0.1, topk=10):
W
WuHaobo 已提交
41 42 43 44 45
    """
    inferred similar architectures
    """
    scores = []
    for idx, n in enumerate(names):
W
WuHaobo 已提交
46 47
        if n.startswith('__'):
            continue
W
WuHaobo 已提交
48
        score = SequenceMatcher(None, n.lower(), name.lower()).quick_ratio()
W
WuHaobo 已提交
49 50
        if score > thresh:
            scores.append((idx, score))
W
WuHaobo 已提交
51 52 53
    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