Paddle.Hub 详解:通过 Paddle.Hub API 分享自己的项目
本文介绍如何通过Paddle Hub在GitHub分享模型。先搭建MLP-Mixer模型,含MlpBlock、MixerBlock等组件及mixer_b、mixer_l预置模型。接
本文介绍如何通过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 来分享自己的模型还是非常简单方便的如果是以前就做好的模型项目,只需要在其中添加一个相应的配置文件即可快速接入
热点追踪提示词
你是一名 AI 行业编辑,请围绕下面这条热点输出一份资讯解读:
热点:Paddle.Hub 详解:通过 Paddle.Hub API 分享自己的项目要求:
1. 先用一句话解释这条热点在讲什么
2. 再总结它为什么重要
3. 说明会影响哪些 AI 产品或内容方向
4. 最后给出 3 个适合资讯站使用的标题
游乐网为非赢利性网站,所展示的游戏/软件/文章内容均来自于互联网或第三方用户上传分享,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系youleyoucom@outlook.com。
相关热点AI热点2026-07-07 20:10
Dzine AI图像设计工具 卓越构图与风格控制
Dzine是一款强调构图控制与风格管理的AI图像设计工具,提供样式库、图层操作、定位和素描工具,支持文生图与图生图,具备生成填充编辑、一键修复增强及最高6144像素超高清导出功能,降低设计门槛,兼顾新手与专业用户。
AI热点2026-07-07 20:09
Arrival基于云的SaaS解决方案
3D虚拟空间的搭建,过去往往依赖专业建模软件和大量手动操作,技术门槛相当高。但现在,一款名为Arrival的云端SaaS解决方案正凭借AI与拖放功能,将这件事变得像搭积木一样轻松便捷。 什么是Arrival? Arrival本质上是一套专业的软件工具,核心目标就是帮助用户快速构建一个3D虚拟空间。它
AI热点2026-07-07 20:09
AI用户访谈:洞察需求加速产品市场匹配
ZENAI通过AI自动完成用户访谈,省去人工招募与主持流程,并自动总结用户场景、痛点及人物画像。产品经理、设计师、研究员可借此快速验证假设、提炼场景、获取市场洞察,加速产品市场契合度(PMF)达成,提供基础与专业两种套餐。
AI热点2026-07-07 20:09
Meshcapade ME AI生成逼真数字人头像平台
MeshcapadeMe基于SMPL人体模型技术,提供API接口支持图像、视频、测量及3D扫描输入,自动生成统一格式的逼真数字分身,无需专业建模技能即可将各类素材转化为可动画、跨平台使用的数字人类,适用于虚拟现实、游戏与影视等领域。
- 日榜
- 周榜
- 月榜
热点快看
