PaddleSeg转换为ONNX后精度丢失改进方法

先前博主将PaddleSeg的语义分割模型转换为ONNX后,其结果发生了明显下降,经过分析,发现可能是其预处理过程中的meanstd值不太合理:

python 复制代码
def preprocess(image, size):
    h, w = image.shape[:2]
    resized = cv2.resize(image, size, interpolation=cv2.INTER_LINEAR)
    # 确保用 float32 计算
    resized = resized.astype(np.float32)
    mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
    std = np.array([0.229, 0.224, 0.225], dtype=np.float32)
    normalized = (resized / 255.0 - mean) / std
    input_tensor = normalized.transpose(2, 0, 1)[np.newaxis, ...].astype(np.float32)  
    return input_tensor, (h, w)

其中:

python 复制代码
mean = np.array([0.485, 0.456, 0.406], dtype=np.float32)
std = np.array([0.229, 0.224, 0.225], dtype=np.float32) 

是取自ImageNet数据集的值,相较于遥感数据集,其存在差异:

这里博主将其改为

python 复制代码
mean = np.array([0.5, 0.5, 0.5], dtype=np.float32)
std = np.array([0.5, 0.5, 0.5], dtype=np.float32)

其结果如下图所示,其结果好了很多:

但这个方法也并不完美,最好是我们在训练时便指定好meanstd,如下:

python 复制代码
- type: Normalize
      mean: [0.5, 0.5, 0.5]   # ← 显式写明
      std: [0.5, 0.5, 0.5]    # ← 显式写明

完整配置文件如下:

python 复制代码
batch_size: 8
iters: 10000

train_dataset:
  type: Dataset
  dataset_root: D:/BaiduNetdiskDownload/yaogan/output
  train_path: D:/BaiduNetdiskDownload/yaogan/output/train.txt
  num_classes: 4
  mode: train
  transforms:
    - type: ResizeStepScaling
      min_scale_factor: 0.5
      max_scale_factor: 2.0
      scale_step_size: 0.25
    - type: RandomPaddingCrop
      crop_size: [512, 512]
    - type: RandomHorizontalFlip
    - type: RandomDistort
      brightness_range: 0.5
      contrast_range: 0.5
      saturation_range: 0.5
    - type: Normalize
      mean: [ 0.5, 0.5, 0.5 ]
      std: [ 0.5, 0.5, 0.5 ]

val_dataset:
  type: Dataset
  dataset_root: D:/BaiduNetdiskDownload/yaogan/output
  val_path: D:/BaiduNetdiskDownload/yaogan/output/val.txt
  num_classes: 4
  mode: val
  transforms:
    - type: Normalize
      mean: [ 0.5, 0.5, 0.5 ]
      std: [ 0.5, 0.5, 0.5 ]

optimizer:
  type: SGD
  momentum: 0.9
  weight_decay: 4.0e-5

lr_scheduler:
  type: PolynomialDecay
  learning_rate: 0.01
  end_lr: 0
  power: 0.9

loss:
  types:
    - type: CrossEntropyLoss
  coef: [1, 1, 1]


model:
  type: PPLiteSeg
  backbone:
    type: STDC2
    pretrained: https://bj.bcebos.com/paddleseg/dygraph/PP_STDCNet2.tar.gz

这样,我们重新训练后,其结果就好很多了。

最终效果如下:

相关推荐
To_OC6 小时前
大模型蒸馏是啥?说白了就是大厨带徒弟的学问
人工智能·llm·agent
新手来了@click6 小时前
JAVA+AI 简化开发操作|文章被 AI Agent 技术社区收录分享
人工智能
GuWenyue7 小时前
Cursor黑盒拆解!1套LangChain.js手写Mini编程Agent,自动生成React项目,效率提升60%
前端·数据库·人工智能
GuWenyue7 小时前
传统Agent工具两大痛点!300行代码落地MCP跨语言工具,彻底解耦LLM与工具
前端·人工智能·算法
老云讲算力市场7 小时前
WAIC首日观察:国产算力与机器人加速落地,奇点算力迎来产业新机遇
人工智能·科技
糖果店的幽灵7 小时前
【DeepAgents 从入门到精通】Context Management 上下文管理
java·人工智能·后端·spring·中间件·langgraph·deepagents
小林ixn7 小时前
大模型随机说话的秘密:Temperature 和 Top K 深度解析,LangChain 实战调优
人工智能·langchain
ALINX技术博客7 小时前
ALINX 亮相 2026 WAIC 世界人工智能大会,展示 AI 视觉 FPGA+GPU 异构计算与电子后视镜解决方案
人工智能·ai·fpga·世界人工智能大会·电子后视镜
程序员老猫8 小时前
当 AI 能写 80% 的代码时,后端工程师的核心价值还剩什么?
人工智能
想会飞的蒲公英8 小时前
计算机怎样读取中文文本:编码、清洗与标准化
人工智能·python·自然语言处理