-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrun-izembek-model.py
147 lines (120 loc) · 4.71 KB
/
run-izembek-model.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
########
#
# run-izembek-model.py
#
# Run the Izembek brant model on an image or folder of images. This script
# is a command-line driver for usgs-geese-inference.py
#
########
#%% Imports and constants
import os
import sys
import shutil
import importlib
import argparse
usgs_geese_inference = importlib.import_module('usgs-geese-inference')
#%% Command-line driver
def main():
parser = argparse.ArgumentParser(
description='Script to run the Izembek brant model on an image or folder of images')
parser.add_argument(
'model_file',
type=str,
help='Path to detector model file (.pt)')
parser.add_argument(
'input_path',
type=str,
help='Path to a folder of images')
parser.add_argument(
'yolo_working_dir',
type=str,
help='Path to the folder where the YOLO repo lives')
parser.add_argument(
'scratch_dir',
type=str,
help='Path to a folder where lots of temporary output will be stored')
parser.add_argument(
'--output_file',
type=str,
default=None,
help='Path to output JSON results file, should end with a .json extension. Always written to the scratch folder; this option just copies the final results file to a specified location.')
parser.add_argument(
'--recursive',
action='store_true',
help='Recurse into directories, only meaningful if image_file points to a directory')
parser.add_argument(
'--no_use_symlinks',
action='store_true',
help='Copy patches to the temporary inference folder, rather than using symlinks')
parser.add_argument(
'--no_cleanup',
action='store_true',
help='Bypass cleanup of temporary files')
parser.add_argument(
'--no_augment',
action='store_true',
help='Disable YOLOv5 test-time augmentation')
parser.add_argument(
'--category_mapping_file',
type=str,
default=None,
help='.yaml or .json file mapping model category IDs to names (defaults to Izembek model mapping)')
parser.add_argument(
'--device',
type=int,
default=usgs_geese_inference.default_devices[0],
help='CUDA device to run on, or -1 to force CPU inference (default {})'.format(
usgs_geese_inference.default_devices[0]))
if len(sys.argv[1:]) == 0:
parser.print_help()
parser.exit()
args = parser.parse_args()
inference_options = usgs_geese_inference.USGSGeeseInferenceOptions(
project_dir=args.scratch_dir,
yolo_working_dir=args.yolo_working_dir,
model_file=args.model_file,
use_symlinks=(not args.no_use_symlinks),
no_augment=(args.no_augment),
no_cleanup=args.no_cleanup,
devices=[args.device],
category_mapping_file=args.category_mapping_file)
results = usgs_geese_inference.run_model_on_folder(args.input_path,inference_options)
if args.output_file is not None:
nms_results_file = results['md_results_image_level_nms_fn']
assert os.path.isfile(nms_results_file), \
'Could not find output file {}'.format(nms_results_file)
shutil.copyfile(nms_results_file,args.output_file)
print('Copied results file to {}'.format(args.output_file))
if __name__ == '__main__':
main()
#%% Interactive driver
if False:
pass
#%% Prepare inference pass
image_dir = r'g:\temp\usgs-test-images'
yolo_working_dir = r'c:\git\yolov5-current'
scratch_dir = r'g:\temp\usgs-scratch'
model_file = r"C:\Users\dmorr\models\usgs-geese\usgs-geese-yolov5x-230820-b8-img1280-e200-best.pt"
use_symlinks = False
cleanup = True
inference_options = usgs_geese_inference.USGSGeeseInferenceOptions(
project_dir=scratch_dir,
yolo_working_dir=yolo_working_dir,
model_file=model_file,
use_symlinks=use_symlinks,
no_cleanup=(not cleanup))
# inference_options.n_cores_patch_generation = 1
#%% Run in Python
results = usgs_geese_inference.run_model_on_folder(image_dir,inference_options)
#%% Generate command
script_name = 'run-izembek-model.py'
output_file = r'g:\temp\wdfw-results-nms.json'
import clipboard
cmd = f'python "{script_name}" "{model_file}" "{image_dir}" "{yolo_working_dir}" ' + \
r'"g:\temp\usgs-geese-inference-test" --recursive'
if not cleanup:
cmd += ' --no_cleanup'
if not use_symlinks:
cmd += ' --no_use_symlinks'
print(cmd)
clipboard.copy(cmd)