nyxrobotics commited on
Commit
59d116a
·
1 Parent(s): 9206874
Files changed (1) hide show
  1. modules/maskrcnn_train.sh +38 -35
modules/maskrcnn_train.sh CHANGED
@@ -39,52 +39,55 @@ if [ ! -f "$TRAIN_NET_FILE" ]; then
39
  echo "Downloading train_net.py file..."
40
  wget $TRAIN_NET_URL -O $TRAIN_NET_FILE
41
  fi
42
-
43
- # Function to extract the number of classes from COCO annotation
44
- NUM_CLASSES=$(python3 - <<END
45
- import json
46
-
47
- # Load the COCO annotation file
48
- with open("$TRAIN_ANNOTATION", 'r') as f:
49
- coco_data = json.load(f)
50
-
51
- # Extract the number of unique categories
52
- num_classes = len(coco_data['categories'])
53
- print(num_classes)
54
- END
55
- )
56
-
57
- echo "Detected $NUM_CLASSES classes from the dataset."
58
-
59
- # Register the datasets with Detectron2 (assuming Detectron2 environment is set up)
60
  python3 - <<END
61
  import os
 
62
  from detectron2.data.datasets import register_coco_instances
 
63
 
64
- # Register COCO datasets
65
  train_annotation = "$TRAIN_ANNOTATION"
66
  train_image_dir = "$TRAIN_IMAGE_DIR"
67
  val_annotation = "$VAL_ANNOTATION"
68
  val_image_dir = "$VAL_IMAGE_DIR"
69
 
70
- # Register with the name coco_roboone_train and coco_roboone_val
 
 
 
 
 
 
 
 
71
  register_coco_instances("coco_roboone_train", {}, train_annotation, train_image_dir)
72
  register_coco_instances("coco_roboone_val", {}, val_annotation, val_image_dir)
73
 
 
74
  print("Datasets registered successfully.")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
75
  END
76
-
77
- # Run the training with Detectron2
78
- python3 $TRAIN_NET_FILE \
79
- --config-file $COCO_CONFIG_FILE \
80
- --num-gpus 1 \
81
- OUTPUT_DIR $OUTPUT_DIR \
82
- DATASETS.TRAIN "('coco_roboone_train',)" \
83
- DATASETS.TEST "('coco_roboone_val',)" \
84
- SOLVER.IMS_PER_BATCH 2 \
85
- SOLVER.BASE_LR 0.00025 \
86
- SOLVER.MAX_ITER 1000 \
87
- MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE 512 \
88
- MODEL.ROI_HEADS.NUM_CLASSES $NUM_CLASSES # Automatically set based on dataset
89
-
90
- echo "Mask R-CNN training completed."
 
39
  echo "Downloading train_net.py file..."
40
  wget $TRAIN_NET_URL -O $TRAIN_NET_FILE
41
  fi
42
+ # Function to extract the number of classes from COCO annotation and run training
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  python3 - <<END
44
  import os
45
+ import json
46
  from detectron2.data.datasets import register_coco_instances
47
+ from detectron2.data import DatasetCatalog
48
 
49
+ # Paths
50
  train_annotation = "$TRAIN_ANNOTATION"
51
  train_image_dir = "$TRAIN_IMAGE_DIR"
52
  val_annotation = "$VAL_ANNOTATION"
53
  val_image_dir = "$VAL_IMAGE_DIR"
54
 
55
+ # Load the COCO annotation file to detect number of classes
56
+ with open(train_annotation, 'r') as f:
57
+ coco_data = json.load(f)
58
+
59
+ # Extract number of unique categories
60
+ num_classes = len(coco_data['categories'])
61
+ print(f"Detected {num_classes} classes from the dataset.")
62
+
63
+ # Register the datasets
64
  register_coco_instances("coco_roboone_train", {}, train_annotation, train_image_dir)
65
  register_coco_instances("coco_roboone_val", {}, val_annotation, val_image_dir)
66
 
67
+ # Confirm the datasets are registered
68
  print("Datasets registered successfully.")
69
+ print("Available datasets:", DatasetCatalog.list())
70
+
71
+ # Import necessary modules for training
72
+ from detectron2.engine import DefaultTrainer
73
+ from detectron2.config import get_cfg
74
+
75
+ # Set up configuration
76
+ cfg = get_cfg()
77
+ cfg.merge_from_file("$COCO_CONFIG_FILE")
78
+ cfg.DATASETS.TRAIN = ("coco_roboone_train",)
79
+ cfg.DATASETS.TEST = ("coco_roboone_val",)
80
+ cfg.SOLVER.IMS_PER_BATCH = 2
81
+ cfg.SOLVER.BASE_LR = 0.00025
82
+ cfg.SOLVER.MAX_ITER = 1000
83
+ cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = 512
84
+ cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes # Automatically set based on dataset
85
+ cfg.OUTPUT_DIR = "$OUTPUT_DIR"
86
+
87
+ # Train the model
88
+ trainer = DefaultTrainer(cfg)
89
+ trainer.resume_or_load(resume=False)
90
+ trainer.train()
91
+
92
+ print("Mask R-CNN training completed.")
93
  END