cv_resnet50_image-classification_cc 是基于 ResNet50 的图像分类模型,专用于 NSFW 内容检测与分类任务。该模型将图像映射到 10 维概率向量空间,可识别 5+1 类内容(部分类别名称未知)。模型基于 ResNet50 架构,参数量约 24.6M。
cv_resnet50_image-classification_cc-ascend/
├── inference.py # 推理测试脚本
├── log.txt # 测试日志
├── README.md # 本文档
├── test_image.jpg # 测试图片
├── inference_result.json # 推理结果
└── precision_result.json # 精度测试结果docker exec -it test-modelagent bashsource /usr/local/Ascend/ascend-toolkit/set_env.sh模型文件位于 /data/ysws/agentsp/5-19-1/cv_resnet50_image-classification_cc/ 目录下:
iic/cv_resnet50_image-classification_cc/pytorch_model.pt - 模型权重(约 98.5MB)pip install opencv-python numpy torch torch_npuRun the inference script for image classification:
cd /data/ysws/agentsp/5-19-1/cv_resnet50_image-classification_cc-ascend/
python3 inference.py运行精度对比测试,验证 NPU 计算结果与 CPU 一致性:
cd /data/ysws/agentsp/5-19-1/cv_resnet50_image-classification_cc-ascend/
python3 inference.py precision_test| 指标 | 实测值 | 阈值 | 状态 |
|---|---|---|---|
| 相对误差 | 0.02% | < 1.00% | PASS |
| 类别预测一致 | True | True | PASS |
| 操作 | 耗时 |
|---|---|
| CPU 推理时间 | 0.4115s |
| NPU 推理时间 | 4.31s |
| 测试图片 | content_check.jpg |
| 输入图片 | 预测类别 | 置信度 |
|---|---|---|
| content_check.jpg | class_8 | 99.96% |
各类别概率分布:
| 类别 | 名称 | 概率 |
|---|---|---|
| 0 | pornography images | 0.00% |
| 1 | hentai images and pornographic drawings | 0.00% |
| 2 | sexually explicit images | 0.00% |
| 3 | safe for work neutral images | 0.00% |
| 4 | safe for work drawings and anime | 0.00% |
| 5 | class_5 | ~0.00% |
| 6 | class_6 | ~0.00% |
| 7 | class_7 | 0.04% |
| 8 | class_8 | 99.96% |
| 9 | class_9 | ~0.00% |
结果: CPU 和 NPU 预测均为 class_8,概率均为 99.96%,完全一致
============================================================
cv_resnet50_image-classification_cc - Ascend NPU Inference Test
Output: /data/ysws/agentsp/5-19-1/cv_resnet50_image-classification_cc-ascend
============================================================
Mode: PRECISION TEST
NPU available: True
Device: npu:0
============================================================
Loading Model and Test Image
============================================================
Model loaded from: /data/ysws/agentsp/5-19-1/cv_resnet50_image-classification_cc/iic/cv_resnet50_image-classification_cc/pytorch_model.pt
Using cached test image: /data/ysws/agentsp/5-19-1/cv_resnet50_image-classification_cc-ascend/test_image.jpg
Test image shape: (1914, 1305, 3)
Input tensor shape: torch.Size([1, 3, 224, 224])
============================================================
Running CPU Inference
============================================================
CPU inference time: 0.4115s
CPU class index: 8 (prob: 0.9996)
============================================================
Running NPU Inference
============================================================
NPU inference time: 4.3059s
NPU class index: 8 (prob: 0.9996)
Speedup: 0.10x
============================================================
Precision Test Results
============================================================
Max absolute error: 7.629395e-02
Max relative error: 2.459372e-04 (0.02%)
Class prediction match: True (class_8 vs class_8)
Precision test: PASS
Test image saved: /data/ysws/agentsp/5-19-1/cv_resnet50_image-classification_cc-ascend/test_image.jpg
============================================================
Test Complete!
============================================================| 组件 | 输出通道 | 说明 |
|---|---|---|
| conv1 | 64 | 7×7,步长=2,填充=3 |
| layer1 | 256 | 3 个 Bottleneck,步长=1 |
| layer2 | 512 | 4 个 Bottleneck,步长=2 |
| layer3 | 1024 | 6 个 Bottleneck,步长=2 |
| layer4 | 2048 | 3 个 Bottleneck,步长=2 |
| fc0 | 512 | 线性层(2048→512) |
| fc3 | 10 | 线性层(512→10) |
import cv2
import numpy as np
import torch
import torch.nn as nn
import torch.nn.functional as F
MODEL_WEIGHT = "/data/ysws/agentsp/5-19-1/cv_resnet50_image-classification_cc/iic/cv_resnet50_image-classification_cc/pytorch_model.pt"
MEAN = np.array([0.485, 0.456, 0.406], dtype=np.float32)
STD = np.array([0.229, 0.224, 0.225], dtype=np.float32)
def preprocess_image(img, target_size=224):
img = cv2.resize(img, (target_size, target_size))
img = img.astype(np.float32) / 255.0
img = (img - MEAN) / STD
img = img.transpose(2, 0, 1)
img = np.expand_dims(img, axis=0)
return img
img = cv2.imread("test.jpg")
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
img_tensor = torch.from_numpy(preprocess_image(img_rgb)).float()
# 加载模型(需自行实现 ResNet50CC 完整结构)
model = ... # 参见 inference.py
model = model.to("npu:0")
model.eval()
with torch.no_grad():
output = model(img_tensor.to("npu:0"))
probs = F.softmax(output, dim=1)
prob, cls = probs.max(dim=1)
print(f"Class: {cls.item()}, Probability: {prob.item():.4f}")A: 检查 NPU 驱动是否正确安装,确保 CANN 环境变量已 source。0.01-0.5% 的数值误差是正常的,因为 NPU 和 CPU 使用不同的计算精度。
A: 对于小模型(24.6M 参数),NPU 的启动开销(模型编译、数据传输)可能大于实际计算时间,导致测得时间比 CPU 长。这是正常现象。在实际大模型推理中 NPU 会展现显著加速优势。
A: 替换 test_image.jpg 文件,或修改 TEST_IMAGE_URL 变量使用其他图片 URL。
本项目遵循原始模型许可证