时间:2025-07-18 作者:游乐小编
本文介绍如何通过Paddle.Hub在GitHub分享模型。先搭建MLP-Mixer模型,含MlpBlock、MixerBlock等组件及mixer_b、mixer_l预置模型。接着构建项目,含模型代码文件和hubconf.py配置文件。经本地测试后,上传至GitHub,他人可方便加载使用,接入过程简单。
# 导入必要的包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登录后复制
model = mixer_b(pretrained=True)out = model(paddle.randn((1, 3, 224, 224)))print(out.shape)登录后复制
[1, 1000]登录后复制
# hubconf.py# 依赖包dependencies = ['paddle', 'numpy']# 导入要分享的模型from model import mixer_b, mixer_l登录后复制
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]登录后复制
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]登录后复制 代码解释
2021-11-05 11:52
手游攻略2021-11-19 18:38
手游攻略2021-10-31 23:18
手游攻略2022-06-03 14:46
游戏资讯2025-06-28 12:37
单机攻略