#!/usr/bin/env python # Copyright (c) OpenMMLab. All rights reserved. # This tool is used to update model-index.yml which is required by MIM, and # will be automatically called as a pre-commit hook. The updating will be # triggered if any change of model information (.md files in configs/) has been # detected before a commit. import glob import os import os.path as osp import re import sys from lxml import etree from mmcv.fileio import dump MMSEG_ROOT = osp.dirname(osp.dirname((osp.dirname(__file__)))) COLLECTIONS = [ 'ANN', 'APCNet', 'BiSeNetV1', 'BiSeNetV2', 'CCNet', 'CGNet', 'DANet', 'DeepLabV3', 'DeepLabV3+', 'DMNet', 'DNLNet', 'DPT', 'EMANet', 'EncNet', 'ERFNet', 'FastFCN', 'FastSCNN', 'FCN', 'GCNet', 'ICNet', 'ISANet', 'KNet', 'NonLocalNet', 'OCRNet', 'PointRend', 'PSANet', 'PSPNet', 'Segformer', 'Segmenter', 'FPN', 'SETR', 'STDC', 'UNet', 'UPerNet' ] COLLECTIONS_TEMP = [] def dump_yaml_and_check_difference(obj, filename, sort_keys=False): """Dump object to a yaml file, and check if the file content is different from the original. Args: obj (any): The python object to be dumped. filename (str): YAML filename to dump the object to. sort_keys (str); Sort key by dictionary order. Returns: Bool: If the target YAML file is different from the original. """ str_dump = dump(obj, None, file_format='yaml', sort_keys=sort_keys) if osp.isfile(filename): file_exists = True with open(filename, 'r', encoding='utf-8') as f: str_orig = f.read() else: file_exists = False str_orig = None if file_exists and str_orig == str_dump: is_different = False else: is_different = True with open(filename, 'w', encoding='utf-8') as f: f.write(str_dump) return is_different def parse_md(md_file): """Parse .md file and convert it to a .yml file which can be used for MIM. Args: md_file (str): Path to .md file. Returns: Bool: If the target YAML file is different from the original. """ collection_name = osp.split(osp.dirname(md_file))[1] configs = os.listdir(osp.dirname(md_file)) collection = dict( Name=collection_name, Metadata={'Training Data': []}, Paper={ 'URL': '', 'Title': '' }, README=md_file, Code={ 'URL': '', 'Version': '' }) collection.update({'Converted From': {'Weights': '', 'Code': ''}}) models = [] datasets = [] paper_url = None paper_title = None code_url = None code_version = None repo_url = None # To avoid re-counting number of backbone model in OpenMMLab, # if certain model in configs folder is backbone whose name is already # recorded in MMClassification, then the `COLLECTION` dict of this model # in MMSegmentation should be deleted, and `In Collection` in `Models` # should be set with head or neck of this config file. is_backbone = None with open(md_file, 'r', encoding='UTF-8') as md: lines = md.readlines() i = 0 current_dataset = '' while i < len(lines): line = lines[i].strip() # In latest README.md the title and url are in the third line. if i == 2: paper_url = lines[i].split('](')[1].split(')')[0] paper_title = lines[i].split('](')[0].split('[')[1] if len(line) == 0: i += 1 continue elif line[:3] == '