提交 07f9189c 编写于 作者: M Megvii Engine Team 提交者: Xinran Xu

Project import generated by Copybara.

GitOrigin-RevId: c4495210d0723c4f22fcb725ee9120d009f91b4d
上级
__pycache__
.mypy_cache
MegEngine is licensed under the Apache License Version 2.0, except for the third-party components listed below.
*********************************************************************************************************************************
Software Licensed under the MIT License:
--------------------------------------------------------------------
1. attrs
Copyright (c) 2015 Hynek Schlawack
2. PyYAML
Copyright (c) 2017-2020 Ingy döt Net
Copyright (c) 2006-2016 Kirill Simonov
Terms of the MIT License:
--------------------------------------------------------------------
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*********************************************************************************************************************************
*********************************************************************************************************************************
Software Licensed under the BSD 3-Clause License:
--------------------------------------------------------------------
1. Mistune
Copyright (c) 2014, Hsiaoming Yang
Terms of the BSD 3-Clause License:
--------------------------------------------------------------------
Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:
1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.
2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.
3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*********************************************************************************************************************************
# Contributor Covenant Code of Conduct
## Our Pledge
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, or sexual identity and orientation.
## Our Standards
Examples of behavior that contributes to a positive environment for our community include:
* Using welcoming and inclusive language
* Being respectful of differing viewpoints and experiences
* Gracefully accepting constructive criticism
* Focusing on what is best for the community
* Showing empathy towards other community members
Examples of unacceptable behavior include:
* The use of sexualized language or imagery, and sexual attention or advances of any kind
* Trolling, insulting or derogatory comments, and personal or political attacks
* Public or private harassment
* Publishing others’ private information, such as a physical or email address, without their explicit permission
* Other conduct which could reasonably be considered inappropriate in a professional setting
All MegEngine forums and spaces are meant for professional interactions, and any behavior which could reasonably be considered inappropriate in a professional setting is unacceptable.
## Our Responsibilities
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
## Scope
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
## Enforcement
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at megengine@megvii.com. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
## Attribution
This Code of Conduct is updated from the Contributor Covenant, version 2.0, available at https://www.contributor-covenant.org/version/2/0/code_of_conduct.html.
# MegEngine Contributor License Agreement
In order to clarify the intellectual property license granted with Contributions from any person or entity, the open source project MegEngine ("MegEngine") must have a Contributor License Agreement (CLA) on file that has been signed by each Contributor, indicating agreement to the license terms below. This license is for your protection as a Contributor as well as the protection of MegEngine and its users; it does not change your rights to use your own Contributions for any other purpose.
This Agreement allows an individual or an entity to submit Contributions to MegEngine, to authorize Contributions submitted by its designated employees to MegEngine, and to grant copyright and patent licenses.
thereto. You accept and agree to the following terms and conditions for Your present and future Contributions submitted to MegEngine. Except for the license granted herein to MegEngine and recipients of software distributed by MegEngine, You reserve all right, title, and interest in and to Your Contributions.
1. **Definitions**. "You" (or "Your") shall mean the copyright owner or legal entity authorized by the copyright owner that is making this Agreement with MegEngine. For legal entities, the entity making a Contribution and all other entities that control, are controlled by, or are under common control with that entity are considered to be a single Contributor.
For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"Contribution" shall mean the code, documentation or any original work of authorship, including any modifications or additions to an existing work, that is intentionally submitted by You to MegEngine for inclusion in, or documentation of, any of the products owned or managed by MegEngine (the "Work").
For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to MegEngine or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, MegEngine for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by You as "Not a Contribution."
2. **Grant of Copyright License**. Subject to the terms and conditions of this Agreement, You hereby grant to MegEngine and to recipients of software distributed by MegEngine a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare derivative works of, publicly display, publicly perform, sublicense, and distribute Your Contributions and such derivative works.
3. **Grant of Patent License**. Subject to the terms and conditions of this Agreement, You hereby grant to MegEngine and to recipients of software distributed by MegEngine a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by You that are necessarily infringed by Your Contribution(s) alone or by combination of Your Contribution(s) with the Work to which such Contribution(s) was submitted. If any entity institutes patent litigation against You or any other entity (including a crossclaim or counterclaim in a lawsuit) alleging that Your Contribution, or the Work to which You have contributed, constitutes direct or contributory patent infringement, then any patent licenses granted to that entity under this Agreement for that Contribution or Work shall terminate as of the date such litigation is filed.
4. You represent that You are legally entitled to grant the above license. If You are an entity, You represent further that each of Your employee designated by You is authorized to submit Contributions on behalf of You. If You are an individual and Your employer(s) has rights to intellectual property that You create that includes Your Contributions, You represent further that You have received permission to make Contributions on behalf of that employer, that Your employer has waived such rights for Your Contributions to MegEngine, or that Your employer has executed a separate CLA with MegEngine.
5. If you do post content or submit material on MegEngine and unless we indicate otherwise, you grant MegEngine a nonexclusive, royalty-free, perpetual, irrevocable, and fully sublicensable right to use, reproduce, modify, adapt, publish, perform, translate, create derivative works from, distribute, and display such content throughout the world in any media. You grant MegEngine and sublicensees the right to use your GitHub Public Profile, including but not limited to name, that you submit in connection with such content. You represent and warrant that you own or otherwise control all of the rights to the content that you post; that the content is accurate; that use of the content you supply does not violate this policy and will not cause injury to any person or entity; and that you will indemnify MegEngine for all claims resulting from content you supply. MegEngine has the right but not the obligation to monitor and edit or remove any activity or content. MegEngine takes no responsibility and assumes no liability for any content posted by you or any third party.
6. You represent that each of Your Contributions is Your original creation. Should You wish to submit work that is not Your original creation, You may submit it to MegEngine separately from any Contribution, identifying the complete details of its source and of any license or other restriction (including, but not limited to, related patents, trademarks, and license agreements) of which You are personally aware, and conspicuously marking the work as "Submitted on behalf of a third party: [named here]".
7. You are not expected to provide support for Your Contributions, except to the extent You desire to provide support. You may provide support for free, for a fee, or not at all. Unless required by applicable law or agreed to in writing, You provide Your Contributions on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE.
8. You agree to notify MegEngine of any facts or circumstances of which You become aware that would make these representations inaccurate in any respect.
9. This the effective date of this Contributor License Agreement is 2020/3/23. MegEngine reserves the right to update or change this Agreement at any time, by posting the most current version of the Agreement on MegEngine, with a new effective date. All such changes in the Agreement are effective from the effective date. Your continued use of MegEngine after we post any such changes signifies your agreement to those changes. If you do not agree to the then-current Agreement, you must immediately discontinue using MegEngine.
MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
Copyright (c) 2014-2020 Megvii Inc. All rights reserved.
Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
Apache License
Version 2.0, January 2004
http://www.apache.org/licenses/
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
1. Definitions.
"License" shall mean the terms and conditions for use, reproduction, and distribution as defined by Sections 1 through 9 of this document.
"Licensor" shall mean the copyright owner or entity authorized by the copyright owner that is granting the License.
"Legal Entity" shall mean the union of the acting entity and all other entities that control, are controlled by, or are under common control with that entity. For the purposes of this definition, "control" means (i) the power, direct or indirect, to cause the direction or management of such entity, whether by contract or otherwise, or (ii) ownership of fifty percent (50%) or more of the outstanding shares, or (iii) beneficial ownership of such entity.
"You" (or "Your") shall mean an individual or Legal Entity exercising permissions granted by this License.
"Source" form shall mean the preferred form for making modifications, including but not limited to software source code, documentation source, and configuration files.
"Object" form shall mean any form resulting from mechanical transformation or translation of a Source form, including but not limited to compiled object code, generated documentation, and conversions to other media types.
"Work" shall mean the work of authorship, whether in Source or Object form, made available under the License, as indicated by a copyright notice that is included in or attached to the work (an example is provided in the Appendix below).
"Derivative Works" shall mean any work, whether in Source or Object form, that is based on (or derived from) the Work and for which the editorial revisions, annotations, elaborations, or other modifications represent, as a whole, an original work of authorship. For the purposes of this License, Derivative Works shall not include works that remain separable from, or merely link (or bind by name) to the interfaces of, the Work and Derivative Works thereof.
"Contribution" shall mean any work of authorship, including the original version of the Work and any modifications or additions to that Work or Derivative Works thereof, that is intentionally submitted to Licensor for inclusion in the Work by the copyright owner or by an individual or Legal Entity authorized to submit on behalf of the copyright owner. For the purposes of this definition, "submitted" means any form of electronic, verbal, or written communication sent to the Licensor or its representatives, including but not limited to communication on electronic mailing lists, source code control systems, and issue tracking systems that are managed by, or on behalf of, the Licensor for the purpose of discussing and improving the Work, but excluding communication that is conspicuously marked or otherwise designated in writing by the copyright owner as "Not a Contribution."
"Contributor" shall mean Licensor and any individual or Legal Entity on behalf of whom a Contribution has been received by Licensor and subsequently incorporated within the Work.
2. Grant of Copyright License.
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable copyright license to reproduce, prepare Derivative Works of, publicly display, publicly perform, sublicense, and distribute the Work and such Derivative Works in Source or Object form.
3. Grant of Patent License.
Subject to the terms and conditions of this License, each Contributor hereby grants to You a perpetual, worldwide, non-exclusive, no-charge, royalty-free, irrevocable (except as stated in this section) patent license to make, have made, use, offer to sell, sell, import, and otherwise transfer the Work, where such license applies only to those patent claims licensable by such Contributor that are necessarily infringed by their Contribution(s) alone or by combination of their Contribution(s) with the Work to which such Contribution(s) was submitted. If You institute patent litigation against any entity (including a cross-claim or counterclaim in a lawsuit) alleging that the Work or a Contribution incorporated within the Work constitutes direct or contributory patent infringement, then any patent licenses granted to You under this License for that Work shall terminate as of the date such litigation is filed.
4. Redistribution.
You may reproduce and distribute copies of the Work or Derivative Works thereof in any medium, with or without modifications, and in Source or Object form, provided that You meet the following conditions:
You must give any other recipients of the Work or Derivative Works a copy of this License; and
You must cause any modified files to carry prominent notices stating that You changed the files; and
You must retain, in the Source form of any Derivative Works that You distribute, all copyright, patent, trademark, and attribution notices from the Source form of the Work, excluding those notices that do not pertain to any part of the Derivative Works; and
If the Work includes a "NOTICE" text file as part of its distribution, then any Derivative Works that You distribute must include a readable copy of the attribution notices contained within such NOTICE file, excluding those notices that do not pertain to any part of the Derivative Works, in at least one of the following places: within a NOTICE text file distributed as part of the Derivative Works; within the Source form or documentation, if provided along with the Derivative Works; or, within a display generated by the Derivative Works, if and wherever such third-party notices normally appear. The contents of the NOTICE file are for informational purposes only and do not modify the License. You may add Your own attribution notices within Derivative Works that You distribute, alongside or as an addendum to the NOTICE text from the Work, provided that such additional attribution notices cannot be construed as modifying the License.
You may add Your own copyright statement to Your modifications and may provide additional or different license terms and conditions for use, reproduction, or distribution of Your modifications, or for any such Derivative Works as a whole, provided Your use, reproduction, and distribution of the Work otherwise complies with the conditions stated in this License.
5. Submission of Contributions.
Unless You explicitly state otherwise, any Contribution intentionally submitted for inclusion in the Work by You to the Licensor shall be under the terms and conditions of this License, without any additional terms or conditions. Notwithstanding the above, nothing herein shall supersede or modify the terms of any separate license agreement you may have executed with Licensor regarding such Contributions.
6. Trademarks.
This License does not grant permission to use the trade names, trademarks, service marks, or product names of the Licensor, except as required for reasonable and customary use in describing the origin of the Work and reproducing the content of the NOTICE file.
7. Disclaimer of Warranty.
Unless required by applicable law or agreed to in writing, Licensor provides the Work (and each Contributor provides its Contributions) on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied, including, without limitation, any warranties or conditions of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A PARTICULAR PURPOSE. You are solely responsible for determining the appropriateness of using or redistributing the Work and assume any risks associated with Your exercise of permissions under this License.
8. Limitation of Liability.
In no event and under no legal theory, whether in tort (including negligence), contract, or otherwise, unless required by applicable law (such as deliberate and grossly negligent acts) or agreed to in writing, shall any Contributor be liable to You for damages, including any direct, indirect, special, incidental, or consequential damages of any character arising as a result of this License or out of the use or inability to use the Work (including but not limited to damages for loss of goodwill, work stoppage, computer failure or malfunction, or any and all other commercial damages or losses), even if such Contributor has been advised of the possibility of such damages.
9. Accepting Warranty or Additional Liability.
While redistributing the Work or Derivative Works thereof, You may choose to offer, and charge a fee for, acceptance of support, warranty, indemnity, or other liability obligations and/or rights consistent with this License. However, in accepting such obligations, You may act only on Your own behalf and on Your sole responsibility, not on behalf of any other Contributor, and only if You agree to indemnify, defend, and hold each Contributor harmless for any liability incurred by, or claims asserted against, such Contributor by reason of your accepting any such warranty or additional liability.
END OF TERMS AND CONDITIONS
# MegEngine Model Hub
## 如何添加新模型
- 请创建一个新的 Github Repo,放置 `hub_conf.py`
- 在 models 这个目录中放置一个描述文件,文件命名请改为`组织名_模型名.md`
- 请参考 [样例文件](./megengine_example.md) 的格式
- 请同时添加中文、英文版本
- `github_link` 请指向 `hub_conf.py` 所在 repo
- 发一个新的 Pull Request
## 如何测试确认
- 请运行 `scripts/generate_data.py --source=../models --output=/data` 生成 json 文件
---
template: hub1
title: ResNet
summary:
en_US: Deep residual networks pre-trained on ImageNet
zh_CN: 深度残差网络(ImageNet 预训练权重)
author: MegEngine Team
tags: [vision, classification]
github-link: https://github.com/megengine/models
---
```python3
import megengine.hub
model = megengine.hub.load('megengine/models', 'resnet18', pretrained=True)
# or any of these variants
# model = megengine.hub.load('megengine/models', 'resnet34', pretrained=True)
# model = megengine.hub.load('megengine/models', 'resnet50', pretrained=True)
# model = megengine.hub.load('megengine/models', 'resnet101', pretrained=True)
model.eval()
```
<!-- section: zh_CN -->
中文文档
<!-- section: en_US -->
English Doc
---
template: hub1
title: BERT for Finetune
summary:
en_US: Bidirectional Encoder Representation from Transformers (BERT)
zh_CN: BERT
author: MegEngine Team
tags: [nlp]
github-link: https://github.com/megengine/models
---
```python
import megengine.hub as hub
model = megengine.hub.load("megengine/models", "wwm_cased_L-24_H-1024_A-16", pretrained=True)
# or any of these variants
# model = megengine.hub.load("megengine/models", "wwm_cased_L-24_H-1024_A-16", pretrained=True)
# model = megengine.hub.load("megengine/models", "wwm_uncased_L-24_H-1024_A-16", pretrained=True)
# model = megengine.hub.load("megengine/models", "cased_L-12_H-768_A-12", pretrained=True)
# model = megengine.hub.load("megengine/models", "cased_L-24_H-1024_A-16", pretrained=True)
# model = megengine.hub.load("megengine/models", "uncased_L-12_H-768_A-12", pretrained=True)
# model = megengine.hub.load("megengine/models", "uncased_L-24_H-1024_A-16", pretrained=True)
# model = megengine.hub.load("megengine/models", "chinese_L-12_H-768_A-12", pretrained=True)
# model = megengine.hub.load("megengine/models", "multi_cased_L-12_H-768_A-12", pretrained=True)
```
<!-- section: zh_CN -->
这个项目中, 我们用MegEngine重新实现了Google开源的BERT模型.
我们提供了以下预训练模型供用户在不同的下游任务中进行finetune.
* `wwm_cased_L-24_H-1024_A-16`
* `wwm_uncased_L-24_H-1024_A-16`
* `cased_L-12_H-768_A-12`
* `cased_L-24_H-1024_A-16`
* `uncased_L-12_H-768_A-12`
* `uncased_L-24_H-1024_A-16`
* `chinese_L-12_H-768_A-12`
* `multi_cased_L-12_H-768_A-12`
模型的权重来自Google的pre-trained models, 其含义也与其一致, 用户可以直接使用`megengine.hub`轻松的调用预训练的bert模型, 以及下载对应的`vocab.txt``bert_config.json`. 我们在[models](https://github.com/megengine/models/official/nlp/bert)中还提供了更加方便的脚本, 可以通过任务名直接获取到对应字典, 配置, 与预训练模型.
```python
import megengine.hub as hub
import urllib
import urllib.request
import os
DATA_URL = 'https://data.megengine.org.cn/models/weights/bert'
CONFIG_NAME = 'bert_config.json'
VOCAB_NAME = 'vocab.txt'
MODEL_NAME = {
'wwm_cased_L-24_H-1024_A-16': 'wwm_cased_L_24_H_1024_A_16',
'wwm_uncased_L-24_H-1024_A-16': 'wwm_uncased_L_24_H_1024_A_16',
'cased_L-12_H-768_A-12': 'cased_L_12_H_768_A_12',
'cased_L-24_H-1024_A-16': 'cased_L_24_H_1024_A_16',
'uncased_L-12_H-768_A-12': 'uncased_L_12_H_768_A_12',
'uncased_L-24_H-1024_A-16': 'uncased_L_24_H_1024_A_16',
'chinese_L-12_H-768_A-12': 'chinese_L_12_H_768_A_12',
'multi_cased_L-12_H-768_A-12': 'multi_cased_L_12_H_768_A_12'
}
def download_file(url, filename):
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
def create_hub_bert(model_name, pretrained):
assert model_name in MODEL_NAME, '{} not in the valid models {}'.format(model_name, MODEL_NAME)
data_dir = './{}'.format(model_name)
if not os.path.exists(data_dir):
os.makedirs(data_dir)
vocab_url = '{}/{}/{}'.format(DATA_URL, model_name, VOCAB_NAME)
config_url = '{}/{}/{}'.format(DATA_URL, model_name, CONFIG_NAME)
vocab_file = './{}/{}'.format(model_name, VOCAB_NAME)
config_file = './{}/{}'.format(model_name, CONFIG_NAME)
download_file(vocab_url, vocab_file)
download_file(config_url, config_file)
config = BertConfig(config_file)
model = hub.load(
"megengine/models",
MODEL_NAME[model_name],
pretrained=pretrained,
)
return model, config, vocab_file
```
为了用户可以更加方便的使用预训练模型, 我们仅保留了模型的`BertModel`的部分, 在实际使用中, 可以将带有预训练的权重的`bert`模型作为其他模型的一部分, 在初始化函数中传入.
```python
class BertForSequenceClassification(Module):
def __init__(self, config, num_labels, bert):
self.bert = bert
self.num_labels = num_labels
self.dropout = Dropout(config.hidden_dropout_prob)
self.classifier = Linear(config.hidden_size, num_labels)
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None):
_, pooled_output = self.bert(
input_ids, token_type_ids,
attention_mask, output_all_encoded_layers=False)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
if labels is not None:
loss = cross_entropy_with_softmax(
logits.reshape(-1, self.num_labels),
labels.reshape(-1))
return logits, loss
else:
return logits, None
bert, config, vocab_file = create_hub_bert('uncased_L-12_H-768_A-12', pretrained=True)
model = BertForSequenceClassification(config, num_labels=2, bert=bert)
```
所有预训练模型希望数据被正确预处理, 其要求与Google中的开源bert一致, 详细可以参考 [bert](https://github.com/google-research/bert), 或者参考在[models](https://github.com/megengine/models/official/nlp/bert)中提供的样例.
### 模型描述
我们在[models](https://github.com/megengine/models/official/nlp/bert)中提供了简单的示例代码.
此示例代码在Microsoft Research Paraphrase(MRPC)数据集上对预训练的`uncased_L-12_H-768_A-12`模型进行微调.
我们的样例代码中使用了原始的超参进行微调, 在测试集中可以得到84%到88%的正确率.
### 参考文献
- [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805), Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova;
<!-- section: en_US -->
This repository contains reimplemented Google's BERT by MegEngine.
We provide the following pre-trained models for users to finetune in different tasks.
* `wwm_cased_L-24_H-1024_A-16`
* `wwm_uncased_L-24_H-1024_A-16`
* `cased_L-12_H-768_A-12`
* `cased_L-24_H-1024_A-16`
* `uncased_L-12_H-768_A-12`
* `uncased_L-24_H-1024_A-16`
* `chinese_L-12_H-768_A-12`
* `multi_cased_L-12_H-768_A-12`
The weight of the model comes from Google's pre-trained models, and its meaning is also consistent with it. Users can use `megengine.hub` to easily use the pre-trained bert model, and download the corresponding` vocab.txt` and `bert_config.json`. We also provide a convenient script in [models] (https://github.com/megengine/models/official/nlp/bert), which can directly obtain the corresponding dictionary, configuration, and pre-trained model by task name. .
```python
import megengine.hub as hub
import urllib
import urllib.request
import os
DATA_URL = 'https://data.megengine.org.cn/models/weights/bert'
CONFIG_NAME = 'bert_config.json'
VOCAB_NAME = 'vocab.txt'
MODEL_NAME = {
'wwm_cased_L-24_H-1024_A-16': 'wwm_cased_L_24_H_1024_A_16',
'wwm_uncased_L-24_H-1024_A-16': 'wwm_uncased_L_24_H_1024_A_16',
'cased_L-12_H-768_A-12': 'cased_L_12_H_768_A_12',
'cased_L-24_H-1024_A-16': 'cased_L_24_H_1024_A_16',
'uncased_L-12_H-768_A-12': 'uncased_L_12_H_768_A_12',
'uncased_L-24_H-1024_A-16': 'uncased_L_24_H_1024_A_16',
'chinese_L-12_H-768_A-12': 'chinese_L_12_H_768_A_12',
'multi_cased_L-12_H-768_A-12': 'multi_cased_L_12_H_768_A_12'
}
def download_file(url, filename):
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
def create_hub_bert(model_name, pretrained):
assert model_name in MODEL_NAME, '{} not in the valid models {}'.format(model_name, MODEL_NAME)
data_dir = './{}'.format(model_name)
if not os.path.exists(data_dir):
os.makedirs(data_dir)
vocab_url = '{}/{}/{}'.format(DATA_URL, model_name, VOCAB_NAME)
config_url = '{}/{}/{}'.format(DATA_URL, model_name, CONFIG_NAME)
vocab_file = './{}/{}'.format(model_name, VOCAB_NAME)
config_file = './{}/{}'.format(model_name, CONFIG_NAME)
download_file(vocab_url, vocab_file)
download_file(config_url, config_file)
config = BertConfig(config_file)
model = hub.load(
"megengine/models",
MODEL_NAME[model_name],
pretrained=pretrained,
)
return model, config, vocab_file
```
In order to make it easier for the user to use the pre-trained model, we only keep the `BertModel` part of the original bert model. For example, The` bert` model with pre-trained weights can be used as a part of other models.
```python
class BertForSequenceClassification(Module):
def __init__(self, config, num_labels, bert):
self.bert = bert
self.num_labels = num_labels
self.dropout = Dropout(config.hidden_dropout_prob)
self.classifier = Linear(config.hidden_size, num_labels)
def forward(self, input_ids, token_type_ids=None, attention_mask=None, labels=None):
_, pooled_output = self.bert(
input_ids, token_type_ids,
attention_mask, output_all_encoded_layers=False)
pooled_output = self.dropout(pooled_output)
logits = self.classifier(pooled_output)
if labels is not None:
loss = cross_entropy_with_softmax(
logits.reshape(-1, self.num_labels),
labels.reshape(-1))
return logits, loss
else:
return logits, None
bert, config, vocab_file = create_hub_bert('uncased_L-12_H-768_A-12', pretrained=True)
model = BertForSequenceClassification(config, num_labels=2, bert=bert)
```
All pre-trained models expect the data to be pre-processed correctly. The requirements are consistent with the Google's bert. For details, please refer to original [bert] (https://github.com/google-research/bert), or refer to our example [models] ( https://github.com/megengine/models/official/nlp/bert).
### Model Description
We provide example code in [models] (https://github.com/megengine/models/official/nlp/bert).
This example code fine-tunes the pre-trained `uncased_L-12_H-768_A-12` model on the Microsoft Research Paraphrase (MRPC) dataset.
Our test ran on the original implementation hyper-parameters gave evaluation results between 84% and 88%.
### References
- [BERT: Pre-training of Deep Bidirectional Transformers for Language Understanding](https://arxiv.org/abs/1810.04805), Jacob Devlin, Ming-Wei Chang, Kenton Lee, Kristina Toutanova;
---
template: hub1
title: deeplabv3plus
summary:
en_US: Deeplabv3plus pre-trained on VOC
zh_CN: Deeplabv3plus (VOC预训练权重)
author: MegEngine Team
tags: [vision]
github-link: https://github.com/megengine/models
---
```python
from megengine import hub
model = hub.load(
"megengine/models",
"deeplabv3plus_res101",
pretrained=True,
)
model.eval()
```
<!-- section: zh_CN -->
所有预训练模型希望数据被正确预处理。模型要求输入BGR的图片, 建议缩放到512x512,最后做归一化处理 (均值为: `[103.530, 116.280, 123.675]`, 标准差为: `[57.375, 57.120, 58.395]`)。
下面是一段处理一张图片的样例代码。
```python
# Download an example image from the megengine data website
import urllib
url, filename = ("https://data.megengine.org.cn/images/cat.jpg", "cat.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# Read and pre-process the image
import cv2
import numpy as np
import megengine.data.transform as T
import megengine.functional as F
import megengine.jit as jit
@jit.trace(symbolic=True, opt_level=2)
def pred_fun(data, net=None):
net.eval()
pred = net(data)
return pred
image = cv2.imread("cat.jpg")
orih, oriw = image.shape[:2]
transform = T.Compose([
T.Resize((512, 512)),
T.Normalize(mean=[103.530, 116.280, 123.675], std=[57.375, 57.120, 58.395]), # BGR
T.ToMode(),
])
processed_img = transform.apply(image)[np.newaxis] # CHW -> 1CHW
pred = pred_fun(processed_img, net=model)
pred = pred.numpy().squeeze().argmax(axis=0)
pred = cv2.resize(pred.astype("uint8"), (oriw, orih), interpolation=cv2.INTER_LINEAR)
```
### 模型描述
目前我们提供了deeplabv3plus的预训练模型, 在voc验证集的表现如下:
Methods | Backbone | TrainSet | EvalSet | mIoU_single | mIoU_multi |
:--: |:--: |:--: |:--: |:--: |:--: |
DeepLab v3+ | ResNet101 | train_aug | val | 79.0 | 79.8 |
### 参考文献
- [Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation](https://arxiv.org/abs/1802.02611.pdf), Liang-Chieh Chen, Yukun Zhu, George Papandreou, Florian Schroff, and
Hartwig Adam; ECCV, 2018
<!-- section: en_US -->
All pre-trained models expect input images normalized in the same way. Input images must be 3-channel BGR images of shape (H x W x 3), reszied to (512 x 512), then normalized using mean = [103.530, 116.280, 123.675] and std = [57.375, 57.120, 58.395]).
Here's a sample execution.
```python
# Download an example image from the megengine data website
import urllib
url, filename = ("https://data.megengine.org.cn/images/cat.jpg", "cat.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# Read and pre-process the image
import cv2
import numpy as np
import megengine.data.transform as T
import megengine.functional as F
import megengine.jit as jit
@jit.trace(symbolic=True, opt_level=2)
def pred_fun(data, net=None):
net.eval()
pred = net(data)
return pred
image = cv2.imread("cat.jpg")
orih, oriw = image.shape[:2]
transform = T.Compose([
T.Resize((512, 512)),
T.Normalize(mean=[103.530, 116.280, 123.675], std=[57.375, 57.120, 58.395]), # BGR
T.ToMode(),
])
processed_img = transform.apply(image)[np.newaxis, :] # CHW -> 1CHW
pred = pred_fun(processed_img, net=model)
pred = pred.numpy().squeeze().argmax(axis=0)
pred = cv2.resize(pred.astype("uint8"), (oriw, orih), interpolation=cv2.INTER_LINEAR)
```
### Model Description
Methods | Backbone | TrainSet | EvalSet | mIoU_single | mIoU_multi |
:--: |:--: |:--: |:--: |:--: |:--: |
DeepLab v3+ | ResNet101 | train_aug | val | 79.0 | 79.8 |
### References
- [Encoder-Decoder with Atrous Separable Convolution for Semantic Image Segmentation](https://arxiv.org/abs/1802.02611.pdf), Liang-Chieh Chen, Yukun Zhu, George Papandreou, Florian Schroff, and
Hartwig Adam; ECCV, 2018
---
template: hub1
title: ResNet
summary:
en_US: Deep residual networks pre-trained on ImageNet
zh_CN: 深度残差网络(ImageNet 预训练权重)
author: MegEngine Team
tags: [vision, classification]
github-link: https://github.com/megengine/models
---
```python
import megengine.hub
model = megengine.hub.load('megengine/models', 'resnet18', pretrained=True)
# or any of these variants
# model = megengine.hub.load('megengine/models', 'resnet34', pretrained=True)
# model = megengine.hub.load('megengine/models', 'resnet50', pretrained=True)
# model = megengine.hub.load('megengine/models', 'resnet101', pretrained=True)
# model = megengine.hub.load('megengine/models', 'resnext50_32x4d', pretrained=True)
model.eval()
```
<!-- section: zh_CN -->
所有预训练模型希望数据被正确预处理。
模型要求输入BGR的图片, 短边缩放到`256`, 并中心裁剪至`(224 x 224)`的大小,最后做归一化处理 (均值为: `[103.530, 116.280, 123.675]`, 标准差为: `[57.375, 57.120, 58.395]`)。
下面是一段处理一张图片的样例代码。
```python
# Download an example image from the megengine data website
import urllib
url, filename = ("https://data.megengine.org.cn/images/cat.jpg", "cat.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# Read and pre-process the image
import cv2
import numpy as np
import megengine.data.transform as T
import megengine.functional as F
image = cv2.imread("cat.jpg")
transform = T.Compose([
T.Resize(256),
T.CenterCrop(224),
T.Normalize(mean=[103.530, 116.280, 123.675], std=[57.375, 57.120, 58.395]), # BGR
T.ToMode("CHW"),
])
processed_img = transform.apply(image)[np.newaxis, :] # CHW -> 1CHW
logits = model(processed_img)
probs = F.softmax(logits)
print(probs)
```
### 模型描述
目前我们提供了以下几个预训练模型,分别是`resnet18`, `resnet34`, `resnet50`, `resnet101``resnext50_32x4d`,它们在ImageNet验证集上的单crop性能如下表:
| 模型 | Top1 acc | Top5 acc |
| --- | --- | --- |
| ResNet18 | 70.312 | 89.430 |
| ResNet34 | 73.960 | 91.630 |
| ResNet50 | 76.254 | 93.056 |
| ResNet101| 77.944 | 93.844 |
| ResNeXt50 32x4d | 77.592 | 93.644 |
### 参考文献
- [Deep Residual Learning for Image Recognition](http://openaccess.thecvf.com/content_cvpr_2016/papers/He_Deep_Residual_Learning_CVPR_2016_paper.pdf), Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun; The IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2016, pp. 770-778
- [Aggregated Residual Transformation for Deep Neural Networks](http://openaccess.thecvf.com/content_cvpr_2017/papers/Xie_Aggregated_Residual_Transformations_CVPR_2017_paper.pdf), Saining Xie, Ross Girshick, Piotr Dollar, Zhuowen Tu, Kaiming He; The IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2017, pp. 1492-1500
<!-- section: en_US -->
All pre-trained models expect input images normalized in the same way,
i.e. input images must be 3-channel BGR images of shape `(H x W x 3)`, and reszied shortedge to `256`, center-cropped to `(224 x 224)`.
The images should be normalized using `mean = [103.530, 116.280, 123.675]` and `std = [57.375, 57.120, 58.395])`.
Here's a sample execution.
```python
# Download an example image from the megengine data website
import urllib
url, filename = ("https://data.megengine.org.cn/images/cat.jpg", "cat.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# Read and pre-process the image
import cv2
import numpy as np
import megengine.data.transform as T
import megengine.functional as F
image = cv2.imread("cat.jpg")
transform = T.Compose([
T.Resize(256),
T.CenterCrop(224),
T.Normalize(mean=[103.530, 116.280, 123.675], std=[57.375, 57.120, 58.395]), # BGR
T.ToMode("CHW"),
])
processed_img = transform.apply(image)[np.newaxis, :] # CHW -> 1CHW
logits = model(processed_img)
probs = F.softmax(logits)
print(probs)
```
### Model Description
Currently we provide these pretrained models: `resnet18`, `resnet34`, `resnet50`, `resnet101`, `resnext50_32x4d`. Their 1-crop accuracy on ImageNet validation dataset can be found in following table.
| model | Top1 acc | Top5 acc |
| --- | --- | --- |
| ResNet18 | 70.312 | 89.430 |
| ResNet34 | 73.960 | 91.630 |
| ResNet50 | 76.254 | 93.056 |
| ResNet101| 77.944 | 93.844 |
| ResNeXt50 32x4d | 77.592 | 93.644 |
### References
- [Deep Residual Learning for Image Recognition](http://openaccess.thecvf.com/content_cvpr_2016/papers/He_Deep_Residual_Learning_CVPR_2016_paper.pdf), Kaiming He, Xiangyu Zhang, Shaoqing Ren, Jian Sun; The IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2016, pp. 770-778
- [Aggregated Residual Transformation for Deep Neural Networks](http://openaccess.thecvf.com/content_cvpr_2017/papers/Xie_Aggregated_Residual_Transformations_CVPR_2017_paper.pdf), Saining Xie, Ross Girshick, Piotr Dollar, Zhuowen Tu, Kaiming He; The IEEE Conference on Computer Vision and Pattern Recognition (CVPR), 2017, pp. 1492-1500
---
template: hub1
title: retinanet
summary:
en_US: RetinaNet pre-trained on COCO
zh_CN: RetinaNet (COCO预训练权重)
author: MegEngine Team
tags: [vision, detection]
github-link: https://github.com/megengine/models
---
```python
from megengine import hub
model = hub.load(
"megengine/models",
"retinanet_res50_1x_800size",
pretrained=True,
)
model.eval()
models_api = hub.import_module(
"megengine/models",
git_host="github.com",
)
```
<!-- section: zh_CN -->
所有预训练模型希望数据被正确预处理。
模型要求输入BGR的图片, 同时需要等比例缩放到:短边和长边分别不超过800/1333
最后做归一化处理 (均值为: `[103.530, 116.280, 123.675]`, 标准差为: `[57.375, 57.120, 58.395]`)。
下面是一段处理一张图片的样例代码。
```python
# Download an example image from the megengine data website
import urllib
url, filename = ("https://data.megengine.org.cn/images/cat.jpg", "cat.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# Read and pre-process the image
import cv2
image = cv2.imread("cat.jpg")
data, im_info = models_api.DetEvaluator.process_inputs(image, 800, 1333)
model.inputs["image"].set_value(data)
model.inputs["im_info"].set_value(im_info)
from megengine import jit
@jit.trace(symbolic=True)
def infer():
predictions = model(model.inputs)
return predictions
print(infer())
```
### 模型描述
目前我们提供了retinanet的预训练模型, 在coco验证集上的结果如下:
| model | mAP<br>@5-95 |
| --- | --- |
| retinanet-res50-1x-800size | 36.0 |
### 参考文献
- [Focal Loss for Dense Object Detection](https://arxiv.org/pdf/1708.02002) Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár. Proceedings of the IEEE international conference on computer vision. 2017: 2980-2988.
- [Microsoft COCO: Common Objects in Context](https://arxiv.org/pdf/1405.0312.pdf) Lin, Tsung-Yi and Maire, Michael and Belongie, Serge and Hays, James and Perona, Pietro and Ramanan, Deva and Dollár, Piotr and Zitnick, C Lawrence
Lin T Y, Maire M, Belongie S, et al. European conference on computer vision. Springer, Cham, 2014: 740-755.
<!-- section: en_US -->
All pre-trained models expect input images normalized in the same way,
i.e. input images must be 3-channel BGR images of shape `(H x W x 3)`, and reszied shortedge/longedge to no more than `800/1333`.
The images should be normalized using `mean = [103.530, 116.280, 123.675]` and `std = [57.375, 57.120, 58.395])`.
Here's a sample execution.
```python
# Download an example image from the megengine data website
import urllib
url, filename = ("https://data.megengine.org.cn/images/cat.jpg", "cat.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# Read and pre-process the image
import cv2
image = cv2.imread("cat.jpg")
data, im_info = models_api.DetEvaluator.process_inputs(image, 800, 1333)
model.inputs["image"].set_value(data)
model.inputs["im_info"].set_value(im_info)
from megengine import jit
@jit.trace(symbolic=True)
def infer():
predictions = model(model.inputs)
return predictions
print(infer())
```
### Model Description
Currently we provide a `retinanet` model which is pretrained on `COCO2017` training set. The mAP on `COCO2017` val set can be found in following table.
| model | mAP<br>@5-95 |
| --- | --- |
| retinanet-res50-1x-800size | 36.0 |
### References
- [Focal Loss for Dense Object Detection](https://arxiv.org/pdf/1708.02002) Tsung-Yi Lin, Priya Goyal, Ross Girshick, Kaiming He, Piotr Dollár. Proceedings of the IEEE international conference on computer vision. 2017: 2980-2988.
- [Microsoft COCO: Common Objects in Context](https://arxiv.org/pdf/1405.0312.pdf) Lin, Tsung-Yi and Maire, Michael and Belongie, Serge and Hays, James and Perona, Pietro and Ramanan, Deva and Dollár, Piotr and Zitnick, C Lawrence
Lin T Y, Maire M, Belongie S, et al. European conference on computer vision. Springer, Cham, 2014: 740-755.
---
template: hub1
title: ResNet
summary:
en_US: "ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design"
zh_CN: ShuffleNet V2(ImageNet 预训练权重)
author: MegEngine Team
tags: [vision, classification]
github-link: https://github.com/megengine/models
---
```python
import megengine.hub
model = megengine.hub.load('megengine/models', 'shufflenet_v2_x1_0', pretrained=True)
model.eval()
```
<!-- section: zh_CN -->
所有预训练模型希望数据被正确预处理。
模型要求输入BGR的图片, 短边缩放到`256`, 并中心裁剪至`(224 x 224)`的大小,最后做归一化处理 (均值为: `[103.530, 116.280, 123.675]`, 标准差为: `[57.375, 57.120, 58.395]`)。
下面是一段处理一张图片的样例代码。
```python
# Download an example image from the megengine data website
import urllib
url, filename = ("https://data.megengine.org.cn/images/cat.jpg", "cat.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# Read and pre-process the image
import cv2
import numpy as np
import megengine.data.transform as T
import megengine.functional as F
image = cv2.imread("cat.jpg")
transform = T.Compose([
T.Resize(256),
T.CenterCrop(224),
T.Normalize(mean=[103.530, 116.280, 123.675], std=[57.375, 57.120, 58.395]), # BGR
T.ToMode("CHW"),
])
processed_img = transform.apply(image)[np.newaxis, :] # CHW -> 1CHW
logits = model(processed_img)
probs = F.softmax(logits)
print(probs)
```
### 模型描述
目前我们提供了部分在ImageNet上的预训练模型(见下表),各个网络结构在ImageNet验证集上的表现如下:
| 模型 | top1 acc | top5 acc |
| --- | --- | --- |
| shufflenet_v2_x1_0 | 69.369 | 88.793 |
### 参考文献
- [ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design](https://arxiv.org/abs/1807.11164), Ma, Ningning, et al. "Shufflenet v2: Practical guidelines for efficient cnn architecture design." Proceedings of the European Conference on Computer Vision (ECCV). 2018.
<!-- section: en_US -->
All pre-trained models expect input images normalized in the same way,
i.e. input images must be 3-channel BGR images of shape `(H x W x 3)`, and reszied shortedge to `256`, center-cropped to `(224 x 224)`.
The images should be normalized using `mean = [103.530, 116.280, 123.675]` and `std = [57.375, 57.120, 58.395])`.
Here's a sample execution.
```python
# Download an example image from the megengine data website
import urllib
url, filename = ("https://data.megengine.org.cn/images/cat.jpg", "cat.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# Read and pre-process the image
import cv2
import numpy as np
import megengine.data.transform as T
import megengine.functional as F
image = cv2.imread("cat.jpg")
transform = T.Compose([
T.Resize(256),
T.CenterCrop(224),
T.Normalize(mean=[103.530, 116.280, 123.675], std=[57.375, 57.120, 58.395]), # BGR
T.ToMode("CHW"),
])
processed_img = transform.apply(image)[np.newaxis, :] # CHW -> 1CHW
logits = model(processed_img)
probs = F.softmax(logits)
print(probs)
```
### Model Description
Currently we provide several pretrained models(see the table below), Their 1-crop accuracy on ImageNet validation dataset can be found in following table.
| model | top1 acc | top5 acc |
| --- | --- | --- |
| shufflenet_v2_x1_0 | 69.369 | 88.793 |
### References
- [ShuffleNet V2: Practical Guidelines for Efficient CNN Architecture Design](https://arxiv.org/abs/1807.11164), Ma, Ningning, et al. "Shufflenet v2: Practical guidelines for efficient cnn architecture design." Proceedings of the European Conference on Computer Vision (ECCV). 2018.
\ No newline at end of file
REQUIRED_LANGUAGES = ("zh_CN",)
ALL_LANGUAGES = ("zh_CN", "en_US")
import os
import json
import argparse
import attr
import parser
import tags
p = argparse.ArgumentParser(description="Process some integers.")
p.add_argument("--source", type=str, help=".md source directory", required=True)
p.add_argument("--output", type=str, help="output directory", required=True)
if __name__ == "__main__":
args = p.parse_args()
os.makedirs(os.path.join(args.output, "api/v1/models"), exist_ok=True)
models = []
for name in os.listdir(args.source):
if not name.endswith(".md"):
continue
if name in ("README.md",):
continue
id = name[:-3]
with open(os.path.join(args.source, name)) as f:
model = parser.parse_file(id, f)
data = model.to_jsondict()
with open(
os.path.join(args.output, "api/v1/models", "{}.json".format(id)), "w"
) as f:
json.dump(data, f)
models.append(data["meta"])
tags_dict = {}
for t in tags.TAGS:
tags_dict[t.id] = t.name
with open(os.path.join(args.output, "api/v1/models.json"), "w") as f:
data = {
"models": models,
"tags": tags_dict,
}
json.dump(data, f)
from datetime import datetime
from typing import List, Tuple, Dict, Any, Iterable, Optional
import attr
import consts
import tags
@attr.s()
class ModelMeta:
id = attr.ib(validator=attr.validators.instance_of(str))
title = attr.ib(validator=attr.validators.instance_of(str))
author = attr.ib(validator=attr.validators.instance_of(str))
summary = attr.ib()
github_link = attr.ib(
validator=attr.validators.instance_of(str)
) # TODO change to URL validator
tags = attr.ib()
update_time = attr.ib(
default=None,
init=False,
validator=attr.validators.optional(attr.validators.instance_of(datetime)),
)
@summary.validator
def summary_validtor(self, attribute, value):
if not isinstance(value, dict):
raise ValueError(
"summary must be a mapping from language to text, not {}".format(
type(value)
)
)
for k, v in value.items():
if k not in consts.ALL_LANGUAGES:
raise ValueError("summary key must be a supported language")
if not isinstance(v, str):
raise ValueError("summary value must be text")
@tags.validator
def tags_validtor(self, attribute, value):
if not isinstance(value, list):
raise ValueError("tags must be a list")
supported_tags = set(map(lambda t: t.id, tags.TAGS))
for t in value:
if t not in supported_tags:
raise ValueError(
"tag '{}' is not in tag list, please update list in tags.py".format(
t
)
)
def to_jsondict(self):
return {
"id": self.id,
"title": self.title,
"author": self.author,
"summary": self.summary,
"githubLink": self.github_link,
"tags": self.tags,
"updateTime": self.update_time,
}
@attr.s(auto_attribs=True)
class ModelContent:
sample_code: str
full_text: Dict[str, str]
def to_jsondict(self):
return {
"sampleCode": self.sample_code,
"fullText": self.full_text,
}
@attr.s(auto_attribs=True)
class Model:
meta: ModelMeta
content: ModelContent
def to_jsondict(self):
return {"meta": self.meta.to_jsondict(), "content": self.content.to_jsondict()}
import re
import json
from typing import List, Tuple, Dict, Iterable
import attr
import yaml
import mistune
import consts
from model import ModelMeta, ModelContent, Model
section_re = re.compile(r"^\s*<!--\s*section:\s*(.+)\s*-->\s*$")
UNSPECIFIC_SECTION = "unspecific"
markdown_ast = mistune.create_markdown(
renderer=mistune.AstRenderer(),
plugins=["strikethrough", "table", "footnotes", "table", "url"],
)
markdown_html = mistune.create_markdown(
renderer=mistune.HTMLRenderer(),
plugins=["strikethrough", "table", "footnotes", "table", "url"],
)
def split_by_meta_and_content(lines: List[str]) -> Tuple[List[str], List[str]]:
meta: List[str] = []
if lines[0] != "---":
return meta, lines
lines.pop(0)
for line in lines:
if line in ("---", "..."):
content_beginning = lines.index(line) + 1
lines = lines[content_beginning:]
break
meta.append(line)
return meta, lines
def split_sections(lines: List[str]) -> Dict[str, List[str]]:
current_section_name = UNSPECIFIC_SECTION
sections: Dict[str, List[str]] = {current_section_name: []}
for line in lines:
m = section_re.match(line)
if m is None:
sections[current_section_name].append(line)
else:
current_section_name = m.group(1).strip()
if current_section_name in sections:
raise ValueError(
"Duplicated section name: {}".format(current_section_name)
)
sections[current_section_name] = []
return sections
def generate_meta(id: str, meta_lines: List[str]) -> ModelMeta:
meta_input = yaml.load("\n".join(meta_lines), yaml.SafeLoader)
if meta_input["template"] != "hub1":
raise ValueError("unsupported template '{}'".format(meta_input["template"]))
meta = ModelMeta(
id=id,
title=meta_input["title"],
author=meta_input["author"],
summary=meta_input["summary"],
github_link=meta_input["github-link"],
tags=meta_input["tags"],
)
return meta
def generate_content(sections: Dict[str, List[str]]) -> ModelContent:
for key in sections.keys():
if key not in [*consts.ALL_LANGUAGES, UNSPECIFIC_SECTION]:
raise ValueError(
"unsupported language section {}, please check supported language in consts".format(
key
)
)
for key in consts.REQUIRED_LANGUAGES:
if key not in sections:
raise ValueError(
"Missing required language sections, please check required language in consts"
)
# Check sample code in unspecific section.
# Make sure it exist and is the one stuff in it.
code_section = sections.pop(UNSPECIFIC_SECTION)
code_ast = markdown_ast("\n".join(code_section))
code_blocks = list(filter(lambda x: x["type"] != "newline", code_ast))
if len(code_blocks) != 1 or code_blocks[0]["type"] != "block_code":
raise ValueError(
"document must start with sample code, follow by section comments"
)
sections_content: Dict[str, str] = {}
# Use markdown engine process each section, confirm it's valid markdown.
for key, section in sections.items():
# Render all section decorate with sample code at beginning.
sections_content[key] = markdown_html("\n".join(code_section + section))
return ModelContent(sample_code=code_blocks[0]["text"], full_text=sections_content)
def parse_file(id: str, lines: Iterable[str]):
# Strip right only to prevent strip indent.
lines = list(map(str.rstrip, lines))
meta_lines, content_lines = split_by_meta_and_content(lines)
meta = generate_meta(id, meta_lines)
sections = split_sections(content_lines)
content = generate_content(sections)
return Model(meta=meta, content=content)
if __name__ == "__main__":
with open("../megengine_example.md") as f:
model = parse_file("megengine_example", f)
# TODO fill update time.
print(json.dumps(attr.asdict(model)))
black==19.10b0
mistune==2.0.0a1
attrs==19.1.0
PyYAML==5.2
from typing import Dict
from attr import attrs
__all__ = ["TAGS"]
@attrs(auto_attribs=True)
class Tag:
id: str
name: Dict[str, str] # Mapping from language to text
TAGS = [
Tag(id="vision", name={"zh_CN": "视觉", "en_US": "Vision"}),
Tag(id="detection", name={"zh_CN": "识别", "en_US": "Detection"}),
Tag(id="classification", name={"zh_CN": "分类", "en_US": "Detection"}),
Tag(id="nlp", name={"zh_CN": "自然语言处理", "en_US": "NLP"}),
]
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册