-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathworking_microg_yolov7_model_v2.py
307 lines (259 loc) · 9.03 KB
/
working_microg_yolov7_model_v2.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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
# -*- coding: utf-8 -*-
"""working_Microg_YoloV7_model V2.ipynb
Automatically generated by Colaboratory.
Original file is located at
https://colab.research.google.com/drive/1egxqeORZTkVPWav0EDOOWCi6SKae6R-B
#Train and Test YOLOV7
"""
!nvidia-smi
!pip install numpy==1.25.2
# Commented out IPython magic to ensure Python compatibility.
!git clone https://github.com/WongKinYiu/yolov7
# %cd yolov7
!pip install -r requirements.txt
!pip install roboflow
from roboflow import Roboflow
rf = Roboflow(api_key="j19bmHHOB6TxRaOzR8X9")
project = rf.workspace("csi-microg").project("microg-2")
version = project.version(1)
dataset = version.download("yolov7")
# Commented out IPython magic to ensure Python compatibility.
# %cd /content/yolov7
!wget "https://github.com/WongKinYiu/yolov7/releases/download/v0.1/yolov7.pt"
# Commented out IPython magic to ensure Python compatibility.
# #modify model parameters
#
# %%writefile cfg/training/yolov7.yaml
#
# # parameters
# nc: 80 # number of classes
# depth_multiple: 1.0 # model depth multiple
# width_multiple: 1.0 # layer channel multiple
#
# # anchors
# anchors:
# - [12,16, 19,36, 40,28] # P3/8
# - [36,75, 76,55, 72,146] # P4/16
# - [142,110, 192,243, 459,401] # P5/32
#
# # yolov7 backbone
# backbone:
# # [from, number, module, args]
# [[-1, 1, Conv, [32, 3, 1]], # 0
#
# [-1, 1, Conv, [64, 3, 2]], # 1-P1/2
# [-1, 1, Conv, [64, 3, 1]],
#
# [-1, 1, Conv, [128, 3, 2]], # 3-P2/4
# [-1, 1, Conv, [64, 1, 1]],
# [-2, 1, Conv, [64, 1, 1]],
# [-1, 1, Conv, [64, 3, 1]],
# [-1, 1, Conv, [64, 3, 1]],
# [-1, 1, Conv, [64, 3, 1]],
# [-1, 1, Conv, [64, 3, 1]],
# [[-1, -3, -5, -6], 1, Concat, [1]],
# [-1, 1, Conv, [256, 1, 1]], # 11
#
# [-1, 1, MP, []],
# [-1, 1, Conv, [128, 1, 1]],
# [-3, 1, Conv, [128, 1, 1]],
# [-1, 1, Conv, [128, 3, 2]],
# [[-1, -3], 1, Concat, [1]], # 16-P3/8
# [-1, 1, Conv, [128, 1, 1]],
# [-2, 1, Conv, [128, 1, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [[-1, -3, -5, -6], 1, Concat, [1]],
# [-1, 1, Conv, [512, 1, 1]], # 24
#
# [-1, 1, MP, []],
# [-1, 1, Conv, [256, 1, 1]],
# [-3, 1, Conv, [256, 1, 1]],
# [-1, 1, Conv, [256, 3, 2]],
# [[-1, -3], 1, Concat, [1]], # 29-P4/16
# [-1, 1, Conv, [256, 1, 1]],
# [-2, 1, Conv, [256, 1, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [[-1, -3, -5, -6], 1, Concat, [1]],
# [-1, 1, Conv, [1024, 1, 1]], # 37
#
# [-1, 1, MP, []],
# [-1, 1, Conv, [512, 1, 1]],
# [-3, 1, Conv, [512, 1, 1]],
# [-1, 1, Conv, [512, 3, 2]],
# [[-1, -3], 1, Concat, [1]], # 42-P5/32
# [-1, 1, Conv, [256, 1, 1]],
# [-2, 1, Conv, [256, 1, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [[-1, -3, -5, -6], 1, Concat, [1]],
# [-1, 1, Conv, [1024, 1, 1]], # 50
# ]
#
# # yolov7 head
# head:
# [[-1, 1, SPPCSPC, [512]], # 51
#
# [-1, 1, Conv, [256, 1, 1]],
# [-1, 1, nn.Upsample, [None, 2, 'nearest']],
# [37, 1, Conv, [256, 1, 1]], # route backbone P4
# [[-1, -2], 1, Concat, [1]],
#
# [-1, 1, Conv, [256, 1, 1]],
# [-2, 1, Conv, [256, 1, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
# [-1, 1, Conv, [256, 1, 1]], # 63
#
# [-1, 1, Conv, [128, 1, 1]],
# [-1, 1, nn.Upsample, [None, 2, 'nearest']],
# [24, 1, Conv, [128, 1, 1]], # route backbone P3
# [[-1, -2], 1, Concat, [1]],
#
# [-1, 1, Conv, [128, 1, 1]],
# [-2, 1, Conv, [128, 1, 1]],
# [-1, 1, Conv, [64, 3, 1]],
# [-1, 1, Conv, [64, 3, 1]],
# [-1, 1, Conv, [64, 3, 1]],
# [-1, 1, Conv, [64, 3, 1]],
# [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
# [-1, 1, Conv, [128, 1, 1]], # 75
#
# [-1, 1, MP, []],
# [-1, 1, Conv, [128, 1, 1]],
# [-3, 1, Conv, [128, 1, 1]],
# [-1, 1, Conv, [128, 3, 2]],
# [[-1, -3, 63], 1, Concat, [1]],
#
# [-1, 1, Conv, [256, 1, 1]],
# [-2, 1, Conv, [256, 1, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [-1, 1, Conv, [128, 3, 1]],
# [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
# [-1, 1, Conv, [256, 1, 1]], # 88
#
# [-1, 1, MP, []],
# [-1, 1, Conv, [256, 1, 1]],
# [-3, 1, Conv, [256, 1, 1]],
# [-1, 1, Conv, [256, 3, 2]],
# [[-1, -3, 51], 1, Concat, [1]],
#
# [-1, 1, Conv, [512, 1, 1]],
# [-2, 1, Conv, [512, 1, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [-1, 1, Conv, [256, 3, 1]],
# [[-1, -2, -3, -4, -5, -6], 1, Concat, [1]],
# [-1, 1, Conv, [512, 1, 1]], # 101
#
# [75, 1, RepConv, [256, 3, 1]],
# [88, 1, RepConv, [512, 3, 1]],
# [101, 1, RepConv, [1024, 3, 1]],
#
# [[102,103,104], 1, IDetect, [nc, anchors]], # Detect(P3, P4, P5)
# ]
# Commented out IPython magic to ensure Python compatibility.
#added adam optimizer, evolving hyperparameters per run
#100 epochs brings accuracy to 95-100% for each class
# %cd /content/yolov7/
!python train.py --batch 16 --cfg cfg/training/yolov7.yaml --epochs 50 --evolve --adam --data microg-2-1/data.yaml --weights 'yolov7.pt' --device 0
!python detect.py --weights runs/train/exp/weights/best.pt --conf 0.5 --source microg-2-1/test/images
#display inference on ALL test images
#display inference on ALL test images
import glob
from IPython.display import Image, display
i = 0
limit = 10000 # max images to print
for imageName in glob.glob('/content/yolov7/runs/detect/exp/*.jpg'): #assuming JPG
if i < limit:
display(Image(filename=imageName))
print("\n")
i = i + 1
# Commented out IPython magic to ensure Python compatibility.
# %load_ext tensorboard
# %tensorboard --logdir runs
from torch.utils.tensorboard import SummaryWriter
import numpy as np
writer = SummaryWriter()
for n_iter in range(100):
writer.add_scalar('Loss/train', np.random.random(), n_iter)
writer.add_scalar('Loss/test', np.random.random(), n_iter)
writer.add_scalar('Accuracy/train', np.random.random(), n_iter)
writer.add_scalar('Accuracy/test', np.random.random(), n_iter)
#save the weights so you can load the model later
#model.load_state_dict(torch.load(path))
from google.colab import files
files.download("/content/yolov7/runs/train/exp/weights/best.pt")
# Copy best.pt to Google Drive
from google.colab import drive
drive.mount('/content/drive')
import shutil
local_best_pt_path = '/content/yolov7/runs/train/exp/weights/best.pt'
drive_destination_path = '/content/drive/My Drive/2023/24 Micro-g NExT/Micro-g YOLO Model Workspace/results'
shutil.copy(local_best_pt_path, drive_destination_path)
drive.flush_and_unmount()
"""#Accessing trained model to run on life camera feed"""
#Once the model has been trained, here's how to access it later
from models.yolov7 import YOLOv7
import torch
# load model frameworrk
model = YOLOv7("cfg/training/yolov7.yaml")
# Load the trained weights from best.pt. only for testing, at the competition, best.pt will be in the same directory
weights_path = '/content/drive/My Drive/2023/24 Micro-g NExT/Micro-g YOLO Model Workspace/results/best.pt'
model.load_state_dict(torch.load(weights_path))
model.eval()
# Access the computer camera feed -> access Jetson Nano later
cap = cv2.VideoCapture(0)
output_dir = 'runs/train/exp/live_feed'
os.makedirs(output_dir, exist_ok=True)
#reads all of the annotated images from the first captured frame and save it in a local directory.
#The GUI should then read this directory and render the images based on precendence (read the class name(s) from file path).
# Read only first frame
ret, frame = cap.read()
# Perform object detection on the first frame
detections = model(frame)
life_ring_count = 0
lpu_count = 0
life_raft_count = 0
orion_count = 0
bbox_color = (255,255,255)
img_no = 0;
for detection in detections:
class_label = detection['class']
if class_label == 'life-ring':
life_ring_count++
bbox_text = f"{class_label} {life_ring_count}"
elif class_label == 'LPU':
lpu_count++
bbox_text = f"{class_label} {lpu_count}"
elif class_label == 'life-raft':
life_raft_count++
bbox_text = f"{class_label} {life_raft_count}"
elif class_label == 'orion':
orion_count++
bbox_text = f"{class_label} {orion_count}"
else:
continue
#draw boundeing box around the object of interest with a class label
x, y, w, h = detection['bbox']
x1, y1, x2, y2 = int(x - w / 2), int(y - h / 2), int(x + w / 2), int(y + h / 2)
cv2.rectangle(frame, (x1, y1), (x2, y2), bbox_color, 2)
cv2.putText(frame, bbox_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, bbox_color, 2)
file_name = f"annotated_frame_{class_label}_{img_no}.jpg"
file_path = os.path.join(output_dir, file_name)
cv2.imwrite(file_path, frame)
cap.release()
cv2.destroyAllWindows()