nyxrobotics commited on
Commit
3aa6f4a
·
1 Parent(s): 6a5403b

Add yolo training

Browse files
modules/convert_labelme_nodata.sh CHANGED
@@ -6,9 +6,18 @@ TARGET_DIR=$SCRIPT_DIR/../export_annotated
6
 
7
  # Process all JSON files in the target directory
8
  for file in "$TARGET_DIR"/*.json; do
9
- # Use jq to set imageData to null and overwrite the original file
10
  jq '.imageData = null' "$file" > tmp && mv tmp "$file"
11
- echo "Converted: $file"
 
 
 
 
 
 
 
 
 
12
  done
13
 
14
- echo "All files have been converted."
 
6
 
7
  # Process all JSON files in the target directory
8
  for file in "$TARGET_DIR"/*.json; do
9
+ # Step 1: Set imageData to null
10
  jq '.imageData = null' "$file" > tmp && mv tmp "$file"
11
+
12
+ # Step 2: Check if imagePath contains a subdirectory and modify it if needed
13
+ # jq '
14
+ # if .imagePath | contains("/") then
15
+ # .imagePath |= (split("/") | last)
16
+ # else
17
+ # .imagePath
18
+ # end' "$file" > tmp && mv tmp "$file"
19
+
20
+ echo "Processed: $file"
21
  done
22
 
23
+ echo "All files have been processed."
modules/labelme2yolo.sh ADDED
@@ -0,0 +1,43 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ # Get script dir
4
+ SCRIPT_DIR=$(cd $(dirname $0); pwd)
5
+ LABELME_DIR=$SCRIPT_DIR/../export_annotated
6
+ YOLO_DIR=$SCRIPT_DIR/../export_yolo
7
+
8
+ # Create necessary directories
9
+ # mkdir -p $YOLO_DIR/train
10
+ # mkdir -p $YOLO_DIR/val
11
+
12
+ # Clean train and val directories by removing all files
13
+ # rm -rf $YOLO_DIR/train/*
14
+ # rm -rf $YOLO_DIR/val/*
15
+
16
+ # Determine the number of files for validation (20%)
17
+ LABELME_FILES=$(find "$LABELME_DIR" -name "*.json")
18
+
19
+ # Copy labelme files and their corresponding images
20
+ echo "$LABELME_FILES" | while read -r lableme_file; do
21
+ cp "$lableme_file" "$YOLO_DIR/"
22
+
23
+ # Copy the corresponding image file (assuming it has the same base name)
24
+ image_file="${lableme_file%.json}.png" # Change the extension to match your images
25
+ if [[ -f "$image_file" ]]; then
26
+ cp "$image_file" "$YOLO_DIR/"
27
+ fi
28
+ done
29
+
30
+ # Automatically generate LABEL_LIST by extracting labels from all JSON files
31
+ LABEL_LIST=$(find "$LABELME_DIR" -name "*.json" -exec jq -r '.shapes[].label' {} + | sort | uniq | paste -sd "," -)
32
+
33
+ echo "Detected labels: $LABEL_LIST"
34
+
35
+ # Convert Labelme JSON to YOLO format for train and val
36
+ cd $YOLO_DIR
37
+ labelme2yolo --output_format bbox --json_dir $YOLO_DIR --val_size 0.1 --test_size 0.1 $LABEL_LIST
38
+
39
+ # Remove temporary labelme files and their corresponding images
40
+ find $YOLO_DIR -maxdepth 1 -name "*.json" -exec rm -f {} +
41
+ find $YOLO_DIR -maxdepth 1 -name "*.png" -exec rm -f {} +
42
+
43
+ echo "YOLO dataset conversion completed."
modules/yolov8_train.sh ADDED
@@ -0,0 +1,66 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ #!/bin/bash
2
+
3
+ SCRIPT_DIR=$(cd $(dirname $0); pwd)
4
+ # Parameters
5
+ YOLO_DATASET_DIR=$SCRIPT_DIR/../export_yolo/YOLODataset
6
+ MODEL="yolov8n.pt" # YOLOv8 model type
7
+ EPOCHS=1000
8
+ OUTPUT_DIR=$SCRIPT_DIR/../export_yolo/train_output
9
+ USE_GPU_TUNING=true
10
+ RESUME_TRAINING=false
11
+
12
+ mkdir -p $OUTPUT_DIR
13
+
14
+ # Install ImageMagick if not already installed
15
+ if ! command -v identify &> /dev/null
16
+ then
17
+ echo "ImageMagick not found. Installing..."
18
+ sudo apt install -y imagemagick
19
+ fi
20
+
21
+ # Automatically detect image size from the training images
22
+ IMG_SIZE=$(identify -format "%wx%h\n" $YOLO_DATASET_DIR/images/train/*.png | head -n 1 | cut -d 'x' -f 1)
23
+
24
+ # If USE_GPU_TUNING is true, adjust parameters based on GPU memory
25
+ if [ "$USE_GPU_TUNING" = true ]; then
26
+ # Get the GPU memory in MB
27
+ GPU_MEMORY=$(nvidia-smi --query-gpu=memory.total --format=csv,noheader,nounits | head -n 1)
28
+
29
+ # Set the batch size and learning rate based on GPU memory
30
+ if [ "$GPU_MEMORY" -ge 24576 ]; then
31
+ BATCH_SIZE=8 # For 24GB+ GPUs
32
+ BATCH_SIZE_PER_IMAGE=512
33
+ elif [ "$GPU_MEMORY" -ge 12288 ]; then
34
+ BATCH_SIZE=4 # For 12GB+ GPUs
35
+ BATCH_SIZE_PER_IMAGE=256
36
+ else
37
+ BATCH_SIZE=2 # For smaller GPUs
38
+ BATCH_SIZE_PER_IMAGE=128
39
+ fi
40
+
41
+ # Adjust learning rate based on batch size
42
+ BASE_LR=$(echo "0.00025 * $BATCH_SIZE / 2" | bc -l)
43
+ echo "GPU memory: $GPU_MEMORY MB"
44
+ echo "Batch size: $BATCH_SIZE, Batch size per image: $BATCH_SIZE_PER_IMAGE"
45
+ echo "Base learning rate: $BASE_LR"
46
+ fi
47
+
48
+ cd $OUTPUT_DIR
49
+ # Run training with the YOLO model
50
+ python3 - <<END
51
+ from ultralytics import YOLO
52
+
53
+ model = YOLO("$MODEL")
54
+ results = model.train(
55
+ data="$YOLO_DATASET_DIR/dataset.yaml",
56
+ model="$MODEL",
57
+ epochs=$EPOCHS,
58
+ batch=$BATCH_SIZE,
59
+ imgsz=$IMG_SIZE,
60
+ project="$OUTPUT_DIR",
61
+ name="train_results",
62
+ lr0=$BASE_LR,
63
+ resume=$([ "$RESUME_TRAINING" = true ] && echo True || echo False) # Convert bash boolean to Python boolean
64
+ )
65
+ print("YOLO training completed.")
66
+ END
setup/install_labelme2yolo.sh ADDED
@@ -0,0 +1,3 @@
 
 
 
 
1
+ #!/bin/bash
2
+ python3 -m pip install -U labelme2yolo
3
+ sudo apt install -y imagemagick