<jupyter_start><jupyter_text>![Image.png](data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAuEAAAG4CAIAAABkS65nAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAJcEhZcwAADsMAAA7DAcdvqGQAAP+lSURBVHhe7L2HmxzHcbj9+zc+4W737gACzEFiULIkKssKVKRsS7Yly5ZlBUuyLNmWRAKXc9zLd8g555wBEiSInHPOOUcC4FfdNVNTUz1p9/YCiJ3nfe6Z7akOu5BdL3t6ev7fmzn5ERmcW5ACQ/IK/cgHcj0oyCuKQmH/YsQsKcwrLjIo7l8CiEJOYW6RoCCnUJAfLxgSAQgLZkgs34/B2UMIcclksAbPqXEYZyEQL0DyWTzBe1EdZQ+BMKRAU6goKHLITysFxfFCP0pyioIpzS2Gv35VWGFhWU5heW6RSUVeMSLK/aB4P0S8HzCeUErjBcGIeG/iheXpIT+UstiQCEBYQQoY4wkhYkUKA0qz8wleLslWlKeA0bsvMYsKILuwUlNFxDT2x+pYEVLTNWrjxQIRAFRDd71EVXZBVXZ+VSxf/c3Oryay8mttavoNqfnImzUfeaO235t+1GUNDqVWMYSa9QECZMUUEMNDRIxNvkFhXT9OEaf2I4UGBVixPrvAjzr4XvYA6rOHAA2x/BTAuqGwb+dNfdaQBk0SjoIIBQlFeAnHdBSyjehwR+GgZHAFQUcJMBVSEw4XFIA8oIsIY+C41MG4KlCOEs8fEncJCgCOUmRriukovAskP3sIqoklKF<KEY>//<KEY>)  Dhaka Traffic Detection using YOLOv5This notebook contains Ultralytics inference and training code for YOLOv5 in PyTorch.<jupyter_code>from google.colab import drive
drive.mount('/content/drive')
!cp /content/drive/My\ Drive/yolov3/competition/obj.zip /content/
!unzip /content/obj.zip; rm /content/obj.zip;<jupyter_output><empty_output><jupyter_text>Create folder for train-validation<jupyter_code>!mkdir data
!mkdir data/images
!mkdir data/labels
!mkdir data/images/train
!mkdir data/images/valid
!mkdir data/labels/train
!mkdir data/labels/valid
import glob, os
import random
# put your own path here
dataset_path = '/content/obj'
# Percentage of images to be used for the validation set
percentage_test = 20

# Populate the folders
p = percentage_test/100
for pathAndFilename in glob.iglob(os.path.join(dataset_path, "*.jpg")):
    title, ext = os.path.splitext(os.path.basename(pathAndFilename))
    if random.random() <=p :
        os.system(f"cp {dataset_path}/{title}.jpg data/images/valid")
        os.system(f"cp {dataset_path}/{title}.txt data/labels/valid")
    else:
        os.system(f"cp {dataset_path}/{title}.jpg data/images/train")
        os.system(f"cp {dataset_path}/{title}.txt data/labels/train")<jupyter_output><empty_output><jupyter_text>Create data.yaml file<jupyter_code># sample of yaml look like
'''
train: training/data/images/train/
val: training/data/images/valid/

# number of classes
nc: 2
# class names
names: ['Cricketball', 'Football']
'''
# clone yolov5 repo
%cd /content
!git clone https://github.com/ultralytics/yolov5.git
%cat /content/yolov5/models/yolov5s.yaml
# write data.yaml file \\ later move into another folder
%%writefile /content/data.yaml

train: training/data/images/train/
val: training/data/images/valid/

# number of classes
nc: 21
# class names
names: ['ambulance',
'auto rickshaw',
'bicycle',
'bus',
'car',
'garbagevan',
'human hauler',
'minibus',
'minivan',
'motorbike',
'pickup',
'army vehicle',
'policecar',
'rickshaw',
'scooter',
'suv',
'taxi',
'three wheelers (CNG)',
'truck', 
'van',
'wheelbarrow']
# write custom_yolov5s.yaml 
%%writefile /content/yolov5/models/custom_yolov5s.yaml

# parameters
nc: 21  # number of classes
depth_multiple: 0.33  # model depth multiple
width_multiple: 0.50  # layer channel multiple

# anchors
anchors:
  - [10,13, 16,30, 33,23]  # P3/8
  - [30,61, 62,45, 59,119]  # P4/16
  - [116,90, 156,198, 373,326]  # P5/32

# YOLOv5 backbone
backbone:
  # [from, number, module, args]
  [[-1, 1, Focus, [64, 3]],  # 0-P1/2
   [-1, 1, Conv, [128, 3, 2]],  # 1-P2/4
   [-1, 3, BottleneckCSP, [128]],
   [-1, 1, Conv, [256, 3, 2]],  # 3-P3/8
   [-1, 9, BottleneckCSP, [256]],
   [-1, 1, Conv, [512, 3, 2]],  # 5-P4/16
   [-1, 9, BottleneckCSP, [512]],
   [-1, 1, Conv, [1024, 3, 2]],  # 7-P5/32
   [-1, 1, SPP, [1024, [5, 9, 13]]],
   [-1, 3, BottleneckCSP, [1024, False]],  # 9
  ]

# YOLOv5 head
head:
  [[-1, 1, Conv, [512, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 6], 1, Concat, [1]],  # cat backbone P4
   [-1, 3, BottleneckCSP, [512, False]],  # 13

   [-1, 1, Conv, [256, 1, 1]],
   [-1, 1, nn.Upsample, [None, 2, 'nearest']],
   [[-1, 4], 1, Concat, [1]],  # cat backbone P3
   [-1, 3, BottleneckCSP, [256, False]],  # 17 (P3/8-small)

   [-1, 1, Conv, [256, 3, 2]],
   [[-1, 14], 1, Concat, [1]],  # cat head P4
   [-1, 3, BottleneckCSP, [512, False]],  # 20 (P4/16-medium)

   [-1, 1, Conv, [512, 3, 2]],
   [[-1, 10], 1, Concat, [1]],  # cat head P5
   [-1, 3, BottleneckCSP, [1024, False]],  # 23 (P5/32-large)

   [[17, 20, 23], 1, Detect, [nc, anchors]],  # Detect(P3, P4, P5)
  ]
!pip install -U PyYAML # otherwise gives an erorr 
!python train.py --img 640 --batch 16 --epochs 300 --data '/content/yolov5/data/data.yaml' --cfg '/content/yolov5/models/custom_yolov5s.yaml' --weights '' --device 0
!python test.py --weights runs/exp0_yolov5s_results/weights/best.pt --data '../data.yaml'  --img 420 --augment
!python detect.py --weights runs/exp0_yolov5s_results/weights/best.pt --img 416 --conf 0.4 --source ../test/images<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1618155912884185, 'fraction_numerical': 0.07592102584978629, 'mean_word_length': 4.622425629290618, 'pattern_counts': {'":': 0, '<': 16, '<?xml version=': 0, '>': 15, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '343044', 'license': 'MIT', 'n_tokens_mistral': 2479, 'n_tokens_neox': 2199, 'n_words': 427, 'path': 'yolov5/Dhaka_Traffic_Detection_Starter_Notebook.ipynb', 'repo_name': 'whoafridi/Dhaka-Ai-Traffic-Detection-Challenge', 'size': '153755'}