g
gcw_AVRCax4T/sam-vit-huge
Model Introduction
Files & versions
Pull Requests
Discussion
Analysis

SAM-ViT-Huge (Ascend NPU Adaptation)

SAM (Segment Anything Model) ViT-Huge adapted for Huawei Ascend NPU inference. This model card documents the NPU adaptation, accuracy verification, and performance benchmarks on Ascend 910 hardware.

Model Description

  • Model: facebook/sam-vit-huge / KyanChen/sam-vit-huge
  • Architecture: Vision Transformer (ViT-Huge) + Mask Decoder
  • Parameters: 641,090,608
  • Input: RGB image + point/box prompts
  • Output: Segmentation masks
  • Framework: PyTorch + Transformers + torch_npu

Environment

ComponentVersion
CANN8.5.1
PyTorch2.9.0
torch_npu25.5.2
transformers4.57.6
NPUAscend 910 (2 chips)
Precisionfloat16 (NPU)

Quick Start

1. Install Dependencies

pip install torch torch_npu transformers modelscope pillow requests numpy

2. Download Model

# Via ModelScope
pip install modelscope
python -c "from modelscope import snapshot_download; snapshot_download('KyanChen/sam-vit-huge')"

Model will be cached at: ~/.cache/modelscope/hub/models/KyanChen/sam-vit-huge/

3. Run Inference

import torch
import torch_npu
from transformers import SamModel, SamProcessor
from PIL import Image

# Load model in float16 for NPU
model_path = "KyanChen/sam-vit-huge"  # or local path
model = SamModel.from_pretrained(model_path, torch_dtype=torch.float16).to("npu:0")
model.eval()
processor = SamProcessor.from_pretrained(model_path)

# Load image
image = Image.open("your_image.jpg").convert("RGB")

# Point prompt inference
inputs = processor(image, input_points=[[[320, 240]]], input_labels=[[1]],
                    return_tensors="pt")
inputs = {k: (v.to("npu:0").to(torch.float16) if v.dtype == torch.float32 else v.to("npu:0"))
          for k, v in inputs.items()}

with torch.no_grad():
    outputs = model(**inputs)

masks = processor.image_processor.post_process_masks(
    outputs.pred_masks.cpu(),
    inputs["original_sizes"].cpu(),
    inputs["reshaped_input_sizes"].cpu()
)

Or use the provided inference script:

python inference.py

Accuracy Verification

NPU (float16) vs CPU (float32) accuracy comparison on COCO val2017 test image.

MetricValueThresholdStatus
Logit Relative Error0.19%<1%PASS
Cosine Similarity1.000005>0.999PASS
Mask IoU0.9859>0.98PASS
Pixel Accuracy99.85%>99%PASS
Normalized Mean Diff4.0e-04<0.01PASS

Result: NPU float16 inference matches CPU float32 reference within tolerance. All accuracy metrics pass.

Performance Benchmark

Single point-prompt inference on 640x480 image, Ascend 910 NPU, float16.

MetricValue
Mean Latency170.82 ms
Median Latency170.68 ms
P95 Latency172.11 ms
Std Deviation0.93 ms
Min Latency169.65 ms
Max Latency172.54 ms

Performance is stable with <1% variance across 20 iterations after warmup.

Project Structure

sam-npu-adapt/
├── inference.py              # NPU inference & evaluation script
├── README.md                 # This file
└── output/
    ├── benchmark_npu.json    # Performance benchmark results
    ├── accuracy_results.json # Accuracy comparison results
    └── accuracy_logit_results.json  # Detailed logit-level comparison

Known Issues & Limitations

  1. Input dtype: When loading the model in float16, input tensors must be explicitly cast to float16 for NPU compatibility.
  2. Large model: ViT-Huge requires ~2.4GB VRAM per NPU chip; ensure sufficient HBM is available (65GB per chip on Ascend 910).
  3. Batch inference: Current implementation supports batch_size=1; multi-batch requires further optimization.

License

Apache License 2.0 (same as original SAM model)

Citation

@article{kirillov2023segany,
  title={Segment Anything},
  author={Kirillov, Alexander and Mintun, Eric and Ravi, Nikhila and Mao, Hanzi and Rolland, Chloe and Gustafson, Laura and Xiao, Tete and Whitehead, Spencer and Berg, Alexander C. and Lo, Wan-Yen and Dollar, Piotr and Girshick, Ross},
  journal={arXiv:2304.02643},
  year={2023}
}