This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
! python train.py --data dataRoad.yaml --cfg yolov5s.yaml --batch-size 32 --epochs 1 --name RoadTrainModel |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from pathlib import Path | |
path = Path(os.getcwd()) | |
parent_path=path.parent.absolute() | |
final_data_path=os.path.join(parent_path,"data","finalRoad") | |
print(final_data_path) | |
yaml_data="train: "+final_data_path+"/images/train\n" | |
yaml_data+="val: "+final_data_path+"/images/val\n" | |
yaml_data+="test: "+final_data_path+"/images/test\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
print("Number of train images = ",len(os.listdir("data/finalRoad/images/train/"))) | |
print("Number of train annotations = ",len(os.listdir("data/finalRoad/labels/train/"))) | |
print("Number of val images = ",len(os.listdir("data/finalRoad/images/val/"))) | |
print("Number of val annotations = ",len(os.listdir("data/finalRoad/labels/val/"))) | |
print("Number of test images = ",len(os.listdir("data/finalRoad/images/test/"))) | |
print("Number of test annotations = ",len(os.listdir("data/finalRoad/labels/test/"))) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os | |
! git clone https://github.com/ultralytics/yolov5 | |
os.chdir(r"yolov5") | |
! pip install -r requirements.txt |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Read images and annotations | |
images = [os.path.join('data/assortedFiles/images', x) for x in os.listdir('data/assortedFiles/images')] | |
annotations = [os.path.join('data/assortedFiles/annotations', x) for x in os.listdir('data/assortedFiles/annotations') if x[-3:] == "txt"] | |
images.sort() | |
annotations.sort() | |
print("Number of images",len(images),"\nNumber of annotation files",len(annotations)) | |
# Split the dataset into train-valid-test splits |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def plot_bounding_box(image, annotation_list): | |
annotations = np.array(annotation_list) | |
w, h = image.size | |
plotted_image = ImageDraw.Draw(image) | |
transformed_annotations = np.copy(annotations) | |
transformed_annotations[:,[1,3]] = annotations[:,[1,3]] * w | |
transformed_annotations[:,[2,4]] = annotations[:,[2,4]] * h |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Function to get the data from XML Annotation | |
def extract_info_from_xml(xml_file): | |
root = ET.parse(xml_file).getroot() | |
# Initialise the info dict | |
info_dict = {} | |
info_dict['bboxes'] = [] | |
# Parse the XML Tree | |
for elem in root: |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
input_path = Path("data/RoadDamageDataset") | |
output_path = Path("data/assortedFiles") | |
try: | |
if not os.path.exists(output_path): | |
os.mkdir(output_path) | |
if not os.path.exists(output_path/'images'): | |
os.mkdir(output_path/'images') | |
if not os.path.exists(output_path/'labels'): | |
os.mkdir(output_path/'labels') |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fname="data/RoadDamageDataset.tar.gz" | |
if fname.endswith("tar.gz"): | |
tar = tarfile.open(fname, "r:gz") | |
tar.extractall("data/") | |
tar.close() | |
elif fname.endswith("tar"): | |
tar = tarfile.open(fname, "r:") | |
tar.extractall("data/") | |
tar.close() |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import tarfile | |
import os | |
from pathlib import Path | |
import shutil | |
import xml.etree.ElementTree as ET | |
from PIL import Image, ImageDraw | |
import numpy as np | |
import matplotlib.pyplot as plt | |
import random | |
from sklearn.model_selection import train_test_split |