myasin nasheed commited on
Commit
802b7c7
·
1 Parent(s): 8ac4654

Update train_net.py

Browse files

Added support for custom augmentations

Co-Authored-By: Nasheed Yasin <nasheed.ny@gmail.com>

Files changed (1) hide show
  1. tools/train_net.py +34 -0
tools/train_net.py CHANGED
@@ -7,8 +7,10 @@ import os
7
  import json
8
  from collections import OrderedDict
9
  import detectron2.utils.comm as comm
 
10
  from detectron2.checkpoint import DetectionCheckpointer
11
  from detectron2.config import get_cfg
 
12
 
13
  from detectron2.data.datasets import register_coco_instances
14
 
@@ -20,6 +22,33 @@ from detectron2.evaluation import (
20
  from detectron2.modeling import GeneralizedRCNNWithTTA
21
  import pandas as pd
22
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
23
  class Trainer(DefaultTrainer):
24
  """
25
  We use the "DefaultTrainer" which contains pre-defined default logic for
@@ -29,6 +58,11 @@ class Trainer(DefaultTrainer):
29
  "tools/plain_train_net.py" as an example.
30
  """
31
 
 
 
 
 
 
32
  @classmethod
33
  def build_evaluator(cls, cfg, dataset_name, output_folder=None):
34
  """
 
7
  import json
8
  from collections import OrderedDict
9
  import detectron2.utils.comm as comm
10
+ import detectron2.data.transforms as T
11
  from detectron2.checkpoint import DetectionCheckpointer
12
  from detectron2.config import get_cfg
13
+ from detectron2.data import DatasetMapper, build_detection_train_loader
14
 
15
  from detectron2.data.datasets import register_coco_instances
16
 
 
22
  from detectron2.modeling import GeneralizedRCNNWithTTA
23
  import pandas as pd
24
 
25
+
26
+ def get_augs(cfg):
27
+ """Add all the desired augmentations here. A list of availble augmentations
28
+ can be found here:
29
+ https://detectron2.readthedocs.io/en/latest/modules/data_transforms.html#detectron2.data.transforms.RandomRotation
30
+ """
31
+ augs = [
32
+ T.ResizeShortestEdge(
33
+ cfg.INPUT.MIN_SIZE_TRAIN, cfg.INPUT.MAX_SIZE_TRAIN, cfg.INPUT.MIN_SIZE_TRAIN_SAMPLING
34
+ )
35
+ ]
36
+ if cfg.INPUT.CROP.ENABLED:
37
+ augs.append(
38
+ T.RandomCrop_CategoryAreaConstraint(
39
+ cfg.INPUT.CROP.TYPE,
40
+ cfg.INPUT.CROP.SIZE,
41
+ cfg.INPUT.CROP.SINGLE_CATEGORY_MAX_AREA,
42
+ cfg.MODEL.SEM_SEG_HEAD.IGNORE_VALUE,
43
+ )
44
+ )
45
+ horizontal_flip: bool = (cfg.INPUT.RANDOM_FLIP == 'horizontal')
46
+ augs.append(T.RandomFlip(horizontal=horizontal_flip,
47
+ vertical=not horizontal_flip))
48
+ # Rotate the image between -90 to 0 degrees clockwise around the centre
49
+ augs.append(T.RandomRotation(angle=[90.0, 0.0]))
50
+ return augs
51
+
52
  class Trainer(DefaultTrainer):
53
  """
54
  We use the "DefaultTrainer" which contains pre-defined default logic for
 
58
  "tools/plain_train_net.py" as an example.
59
  """
60
 
61
+ @classmethod
62
+ def build_train_loader(cls, cfg):
63
+ mapper = DatasetMapper(cfg, is_train=True, augmentations=get_augs(cfg))
64
+ return build_detection_train_loader(cfg, mapper=mapper)
65
+
66
  @classmethod
67
  def build_evaluator(cls, cfg, dataset_name, output_folder=None):
68
  """