Detecting Objects in Images and Videos using darknet and YOLOv3 Convolutional Neural Networks

Source: Deep Learning on Medium

8) Prepare Data Files

Before running my darknet executable file, I want to prepare data files (like coco.data). Each data file will provide darknet with information on the number of classes of objects I have identified in the images in my training dataset, the location of a text-file listing of the paths to images in my training dataset, the location of a text-file listing of the paths to images in my test dataset, the location of a text-file listing of the names of classes of objects, and the location of a folder in which to save a weights file that the darknet binary will use to initialize the values in each cell in each filter in the YOLOv3 convolutional neural network.

Joseph Chet Redmon is the creator of the original darknet deep-learning framework. AlexeyAB offers an optimized version of Redmon’s framework. Instructions on how to use Redmon’s, and AlexeyAB’s, frameworks are available on Redmon’s YOLOv3 webpage. To navigate to Redmon’s YOLOv3 webpage, open a new tab in Chrome, type “https://pjreddie.com” in Chrome’s OmniBar, press “enter”, left click “darknet”, scroll down a little bit, and left click “YOLO: Real-Time Object Detection”.

8.1) Preparing coco.data

The data file corresponding to Joseph Chet Redmon’s first presented command to locate objects within an image (“./darknet detector test cfg/coco.data cfg/yolov3.cfg yolov3.weights data/dog.jpg”), which corresponds to locating objects associated with the COCO dataset, is coco.data. One version of coco.data is located on our virtual machine in “ROOT/content/darknet/cfg/coco.data”. Execute the command “!head -6 cfg/coco.data” to display the first six lines of coco.data.

Figure 8.1.1: The first six lines of the default version of “ROOT/content/darknet/cfg/coco.data”.

I want to revise coco.data to reference a text-file listing on my virtual machine of paths to training images, a text-file listing on my virtual machine of paths to test images, a text-file listing on my virtual machine of the names of classes of objects, and a folder on my virtual machine in which to save a weights file. I want coco.data to look like this:

classes= 80
train = data/coco/trainvalno5k.txt
valid = data/coco/5k.txt
#valid = data/coco_val_5k.list
names = data/coco.names
backup = backup

To get coco.data to reference files and a folder on my virtual machine, I execute the commands

!sed -i ‘s|train = /home/pjreddie/data/coco/trainvalno5k.txt|train = data/coco/trainvalno5k.txt|’ cfg/coco.data
!sed -i ‘s|valid = coco_testdev|valid = data/coco/5k.txt|’ cfg/coco.data
!sed -i ‘s|backup = /home/pjreddie/backup/|backup = backup|’ cfg/coco.data

I will check that my version of coco.data references files and a folder on my virtual machine by executing the command “!head -6 cfg/coco.data”.

Figure 8.1.2: The first six lines of my revised version of “ROOT/content/darknet/cfg/coco.data”.

8.2) Preparing voc.data

The data file corresponding to locating objects associated with the Pascal VOC 2007 and 2012 datasets is voc.data. One version of voc.data is located on our virtual machine in “ROOT/content/darknet/cfg/voc.data”. Execute the command “!head -6 cfg/voc.data” to display the first six lines of voc.data.

Figure 8.2.1: The first six lines of the default version of “ROOT/content/darknet/cfg/voc.data”.

I want to revise voc.data to reference a text-file listing on my virtual machine of paths to training images, a text-file listing on my virtual machine of paths to test images, a text-file listing on my virtual machine of the names of classes of objects, and a folder on my virtual machine in which to save a weights file. I want coco.data to look like this:

classes= 20
train = VOC_data_from_2007_to_2012/train.txt
valid = VOC_data_from_2007_to_2012/2007_test.txt
names = data/voc.names
backup = backup

To get voc.data to reference files and a folder on my virtual machine, I execute the commands

!sed -i ‘s|train = /home/pjreddie/data/voc/train.txt|train = VOC_data_from_2007_to_2012/train.txt|’ cfg/voc.data
!sed -i ‘s|valid = /home/pjreddie/data/voc/2007_test.txt|valid = VOC_data_from_2007_to_2012/2007_test.txt|’ cfg/voc.data
!sed -i ‘s|backup = /home/pjreddie/backup/|backup = backup|’ cfg/voc.data

I will check that my version of voc.data references files and a folder on my virtual machine by executing the command “!head -6 cfg/voc.data”.

Figure 8.2.2: The first six lines of my revised version of “ROOT/content/darknet/cfg/voc.data”.

8.3) Preparing obj.data

To create a data file corresponding to locating objects associated with my own dataset of one hundred images of oak leaves from various species in Quercus, the genus of oak trees, on my Windows-10 PC, I minimize my jupyter notebook, press and hold “shift”, right click my desktop, left click “Open PowerShell window here”, reposition and resize my PowerShell window, write “Notepad”, press “enter”, re-position and resize my Notepad window, left click “File”, left click “Save As…”, delete the suggested file name, press the down-pointing chevron right of “Save as type:”, left click “All Files”, enter “obj.data” as file name, and left click “Save”. To fill in obj.data, I write:

classes = 1
train = Quercus_Data/train.txt
valid = Quercus_Data/test.txt
names = data/obj.names
backup = backup

I will save obj.data by left clicking “File” and left clicking “Save”. I will return to view our jupyter notebook YOLOv3_Detecting.ipynb.

To copy obj.data from my desktop on my Windows-10 PC into the file “ROOT/content/darknet/cfg/obj.data”, I navigate to my Google Drive’s darknet folder, right click within the window to all the folders and files in my darknet folder, left click “Upload files”, left click “obj.data”, and left click “Open”. I then navigate back my jupyter notebook YOLOv3_Detecting.ipynb and navigate to the “ROOT/content” directory by executing the command “%cd ..”.

While in the “ROOT/content” directory, I mount my Google Drive as a virtual drive accessible by my virtual machine by executing the Python code

from google.colab import drive
drive.mount(“/content/drive”)

I left click the URL, left click my name, scroll down a little bit, left click “Allow”, left click the “Copy” button, navigate back to my jupyter notebook, right click in the text box under “Enter your authorization code:”, left click “Paste”, press “enter”, left click “Refresh”, and close the authentication window.

I’m still in the “ROOT/content” directory. To copy obj.data from my Google Drive’s darknet folder to the “ROOT/content/darknet/cfg/obj.data” file, I execute the command “!cp drive/My\ Drive/darknet/obj.data darknet/cfg/obj.data”. I then navigate back to “ROOT/content/darknet” directory by executing the command “%cd darknet”.

Figure 8.3: Creating “obj.data” on the Desktop of my Windows-10 PC.