training_args.py 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import abc
  2. from cvargparse import Arg
  3. from cvargparse import ArgFactory
  4. from cvargparse import BaseParser
  5. from cvfinetune.parser.utils import parser_extender
  6. from chainer_addons.training import OptimizerType
  7. @parser_extender
  8. def add_training_args(parser: BaseParser) -> None:
  9. _args = ArgFactory([
  10. Arg("--warm_up", type=int, help="warm up epochs"),
  11. OptimizerType.as_arg("optimizer", "opt",
  12. help_text="type of the optimizer"),
  13. Arg("--cosine_schedule", type=int,
  14. default=-1,
  15. help="enable cosine annealing LR schedule. This parameter sets the number of schedule stages"),
  16. Arg("--l1_loss", action="store_true",
  17. help="(only with \"--only_head\" option!) use L1 Hinge Loss instead of Softmax Cross-Entropy"),
  18. Arg("--from_scratch", action="store_true",
  19. help="Do not load any weights. Train the model from scratch"),
  20. Arg("--label_smoothing", type=float, default=0,
  21. help="Factor for label smoothing"),
  22. Arg("--only_head", action="store_true", help="fine-tune only last layer"),
  23. ])\
  24. .seed()\
  25. .batch_size()\
  26. .epochs()\
  27. .debug()\
  28. .learning_rate(lr=1e-2, lrs=10, lrt=1e-5, lrd=1e-1)\
  29. .weight_decay(default=5e-4)
  30. parser.add_args(_args, group_name="Training arguments")
  31. _args = [
  32. Arg("--augmentations",
  33. choices=[
  34. "random_crop",
  35. "random_flip",
  36. "random_rotation",
  37. "center_crop",
  38. "color_jitter"
  39. ],
  40. default=["random_crop", "random_flip", "color_jitter"],
  41. nargs="*"),
  42. Arg("--center_crop_on_val", action="store_true"),
  43. Arg("--brightness_jitter", type=int, default=0.3),
  44. Arg("--contrast_jitter", type=int, default=0.3),
  45. Arg("--saturation_jitter", type=int, default=0.3),
  46. ]
  47. parser.add_args(_args, group_name="Augmentation arguments")
  48. _args = [
  49. Arg("--only_eval", action="store_true", help="evaluate the model only. do not train!"),
  50. Arg("--init_eval", action="store_true", help="evaluate the model before training"),
  51. ]
  52. parser.add_args(_args, group_name="Evaluation arguments")
  53. _args = [
  54. Arg("--no_progress", action="store_true", help="dont show progress bar"),
  55. Arg("--no_snapshot", action="store_true", help="do not save trained model"),
  56. Arg("--output", "-o", type=str, default=".out", help="output folder"),
  57. ]
  58. parser.add_args(_args, group_name="Output arguments")
  59. class TrainingParserMixin(abc.ABC):
  60. def __init__(self, *args, **kwargs):
  61. super(TrainingParserMixin, self).__init__(*args, **kwargs)
  62. add_training_args(self)
  63. __all__ = [
  64. "TrainingParserMixin",
  65. "add_training_args"
  66. ]