nyxrobotics commited on
Commit
9206874
·
1 Parent(s): 9d283bc

train data

Browse files
Files changed (1) hide show
  1. modules/maskrcnn_train.sh +90 -0
modules/maskrcnn_train.sh ADDED
@@ -0,0 +1,90 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Get script dir
4
+ SCRIPT_DIR=$(cd $(dirname $0); pwd)
5
+
6
+ # Set the paths for the dataset and configuration files
7
+ TRAIN_ANNOTATION=$SCRIPT_DIR/../export_coco/annotations/train.json
8
+ TRAIN_IMAGE_DIR=$SCRIPT_DIR/../export_coco/train
9
+ VAL_ANNOTATION=$SCRIPT_DIR/../export_coco/annotations/val.json
10
+ VAL_IMAGE_DIR=$SCRIPT_DIR/../export_coco/val
11
+ COCO_CONFIG_FILE=$SCRIPT_DIR/../export_coco/config/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml
12
+ BASE_CONFIG_FILE=$SCRIPT_DIR/../export_coco/config/Base-RCNN-FPN.yaml # Base config file path
13
+ CONFIG_URL="https://raw.githubusercontent.com/facebookresearch/detectron2/main/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml"
14
+ BASE_CONFIG_URL="https://raw.githubusercontent.com/facebookresearch/detectron2/main/configs/Base-RCNN-FPN.yaml" # Base config URL
15
+ TRAIN_NET_DIR=$SCRIPT_DIR/../export_coco/detectron2
16
+ TRAIN_NET_FILE=$TRAIN_NET_DIR/train_net.py
17
+ TRAIN_NET_URL="https://raw.githubusercontent.com/facebookresearch/detectron2/main/tools/train_net.py"
18
+ OUTPUT_DIR=$SCRIPT_DIR/../export_coco/output
19
+
20
+ # Create necessary directories
21
+ mkdir -p $(dirname $COCO_CONFIG_FILE)
22
+ mkdir -p $TRAIN_NET_DIR
23
+ mkdir -p $OUTPUT_DIR
24
+
25
+ # Download the COCO config file if it doesn't exist
26
+ if [ ! -f "$COCO_CONFIG_FILE" ]; then
27
+ echo "Downloading Mask R-CNN configuration file..."
28
+ wget $CONFIG_URL -O $COCO_CONFIG_FILE
29
+ fi
30
+
31
+ # Download the base config file if it doesn't exist
32
+ if [ ! -f "$BASE_CONFIG_FILE" ]; then
33
+ echo "Downloading Base-RCNN-FPN.yaml configuration file..."
34
+ wget $BASE_CONFIG_URL -O $BASE_CONFIG_FILE
35
+ fi
36
+
37
+ # Download train_net.py if it doesn't exist
38
+ 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."