Skip to content

Commit

Permalink
Merge pull request #6 from Lukasz825700516/lukasz-train_model
Browse files Browse the repository at this point in the history
Model training
  • Loading branch information
Lukasz825700516 authored Apr 5, 2024
2 parents 535dc14 + e7260be commit 3ae7e4d
Show file tree
Hide file tree
Showing 25 changed files with 290 additions and 0 deletions.
13 changes: 13 additions & 0 deletions Datasets/data.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
names:
- Eldery
- Adult
- Child
- Wheelchair
- Blind
- Suitcase
nc: 6
path: ../KPZ-2024-traffic_lights/Datasets
test: test/images
train: train/images
val: valid/images

Binary file added last_run/F1_curve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/PR_curve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/P_curve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/R_curve.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
106 changes: 106 additions & 0 deletions last_run/args.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
task: detect
mode: train
model: yolov8n.pt
data: Datasets/data.yaml
epochs: 3
time: null
patience: 100
batch: 16
imgsz: 640
save: true
save_period: -1
cache: false
device: null
workers: 8
project: null
name: train9
exist_ok: false
pretrained: true
optimizer: auto
verbose: true
seed: 0
deterministic: true
single_cls: false
rect: false
cos_lr: false
close_mosaic: 10
resume: false
amp: true
fraction: 1.0
profile: false
freeze: null
multi_scale: false
overlap_mask: true
mask_ratio: 4
dropout: 0.0
val: true
split: val
save_json: false
save_hybrid: false
conf: null
iou: 0.7
max_det: 300
half: false
dnn: false
plots: true
source: null
vid_stride: 1
stream_buffer: false
visualize: false
augment: false
agnostic_nms: false
classes: null
retina_masks: false
embed: null
show: false
save_frames: false
save_txt: false
save_conf: false
save_crop: false
show_labels: true
show_conf: true
show_boxes: true
line_width: null
format: torchscript
keras: false
optimize: false
int8: false
dynamic: false
simplify: false
opset: null
workspace: 4
nms: false
lr0: 0.01
lrf: 0.01
momentum: 0.937
weight_decay: 0.0005
warmup_epochs: 3.0
warmup_momentum: 0.8
warmup_bias_lr: 0.1
box: 7.5
cls: 0.5
dfl: 1.5
pose: 12.0
kobj: 1.0
label_smoothing: 0.0
nbs: 64
hsv_h: 0.015
hsv_s: 0.7
hsv_v: 0.4
degrees: 0.0
translate: 0.1
scale: 0.5
shear: 0.0
perspective: 0.0
flipud: 0.0
fliplr: 0.5
bgr: 0.0
mosaic: 1.0
mixup: 0.0
copy_paste: 0.0
auto_augment: randaugment
erasing: 0.4
crop_fraction: 1.0
cfg: null
tracker: botsort.yaml
save_dir: /var/home/me/Projects/KPZ-2024-traffic_lights/runs/detect/train9
Binary file added last_run/confusion_matrix.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/confusion_matrix_normalized.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/labels.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/labels_correlogram.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 4 additions & 0 deletions last_run/results.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
epoch, train/box_loss, train/cls_loss, train/dfl_loss, metrics/precision(B), metrics/recall(B), metrics/mAP50(B), metrics/mAP50-95(B), val/box_loss, val/cls_loss, val/dfl_loss, lr/pg0, lr/pg1, lr/pg2
1, 1.699, 1.9112, 1.2726, 0.73916, 0.53425, 0.635, 0.32422, 1.3878, 1.3308, 1.3559, 0.00033298, 0.00033298, 0.00033298
2, 1.5975, 1.2346, 1.2225, 0.76788, 0.609, 0.69417, 0.39321, 1.3002, 0.90596, 1.2696, 0.00044643, 0.00044643, 0.00044643
3, 1.5252, 1.0531, 1.1834, 0.77691, 0.69035, 0.77057, 0.45187, 1.2254, 0.78682, 1.2173, 0.00033988, 0.00033988, 0.00033988
Binary file added last_run/results.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/train_batch0.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/train_batch1.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/train_batch2.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/val_batch0_labels.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/val_batch0_pred.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/val_batch1_labels.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/val_batch1_pred.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/val_batch2_labels.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/val_batch2_pred.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added last_run/weights/best.pt
Binary file not shown.
Binary file added last_run/weights/last.pt
Binary file not shown.
29 changes: 29 additions & 0 deletions report/20-training.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# Training

For training four datasets have been used

- Child Elderly Adult dataset
https://universe.roboflow.com/gist-awllb/dl-bhh3b

- Blind dataset
https://www.kaggle.com/datasets/jangbyeonghui/visually-impairedwhitecane

- Suitcase dataset
https://www.kaggle.com/datasets/dataclusterlabs/suitcaseluggage-dataset

- Wheelchair dataset
https://universe.roboflow.com/obj-detection-gmggm/objectdetect-iga7u

Before usage those datasets have been merged into one large dataset with
label class space spanning all unique classes from original datasets.


The trained model was based on the YOLOv8 architecture using the cli tool
from the `urtlanalisis` python package, and the results of training were saved in
the `last_run` directory.


![confiusion graph](./last_run/confusion/matrix_notmalized.png "Confusion graph of trained model")

Above confusion matrix presents the results of model training.

138 changes: 138 additions & 0 deletions scripts/yolop_zebra_crossing.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,36 @@
"""Script with configuration of model that will be used in the project"""

import os
import time
import tempfile
import itertools
import typing
import dotenv

import PIL
import torch
from torch import nn
import torchvision
from torchvision.transforms import v2 as transforms_v2
from torchvision import datasets
import ultralytics

import roboflow

dotenv.load_dotenv()

rf = roboflow.Roboflow(api_key=os.environ['ROBOFLOW_KEY'])

project = rf.workspace("obj-detection-gmggm").project("objectdetect-iga7u")
version = project.version(4)
datasetWheelchair = version.download("yolov8")

project = rf.workspace("gist-awllb").project("dl-bhh3b")
version = project.version(4)
datasetChildrenElderyAdults = version.download("yolov8")


BATCH_SIZE=32

device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

Expand All @@ -17,4 +46,113 @@
for k, v in model.named_parameters():
v.requires_grad = False

PHASE_TRAIN = 'train'
PHASE_TEST = 'test'
phases = [PHASE_TRAIN, PHASE_TEST]


class NamespaceStuff:
def __init__(self):
self.imgsz = 640
self.fraction = 0.4
self.scale = 1
self.fliplr = False
self.flipud = False
self.cache = True
self.auto_augument = True
self.hsv_h = False
self.hsv_s = False
self.hsv_v = False
self.crop_fraction = 0.1

dataset = ultralytics.data.dataset.ClassificationDataset('Datasets', NamespaceStuff())
dataset_loader = torch.utils.data.DataLoader(dataset)


def train_model(
model,
criterion,
optimizer: torch.optim.Optimizer,
scheduler: torch.optim.lr_scheduler.LRScheduler,
num_epochs=25
):
"""Function provided in
https://pytorch.org/tutorials/beginner/transfer_learning_tutorial.html"""


since = time.time()

# Create a temporary directory to save training checkpoints
with tempfile.TemporaryDirectory() as tempdir:
best_model_params_path = os.path.join(tempdir, 'best_model_params.pt')

torch.save(model.state_dict(), best_model_params_path)
best_acc = 0.0

for epoch in range(num_epochs):
print(f'Epoch {epoch}/{num_epochs - 1}')
print('-' * 10)

# Each epoch has a training and validation phase
for phase in phases:
if phase == PHASE_TRAIN:
model.train() # Set model to training mode
else:
model.eval() # Set model to evaluate mode

running_loss = 0.0
running_corrects = 0

# Iterate over data.
for inputs, labels in dataset_loader:
inputs = inputs.to(device)
labels = labels.to(device)

# zero the parameter gradients
optimizer.zero_grad()

# forward
# track history if only in train
with torch.set_grad_enabled(phase == PHASE_TRAIN):
outputs = model(inputs)
_, preds = torch.max(outputs, 1)
loss = criterion(outputs, labels)

# backward + optimize only if in training phase
if phase == PHASE_TRAIN:
loss.backward()
optimizer.step()

# statistics
running_loss += loss.item() * inputs.size(0)
running_corrects += torch.sum(preds == labels.data)
if phase == 'train':
scheduler.step()

epoch_loss = running_loss / len(dataset_loader.imgs)
epoch_acc = float(running_corrects) / len(dataset_loader.imgs)

print(f'{phase} Loss: {epoch_loss:.4f} Acc: {epoch_acc:.4f}')

# deep copy the model
if phase == PHASE_TEST and epoch_acc > best_acc:
best_acc = epoch_acc
torch.save(model.state_dict(), best_model_params_path)

print()

time_elapsed = time.time() - since
print(f'Training complete in {time_elapsed // 60:.0f}m {time_elapsed % 60:.0f}s')
print(f'Best val Acc: {best_acc:4f}')

# load best model weights
model.load_state_dict(torch.load(best_model_params_path))
return model

criterion = nn.CrossEntropyLoss()
optimizer = torch.optim.SGD(model.parameters(), lr=0.001, momentum=0.9)
scheduler = torch.optim.lr_scheduler.StepLR(optimizer, step_size=7, gamma=0.1)

train_model(model, criterion, optimizer, scheduler)

# ...

0 comments on commit 3ae7e4d

Please sign in to comment.