config_check.py 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  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. import numpy as np
  15. def config_check(cfg, train_dataset=None, val_dataset=None):
  16. """
  17. To check config。
  18. Args:
  19. cfg (paddleseg.cvlibs.Config): An object of paddleseg.cvlibs.Config.
  20. train_dataset (paddle.io.Dataset): Used to read and process training datasets.
  21. val_dataset (paddle.io.Dataset, optional): Used to read and process validation datasets.
  22. """
  23. num_classes_check(cfg, train_dataset, val_dataset)
  24. def num_classes_check(cfg, train_dataset, val_dataset):
  25. """"
  26. Check that the num_classes in model, train_dataset and val_dataset is consistent.
  27. """
  28. num_classes_set = set()
  29. if train_dataset and hasattr(train_dataset, 'num_classes'):
  30. num_classes_set.add(train_dataset.num_classes)
  31. if val_dataset and hasattr(val_dataset, 'num_classes'):
  32. num_classes_set.add(val_dataset.num_classes)
  33. if cfg.dic.get('model', None) and cfg.dic['model'].get('num_classes',
  34. None):
  35. num_classes_set.add(cfg.dic['model'].get('num_classes'))
  36. if (not cfg.train_dataset) and (not cfg.val_dataset):
  37. raise ValueError(
  38. 'One of `train_dataset` or `val_dataset should be given, but there are none.'
  39. )
  40. if len(num_classes_set) == 0:
  41. raise ValueError(
  42. '`num_classes` is not found. Please set it in model, train_dataset or val_dataset'
  43. )
  44. elif len(num_classes_set) > 1:
  45. raise ValueError(
  46. '`num_classes` is not consistent: {}. Please set it consistently in model or train_dataset or val_dataset'
  47. .format(num_classes_set))
  48. else:
  49. num_classes = num_classes_set.pop()
  50. if train_dataset:
  51. train_dataset.num_classes = num_classes
  52. if val_dataset:
  53. val_dataset.num_classes = num_classes