Skip to content
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

Merged
merged 2 commits into from
Jul 17, 2023
Merged

Conversation

chenyang23333
Copy link
Collaborator

@chenyang23333 chenyang23333 commented Jul 7, 2023

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)

@chenyang23333 chenyang23333 added the documentation Improvements or additions to documentation label Jul 7, 2023
@chenyang23333 chenyang23333 added this to the mindyolo-0.1 milestone Jul 7, 2023
@chenyang23333 chenyang23333 self-assigned this Jul 7, 2023
Comment on lines 17 to 22
__BASE__:
- '../coco.yaml'
- './hyp.scratch-high.yaml'
#可以通过__BASE__指定基础配置文件的路径,如上<br>
#其中coco.yaml为数据相关配置文件,hyp.scratch-high.yaml用于定义训练时的超参数
#可参考[configs目录](../../../configs)下其他模型
Copy link
Collaborator

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'

Comment on lines 34 to 46
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 层。
Copy link
Collaborator

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代码编写网络结构。
Copy link
Collaborator

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代码来编写网络
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

把直接定义的方式放到前面去

Comment on lines 195 to 200
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,
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

如果要在create_model中使用文件名创建自定义的模型,那么需要先使用注册器进行注册

Comment on lines 106 to 113
self.reset_parameter()

def construct(self, x):
return self.model(x)

def reset_parameter(self): #定义了reset_parameter方法用于初始化模型的参数。

initialize_defult(self)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. reset_parameter 定义可以删掉,这个不是必须的;
  2. 可以直接在 init 中直接调用 initialize_defult 并描述原因:
initialize_defult(self)  # 可选,你可能需要initialize_defult方法以获得和pytorch一样的conv2d、dense层的初始化方式;

Comment on lines 116 to 170
## 在模型函数中创建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
```
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这段可以删掉,和下面一样

model = MyModel()

```

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

注册模型 (可选)

如果需要使用mindyolo接口初始化自定义的模型,那么需要先对模型进行 注册导入

  1. 模型注册 # 注册后的模型可以通过 create_model 接口以模型名的方式进行访问;
    ...
  2. 模型导入
    ...

Comment on lines 34 to 37
```
在construct方法中,定义了模型的前向传播过程。输入x经过卷积、激活函数、池化等操作后,通过展平操作将特征张量变为一维向量,然后通过全连接层得到最终的输出结果。

```python
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这两段连在一起?

Copy link
Collaborator Author

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部分的配置
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  #head部分的配置
  head:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

Comment on lines 140 to 141


Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

空行删掉

Copy link
Collaborator Author

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()
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

157行删掉

Copy link
Collaborator Author

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代码来编写网络
### 模块导入
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

模块导入

Copy link
Collaborator Author

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__中,定义模型的各个组件:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

创建模型

Copy link
Collaborator Author

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
```
### 创建模型实例
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

创建模型实例

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed


```

## 2.使用yaml文件编写网络
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

2.使用yaml文件编写网络

Copy link
Collaborator Author

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文件的详细指导
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

以下是编写mymodel.yaml文件的详细指导:

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

@CaitinZhao CaitinZhao merged commit d459bea into mindspore-lab:master Jul 17, 2023
4 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants