eval.py 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. # 环境变量配置,用于控制是否使用GPU
  2. # 说明文档:https://paddlex.readthedocs.io/zh_CN/develop/appendix/parameters.html#gpu
  3. import os
  4. import os.path as osp
  5. os.environ['CUDA_VISIBLE_DEVICES'] = '0'
  6. import numpy as np
  7. import cv2
  8. from PIL import Image
  9. from collections import OrderedDict
  10. import paddlex as pdx
  11. import paddlex.utils.logging as logging
  12. from paddlex.seg import transforms
  13. from paddlex.cv.models.utils.seg_eval import ConfusionMatrix
  14. model_dir = 'output/unet/best_model'
  15. data_dir = 'google_change_det_dataset'
  16. file_list = 'google_change_det_dataset/val_list.txt'
  17. def update_confusion_matrix(confusion_matrix, predction, label):
  18. pred = predction["label_map"]
  19. pred = pred[np.newaxis, :, :, np.newaxis]
  20. pred = pred.astype(np.int64)
  21. label = label[np.newaxis, np.newaxis, :, :]
  22. mask = label != model.ignore_index
  23. confusion_matrix.calculate(pred=pred, label=label, ignore=mask)
  24. model = pdx.load_model(model_dir)
  25. conf_mat = ConfusionMatrix(model.num_classes, streaming=True)
  26. with open(file_list, 'r') as f:
  27. for line in f:
  28. items = line.strip().split()
  29. full_path_im1 = osp.join(data_dir, items[0])
  30. full_path_im2 = osp.join(data_dir, items[1])
  31. full_path_label = osp.join(data_dir, items[2])
  32. # 原图是tiff格式的图片,PaddleX统一使用gdal库读取
  33. # 因训练数据已经转换成bmp格式,故此处使用opencv读取三通道的tiff图片
  34. #image1 = transforms.Compose.read_img(full_path_im1)
  35. #image2 = transforms.Compose.read_img(full_path_im2)
  36. image1 = cv2.imread(full_path_im1)
  37. image2 = cv2.imread(full_path_im2)
  38. image = np.concatenate((image1, image2), axis=-1)
  39. # API说明:https://paddlex.readthedocs.io/zh_CN/develop/apis/models/semantic_segmentation.html#overlap-tile-predict
  40. overlap_tile_predict = model.overlap_tile_predict(
  41. img_file=image,
  42. tile_size=(769, 769),
  43. pad_size=[512, 512],
  44. batch_size=4)
  45. # 将三通道的label图像转换成单通道的png格式图片
  46. # 且将标注0和255转换成0和1
  47. label = cv2.imread(full_path_label)
  48. label = label[:, :, 0]
  49. label = label != 0
  50. label = label.astype(np.uint8)
  51. update_confusion_matrix(conf_mat, overlap_tile_predict, label)
  52. category_iou, miou = conf_mat.mean_iou()
  53. category_acc, oacc = conf_mat.accuracy()
  54. category_f1score = conf_mat.f1_score()
  55. logging.info(
  56. "miou={:.6f} category_iou={} oacc={:.6f} category_acc={} kappa={:.6f} category_F1-score={}".
  57. format(miou, category_iou, oacc, category_acc,
  58. conf_mat.kappa(), conf_mat.f1_score()))