eval.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. # 环境变量配置,用于控制是否使用GPU
  2. # 说明文档:https://paddlex.readthedocs.io/zh_CN/develop/appendix/parameters.html#gpu
  3. import os
  4. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  5. import numpy as np
  6. import cv2
  7. from PIL import Image
  8. from collections import OrderedDict
  9. import paddlex as pdx
  10. import paddlex.utils.logging as logging
  11. from paddlex.cv.models.utils.seg_eval import ConfusionMatrix
  12. def update_confusion_matrix(confusion_matrix, predction, label):
  13. pred = predction["label_map"]
  14. pred = pred[np.newaxis, :, :, np.newaxis]
  15. pred = pred.astype(np.int64)
  16. label = label[np.newaxis, np.newaxis, :, :]
  17. mask = label != model.ignore_index
  18. confusion_matrix.calculate(pred=pred, label=label, ignore=mask)
  19. model_dir = 'output/deeplabv3p_mobilenetv3_large_ssld/best_model'
  20. img_file = "dataset/JPEGImages/5.png"
  21. label_file = "dataset/Annotations/5_class.png"
  22. model = pdx.load_model(model_dir)
  23. conf_mat = ConfusionMatrix(model.num_classes, streaming=True)
  24. # API说明:https://paddlex.readthedocs.io/zh_CN/develop/apis/models/semantic_segmentation.html#overlap-tile-predict
  25. overlap_tile_predict = model.overlap_tile_predict(
  26. img_file=img_file, tile_size=(769, 769), pad_size=[64, 64], batch_size=32)
  27. label = np.asarray(Image.open(label_file))
  28. update_confusion_matrix(conf_mat, overlap_tile_predict, label)
  29. category_iou, miou = conf_mat.mean_iou()
  30. category_acc, macc = conf_mat.accuracy()
  31. logging.info(
  32. "miou={:.6f} category_iou={} macc={:.6f} category_acc={} kappa={:.6f}".
  33. format(miou, category_iou, macc, category_acc, conf_mat.kappa()))