-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrain.sh
166 lines (135 loc) · 8.02 KB
/
train.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#!/bin/bash
###################################################################################################################
##################################### UPDATE BELOW PATHS BEFORE RUNNING ###########################################
###################################################################################################################
# Provide full path to the darknet repository
DARKNET_REPO_PATH=/home/sourab/Data/repos/darknet #e.g. /home/user/darknet
# Provide full paths to the road-object-detection-using-yolov4 repository
ROAD_REPO_PATH=/home/sourab/Data/repos/road-object-detection-using-yolov4 #e.g. /home/user/repo-name
# Provide full path to the dataset
DATA_PATH=/home/sourab/Data/dataset/DeepDrive #e.g. /home/user/DeepDrive
###################################################################################################################
##################################### UPDATE BELOW FLAGS BEFORE RUNNING ###########################################
###################################################################################################################
# Set up flags for building darknet
# (More information present here: https://github.com/AlexeyAB/darknet#how-to-compile-on-linux-using-make)
OPENCV=1 #e.g. 0, 1
GPU=1 #e.g. 0, 1
CUDNN=1 #e.g. 0, 1
CUDNN_HALF=0 #e.g. 0, 1
LIBSO=1 #e.g. 0, 1
###################################################################################################################
##################################### DO NOT MODIFY THE SETTINGS BELOW ############################################
###################################################################################################################
# Cleanup environment
echo "Cleaning up environment..."
echo ""
rm -rf $DARKNET_REPO_PATH
conda remove --name yolo --all --yes
# Print current path and provided path
echo "Path to the dataset: $DATA_PATH"
echo "Path to the road-object-detection-using-yolov4 repository: $ROAD_REPO_PATH"
echo "Path to the darknet repository: $DARKNET_REPO_PATH"
echo ""
# Clone darknet repositories
echo "Cloning darknet repositories..."
echo ""
git clone https://github.com/AlexeyAB/darknet.git $DARKNET_REPO_PATH
# Activate conda virtual environment to run from bash
echo "Activating conda virtual environment to run from bash..."
echo ""
conda init bash && echo "Conda virtual environment initiated successfully!"
# Create a conda virtual environment
echo "Creating a conda virtual environment..."
echo ""
CONDA_BASE=$(conda info --base) && echo "Path to the conda base environment is $CONDA_BASE"
source $CONDA_BASE/etc/profile.d/conda.sh && echo "Conda functions made available to subshells successfully!"
conda env create -f environment.yml && echo "Conda virtual environment created successfully!"
conda activate yolo && echo "Conda virtual environment activated successfully!"
# Update Makefile based on requirements
echo "Updating makefile based on requirements..."
echo ""
cd $DARKNET_REPO_PATH
if [[ $OPENCV -eq 1 ]] # Check if OPENCV is set to 1
then
sed -i 's/OPENCV=0/OPENCV=1/' Makefile && echo "Flag to build with OpenCV updated"
fi
if [[ $GPU -eq 1 ]] # Check if GPU is set to 1
then
sed -i 's/GPU=0/GPU=1/' Makefile && echo "Flag to build with GPU updated"
fi
if [[ $CUDNN -eq 1 ]] # Check if CUDNN is set to 1
then
sed -i 's/CUDNN=0/CUDNN=1/' Makefile && echo "Flag to build with CUDNN updated"
fi
if [[ $CUDNN_HALF -eq 1 ]] # Check if CUDNN_HALF is set to 1
then
sed -i 's/CUDNN_HALF=0/CUDNN_HALF=1/' Makefile && echo "Flag to build with CUDNN_HALF updated"
fi
if [[ $LIBSO -eq 1 ]] # Check if LIBSO is set to 1
then
sed -i 's/LIBSO=0/LIBSO=1/' Makefile && echo "Flag to build with LIBSO updated"
fi
# Build darknet
echo "Building darknet..."
echo ""
make && echo "Darknet build successful"
# Download pre-trained weights file
echo "Downloading pre-trained weights file..."
echo ""
wget https://github.com/AlexeyAB/darknet/releases/download/darknet_yolo_v4_pre/yolov4-tiny.conv.29 && echo "Pre-trained weights downloaded successfully!"
# Setup the Berkley DeepDrive dataset
echo "Setting up the Berkley DeepDrive dataset..."
echo ""
cp -rvi $DATA_PATH/bdd100k_images.zip $DARKNET_REPO_PATH/data/ && echo "Copied image data successfully!"
cp -rvi $DATA_PATH/bdd100k_labels_release.zip $DARKNET_REPO_PATH/data/ && echo "Copied label data successfully!"
# Unzip the dataset and the annotations in the data folder
echo "Unziping the dataset and the annotations in the data folder..."
echo ""
unzip $DARKNET_REPO_PATH/data/bdd100k_images.zip -d $DARKNET_REPO_PATH/data/ && echo "Images unzipped successfully!"
unzip $DARKNET_REPO_PATH/data/bdd100k_labels_release.zip -d $DARKNET_REPO_PATH/data/ && echo "Labels unzipped successfully!"
# Delete unwanted files
echo "Deleting unwanted files..."
echo ""
rm -rf $DARKNET_REPO_PATH/data/bdd100k_images.zip && echo "bdd100k_images.zip delected successfully!"
rm -rf $DARKNET_REPO_PATH/data/bdd100k_labels_release.zip && echo "bdd100k_labels-release.zip delected successfully!"
rm -rf $DARKNET_REPO_PATH/data/bdd100k/images/10k && echo "10k image folder deleted successfully!"
# Copy bdd100k.names files
echo "Copying bdd100k.names files..."
echo ""
cp -vi $ROAD_REPO_PATH/data/* $DARKNET_REPO_PATH/data/bdd100k/ && echo "Copied bdd100k.names successfully!"
# Convert labels from JSON files to text files
echo "Converting JSON files to text files..."
echo ""
python $ROAD_REPO_PATH/utils/convert_labels.py -ij $DARKNET_REPO_PATH/data/bdd100k/labels/bdd100k_labels_images_train.json -in $DARKNET_REPO_PATH/data/bdd100k/bdd100k.names -o $DARKNET_REPO_PATH/data/bdd100k/images/100k/train/ && echo "Training labels converted successfully!"
python $ROAD_REPO_PATH/utils/convert_labels.py -ij $DARKNET_REPO_PATH/data/bdd100k/labels/bdd100k_labels_images_val.json -in $DARKNET_REPO_PATH/data/bdd100k/bdd100k.names -o $DARKNET_REPO_PATH/data/bdd100k/images/100k/val/ && echo "Validation labels converted successfully!"
# Remove data without annotations
echo "Removing data without annotations..."
echo ""
python $ROAD_REPO_PATH/utils/data_cleanup.py -i $DARKNET_REPO_PATH/data/bdd100k/images/100k/train/ && echo "Removed unwanted training data successfully!"
python $ROAD_REPO_PATH/utils/data_cleanup.py -i $DARKNET_REPO_PATH/data/bdd100k/images/100k/val/ && echo "Removed unwanted validation data successfully!"
# Generate paths for training and validation images
echo "Generating paths for training and validation images..."
echo ""
python $ROAD_REPO_PATH/utils/generate_paths.py -it data/bdd100k/images/100k/train/ -iv data/bdd100k/images/100k/val/ -o $DARKNET_REPO_PATH/data/bdd100k/ && echo "Generated paths for training and validation images successfully!"
# Generate data file containing relative paths to the training, validation and backup folders for YOLOv4
echo "Generating data file containing relative paths to the training, validation and backup folders for YOLOv4..."
echo ""
python $ROAD_REPO_PATH/utils/generate_data_file.py -c 10 -t data/bdd100k/bdd100k_train.txt -v data/bdd100k/bdd100k_val.txt -n data/bdd100k/bdd100k.names -b backup/ -o $DARKNET_REPO_PATH/data/bdd100k/
# Copy pre-defined YOLOv4 network configuration file to cfg folder
echo "Copying pre-defined YOLOv4 network configuration file to cfg folder..."
echo ""
cp -vi $ROAD_REPO_PATH/config/* $DARKNET_REPO_PATH/cfg/ && echo "Pre-defined YOLOv4 config copied successfully!"
# Train YOLOV4 on Berkley DeepDrive dataset
echo "Training YOLOv4 on Berkley DeepDrive dataset..."
echo ""
chmod +x darknet
./darknet detector train data/bdd100k/bdd100k.data cfg/yolov4-tiny-bdd100k.cfg yolov4-tiny.conv.29 -dont_show -map
# Deactivate Conda Environment
echo "Deactivating Conda Environment..."
echo ""
conda deactivate
# Exit script
echo "Exiting script..."
echo ""
exit 0