Skip to content

Commit 157336a

Browse files
committed
Amend the TinyMS hub design document
1 parent 5e825a0 commit 157336a

File tree

2 files changed

+110
-0
lines changed

2 files changed

+110
-0
lines changed

docs/en/source/design/concepts.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,61 @@ model = Model(net)
149149
model.compile(metrics={"Accuracy": Accuracy())
150150
```
151151

152+
### Pre-trained model loading (*hub*)
153+
154+
TinyMS Hub is a pre-trained model application tool, serving as a channel for model developers and application developers.
155+
156+
- Provide model developers with a convenient and fast channel for model release and submission.
157+
- Provide application developers with high-quality pre-trained models, and complete the work of model migration to deployment quickly using model loading and fine-tuning APIs.
158+
159+
Current pre-trained models in TinyMS Hub mainly cover four mainstream task scenarios including `image classification`, `object detection`, `semantic segmentation` and `recommendation`.
160+
161+
There are several of scenarios for users to leverage `hub` to easily load the pre-trained model:
162+
163+
* Load pre-trained model
164+
165+
```python
166+
from PIL import Image
167+
from tinyms import hub
168+
from tinyms.vision import mnist_transform
169+
from tinyms.model import Model
170+
171+
img = Image.open(img_path)
172+
img = mnist_transform(img)
173+
174+
# load LeNet5 pre-trained model
175+
net= hub.load('tinyms/0.2/lenet5_v1_mnist', class_num=10)
176+
model = Model(net)
177+
178+
res = model.predict(ts.expand_dims(ts.array(img), 0)).asnumpy()
179+
print("The label is:", mnist_transform.postprocess(res))
180+
```
181+
182+
* Load model checkpoint
183+
184+
```python
185+
from tinyms import hub
186+
from tinyms.model import lenet5
187+
from tinyms.utils.train import load_checkpoint
188+
189+
ckpt_dist_file = '/tmp/lenet5.ckpt'
190+
hub.load_checkpoint('tinyms/0.2/lenet5_v1_mnist', ckpt_dist_file)
191+
net = lenet5()
192+
load_checkpoint(ckpt_dist_file, net=net)
193+
```
194+
195+
* Load model weights
196+
197+
```python
198+
from tinyms import hub
199+
from tinyms.model import lenet5
200+
from tinyms.utils.train import load_param_into_net
201+
202+
param_dict = hub.load_weights('tinyms/0.2/lenet5_v1_mnist')
203+
net = lenet5()
204+
load_param_into_net(net, param_dict)
205+
```
206+
152207
### Model deployment (*serving*)
153208

154209
Model deployment refers to the process of servicing pre-trained models so that they can quickly and efficiently process data input by users and obtain results. MindSpore provides the [predict](https://mindspore.cn/doc/api_python/en/r1.2/_modules/mindspore/train/model.html#Model.predict) function for inference. TinyMS provides a complete set of start server (`start_server`), check backend (`list_servables`), check start status (`server_started`) and shut down the server (`shutdown`) and other functions based on [Flask](https://flask.palletsprojects.com/en/1.1.x/) ; Take the `LeNet5` network as an example:

docs/zh_CN/source/design/concepts.md

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,61 @@ model = Model(net)
150150
model.compile(metrics={"Accuracy": Accuracy())
151151
```
152152

153+
### 预训练模型加载(*hub*)
154+
155+
TinyMS Hub是TinyMS生态的预训练模型应用工具,作为模型开发者和应用开发者的管道:
156+
157+
- 向模型开发者提供方便快捷的模型发布、提交通道;
158+
- 向应用开发者提供高质量的预训练模型,结合模型加载以及模型Fine-tune API快速完成模型的迁移到部署的工作。
159+
160+
TinyMS Hub提供的预训练模型主要包括`图像分类``目标检测``语义模型``推荐模型`等。
161+
162+
当前`hub`模块为开发者提供了多种加载预训练模型的接口:
163+
164+
* 加载预训练模型
165+
166+
```python
167+
from PIL import Image
168+
from tinyms import hub
169+
from tinyms.vision import mnist_transform
170+
from tinyms.model import Model
171+
172+
img = Image.open(img_path)
173+
img = mnist_transform(img)
174+
175+
# load LeNet5 pre-trained model
176+
net= hub.load('tinyms/0.2/lenet5_v1_mnist', class_num=10)
177+
model = Model(net)
178+
179+
res = model.predict(ts.expand_dims(ts.array(img), 0)).asnumpy()
180+
print("The label is:", mnist_transform.postprocess(res))
181+
```
182+
183+
* 加载模型ckpt文件
184+
185+
```python
186+
from tinyms import hub
187+
from tinyms.model import lenet5
188+
from tinyms.utils.train import load_checkpoint
189+
190+
ckpt_dist_file = '/tmp/lenet5.ckpt'
191+
hub.load_checkpoint('tinyms/0.2/lenet5_v1_mnist', ckpt_dist_file)
192+
net = lenet5()
193+
load_checkpoint(ckpt_dist_file, net=net)
194+
```
195+
196+
* 加载模型权重
197+
198+
```python
199+
from tinyms import hub
200+
from tinyms.model import lenet5
201+
from tinyms.utils.train import load_param_into_net
202+
203+
param_dict = hub.load_weights('tinyms/0.2/lenet5_v1_mnist')
204+
net = lenet5()
205+
load_param_into_net(net, param_dict)
206+
```
207+
153208
### 模型部署推理(*serving*)
154209

155210
模型部署推理是指将预训练好的模型服务化,使其快速、高效地对用户输入的数据进行处理,得到结果的过程。MindSpore提供了[predict](https://mindspore.cn/doc/api_python/zh-CN/r1.2/_modules/mindspore/train/model.html#Model.predict)函数用于推理,同样的,TinyMS针对这个函数进行了相应的封装,以同一个接口对接不同的后端网络。为了实现服务化,TinyMS基于[Flask](https://flask.palletsprojects.com/en/1.1.x/)提供了整套的启动服务器(`start_server`)、检查后端(`list_servables`)、检查是否启动(`server_started`)和关闭服务器(`shutdown`)等功能; 以`LeNet5`网络为例:

0 commit comments

Comments
 (0)