Commit
·
b2cfa3d
1
Parent(s):
e9a4aaa
Parameter tuning
Browse files- modules/maskrcnn_train.sh +55 -43
modules/maskrcnn_train.sh
CHANGED
@@ -1,7 +1,36 @@
|
|
1 |
#!/bin/bash
|
2 |
|
3 |
-
#
|
4 |
-
USE_GPU_TUNING=true
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
5 |
|
6 |
# Get script dir
|
7 |
SCRIPT_DIR=$(cd $(dirname $0); pwd)
|
@@ -32,7 +61,7 @@ if [ ! -f "$COCO_CONFIG_FILE" ]; then
|
|
32 |
fi
|
33 |
|
34 |
# Download the base config file if it doesn't exist
|
35 |
-
if [ ! -f "$BASE_CONFIG_FILE" ];then
|
36 |
echo "Downloading Base-RCNN-FPN.yaml configuration file..."
|
37 |
wget $BASE_CONFIG_URL -O $BASE_CONFIG_FILE
|
38 |
fi
|
@@ -43,44 +72,28 @@ if [ ! -f "$TRAIN_NET_FILE" ]; then
|
|
43 |
wget $TRAIN_NET_URL -O $TRAIN_NET_FILE
|
44 |
fi
|
45 |
|
46 |
-
#
|
47 |
-
IMS_PER_BATCH=2
|
48 |
-
BATCH_SIZE_PER_IMAGE=512
|
49 |
-
BASE_LR=0.00025
|
50 |
-
|
51 |
-
# If USE_GPU_TUNING is true, adjust parameters based on GPU memory
|
52 |
-
if [ "$USE_GPU_TUNING" = true ]; then
|
53 |
-
# Get the GPU memory in MB
|
54 |
-
GPU_MEMORY=$(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | head -n 1)
|
55 |
-
|
56 |
-
# Set the batch size and learning rate based on GPU memory
|
57 |
-
if [ "$GPU_MEMORY" -ge 24576 ]; then
|
58 |
-
IMS_PER_BATCH=8 # For 24GB+ GPUs
|
59 |
-
BATCH_SIZE_PER_IMAGE=512
|
60 |
-
elif [ "$GPU_MEMORY" -ge 12288 ]; then
|
61 |
-
IMS_PER_BATCH=4 # For 12GB+ GPUs
|
62 |
-
BATCH_SIZE_PER_IMAGE=256
|
63 |
-
else
|
64 |
-
IMS_PER_BATCH=2 # For smaller GPUs
|
65 |
-
BATCH_SIZE_PER_IMAGE=128
|
66 |
-
fi
|
67 |
-
|
68 |
-
# Adjust learning rate based on batch size
|
69 |
-
BASE_LR=$(echo "0.00025 * $IMS_PER_BATCH / 2" | bc -l)
|
70 |
-
fi
|
71 |
-
|
72 |
-
# Function to extract the number of classes from COCO annotation and run training
|
73 |
python3 - <<END
|
74 |
import os
|
75 |
import json
|
76 |
from detectron2.data.datasets import register_coco_instances
|
77 |
from detectron2.data import DatasetCatalog
|
|
|
|
|
78 |
|
79 |
-
# Paths
|
80 |
train_annotation = "$TRAIN_ANNOTATION"
|
81 |
train_image_dir = "$TRAIN_IMAGE_DIR"
|
82 |
val_annotation = "$VAL_ANNOTATION"
|
83 |
val_image_dir = "$VAL_IMAGE_DIR"
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
84 |
|
85 |
# Load the COCO annotation file to detect number of classes
|
86 |
with open(train_annotation, 'r') as f:
|
@@ -98,25 +111,24 @@ register_coco_instances("coco_roboone_val", {}, val_annotation, val_image_dir)
|
|
98 |
print("Datasets registered successfully.")
|
99 |
print("Available datasets:", DatasetCatalog.list())
|
100 |
|
101 |
-
# Import necessary modules for training
|
102 |
-
from detectron2.engine import DefaultTrainer
|
103 |
-
from detectron2.config import get_cfg
|
104 |
-
|
105 |
# Set up configuration
|
106 |
cfg = get_cfg()
|
107 |
-
cfg.merge_from_file(
|
108 |
cfg.DATASETS.TRAIN = ("coco_roboone_train",)
|
109 |
cfg.DATASETS.TEST = ("coco_roboone_val",)
|
110 |
-
cfg.
|
111 |
-
cfg.
|
112 |
-
|
113 |
-
|
114 |
-
cfg.
|
115 |
-
cfg.
|
|
|
|
|
|
|
116 |
|
117 |
# Train the model
|
118 |
trainer = DefaultTrainer(cfg)
|
119 |
-
trainer.resume_or_load(resume=
|
120 |
trainer.train()
|
121 |
|
122 |
print("Mask R-CNN training completed.")
|
|
|
1 |
#!/bin/bash
|
2 |
|
3 |
+
# Parameters
|
4 |
+
USE_GPU_TUNING=true # Set this to true to enable GPU tuning
|
5 |
+
RESUME_TRAINING=false # Set this to true to resume training from last checkpoint
|
6 |
+
MAX_ITER=100000 # Default maximum iterations
|
7 |
+
CHECKPOINT_PERIOD=$(($MAX_ITER / 10)) # Set to 10% of MAX_ITER
|
8 |
+
|
9 |
+
# Default values
|
10 |
+
IMS_PER_BATCH=2
|
11 |
+
BATCH_SIZE_PER_IMAGE=512
|
12 |
+
BASE_LR=0.00025
|
13 |
+
|
14 |
+
# If USE_GPU_TUNING is true, adjust parameters based on GPU memory
|
15 |
+
if [ "$USE_GPU_TUNING" = true ]; then
|
16 |
+
# Get the GPU memory in MB
|
17 |
+
GPU_MEMORY=$(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | head -n 1)
|
18 |
+
|
19 |
+
# Set the batch size and learning rate based on GPU memory
|
20 |
+
if [ "$GPU_MEMORY" -ge 24576 ]; then
|
21 |
+
IMS_PER_BATCH=8 # For 24GB+ GPUs
|
22 |
+
BATCH_SIZE_PER_IMAGE=512
|
23 |
+
elif [ "$GPU_MEMORY" -ge 12288 ]; then
|
24 |
+
IMS_PER_BATCH=4 # For 12GB+ GPUs
|
25 |
+
BATCH_SIZE_PER_IMAGE=256
|
26 |
+
else
|
27 |
+
IMS_PER_BATCH=2 # For smaller GPUs
|
28 |
+
BATCH_SIZE_PER_IMAGE=128
|
29 |
+
fi
|
30 |
+
|
31 |
+
# Adjust learning rate based on batch size
|
32 |
+
BASE_LR=$(echo "0.00025 * $IMS_PER_BATCH / 2" | bc -l)
|
33 |
+
fi
|
34 |
|
35 |
# Get script dir
|
36 |
SCRIPT_DIR=$(cd $(dirname $0); pwd)
|
|
|
61 |
fi
|
62 |
|
63 |
# Download the base config file if it doesn't exist
|
64 |
+
if [ ! -f "$BASE_CONFIG_FILE" ]; then
|
65 |
echo "Downloading Base-RCNN-FPN.yaml configuration file..."
|
66 |
wget $BASE_CONFIG_URL -O $BASE_CONFIG_FILE
|
67 |
fi
|
|
|
72 |
wget $TRAIN_NET_URL -O $TRAIN_NET_FILE
|
73 |
fi
|
74 |
|
75 |
+
# Python script to configure and run the training
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
76 |
python3 - <<END
|
77 |
import os
|
78 |
import json
|
79 |
from detectron2.data.datasets import register_coco_instances
|
80 |
from detectron2.data import DatasetCatalog
|
81 |
+
from detectron2.engine import DefaultTrainer
|
82 |
+
from detectron2.config import get_cfg
|
83 |
|
84 |
+
# Paths from Bash script
|
85 |
train_annotation = "$TRAIN_ANNOTATION"
|
86 |
train_image_dir = "$TRAIN_IMAGE_DIR"
|
87 |
val_annotation = "$VAL_ANNOTATION"
|
88 |
val_image_dir = "$VAL_IMAGE_DIR"
|
89 |
+
resume_training = $([ "$RESUME_TRAINING" = true ] && echo True || echo False) # Convert bash boolean to Python boolean
|
90 |
+
max_iter = $MAX_ITER
|
91 |
+
checkpoint_period = $CHECKPOINT_PERIOD
|
92 |
+
output_dir = "$OUTPUT_DIR"
|
93 |
+
coco_config_file = "$COCO_CONFIG_FILE"
|
94 |
+
ims_per_batch = $IMS_PER_BATCH
|
95 |
+
batch_size_per_image = $BATCH_SIZE_PER_IMAGE
|
96 |
+
base_lr = $BASE_LR
|
97 |
|
98 |
# Load the COCO annotation file to detect number of classes
|
99 |
with open(train_annotation, 'r') as f:
|
|
|
111 |
print("Datasets registered successfully.")
|
112 |
print("Available datasets:", DatasetCatalog.list())
|
113 |
|
|
|
|
|
|
|
|
|
114 |
# Set up configuration
|
115 |
cfg = get_cfg()
|
116 |
+
cfg.merge_from_file(coco_config_file)
|
117 |
cfg.DATASETS.TRAIN = ("coco_roboone_train",)
|
118 |
cfg.DATASETS.TEST = ("coco_roboone_val",)
|
119 |
+
cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes
|
120 |
+
cfg.OUTPUT_DIR = output_dir
|
121 |
+
|
122 |
+
# Set solver parameters
|
123 |
+
cfg.SOLVER.MAX_ITER = max_iter
|
124 |
+
cfg.SOLVER.CHECKPOINT_PERIOD = checkpoint_period
|
125 |
+
cfg.SOLVER.IMS_PER_BATCH = ims_per_batch
|
126 |
+
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = batch_size_per_image
|
127 |
+
cfg.SOLVER.BASE_LR = base_lr
|
128 |
|
129 |
# Train the model
|
130 |
trainer = DefaultTrainer(cfg)
|
131 |
+
trainer.resume_or_load(resume=resume_training)
|
132 |
trainer.train()
|
133 |
|
134 |
print("Mask R-CNN training completed.")
|