-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
add write a new model file #167
Conversation
__BASE__: | ||
- '../coco.yaml' | ||
- './hyp.scratch-high.yaml' | ||
#可以通过__BASE__指定基础配置文件的路径,如上<br> | ||
#其中coco.yaml为数据相关配置文件,hyp.scratch-high.yaml用于定义训练时的超参数 | ||
#可参考[configs目录](../../../configs)下其他模型 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# __BASE__中的yaml表示用于继承的基础配置文件,重复的参数会被当前文件覆盖;
__BASE__:
- '../coco.yaml'
- './hyp.scratch-high.yaml'
backbone: #骨干网络部分的配置 | ||
[[-1, 1, ConvNormAct, [32, 3, 1]], # 0 | ||
[-1, 1, ConvNormAct, [64, 3, 2]], # 1-P1/2 | ||
[-1, 1, Bottleneck, [64]], | ||
[-1, 1, ConvNormAct, [128, 3, 2]], # 3-P2/4 | ||
[-1, 2, Bottleneck, [128]], | ||
[-1, 1, ConvNormAct, [256, 3, 2]], # 5-P3/8 | ||
[-1, 8, Bottleneck, [256]], | ||
] | ||
#第一个元素表示该层的 ID,-1 表示自动分配。 | ||
#第二个元素表示该层的重复次数,1 表示只有一次,-1 表示不限次数。 | ||
#第三个元素表示该层使用的操作类型,这里为 ConvNormAct 和 Bottleneck。其中,ConvNormAct 表示包含卷积、归一化和激活操作的基本卷积层,Bottleneck 表示包含瓶颈结构的卷积层。 | ||
#第四个元素表示该层的超参数,包括输出通道数、卷积核尺寸、步长等。例如,[-1, 1, ConvNormAct, [32, 3, 1]] 表示一个输出通道数为 32、卷积核大小为 3x3、步长为 1 的 ConvNormAct 层。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
# 骨干网络部分的配置,每层的元素含义为
# [from, number, module, args]
# 以第一层为例,[-1, 1, ConvNormAct, [32, 3, 1]], 表示输入来自 `-1`(上一层) ,重复次数为 1,模块名为 ConvNormAct,模块输入参数为 [32, 3, 1];
backbone:
[[-1, 1, ConvNormAct, [32, 3, 1]], # 0
[-1, 1, ConvNormAct, [64, 3, 2]], # 1-P1/2
[-1, 1, Bottleneck, [64]],
[-1, 1, ConvNormAct, [128, 3, 2]], # 3-P2/4
[-1, 2, Bottleneck, [128]],
[-1, 1, ConvNormAct, [256, 3, 2]], # 5-P3/8
[-1, 8, Bottleneck, [256]],
]
@@ -1,2 +1,205 @@ | |||
# 模型编写指南 | |||
即将到来 | |||
本文档提供MindYOLO编写自定义模型的教程。<br> | |||
支持使用YAML文件编写网络结构,也可以直接使用Python代码编写网络结构。 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
我们可以 直接定义一个网络 也可以 使用yaml文件方式定义一个网络。
from .mymodel import * | ||
``` | ||
|
||
## 方法二 直接使用python代码来编写网络 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
把直接定义的方式放到前面去
model = create_model( | ||
model_name="mymodel", | ||
model_cfg=opt.net, | ||
num_classes=opt.data.nc, | ||
sync_bn=opt.sync_bn if hasattr(opt, "sync_bn") else False, | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
如果要在create_model中使用文件名创建自定义的模型,那么需要先使用注册器进行注册
self.reset_parameter() | ||
|
||
def construct(self, x): | ||
return self.model(x) | ||
|
||
def reset_parameter(self): #定义了reset_parameter方法用于初始化模型的参数。 | ||
|
||
initialize_defult(self) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- reset_parameter 定义可以删掉,这个不是必须的;
- 可以直接在 init 中直接调用 initialize_defult 并描述原因:
initialize_defult(self) # 可选,你可能需要initialize_defult方法以获得和pytorch一样的conv2d、dense层的初始化方式;
## 在模型函数中创建Mymodel模型实例 | ||
mymodel函数被@register_model装饰器修饰,使其成为可调用的模型。在函数内通过MYmodel类创建了一个模型对象,并返回该对象作为模型。 | ||
```python | ||
@register_model | ||
def mymodel(cfg, in_channels=3, num_classes=None, **kwargs) -> MYmodel: | ||
"""Get GoogLeNet model. | ||
Refer to the base class `models.GoogLeNet` for more details.""" | ||
model = MYmodel(cfg=cfg, in_channels=in_channels, num_classes=num_classes, **kwargs) | ||
return model | ||
``` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这段可以删掉,和下面一样
model = MyModel() | ||
|
||
``` | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
注册模型 (可选)
如果需要使用mindyolo接口初始化自定义的模型,那么需要先对模型进行 注册
和 导入
- 模型注册 # 注册后的模型可以通过 create_model 接口以模型名的方式进行访问;
... - 模型导入
...
``` | ||
在construct方法中,定义了模型的前向传播过程。输入x经过卷积、激活函数、池化等操作后,通过展平操作将特征张量变为一维向量,然后通过全连接层得到最终的输出结果。 | ||
|
||
```python |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
这两段连在一起?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
[-1, 8, Bottleneck, [256]], | ||
] | ||
|
||
head: #head部分的配置 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
#head部分的配置
head:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
空行删掉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
|
||
initialize_defult() # 可选,你可能需要initialize_defult方法以获得和pytorch一样的conv2d、dense层的初始化方式; | ||
|
||
self.reset_parameter() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
157行删掉
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
write a new model file final edition write a new model file final2 edition write a new model file clean code
|
||
## 模型定义 | ||
### 1.直接使用python代码来编写网络 | ||
### 模块导入 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
模块导入
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
``` | ||
|
||
### 创建模型 | ||
定义了一个继承自nn.Cell的模型类MyModel。在构造函数__init__中,定义模型的各个组件: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
创建模型
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
x = self.fc(x) | ||
return x | ||
``` | ||
### 创建模型实例 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
创建模型实例
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
|
||
``` | ||
|
||
## 2.使用yaml文件编写网络 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
2.使用yaml文件编写网络
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
- 新建对应的mymodel.py文件 | ||
- 在mindyolo/models/_init_.py文件中引入该模型 | ||
|
||
### 以下是编写mymodel.yaml文件的详细指导 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
以下是编写mymodel.yaml文件的详细指导:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fixed
Thank you for your contribution to the MindYOLO repo.
Before submitting this PR, please make sure:
Motivation
(Write your motivation for proposed changes here.)
Test Plan
(How should this PR be tested? Do you require special setup to run the test or repro the fixed bug?)
Related Issues and PRs
(Is this PR part of a group of changes? Link the other relevant PRs and Issues here. Use https://help.github.com/en/articles/closing-issues-using-keywords for help on GitHub syntax)