-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwildlife.py
113 lines (77 loc) · 4.38 KB
/
wildlife.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
# -*- coding: utf-8 -*-
"""Wildlife.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1-p1w5Loe-lkTA4NU9wlKpgXfBb91UIxj
"""
!pip install onnx
!pip install torch
"""## Wildlife Tracker
**Overview**
Human wildlife conflict has been on the rise esecially during on drought seasons and this poses a major risk for communities living near game parks and reserves. Providing real time tracking of such wild animals especially elephants will be of great help to the body tasked with managing and controlling the movement of such animals as well as provide quick and swift response to the affected communities to prevent loss of life and property.
**Objectives**
Build an object detection model that provides real-time tracking of wild animals, especially elephants, zebras, buffallos and Rhinos. This tracking system aims to enhance the management and control of animal movement, as well as provide timely response to affected communities to prevent loss of life and property.
"""
!pip install ultralytics
"""## **Model training**
To train the **YOLO** model, we have to connect it to our drive to access the data especially the docker file that has the necessary information needed to train the model. So as not to run into problems when declaring the path, using the root directory comes in handy rather than using the relative path.
"""
from google.colab import drive
drive.mount('/content/gdrive')
ROOT_DIR = '/content/gdrive/My Drive/Computer vision/'
from ultralytics import YOLO
import os
import torch
def object_detection_model(model_config, data_config, epochs):
# Initialize YOLO model
model = YOLO(model_config)
# Train the model
model.train(data=data_config, epochs=epochs)
# Validate model performance
metrics = model.val()
# Export model to ONNX format
# path = model.export(format="onnx")
# # Save trained model
# torch.save(model.state_dict(), save_model_path)
return metrics
# Directory for saving the runs
#runs_dir = "/content/gdrive/My Drive/Computer vision/runs"
# Data configuration file
data_config = os.path.join(ROOT_DIR, "data_wl.yaml")
# Model configuration file
model_config = "yolov9c.yaml"
# Number of epochs
epochs = 7
# Path to save trained model
save_model_path = "/content/gdrive/My Drive/Computer vision/wildlife1_vision.pth"
# Train the model
metrics = object_detection_model(model_config, data_config, epochs)
# Data configuration file
data_config = os.path.join(ROOT_DIR, "data_wl.yaml")
# Model configuration file
model_config = "yolov9c.yaml"
# Number of epochs
epochs = 15
# # Path to save trained model
# save_model_path = "/content/gdrive/My Drive/Computer vision/wildlife2_vision.pth"
# Train the model
metrics = object_detection_model(model_config, data_config, epochs)
# Data configuration file
data_config = os.path.join(ROOT_DIR, "data_wl.yaml")
# Model configuration file
model_config = "yolov9c.yaml"
# Number of epochs
epochs = 25
# # Path to save trained model
# save_model_path = "/content/gdrive/My Drive/Computer vision/wildlife3_vision.pth"
# Train the model
metrics = object_detection_model(model_config, data_config, epochs)
!scp -r /content/runs '/content/gdrive/My Drive/Computer vision/'
"""Training teh model on a very high number of epochs has the downside of overfitting. This means very good performance on the training data but very poor performance on the testing data. This is evident from the drop of the precision scores in the training and test sets.
To improve the recall score of the model, we seek an optimum number of epochs between 25 and 30. An improved recall means a better object detection model for identifying animals present in the picture.
"""
#Saving the results to the project direcory
!scp -r /content/runs '/content/gdrive/My Drive/Computer vision/'
"""Training the model on high number of epochs guarantees a better precision and recall for all the classes to be identified. This is because increasing the number of epochs allows the model more opportunities to learn complex patterns in the data. Some patterns may require multiple passes through the data to be properly understood and learned by the model.
A high number of epochs is not reccommended though because it often leads to overfitting. Overfitting occurs when the model performs very well on training data but very poorly on new data especially test data or when put into production.
"""