Making a license plate AI model Part 2
My previous attempt didn’t quite give me the results I was hoping for, so I looked for another way to train the model.
I still use Label Studio to make the dataset, the only thing that changed here is the export. I now use YOLO
instead of Pascal
.
These are my new files:
from ultralytics import YOLO
import datetime
import os
# Function to generate a timestamped name
def generate_experiment_name():
return "train_" + datetime.datetime.now().strftime("%Y-%m-%d_%I:%M%p")
# Load a pretrained YOLO model (recommended for training)
model = YOLO('yolov8n.pt')
# Set project details
project_name = 'license_plate_project'
project_path = f'models/{project_name}'
current_experiment_name = generate_experiment_name()
# Train the model using the 'licensePlate.yaml' dataset for 400 epochs
results = model.train(
data='licensePlate.yaml',
epochs=800,
resume=False,
lr0=0.0004,
project=project_path,
name=current_experiment_name,
batch=-1, # Automatically set based on available resources in YOLO library.
save_period=50, # Save checkpoints every 50 steps (if supported by the library)
degrees=180,
shear=180,
perspective=0.001,
fliplr=0.5,
cache=True,
cos_lr=True,
val=False,
)
# Export the final trained model to ONNX format and TFLite format
success_onnx = model.export(format='onnx')
# # Perform object detection on an image using the final trained model
# detection_results = model('../dataset/licensePlate/pascal_data/images/eed254b9-frame_1714924030.jpg', save=True)
import datetime
import os
from pathlib import Path
from ultralytics import YOLO
script_dir = Path(os.path.dirname(os.path.realpath(__file__)))
project_name = 'license_plate_project'
project_path = script_dir / 'models' / project_name
# Find the newest folder in models/license_plate_project/ that starts with a date
model_folders = [f for f in project_path.glob('*') if f.is_dir()]
newest_folder = max(model_folders, key=os.path.getctime)
# Load a pretrained YOLO model (recommended for training)
model = YOLO("/home/yuki/source/repos/license_plate_test/ultralytics/models/license_plate_project/train_2024-06-01_01:56PM2/" + 'weights/best.onnx')
# Perform object detection on an image using the model
results = model.predict('PARKEREN_Opladen_elektrische_autos_in_de_rij_2.jpg', save=True, project=project_path)
# Train/val/test sets as 1) dir: path/to/imgs, 2) file: path/to/imgs.txt, or 3) list: [path/to/imgs1, path/to/imgs2, ..]
path: /home/yuki/source/repos/license_plate_test/dataset/licensePlate/yolo/
train: images
val: images
# Classes
names:
0: car
1: licensePlate
Filed under: AI,Object Recognition - @ June 1, 2024 1:44 pm