You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

accuracy.py 2.0 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # Copyright 2023 The KubeEdge Authors.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. from tqdm import tqdm
  15. from sedna.common.class_factory import ClassType, ClassFactory
  16. from sedna.common.log import LOGGER
  17. from utils.args import EvaluationArguments
  18. from utils.metrics import Evaluator
  19. from dataloaders import make_data_loader
  20. __all__ = ('accuracy', )
  21. @ClassFactory.register(ClassType.GENERAL)
  22. def accuracy(y_true, y_pred, **kwargs):
  23. args = EvaluationArguments()
  24. _, _, test_loader = make_data_loader(args, test_data=y_true)
  25. evaluator = Evaluator(args.num_class)
  26. tbar = tqdm(test_loader, desc='\r')
  27. for i, (sample, _) in enumerate(tbar):
  28. if args.depth:
  29. image, depth, target = sample['image'], sample['depth'], sample['label']
  30. else:
  31. image, target = sample['image'], sample['label']
  32. if args.cuda:
  33. image, target = image.cuda(), target.cuda()
  34. if args.depth:
  35. depth = depth.cuda()
  36. target[target > evaluator.num_class - 1] = 255
  37. target = target.cpu().numpy()
  38. # Add batch sample into evaluator
  39. evaluator.add_batch(target, y_pred[i])
  40. # Test during the training
  41. CPA = evaluator.Pixel_Accuracy_Class()
  42. mIoU = evaluator.Mean_Intersection_over_Union()
  43. FWIoU = evaluator.Frequency_Weighted_Intersection_over_Union()
  44. LOGGER.info("CPA:{}, mIoU:{}, fwIoU: {}".format(CPA, mIoU, FWIoU))
  45. return mIoU