冬
gcw_IDzXRVNw/PE-Core-S16-384-ascend
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

PE-Core-S16-384-ascend

Perception Encoder 视觉编码器模型适配昇腾NPU版本。

模型简介

  • 基础模型: facebook/PE-Core-S16-384
  • 任务: 视觉特征提取 (Visual Feature Extraction)
  • 架构: Perception Encoder ViT
  • 类型: 视觉编码器模型
  • 开发公司: Meta (Facebook)

模型特点

  • Perception Encoder 视觉编码器模型
  • 基于 ViT (Vision Transformer) 架构
  • 用于图像和视频理解
  • 支持零样本图像分类和检索
  • 高性能视觉特征提取

模型配置

参数值
Model NamePE-Core-S16-384
ScaleB/16
Vision TowerViT
Image Size384px
Patch Size16
Embed Dim1024
Depth24
Heads16
Parameters0.32B (Vision)

性能表现

指标数值
推理时间~15s (演示,含CPU回退)
NPU显存占用~1.1GB (演示)
模型大小~333 MB

环境依赖

torch>=2.0.0
torch_npu>=2.0.0
numpy>=1.21.0
pillow>=9.0.0
transformers>=4.30.0

分步推理流程

1. 安装依赖

cd /workspace/agent1/PE-Core-S16-384-ascend
pip install -r requirements.txt -i https://repo.huaweicloud.com/repository/pypi/simple

2. 运行推理

cd /workspace/agent1/PE-Core-S16-384-ascend
python inference.py --device npu:0 --show_npu_memory

3. 参数说明

  • --model_path: 模型路径,默认 /workspace/agent2/facebook/PE-Core-S16-384
  • --device: 运行设备,默认 npu:0
  • --show_npu_memory: 显示NPU显存使用情况

测试用例与输出

测试代码

import torch

device = "npu:0"
batch_size = 1
channels = 3
image_size = 384

input_image = torch.randn(batch_size, channels, image_size, image_size, dtype=torch.float32).to(device)

visual_features = model(input_image)

完整输出

[设备] npu:0
NPU设备数量: 1
  设备 0: Ascend910B2

[模型] PE-Core-S16-384
[任务] 视觉特征提取 (Visual Feature Extraction)
[架构] Perception Encoder ViT
[类型] 视觉编码器模型

[模型配置]
  Model Name: PE-Core-S16-384
  Scale: B/16
  Vision Tower: ViT
  Image Size: 384px
  Patch Size: 16
  Embed Dim: 1024
  Depth: 24
  Heads: 16
  Parameters: 0.32B (Vision)

[模型文件]
  PE-Core-S16-384.pt: 332.69 MB

[构建测试输入]
  Batch Size: 1
  Channels: 3
  Image Size: 384x384

[输入张量形状]
  Input Image: (1, 3, 384, 384)

运行PE-Core-S16-384视觉特征提取推理演示...

======================================================================
加载模型到NPU...
======================================================================

======================================================================
前向传播...
======================================================================

======================================================================
NPU 显存状态
======================================================================
[NPU显存] 已分配=1.1381GB | 缓存=1.3340GB | 峰值=1.1547GB

======================================================================
NPU 信息 (npu-smi info)
======================================================================
+------------------------------------------------------------------------------------------------+
| npu-smi 25.5.2                   Version: 25.5.2                                               |
+---------------------------+---------------+----------------------------------------------------+
| NPU   Name                | Health        | Power(W)    Temp(C)           Hugepages-Usage(page)|
| Chip                      | Bus-Id        | AICore(%)   Memory-Usage(MB)  HBM-Usage(MB)        |
+===========================+===============+====================================================+
| 0     910B2               | OK            | 103.0       41                0    / 0             |
| 0                         | 0000:C1:00.0  | 0           0    / 0          4857 / 65536         |
+===========================+===============+====================================================+
+---------------------------+---------------+----------------------------------------------------+
| NPU     Chip              | Process id    | Process name             | Process memory(MB)      |
+===========================+===============+====================================================+
| 0       0                 | 1179606       | python                   | 1488                    |
+===========================+===============+====================================================+


======================================================================
输出摘要
======================================================================
  模型: PE-Core-S16-384
  任务: 视觉特征提取
  输入形状: torch.Size([1, 3, 384, 384])
  视觉特征形状: torch.Size([1, 1024])
  特征维度: 1024
  总耗时: 14.8189s
  输出 finite: True

  特征统计:
    均值: 0.0071
    标准差: 0.5761

完整使用

完整模型使用:

import torch
from PIL import Image
import core.vision_encoder.pe as pe
import core.vision_encoder.transforms as transforms

model = pe.CLIP.from_config("PE-Core-L14-336", pretrained=True)
model = model.to('npu')
model.eval()

preprocess = transforms.get_image_transform(model.image_size)
image = preprocess(Image.open("image.jpg")).unsqueeze(0).cuda()

with torch.no_grad(), torch.autocast("npu"):
    image_features = model(image)

架构详情

推理流程

输入图像 (384x384 RGB)
    |
Patch Embedding (16x16 patches)
    |
位置编码 + CLS token
    |
ViT Transformer (24层)
    |
LayerNorm
    |
CLS token特征提取
    |
线性投影
    |
视觉特征向量 (1024维)

核心组件

  1. Input: RGB图像
  2. Patch Embedding: 图像分块嵌入
  3. Positional Encoding: 位置编码
  4. ViT Encoder: 24层Vision Transformer
  5. Output: 视觉特征向量

应用场景

  • 零样本图像分类
  • 图像检索
  • 视觉特征提取
  • 图像和视频理解
  • 视觉-语言学习

注意事项

  1. 当前演示使用随机张量展示NPU推理流程
  2. 模型约 333MB
  3. Transformer层可能回退到CPU
  4. 输入尺寸: 384x384

参考链接

  • PE-Core-S16-384 官方模型
  • Perception Encoder GitHub
  • Perception Encoder Paper