lb-desupervised
Lewis Belcher
commited on
Slightly modify CLI execution (#3687)
Browse files* Slightly modify CLI execution
This simple change makes it easier to run the primary functions of this
repo (train/detect/test) from within Python. An object which represents
`opt` can be constructed and fed to the `main` function of each of these
modules, rather than having to call the lower level functions directly,
or run the module as a script.
* Update export.py
Add CLI parsing update for more convenient module usage within Python.
Co-authored-by: Lewis Belcher <[email protected]>
detect.py
CHANGED
@@ -172,7 +172,7 @@ def detect(weights='yolov5s.pt', # model.pt path(s)
|
|
172 |
print(f'Done. ({time.time() - t0:.3f}s)')
|
173 |
|
174 |
|
175 |
-
|
176 |
parser = argparse.ArgumentParser()
|
177 |
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
|
178 |
parser.add_argument('--source', type=str, default='data/images', help='file/dir/URL/glob, 0 for webcam')
|
@@ -198,7 +198,15 @@ if __name__ == '__main__':
|
|
198 |
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
|
199 |
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
|
200 |
opt = parser.parse_args()
|
|
|
|
|
|
|
|
|
201 |
print(opt)
|
202 |
check_requirements(exclude=('tensorboard', 'thop'))
|
203 |
-
|
204 |
detect(**vars(opt))
|
|
|
|
|
|
|
|
|
|
|
|
172 |
print(f'Done. ({time.time() - t0:.3f}s)')
|
173 |
|
174 |
|
175 |
+
def parse_opt():
|
176 |
parser = argparse.ArgumentParser()
|
177 |
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
|
178 |
parser.add_argument('--source', type=str, default='data/images', help='file/dir/URL/glob, 0 for webcam')
|
|
|
198 |
parser.add_argument('--hide-conf', default=False, action='store_true', help='hide confidences')
|
199 |
parser.add_argument('--half', action='store_true', help='use FP16 half-precision inference')
|
200 |
opt = parser.parse_args()
|
201 |
+
return opt
|
202 |
+
|
203 |
+
|
204 |
+
def main(opt):
|
205 |
print(opt)
|
206 |
check_requirements(exclude=('tensorboard', 'thop'))
|
|
|
207 |
detect(**vars(opt))
|
208 |
+
|
209 |
+
|
210 |
+
if __name__ == "__main__":
|
211 |
+
opt = parse_opt()
|
212 |
+
main(opt)
|
models/export.py
CHANGED
@@ -144,7 +144,7 @@ def export(weights='./yolov5s.pt', # weights path
|
|
144 |
print(f'\nExport complete ({time.time() - t:.2f}s). Visualize with https://github.com/lutzroeder/netron.')
|
145 |
|
146 |
|
147 |
-
|
148 |
parser = argparse.ArgumentParser()
|
149 |
parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path')
|
150 |
parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image (height, width)')
|
@@ -159,7 +159,15 @@ if __name__ == '__main__':
|
|
159 |
parser.add_argument('--simplify', action='store_true', help='ONNX: simplify model')
|
160 |
parser.add_argument('--opset-version', type=int, default=12, help='ONNX: opset version')
|
161 |
opt = parser.parse_args()
|
|
|
|
|
|
|
|
|
162 |
print(opt)
|
163 |
set_logging()
|
164 |
-
|
165 |
export(**vars(opt))
|
|
|
|
|
|
|
|
|
|
|
|
144 |
print(f'\nExport complete ({time.time() - t:.2f}s). Visualize with https://github.com/lutzroeder/netron.')
|
145 |
|
146 |
|
147 |
+
def parse_opt():
|
148 |
parser = argparse.ArgumentParser()
|
149 |
parser.add_argument('--weights', type=str, default='./yolov5s.pt', help='weights path')
|
150 |
parser.add_argument('--img-size', nargs='+', type=int, default=[640, 640], help='image (height, width)')
|
|
|
159 |
parser.add_argument('--simplify', action='store_true', help='ONNX: simplify model')
|
160 |
parser.add_argument('--opset-version', type=int, default=12, help='ONNX: opset version')
|
161 |
opt = parser.parse_args()
|
162 |
+
return opt
|
163 |
+
|
164 |
+
|
165 |
+
def main(opt):
|
166 |
print(opt)
|
167 |
set_logging()
|
|
|
168 |
export(**vars(opt))
|
169 |
+
|
170 |
+
|
171 |
+
if __name__ == "__main__":
|
172 |
+
opt = parse_opt()
|
173 |
+
main(opt)
|
test.py
CHANGED
@@ -294,7 +294,7 @@ def test(data,
|
|
294 |
return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t
|
295 |
|
296 |
|
297 |
-
|
298 |
parser = argparse.ArgumentParser(prog='test.py')
|
299 |
parser.add_argument('--data', type=str, default='data/coco128.yaml', help='dataset.yaml path')
|
300 |
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
|
@@ -319,6 +319,10 @@ if __name__ == '__main__':
|
|
319 |
opt.save_json |= opt.data.endswith('coco.yaml')
|
320 |
opt.save_txt |= opt.save_hybrid
|
321 |
opt.data = check_file(opt.data) # check file
|
|
|
|
|
|
|
|
|
322 |
print(opt)
|
323 |
check_requirements(exclude=('tensorboard', 'thop'))
|
324 |
|
@@ -344,3 +348,8 @@ if __name__ == '__main__':
|
|
344 |
np.savetxt(f, y, fmt='%10.4g') # save
|
345 |
os.system('zip -r study.zip study_*.txt')
|
346 |
plot_study_txt(x=x) # plot
|
|
|
|
|
|
|
|
|
|
|
|
294 |
return (mp, mr, map50, map, *(loss.cpu() / len(dataloader)).tolist()), maps, t
|
295 |
|
296 |
|
297 |
+
def parse_opt():
|
298 |
parser = argparse.ArgumentParser(prog='test.py')
|
299 |
parser.add_argument('--data', type=str, default='data/coco128.yaml', help='dataset.yaml path')
|
300 |
parser.add_argument('--weights', nargs='+', type=str, default='yolov5s.pt', help='model.pt path(s)')
|
|
|
319 |
opt.save_json |= opt.data.endswith('coco.yaml')
|
320 |
opt.save_txt |= opt.save_hybrid
|
321 |
opt.data = check_file(opt.data) # check file
|
322 |
+
return opt
|
323 |
+
|
324 |
+
|
325 |
+
def main(opt):
|
326 |
print(opt)
|
327 |
check_requirements(exclude=('tensorboard', 'thop'))
|
328 |
|
|
|
348 |
np.savetxt(f, y, fmt='%10.4g') # save
|
349 |
os.system('zip -r study.zip study_*.txt')
|
350 |
plot_study_txt(x=x) # plot
|
351 |
+
|
352 |
+
|
353 |
+
if __name__ == "__main__":
|
354 |
+
opt = parse_opt()
|
355 |
+
main(opt)
|
train.py
CHANGED
@@ -463,7 +463,7 @@ def train(hyp, # path/to/hyp.yaml or hyp dictionary
|
|
463 |
return results
|
464 |
|
465 |
|
466 |
-
|
467 |
parser = argparse.ArgumentParser()
|
468 |
parser.add_argument('--weights', type=str, default='yolov5s.pt', help='initial weights path')
|
469 |
parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
|
@@ -504,6 +504,11 @@ if __name__ == '__main__':
|
|
504 |
# Set DDP variables
|
505 |
opt.world_size = int(getattr(os.environ, 'WORLD_SIZE', 1))
|
506 |
opt.global_rank = int(getattr(os.environ, 'RANK', -1))
|
|
|
|
|
|
|
|
|
|
|
507 |
set_logging(opt.global_rank)
|
508 |
if opt.global_rank in [-1, 0]:
|
509 |
check_git_status()
|
@@ -628,3 +633,8 @@ if __name__ == '__main__':
|
|
628 |
plot_evolution(yaml_file)
|
629 |
print(f'Hyperparameter evolution complete. Best results saved as: {yaml_file}\n'
|
630 |
f'Command to train a new model with these hyperparameters: $ python train.py --hyp {yaml_file}')
|
|
|
|
|
|
|
|
|
|
|
|
463 |
return results
|
464 |
|
465 |
|
466 |
+
def parse_opt():
|
467 |
parser = argparse.ArgumentParser()
|
468 |
parser.add_argument('--weights', type=str, default='yolov5s.pt', help='initial weights path')
|
469 |
parser.add_argument('--cfg', type=str, default='', help='model.yaml path')
|
|
|
504 |
# Set DDP variables
|
505 |
opt.world_size = int(getattr(os.environ, 'WORLD_SIZE', 1))
|
506 |
opt.global_rank = int(getattr(os.environ, 'RANK', -1))
|
507 |
+
return opt
|
508 |
+
|
509 |
+
|
510 |
+
def main(opt):
|
511 |
+
print(opt)
|
512 |
set_logging(opt.global_rank)
|
513 |
if opt.global_rank in [-1, 0]:
|
514 |
check_git_status()
|
|
|
633 |
plot_evolution(yaml_file)
|
634 |
print(f'Hyperparameter evolution complete. Best results saved as: {yaml_file}\n'
|
635 |
f'Command to train a new model with these hyperparameters: $ python train.py --hyp {yaml_file}')
|
636 |
+
|
637 |
+
|
638 |
+
if __name__ == "__main__":
|
639 |
+
opt = parse_opt()
|
640 |
+
main(opt)
|