So I was greatly slowed down by Google Colabs’ limitations but found out that setting AVX and OPENMP to 1 will make Darknet use the CPU, making it pointless to run a GPU runtime
I took the opportunity to correct the paths.
from google.colab import drive
drive.mount('/content/gdrive', force_remount=True)
root_dir = "/content/gdrive/MyDrive/Colab"
base_dir = root_dir + '/Cars'
Now everything will be in Google Drive.
DN = root_dir + "/darknet"
!git clone https://github.com/AlexeyAB/darknet $DN # Makes a darknet folder
Remember to set it to GPU.
# https://stackoverflow.com/questions/39086/search-and-replace-a-line-in-a-file-in-python
TARGET = DN + '/Makefile'
f = open(TARGET, 'r')
lines = ''
for line in f:
if line.find('GPU=0') != -1:
line = line.replace('GPU=0', 'GPU=1')
if line.find('CUDNN=0') != -1:
line = line.replace('CUDNN=0', 'CUDNN=1')
if line.find('CUDNN_HALF=0') != -1:
line = line.replace('CUDNN_HALF=0', 'CUDNN_HALF=1')
if line.find('OPENCV=0') != -1:
line = line.replace('OPENCV=0', 'OPENCV=1')
# if line.find('AVX=0') != -1:
# line = line.replace('AVX=0', 'AVX=1')
# if line.find('OPENMP=0') != -1:
# line = line.replace('OPENMP=0', 'OPENMP=1')
lines += line
f.close()
g = open(TARGET, 'w')
g.write(lines)
g.close()
!cd $DN; make --silent; clear; echo "Darknet Compiled!"
And use variables instead of hard-coded paths.
!cp $DN/cfg/yolov3.cfg $DN/cfg/yolov3-train.cfg
TARGET = DN + '/cfg/yolov3-train.cfg'
f = open(TARGET, 'r')
lines = ''
for line in f:
if line.find('batch=1') != -1:
line = line.replace('batch=1', 'batch=64')
if line.find('subdivisions=1') != -1:
line = line.replace('subdivisions=1', 'subdivisions=16')
if line.find('max_batches=500200') != -1:
line = line.replace('max_batches=500200', 'max_batches=2000')
if line.find('filters=255') != -1:
line = line.replace('filters=255', 'filters=18')
if line.find('classes=80') != -1:
line = line.replace('classes=80', 'classes=1')
lines += line
f.close()
with open(TARGET, 'w') as f:
f.write(lines)
OUTPUT="$root_dir/output"
!mkdir "$OUTPUT"
!echo -e "classes = 1\ntrain = $DN/data/train.txt\nvalid = $DN/data/test.txt\nlabels = $DN/data/labels.txt\nnames = $DN/data/cars/classes.txt\nbackup = $OUTPUT" > $DN/data/obj.data
Finally run Darknet with the last weights.
!cd $DN; ./darknet detector -dont_show -map train "data/obj.data" "cfg/yolov3-train.cfg" "output/yolov3-train_last.weights"