Skip to content

Commit

Permalink
Add accuracy metrics as iou default
Browse files Browse the repository at this point in the history
  • Loading branch information
kshitijrajsharma committed Nov 5, 2024
1 parent 6f43ce7 commit 0936a78
Show file tree
Hide file tree
Showing 5 changed files with 71 additions and 12 deletions.
11 changes: 9 additions & 2 deletions hot_fair_utilities/training/yolo_v8_v1/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

# Reader imports
from hot_fair_utilities.model.yolo import YOLOSegWithPosWeight

from ...utils import compute_iou_chart_from_yolo_results, get_yolo_iou_metrics
# Get environment variables with fallbacks
ROOT = Path(os.getenv("YOLO_ROOT", Path(__file__).parent.absolute()))
DATA_ROOT = str(Path(os.getenv("YOLO_DATA_ROOT", ROOT / "yolo-training")))
Expand Down Expand Up @@ -121,7 +121,14 @@ def train(
device=[int(i) for i in gpu.split(",")] if "," in gpu else gpu,
**kwargs,
)
return weights
compute_iou_chart_from_yolo_results(results_csv_path=os.path.join(output_path,"checkpoints", name,'results.csv'),results_output_chart_path=os.path.join(output_path,"checkpoints", name,'iou_chart.png'))

output_model_path=os.path.join(os.path.join(output_path,"checkpoints"), name, "weights", "best.pt")

iou_model_accuracy=get_yolo_iou_metrics(output_model_path)

return output_model_path,iou_model_accuracy



def check4checkpoint(name, weights,output_path):
Expand Down
10 changes: 8 additions & 2 deletions hot_fair_utilities/training/yolo_v8_v2/train.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Third party imports
import torch
import ultralytics

from ...utils import get_yolo_iou_metrics,compute_iou_chart_from_yolo_results
# Reader imports
from hot_fair_utilities.model.yolo import YOLOSegWithPosWeight

Expand Down Expand Up @@ -86,7 +86,13 @@ def train(data, weights, epochs, batch_size, pc, output_path, dataset_yaml_path,

# metrics = model.val(save_json=True, plots=True)
# print(model.val())
return os.path.join(os.path.join(output_path,"checkpoints"), name, "weights", "best.pt")
compute_iou_chart_from_yolo_results(results_csv_path=os.path.join(output_path,"checkpoints", name,'results.csv'),results_output_chart_path=os.path.join(output_path,"checkpoints", name,'iou_chart.png'))

output_model_path=os.path.join(os.path.join(output_path,"checkpoints"), name, "weights", "best.pt")

iou_model_accuracy=get_yolo_iou_metrics(output_model_path)

return output_model_path,iou_model_accuracy


def check4checkpoint(name, weights,output_path):
Expand Down
47 changes: 46 additions & 1 deletion hot_fair_utilities/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,15 @@
import math
import os
import re
import ultralytics

import time
import urllib.request
import zipfile
from glob import glob
from typing import Tuple

import pandas as pd
import matplotlib.pyplot as plt
# Third party imports
# Third-party imports
import geopandas
Expand Down Expand Up @@ -251,3 +254,45 @@ def fetch_osm_data(payload: json, API_URL="https://api-prod.raw-data.hotosm.org/
with zip_ref.open("Export.geojson") as file:
my_export_geojson = json.loads(file.read())
return my_export_geojson


import pandas as pd
import matplotlib.pyplot as plt


def compute_iou_chart_from_yolo_results(results_csv_path,results_output_chart_path):

data = pd.read_csv(results_csv_path)


data['IoU(M)'] = 1 / (
1 / data['metrics/precision(M)'] + 1 / data['metrics/recall(M)'] - 1
)

plt.figure(figsize=(10, 5))
plt.plot(data['epoch'], data['IoU(M)'], label='IoU (Mask)')
plt.xlabel('Epoch')
plt.xticks(data['epoch'].astype(int))
plt.ylabel('IoU')
plt.title('IoU over Epochs')
plt.legend()
plt.grid()

plt.savefig(results_output_chart_path)
return results_output_chart_path


def get_yolo_iou_metrics(model_path):

model_val = ultralytics.YOLO(model_path)
model_val_metrics = (
model_val.val().results_dict
) ### B and M denotes bounding box and mask respectively
# print(metrics)
iou_accuracy = 1 / (
1 / model_val_metrics["metrics/precision(M)"]
+ 1 / model_val_metrics["metrics/recall(M)"]
- 1
) # ref here https://github.com/ultralytics/ultralytics/issues/9984#issuecomment-2422551315
final_accuracy = iou_accuracy * 100
return final_accuracy
5 changes: 3 additions & 2 deletions test_yolo_v1.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,17 +53,18 @@ def __exit__(self, type, value, traceback):
p_val=0.05,
)

output_model_path = train_yolo(
output_model_path,output_model_iou_accuracy = train_yolo(
data=f"{base_path}",
weights=f"{os.getcwd()}/yolov8s_v1-seg-best.pt",
gpu="cpu",
# gpu="cpu",
epochs=2,
batch_size=16,
pc=2.0,
output_path=yolo_data_dir,
dataset_yaml_path=os.path.join(yolo_data_dir,'yolo_dataset.yaml')

)
print(output_model_iou_accuracy)

prediction_output = f"{base_path}/prediction/output"
# model_path = f"{output_path}/weights/best.pt"
Expand Down
10 changes: 5 additions & 5 deletions test_yolo_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
import os
import time
import warnings
import ultralytics

os.environ.update(os.environ)
os.environ["RAMP_HOME"] = os.getcwd()
Expand Down Expand Up @@ -50,18 +51,17 @@ def __exit__(self, type, value, traceback):
output_path=yolo_data_dir,
)

output_model_path = train_yolo(
output_model_path,output_model_iou_accuracy = train_yolo(
data=f"{base_path}",
weights=f"{os.getcwd()}/yolov8s_v2-seg.pt", ## Todo : replace with finetuned ramp model checkpoint
gpu="cpu",
weights=f"{os.getcwd()}/yolov8s_v2-seg.pt",
# gpu="cpu",
epochs=2,
batch_size=16,
pc=2.0,
output_path=yolo_data_dir,
dataset_yaml_path=os.path.join(yolo_data_dir,'yolo_dataset.yaml')
)

print(output_model_path)
print(output_model_iou_accuracy)

prediction_output = f"{base_path}/prediction/output"
# model_path = f"{output_path}/weights/best.pt"
Expand Down

0 comments on commit 0936a78

Please sign in to comment.