import torch
model = torch.hub.load('huawei-noah/Efficient-AI-Backbones', 'snnmlp_t', pretrained=True)
# or
# model = torch.hub.load('huawei-noah/Efficient-AI-Backbones', 'snnmlp_s', pretrained=True)
# or
# model = torch.hub.load('huawei-noah/Efficient-AI-Backbones', 'snnmlp_b', pretrained=True)
model.eval()

所有预训练模型都要求输入图像以相同方式进行归一化,即形状为 (3 x H x W) 的三通道 RGB 图像 mini-batch,其中 HW 预期至少为 224。图像必须加载到 [0, 1] 范围,然后使用 mean = [0.485, 0.456, 0.406]std = [0.229, 0.224, 0.225] 进行归一化。

这里有一个示例执行。

# Download an example image from the pytorch website
import urllib
url, filename = ("https://github.com/pytorch/hub/raw/master/dog.jpg", "dog.jpg")
try: urllib.URLopener().retrieve(url, filename)
except: urllib.request.urlretrieve(url, filename)
# sample execution (requires torchvision)
from PIL import Image
from torchvision import transforms
input_image = Image.open(filename)
preprocess = transforms.Compose([
    transforms.Resize(256),
    transforms.CenterCrop(224),
    transforms.ToTensor(),
    transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225]),
])
input_tensor = preprocess(input_image)
input_batch = input_tensor.unsqueeze(0) # create a mini-batch as expected by the model

# move the input and model to GPU for speed if available
if torch.cuda.is_available():
    input_batch = input_batch.to('cuda')
    model.to('cuda')

with torch.no_grad():
    output = model(input_batch)
# Tensor of shape 1000, with confidence scores over ImageNet's 1000 classes
print(output[0])
# The output has unnormalized scores. To get probabilities, you can run a softmax on it.
print(torch.nn.functional.softmax(output[0], dim=0))

模型描述

SNNMLP 将 LIF 神经元机制融入 MLP 模型,在不增加额外 FLOPs 的情况下实现更高的精度。我们提出一种全精度 LIF 操作用于 patch 间通信,包括水平 LIF 和不同方向的垂直 LIF。我们还提出使用 Group LIF 来提取更好的局部特征。借助 LIF 模块,我们的 SNNMLP 模型在 ImageNet 数据集上分别以仅 4.4G、8.5G 和 15.2G 的 FLOPs 实现了 81.9%、83.3% 和 83.6% 的 Top-1 精度。

下面列出了使用预训练模型在 ImageNet 数据集上的相应精度。

模型 #参数 FLOPs Top-1 精度
SNNMLP Tiny 28M 4.4G 81.88
SNNMLP Small 50M 8.5G 83.30
SNNMLP Base 88M 15.2G 85.59

参考文献

你可以在此处阅读完整论文。

@inproceedings{li2022brain,
  title={Brain-inspired multilayer perceptron with spiking neurons},
  author={Li, Wenshuo and Chen, Hanting and Guo, Jianyuan and Zhang, Ziyang and Wang, Yunhe},
  booktitle={Proceedings of the IEEE/CVF Conference on Computer Vision and Pattern Recognition},
  pages={783--793},
  year={2022}
}