Learn A-Z Deep Learning in 15 Days
You Only Look Once
YOLO first takes an input image. The framework then divides the input image into grids (say a n X n grid- Each grid predict):
- Predicts B boundary boxes and each box has one box confidence score Pr(Object) ∗ IOU
- Detects one object only regardless of the number of boxes B,
- Predicts C conditional class probabilities (one per class for the likeliness of the object class).
- The bounding boxes having the class probability above a threshold value is selected and used to locate the object within the image
- Total loss = classification loss+ localization loss (errors between the predicted boundary box and the ground truth) + confidence loss (the objectness of the box)
Data Preprocessing with Kitti dataset
Categories frequency
Car----------------------28742
Pedestrian----------------4487
Van------------------------2914
Cyclist-------------------1627
Truck--------------------1094
Misc----------------------973
Tram----------------------511
Person_sitting------------222
- Total annotation count = 40570
- Total images = 7481
import pandas as pd
path="kitti/training/label_2/"
df = pd.DataFrame()
# iterate the list of files in the directory 'label_2'
for filename in os.listdir(path):
filepath= os.path.join(path,filename)
# read file in csv format
pdtxtdata=pd.read_csv(filepath, delimiter = ' ', header=None)
# create the folder 'updatelabel' in the current folder
with open('./updatelabel/'+filename,'a') as f:
# iloc takes the values at 0,4,5,6,7 i,e class_name, xmin, ymin, xmax and ymax.
for label, xmin, ymin, xmax, ymax in pdtxtdata.iloc[:,[0,4,5,6,7]].values:
# categories 'Pedestrian' and "Person_sitting" are considered
if label=='Pedestrian' or label=="Person_sitting":
f.write(str(0)+' '+str(xmin)+' '+str(ymin)+' '+str(xmax)+' '+str(ymax)+'\n')
path="kitti/training/label_2/"
df = pd.DataFrame()
# iterate the list of files in the directory 'label_2'
for filename in os.listdir(path):
filepath= os.path.join(path,filename)
# read file in csv format
pdtxtdata=pd.read_csv(filepath, delimiter = ' ', header=None)
# create the folder 'updatelabel' in the current folder
with open('./updatelabel/'+filename,'a') as f:
# iloc takes the values at 0,4,5,6,7 i,e class_name, xmin, ymin, xmax and ymax.
for label, xmin, ymin, xmax, ymax in pdtxtdata.iloc[:,[0,4,5,6,7]].values:
# categories 'Pedestrian' and "Person_sitting" are considered
if label=='Pedestrian' or label=="Person_sitting":
f.write(str(0)+' '+str(xmin)+' '+str(ymin)+' '+str(xmax)+' '+str(ymax)+'\n')
# Remove the files with 0 size
import os
path="./updatelabel/"
# Iterate the list of files in the directory 'updatelabel'
for filename in os.listdir(path):
filepath= os.path.join(path,filename)
if os.path.getsize(filepath)==0:
os.remove(filepath)
import os
path="./updatelabel/"
# Iterate the list of files in the directory 'updatelabel'
for filename in os.listdir(path):
filepath= os.path.join(path,filename)
if os.path.getsize(filepath)==0:
os.remove(filepath)
# Copy and change the file name
import os
import shutil
path="./updatelabel/"
for filename in os.listdir(path):
shutil.copy("/kitti/training/image_2/"+filename.split('.')[0]+'.png',"./yoloimagelabel/" +filename.split('.')[0]+'.png')
import os
import shutil
path="./updatelabel/"
for filename in os.listdir(path):
shutil.copy("/kitti/training/image_2/"+filename.split('.')[0]+'.png',"./yoloimagelabel/" +filename.split('.')[0]+'.png')
# Convert the kitti to Yolo format i.e (xmin, ymin, xmax ,ymax) to (x, y, w, h)
import pandas as pd
path="./udatelabel/"
df = pd.DataFrame()
for filename in os.listdir(path):
filepath= os.path.join(path,filename)
image= cv2.imread("./image/"+filename.split('.')[0]+'.png')
image_width= image.shape[1]
image_height= image.shape[0]
pdtxtdata=pd.read_csv(filepath, delimiter = ' ', header=None)
with open('./yoloimagelabel/'+filename,'a') as f:
for label, xmin, ymin, xmax, ymax in pdtxtdata.iloc[:,[0,1,2,3,4]].values:
f.write(str(0)+' '+str('%.6f' % float((xmin+xmax)/(2.0*image_width)))+' '+str('%.6f' % float((ymin+ymax)/(2.0*image_height)))+' '+str('%.6f' % float((xmax-xmin)/image_width))+' '+str('%.6f' % float((ymax-ymin)/image_height))+'\n')
import pandas as pd
path="./udatelabel/"
df = pd.DataFrame()
for filename in os.listdir(path):
filepath= os.path.join(path,filename)
image= cv2.imread("./image/"+filename.split('.')[0]+'.png')
image_width= image.shape[1]
image_height= image.shape[0]
pdtxtdata=pd.read_csv(filepath, delimiter = ' ', header=None)
with open('./yoloimagelabel/'+filename,'a') as f:
for label, xmin, ymin, xmax, ymax in pdtxtdata.iloc[:,[0,1,2,3,4]].values:
f.write(str(0)+' '+str('%.6f' % float((xmin+xmax)/(2.0*image_width)))+' '+str('%.6f' % float((ymin+ymax)/(2.0*image_height)))+' '+str('%.6f' % float((xmax-xmin)/image_width))+' '+str('%.6f' % float((ymax-ymin)/image_height))+'\n')
# Split the train & test
# Generate the train.txt and test.txt
import glob, os
current_dir = 'dataset'
# Percentage of images to be used for the test set
percentage_test = 20;
# Create and/or truncate train.txt and test.txt
file_train = open('train.txt', 'w')
file_test = open('test.txt', 'w')
# Populate train.txt and test.txt
counter = 1
index_test = round(100 / percentage_test)
for pathAndFilename in glob.glob(os.path.join(current_dir, "*.*g")):
if counter == index_test:
counter = 1
file_test.write(current_dir + "/" + os.path.basename(pathAndFilename) + "\n")
else:
file_train.write(current_dir + "/" + os.path.basename(pathAndFilename) + "\n")
counter = counter + 1
file_test.close()
file_train.close()
# Generate the train.txt and test.txt
import glob, os
current_dir = 'dataset'
# Percentage of images to be used for the test set
percentage_test = 20;
# Create and/or truncate train.txt and test.txt
file_train = open('train.txt', 'w')
file_test = open('test.txt', 'w')
# Populate train.txt and test.txt
counter = 1
index_test = round(100 / percentage_test)
for pathAndFilename in glob.glob(os.path.join(current_dir, "*.*g")):
if counter == index_test:
counter = 1
file_test.write(current_dir + "/" + os.path.basename(pathAndFilename) + "\n")
else:
file_train.write(current_dir + "/" + os.path.basename(pathAndFilename) + "\n")
counter = counter + 1
file_test.close()
file_train.close()
!git clone https://github.com/AlexeyAB/darknet.git
cd /content/darknet
- Download the Makefile
- Change GPU = 1, CUDNN = 1 & OPENCV = 1
- Upload the Modified Makefile in darknet folder
!make
!./darknet
- Upload the train & test file in darknet folder
cd /content/darknet/cfg
Yolov3 Network¶
- Download the Yolov3.cfg from cfg folder
- classes and filters params of [yolo] and [convolutional] layers that are just before the [yolo] layers
- filters=(classes + 5) * 3. For a single class we should set filters= (1+5)*3 = 18
- classes = 1
- filters = 18
- Upload the Yolov3.cfg to cfg folder
Download Weight
cd /content/darknet
!wget https://pjreddie.com/media/files/darknet19_448.conv.23
!wget https://pjreddie.com/media/files/darknet19_448.conv.23
!./darknet detector train cfg/obj.data cfg/yolov3.cfg darknet19_448.conv.23 -dont_show 0
In the next blog, we will cover Deeping Learning NLP.
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-1-course.html
https://sngurukuls247.blogspot.com/2018/09/python-ninja-bootcamp-1-course.html
Feel free contact me on-
Email - sn.gurukul24.7uk@gmail.com