|
#!/bin/bash |
|
|
|
|
|
USE_GPU_TUNING=true |
|
RESUME_TRAINING=false |
|
MAX_ITER=100000 |
|
CHECKPOINT_PERIOD=$(($MAX_ITER / 10)) |
|
|
|
|
|
IMS_PER_BATCH=2 |
|
BATCH_SIZE_PER_IMAGE=512 |
|
BASE_LR=0.00025 |
|
|
|
|
|
if [ "$USE_GPU_TUNING" = true ]; then |
|
|
|
GPU_MEMORY=$(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | head -n 1) |
|
|
|
|
|
if [ "$GPU_MEMORY" -ge 24576 ]; then |
|
IMS_PER_BATCH=8 |
|
BATCH_SIZE_PER_IMAGE=512 |
|
elif [ "$GPU_MEMORY" -ge 12288 ]; then |
|
IMS_PER_BATCH=4 |
|
BATCH_SIZE_PER_IMAGE=256 |
|
else |
|
IMS_PER_BATCH=2 |
|
BATCH_SIZE_PER_IMAGE=128 |
|
fi |
|
|
|
|
|
BASE_LR=$(echo "0.00025 * $IMS_PER_BATCH / 2" | bc -l) |
|
fi |
|
|
|
|
|
SCRIPT_DIR=$(cd $(dirname $0); pwd) |
|
|
|
|
|
TRAIN_ANNOTATION=$SCRIPT_DIR/../export_coco/annotations/train.json |
|
TRAIN_IMAGE_DIR=$SCRIPT_DIR/../export_coco/train |
|
VAL_ANNOTATION=$SCRIPT_DIR/../export_coco/annotations/val.json |
|
VAL_IMAGE_DIR=$SCRIPT_DIR/../export_coco/val |
|
COCO_CONFIG_FILE=$SCRIPT_DIR/../export_coco/config/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml |
|
BASE_CONFIG_FILE=$SCRIPT_DIR/../export_coco/config/Base-RCNN-FPN.yaml |
|
CONFIG_URL="https://raw.githubusercontent.com/facebookresearch/detectron2/main/configs/COCO-InstanceSegmentation/mask_rcnn_R_50_FPN_3x.yaml" |
|
BASE_CONFIG_URL="https://raw.githubusercontent.com/facebookresearch/detectron2/main/configs/Base-RCNN-FPN.yaml" |
|
TRAIN_NET_DIR=$SCRIPT_DIR/../export_coco/detectron2 |
|
TRAIN_NET_FILE=$TRAIN_NET_DIR/train_net.py |
|
TRAIN_NET_URL="https://raw.githubusercontent.com/facebookresearch/detectron2/main/tools/train_net.py" |
|
OUTPUT_DIR=$SCRIPT_DIR/../export_coco/output |
|
|
|
|
|
mkdir -p $(dirname $COCO_CONFIG_FILE) |
|
mkdir -p $TRAIN_NET_DIR |
|
mkdir -p $OUTPUT_DIR |
|
|
|
|
|
if [ ! -f "$COCO_CONFIG_FILE" ]; then |
|
echo "Downloading Mask R-CNN configuration file..." |
|
wget $CONFIG_URL -O $COCO_CONFIG_FILE |
|
fi |
|
|
|
|
|
if [ ! -f "$BASE_CONFIG_FILE" ]; then |
|
echo "Downloading Base-RCNN-FPN.yaml configuration file..." |
|
wget $BASE_CONFIG_URL -O $BASE_CONFIG_FILE |
|
fi |
|
|
|
|
|
if [ ! -f "$TRAIN_NET_FILE" ]; then |
|
echo "Downloading train_net.py file..." |
|
wget $TRAIN_NET_URL -O $TRAIN_NET_FILE |
|
fi |
|
|
|
|
|
python3 - <<END |
|
import os |
|
import json |
|
from detectron2.data.datasets import register_coco_instances |
|
from detectron2.data import DatasetCatalog |
|
from detectron2.engine import DefaultTrainer |
|
from detectron2.config import get_cfg |
|
|
|
# Paths from Bash script |
|
train_annotation = "$TRAIN_ANNOTATION" |
|
train_image_dir = "$TRAIN_IMAGE_DIR" |
|
val_annotation = "$VAL_ANNOTATION" |
|
val_image_dir = "$VAL_IMAGE_DIR" |
|
resume_training = $([ "$RESUME_TRAINING" = true ] && echo True || echo False) # Convert bash boolean to Python boolean |
|
max_iter = $MAX_ITER |
|
checkpoint_period = $CHECKPOINT_PERIOD |
|
output_dir = "$OUTPUT_DIR" |
|
coco_config_file = "$COCO_CONFIG_FILE" |
|
ims_per_batch = $IMS_PER_BATCH |
|
batch_size_per_image = $BATCH_SIZE_PER_IMAGE |
|
base_lr = $BASE_LR |
|
|
|
# Load the COCO annotation file to detect number of classes |
|
with open(train_annotation, 'r') as f: |
|
coco_data = json.load(f) |
|
|
|
# Extract number of unique categories |
|
num_classes = len(coco_data['categories']) |
|
print(f"Detected {num_classes} classes from the dataset.") |
|
|
|
# Register the datasets |
|
register_coco_instances("coco_roboone_train", {}, train_annotation, train_image_dir) |
|
register_coco_instances("coco_roboone_val", {}, val_annotation, val_image_dir) |
|
|
|
# Confirm the datasets are registered |
|
print("Datasets registered successfully.") |
|
print("Available datasets:", DatasetCatalog.list()) |
|
|
|
# Set up configuration |
|
cfg = get_cfg() |
|
cfg.merge_from_file(coco_config_file) |
|
cfg.DATASETS.TRAIN = ("coco_roboone_train",) |
|
cfg.DATASETS.TEST = ("coco_roboone_val",) |
|
cfg.MODEL.ROI_HEADS.NUM_CLASSES = num_classes |
|
cfg.OUTPUT_DIR = output_dir |
|
|
|
# Set solver parameters |
|
cfg.SOLVER.MAX_ITER = max_iter |
|
cfg.SOLVER.CHECKPOINT_PERIOD = checkpoint_period |
|
cfg.SOLVER.IMS_PER_BATCH = ims_per_batch |
|
cfg.MODEL.ROI_HEADS.BATCH_SIZE_PER_IMAGE = batch_size_per_image |
|
cfg.SOLVER.BASE_LR = base_lr |
|
|
|
# Train the model |
|
trainer = DefaultTrainer(cfg) |
|
trainer.resume_or_load(resume=resume_training) |
|
trainer.train() |
|
|
|
print("Mask R-CNN training completed.") |
|
END |
|
|