本文介绍如何通过Paddle.Hub在GitHub分享模型。先搭建MLP-Mixer模型,含MlpBlock、MixerBlock等组件及mixer_b、mixer_l预置模型。接着构建项目,含模型代码文件和hubconf.py配置文件。经本地测试后,上传至GitHub,他人可方便加载使用,接入过程简单。
引入 上个项目 Paddle.Hub 初探:快速基于预训练模型实现猫的 12 分类 简单介绍了下 paddle.hub api 的功能和用法当然除了使用最新开发的模型以为,个人也可以通过这个 api 来分享自己编写的模型本次就通过一个案例详细讲解一下,如何在 GitHub 上分享自己的模型
搭建模型 分享模型之前首先需要自己将模型搭建出来这里就用一个前不久发布的实现非常简洁的模型 MLP-Mixer 来作为演示有几个需要修改的小点,都在代码中做了比较详细的注释参考项目:MLP is all you need ?In [ ]
# 导入必要的包import paddleimport paddle.nn as nn# MLP Blockclass MlpBlock(nn.Layer): def __init__(self, features_dim, mlp_dim): super().__init__() self.fc_0 = nn.Linear(features_dim, mlp_dim) self.fc_1 = nn.Linear(mlp_dim, features_dim) def forward(self, x): y = self.fc_0(x) y = nn.functional.gelu(y) y = self.fc_1(y) return y# Mixer Blockclass MixerBlock(nn.Layer): def __init__(self, token_dim, channels_dim, tokens_mlp_dim, channels_mlp_dim, norm_layer=nn.LayerNorm, epsilon=1e-6): super().__init__() self.norm_0 = norm_layer(channels_dim, epsilon=epsilon) self.token_mixing = MlpBlock(token_dim, tokens_mlp_dim) self.norm_1 = norm_layer(channels_dim, epsilon=epsilon) self.channel_mixing = MlpBlock(channels_dim, channels_mlp_dim) def forward(self, x): y = self.norm_0(x) y = y.transpose((0, 2, 1)) y = self.token_mixing(y) y = y.transpose((0, 2, 1)) x = x + y y = self.norm_1(x) y = self.channel_mixing(y) x = x + y return x# MLP-Mixerclass MlpMixer(nn.Layer): def __init__(self, img_size=(224, 224), patch_size=(16, 16), num_blocks=12, hidden_dim=768, tokens_mlp_dim=384, channels_mlp_dim=3072, norm_layer=nn.LayerNorm, epsilon=1e-6, class_dim=1000): super().__init__() self.class_dim = class_dim self.stem = nn.Conv2D( 3, hidden_dim, kernel_size=patch_size, stride=patch_size) blocks = [ MixerBlock( (img_size[0] // patch_size[0]) ** 2, hidden_dim, tokens_mlp_dim, channels_mlp_dim, norm_layer, epsilon ) for _ in range(num_blocks) ] self.blocks = nn.Sequential(*blocks) self.pre_head_layer_norm = norm_layer(hidden_dim, epsilon=epsilon) if class_dim > 0: self.head = nn.Linear(hidden_dim, class_dim) def forward(self, inputs): x = self.stem(inputs) x = x.transpose((0, 2, 3, 1)) x = x.flatten(1, 2) x = self.blocks(x) x = self.pre_head_layer_norm(x) if self.class_dim > 0: x = x.mean(axis=1) x = self.head(x) return x# 预置模型 mixer_b# 如果需要给出帮助信息# 需要在下方添加类似的注释def mixer_b(pretrained=False, **kwargs): ''' Model: MLP-mixer-base Params: pretrained: load the pretrained model img_size: input image size patch_size: patch size num_classes: number of classes num_blocks: number of MixerBlock hidden_dim: dim of hidden tokens_mlp_dim: dim of tokens_mlp channels_mlp_dim: dim of channels_mlp ''' model = MlpMixer( hidden_dim=768, num_blocks=12, tokens_mlp_dim=384, channels_mlp_dim=3072, **kwargs ) # 一般分享模型都是包含预训练模型的 # 可通过网络加载 # 或者通过存放于项目目录中直接加载(GitHub 托管时不推荐,会极大的增加项目大小,拉取时间会非常漫长) if pretrained: path = paddle.utils.download.get_weights_path_from_url('https://bj.bcebos.com/v1/ai-studio-online/8fcd0b6ba98042d68763bbcbfe96375cbfd97ffed8334ac09787ef73ecf9989f?responseContentDisposition=attachment%3B%20filename%3Dimagenet1k_Mixer-B_16.pdparams') model.set_dict(paddle.load(path)) return model# 预置模型 mixer_l# 如果需要给出帮助信息# 需要在下方添加类似的注释def mixer_l(pretrained=False, **kwargs): ''' Model: MLP-mixer-large Params: pretrained: load the pretrained model img_size: input image size patch_size: patch size num_classes: number of classes num_blocks: number of MixerBlock hidden_dim: dim of hidden tokens_mlp_dim: dim of tokens_mlp channels_mlp_dim: dim of channels_mlp ''' model = MlpMixer( hidden_dim=1024, num_blocks=24, tokens_mlp_dim=512, channels_mlp_dim=4096, **kwargs ) # 一般分享模型都是包含预训练模型的 # 可通过网络加载 # 或者通过存放于项目目录中直接加载(GitHub 托管时不推荐,会极大的增加项目大小,拉取时间会非常漫长) if pretrained: path = paddle.utils.download.get_weights_path_from_url('https://bj.bcebos.com/v1/ai-studio-online/ca74ababd4834e34b089c1485989738de4fdf6a97be645ed81b6e39449c5815c?responseContentDisposition=attachment%3B%20filename%3Dimagenet1k_Mixer-L_16.pdparams') model.set_dict(paddle.load(path)) return model 登录后复制
测试模型 In [ ]
model = mixer_b(pretrained=True)out = model(paddle.randn((1, 3, 224, 224)))print(out.shape) 登录后复制
[1, 1000] 登录后复制
项目搭建 第一步:建立一个项目文件夹,比如:MLP-Mixer-Paddle第二步:新建模型代码文件,比如:MLP-Mixer-Paddle/model.py,并将上面的模型代码复制粘贴到文件中第三步:新建 paddle.hub 配置文件:MLP-Mixer-Paddle/hubconf.py,具体代码如下:
# hubconf.py# 依赖包dependencies = ['paddle', 'numpy']# 导入要分享的模型from model import mixer_b, mixer_l 登录后复制
项目测试 In [1]
import paddle# 获取模型列表model_list = paddle.hub.list('MLP-Mixer-Paddle', source='local', force_reload=False)print("模型列表如下:\n", model_list)# 获取 mixer_b 的帮助文档model_help = paddle.hub.help('MLP-Mixer-Paddle', 'mixer_b', source='local', force_reload=False)print("mixer_b 的帮助文档如下:\n", model_help)# 加载 mixer_b 模型并测试model = paddle.hub.load('MLP-Mixer-Paddle', 'mixer_b', source='local', force_reload=False, pretrained=True)out = model(paddle.randn((1, 3, 224, 224)))print("输出的形状为:", out.shape) 登录后复制
模型列表如下: ['mixer_b', 'mixer_l']mixer_b 的帮助文档如下: Model: MLP-mixer-base Params: pretrained: load the pretrained model img_size: input image size patch_size: patch size num_classes: number of classes num_blocks: number of MixerBlock hidden_dim: dim of hidden tokens_mlp_dim: dim of tokens_mlp channels_mlp_dim: dim of channels_mlp 输出的形状为: [1, 1000] 登录后复制
上传代码 测试成功,项目和模型都正常之后,就可以将代码上传至 GitHub比如这个项目:jm12138/MLP-Mixer-Paddle然后大家就可以方便的通过 GitHub 来加载使用你分享的模型了In [2]
import paddle# 获取模型列表model_list = paddle.hub.list('jm12138/MLP-Mixer-Paddle:main', source='github', force_reload=False)print("模型列表如下:\n", model_list)# 获取 mixer_b 的帮助文档model_help = paddle.hub.help('jm12138/MLP-Mixer-Paddle:main', 'mixer_b', source='github', force_reload=False)print("mixer_b 的帮助文档如下:\n", model_help)# 加载 mixer_b 模型并测试model = paddle.hub.load('jm12138/MLP-Mixer-Paddle:main', 'mixer_b', source='github', force_reload=False, pretrained=True)out = model(paddle.randn((1, 3, 224, 224)))print("输出的形状为:", out.shape) 登录后复制
模型列表如下: ['mixer_b', 'mixer_l']mixer_b 的帮助文档如下: Model: MLP-mixer-base Params: pretrained: load the pretrained model img_size: input image size patch_size: patch size num_classes: number of classes num_blocks: number of MixerBlock hidden_dim: dim of hidden tokens_mlp_dim: dim of tokens_mlp channels_mlp_dim: dim of channels_mlp 登录后复制
Using cache found in /home/aistudio/.cache/paddle/hub/jm12138_MLP-Mixer-Paddle_mainUsing cache found in /home/aistudio/.cache/paddle/hub/jm12138_MLP-Mixer-Paddle_main 登录后复制
输出的形状为: [1, 1000] 登录后复制
代码解释
总结 其实接入 paddle.hub 这个 API 来分享自己的模型还是非常简单方便的如果是以前就做好的模型项目,只需要在其中添加一个相应的配置文件即可快速接入