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

entrypoint.py 5.8KB

1 year ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. import sys
  2. from typing import Tuple
  3. import argparse
  4. import os
  5. from abc import ABC, abstractmethod
  6. from ..models.model import Model
  7. from ..configs.base_config import BaseConfig, PhaseType
  8. class BaseEntrypoint(ABC):
  9. """Base class for all entrypoints.
  10. """
  11. description = ''
  12. def __init__(self, phase_type: PhaseType) -> None:
  13. super().__init__()
  14. self.phase_type = phase_type
  15. self.conf, self.model = self._get_conf_model()
  16. self.parser = self._create_parser(sys.argv[0], sys.argv[1])
  17. self.conf.update(self.parser.parse_args(sys.argv[2:]))
  18. def _create_parser(self, command: str, entrypoint_name: str) -> argparse.ArgumentParser:
  19. parser = argparse.ArgumentParser(
  20. prog=f'{os.path.basename(command)} {entrypoint_name}',
  21. description=self.description or None
  22. )
  23. parser.add_argument('--device', default='cpu', type=str,
  24. help='Analysis will run over device, use cpu, cuda:#, cuda (for all gpus)')
  25. parser.add_argument('--samples-dir', default=None, type=str,
  26. help='The directory/dataGroup to be evaluated. In the case of dataGroup can be train/test/val. In the case of directory must contain 0 and 1 subdirectories. Use:FilterName to do over samples containing /FilterName/ in their path')
  27. parser.add_argument('--final-model-dir', default=None, type=str,
  28. help='The directory to load the model from, when not given will be calculated!')
  29. parser.add_argument('--save-dir', default=None, type=str,
  30. help='The directory to save the model from, when not given will be calculated!')
  31. parser.add_argument('--report-dir', default=None, type=str,
  32. help='The dir to save reports per slice per sample in.')
  33. parser.add_argument('--epoch', default=None, type=str,
  34. help='The epoch to load.')
  35. parser.add_argument('--try-name', default=None, type=str,
  36. help='The run name specifying what run is doing')
  37. parser.add_argument('--try-num', default=None, type=int,
  38. help='The try number to load')
  39. parser.add_argument('--data-separation', default=None, type=str,
  40. help='The data_separation to be used.')
  41. parser.add_argument('--batch-size', default=None, type=int,
  42. help='The batch size to be used.')
  43. parser.add_argument('--pretrained-model-file', default=None, type=str,
  44. help='Address of .pt pretrained model')
  45. parser.add_argument('--max-epochs', default=None, type=int,
  46. help='The maximum epochs of training!')
  47. parser.add_argument('--big-batch-size', default=None, type=int,
  48. help='The big batch size (iteration per optimization)!')
  49. parser.add_argument('--iters-per-epoch', default=None, type=int,
  50. help='The number of big batches per epoch!')
  51. parser.add_argument('--interpretation-method', default=None, type=str,
  52. help='The method used for interpreting the results!')
  53. parser.add_argument('--cut-threshold', default=None, type=float,
  54. help='The threshold for cutting interpretations!')
  55. parser.add_argument('--global-threshold', action='store_true',
  56. help='Whether the given cut threshold must be applied global or to the relative values!')
  57. parser.add_argument('--dynamic-threshold', action='store_true',
  58. help='Whether to use dynamic threshold in interpretation!')
  59. parser.add_argument('--class-label-for-interpretation', default=None, type=int,
  60. help='The class label we want to explain why it has been chosen. None means the decision of the model!')
  61. parser.add_argument('--interpret-predictions-vs-gt', default='1', type=(lambda x: x == '1'),
  62. help='If the class_label for_interpretation is None this will be considered. If 1, interpretations would be done for the predicted label, otherwise for the ground truth.')
  63. parser.add_argument('--mapped-labels-to-use', default=None, type=(lambda x: [int(y) for y in x.split(',')]),
  64. help='The labels to do the analysis on them only (comma separated), default is all the labels.')
  65. parser.add_argument('--skip-overlay', action='store_true', default=None,
  66. help='Passing this flag prevents the interpretation phase from storing overlay images.')
  67. parser.add_argument('--skip-raw', action='store_true', default=None,
  68. help='Passing this flag prevents the interpretation phase from storing the raw interpretation values.')
  69. parser.add_argument('--overlay-only', action='store_true',
  70. help='Passing this flag makes the interpretation phase to store just overlay images '
  71. 'and not the `.npy` files.')
  72. parser.add_argument('--save-by-file-name', action='store_true',
  73. help='Saves sample-specific files by their file names only, not the whole path.')
  74. parser.add_argument('--n-interpretation-samples', default=None, type=int,
  75. help='The max number of samples to be interpreted.')
  76. parser.add_argument('--interpretation-tag-to-evaluate', default=None, type=str,
  77. help='The tag to be used as interpretation for evaluation phase, otherwise the first one will be used.')
  78. return parser
  79. @abstractmethod
  80. def _get_conf_model(self) -> Tuple[BaseConfig, Model]:
  81. pass