赞
踩
Implementation of ResMLP, an all MLP solution to image classification out of Facebook AI, in Pytorch
$ pip install res-mlp-pytorch
import torch
from res_mlp_pytorch import ResMLP
model = ResMLP(
image_size = 256,
patch_size = 16,
dim = 512,
depth = 12,
num_classes = 1000
)
img = torch.randn(1, 3, 256, 256)
pred = model(img) # (1, 1000)
Rectangular image
import torch
from res_mlp_pytorch import ResMLP
model = ResMLP(
image_size = (128, 256), # (128 x 256)
patch_size = 16,
dim = 512,
depth = 12,
num_classes = 1000
)
img = torch.randn(1, 3, 128, 256)
pred = model(img) # (1, 1000)
@misc{touvron2021resmlp,
title = {ResMLP: Feedforward networks for image classification with data-efficient training},
author = {Hugo Touvron and Piotr Bojanowski and Mathilde Caron and Matthieu Cord and Alaaeldin El-Nouby and Edouard Grave and Armand Joulin and Gabriel Synnaeve and Jakob Verbeek and Hervé Jégou},
year = {2021},
eprint = {2105.03404},
archivePrefix = {arXiv},
primaryClass = {cs.CV}
}
.\lucidrains\res-mlp-pytorch\res_mlp_pytorch\res_mlp_pytorch.py
import torch from torch import nn, einsum from einops.layers.torch import Rearrange, Reduce # 导入必要的库 # 定义一个函数,如果输入不是元组,则返回一个包含相同值的元组 def pair(val): return (val, val) if not isinstance(val, tuple) else val # 定义一个仿射变换类 class Affine(nn.Module): def __init__(self, dim): super().__init__() self.g = nn.Parameter(torch.ones(1, 1, dim)) self.b = nn.Parameter(torch.zeros(1, 1, dim)) def forward(self, x): return x * self.g + self.b # 定义一个预仿射后层缩放类 class PreAffinePostLayerScale(nn.Module): # https://arxiv.org/abs/2103.17239 def __init__(self, dim, depth, fn): super().__init__() # 根据深度选择初始化值 if depth <= 18: init_eps = 0.1 elif depth > 18 and depth <= 24: init_eps = 1e-5 else: init_eps = 1e-6 scale = torch.zeros(1, 1, dim).fill_(init_eps) self.scale = nn.Parameter(scale) self.affine = Affine(dim) self.fn = fn def forward(self, x): return self.fn(self.affine(x)) * self.scale + x # 定义一个ResMLP模型 def ResMLP(*, image_size, patch_size, dim, depth, num_classes, expansion_factor = 4): image_height, image_width = pair(image_size) assert (image_height % patch_size) == 0 and (image_width % patch_size) == 0, 'image height and width must be divisible by patch size' num_patches = (image_height // patch_size) * (image_width // patch_size) wrapper = lambda i, fn: PreAffinePostLayerScale(dim, i + 1, fn) return nn.Sequential( Rearrange('b c (h p1) (w p2) -> b (h w) (p1 p2 c)', p1 = patch_size, p2 = patch_size), nn.Linear((patch_size ** 2) * 3, dim), *[nn.Sequential( wrapper(i, nn.Conv1d(num_patches, num_patches, 1)), wrapper(i, nn.Sequential( nn.Linear(dim, dim * expansion_factor), nn.GELU(), nn.Linear(dim * expansion_factor, dim) )) ) for i in range(depth)], Affine(dim), Reduce('b n c -> b c', 'mean'), nn.Linear(dim, num_classes) ) # 返回一个包含ResMLP模型结构的序列
.\lucidrains\res-mlp-pytorch\res_mlp_pytorch\__init__.py
# 从 res_mlp_pytorch.res_mlp_pytorch 模块中导入 ResMLP 类
from res_mlp_pytorch.res_mlp_pytorch import ResMLP
.\lucidrains\res-mlp-pytorch\setup.py
# 导入设置工具和查找包的函数 from setuptools import setup, find_packages # 设置包的元数据 setup( name = 'res-mlp-pytorch', # 包的名称 packages = find_packages(exclude=[]), # 查找并包含所有包 version = '0.0.6', # 版本号 license='MIT', # 许可证 description = 'ResMLP - Pytorch', # 描述 author = 'Phil Wang', # 作者 author_email = 'lucidrains@gmail.com', # 作者邮箱 url = 'https://github.com/lucidrains/res-mlp-pytorch', # 项目链接 keywords = [ # 关键词列表 'artificial intelligence', 'deep learning', 'image recognition' ], install_requires=[ # 安装依赖 'einops>=0.3', 'torch>=1.6' ], classifiers=[ # 分类器列表 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'Topic :: Scientific/Engineering :: Artificial Intelligence', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 3.6', ], )
Implementation of Retrieval-Augmented Denoising Diffusion Probabilistic Models in Pytorch
This will make use of the Clip Retrieval library made by @rom1504
@article{Blattmann2022RetrievalAugmentedDM,
title = {Retrieval-Augmented Diffusion Models},
author = {A. Blattmann and Robin Rombach and K Oktay and Bj{\"o}rn Ommer},
journal = {ArXiv},
year = {2022},
volume = {abs/2204.11824}
}
.\lucidrains\retrieval-augmented-ddpm\retrieval_augmented_ddpm\retrieval_augmented_ddpm.py
# 定义一个名为calculate_area的函数,用于计算矩形的面积
def calculate_area(length, width):
# 计算矩形的面积
area = length * width
# 返回计算得到的面积
return area
.\lucidrains\retrieval-augmented-ddpm\retrieval_augmented_ddpm\__init__.py
# 定义一个名为calculate_area的函数,用于计算矩形的面积
def calculate_area(length, width):
# 计算矩形的面积
area = length * width
# 返回计算得到的面积
return area
.\lucidrains\retrieval-augmented-ddpm\setup.py
# 导入设置安装和查找包的函数 from setuptools import setup, find_packages # 设置包的元数据 setup( name = 'retrieval-augmented-ddpm', # 包的名称 packages = find_packages(exclude=[]), # 查找所有包 version = '0.0.1', # 版本号 license='MIT', # 许可证 description = 'Retrieval-Augmented Denoising Diffusion Probabilistic Models', # 描述 author = 'Phil Wang', # 作者 author_email = 'lucidrains@gmail.com', # 作者邮箱 url = 'https://github.com/lucidrains/retrieval-augmented-ddpm', # 项目链接 keywords = [ # 关键词列表 'artificial intelligence', 'deep learning', 'denoising diffusion', 'retrieval' ], install_requires=[ # 安装依赖的包 'clip-retrieval', 'einops>=0.4', 'torch>=1.6', ], classifiers=[ # 分类器列表 'Development Status :: 4 - Beta', 'Intended Audience :: Developers', 'Topic :: Scientific/Engineering :: Artificial Intelligence', 'License :: OSI Approved :: MIT License', 'Programming Language :: Python :: 3.6', ], )
Implementation of RETRO, Deepmind’s Retrieval based Attention net, in Pytorch. This will deviate from the paper slightly, using rotary embeddings for relative positional encoding, as well as Faiss library instead of Scann.
This library leverages autofaiss for building the index and calculating the k-nearest neighbors for all chunks.
Jay Alammar explanatory blogpost
The selling point of this retriever approach is reaching GPT-3 performance at 10x less parameters. More research is definitely deserved in this area.
I have also included the features necessary to scale the retrieval transformer to 1000 layers, if the claims of DeepNet paper is to be believed.
Update: Someone on Reddit has gifted me a Gold Award. Not sure what it is, but thank you!
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。