Skip to content

Commit 8a0ae92

Browse files
committed
benchmarking with restart
1 parent c4595b2 commit 8a0ae92

File tree

2 files changed

+46
-36
lines changed

2 files changed

+46
-36
lines changed

benchmarking/run.py

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -22,12 +22,12 @@
2222
pixels = [2500, 10000, 40000, 160000, 320000, 640000]
2323
ind = 1
2424

25-
for m in dog_models:
26-
print("\n\nMODEL {:d} / 8".format(ind))
27-
benchmark_model_by_size(m, dog_video, output=out_dir, n_frames=n_frames, pixels=pixels)
28-
ind += 1
29-
30-
for m in mouse_models:
31-
print("\n\nMODEL {:d} / 8".format(ind))
32-
benchmark_model_by_size(m, mouse_video, output=out_dir, n_frames=n_frames, pixels=pixels)
33-
ind += 1
25+
for ind_m, m in enumerate(dog_models):
26+
print("\n\nMODEL {:d} / 8".format(ind_m))
27+
benchmark_model_by_size(m, dog_video, ind_m, out_dir=out_dir, n_frames=n_frames, pixels=pixels)
28+
29+
offset=ind_m+1
30+
31+
for ind_m, m in enumerate(mouse_models):
32+
print("\n\nMODEL {:d} / 8".format(ind_m))
33+
benchmark_model_by_size(m, mouse_video, ind_m + offset, out_dir=out_dir, n_frames=n_frames, pixels=pixels)

dlclive/bench.py

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -141,7 +141,16 @@ def run_benchmark(model_path, video_path, resize=None, pixels=None, n_frames=100
141141
return inf_times, resize*im_size[0] * resize*im_size[1], TFGPUinference
142142

143143

144-
def save_benchmark(sys_info, inf_times, pixels, iter , TFGPUinference, model=None, out_dir=None):
144+
def get_savebenchmarkfn(sys_info ,i, fn_ind, out_dir=None):
145+
''' get filename to save data (definitions see save_benchmark)'''
146+
out_dir = out_dir if out_dir is not None else os.getcwd()
147+
host_name, op_sys, host_python, dev = sys_info
148+
149+
base_name = "benchmark_{}_{}_{}_{}.pickle".format(host_name, dev[0], fn_ind, i)
150+
datafilename = out_dir + '/' + base_name
151+
return datafilename
152+
153+
def save_benchmark(sys_info, inf_times, pixels, i, fn_ind, TFGPUinference, model=None, out_dir=None,datafilename=None):
145154
""" Save benchmarking data with system information to a pickle file
146155
147156
Parameters
@@ -152,7 +161,7 @@ def save_benchmark(sys_info, inf_times, pixels, iter , TFGPUinference, model=Non
152161
array of inference times generated by :func:`run_benchmark`
153162
pixels : float or :class:`numpy.ndarray`
154163
number of pixels for each benchmark run. If an array, each index corresponds to a row in inf_times
155-
iter: integer
164+
i: integer
156165
number of the specific instance of experiment (so every part is saved individually)
157166
TFGPUinference: bool
158167
flag if using tensorflow inference or numpy inference DLC model
@@ -168,7 +177,10 @@ def save_benchmark(sys_info, inf_times, pixels, iter , TFGPUinference, model=Non
168177
flag indicating successful save
169178
"""
170179

171-
out_dir = out_dir if out_dir is not None else os.getcwd()
180+
if datafilename is None:
181+
#out_dir = out_dir if out_dir is not None else os.getcwd()
182+
datafilename=get_savebenchmarkfn(sys_info ,iter, fn_ind, out_dir=out_dir)
183+
172184
host_name, op_sys, host_python, dev = sys_info
173185
host_name = host_name.replace(" ", "")
174186

@@ -181,14 +193,6 @@ def save_benchmark(sys_info, inf_times, pixels, iter , TFGPUinference, model=Non
181193
else:
182194
model_type = None
183195

184-
fn_ind = 0
185-
base_name = "benchmark_{}_{}_{}_{}.pickle".format(host_name, dev[0], fn_ind,iter)
186-
while os.path.isfile(os.path.normpath(out_dir + '/' + base_name)):
187-
fn_ind += 1
188-
base_name = "benchmark_{}_{}_{}_{}.pickle".format(host_name, dev[0], fn_ind,iter)
189-
190-
fn = os.path.normpath(out_dir)
191-
192196
data = {'host_name' : host_name,
193197
'op_sys' : op_sys,
194198
'python' : host_python,
@@ -200,7 +204,7 @@ def save_benchmark(sys_info, inf_times, pixels, iter , TFGPUinference, model=Non
200204
'pixels' : pixels,
201205
'inference_times' : inf_times}
202206

203-
pickle.dump(data, open(os.path.normpath(out_dir + '/' + base_name), 'wb'))
207+
pickle.dump(data, open(os.path.normpath(datafilename), 'wb'))
204208

205209
return True
206210

@@ -209,7 +213,7 @@ def read_pickle(filename):
209213
with open(filename, "rb") as handle:
210214
return pickle.load(handle)
211215

212-
def benchmark_model_by_size(model_path, video_path, output=None, n_frames=10000, resize=None, pixels=None, print_rate=False):
216+
def benchmark_model_by_size(model_path, video_path, fn_ind, out_dir=None, n_frames=10000, resize=None, pixels=None, print_rate=False):
213217
"""Benchmark DLC model by image size
214218
215219
Parameters
@@ -218,7 +222,9 @@ def benchmark_model_by_size(model_path, video_path, output=None, n_frames=10000,
218222
path to exported DLC model
219223
video_path : str
220224
path to video file
221-
output : str, optional
225+
fn_ind : integer
226+
auxiliary variable for creating a unique identifier for saving the models
227+
out_dir : str, optional
222228
directory to save data, will not save data if None, by default None
223229
n_frames : int, optional
224230
number of frames to run, by default 10000
@@ -228,7 +234,7 @@ def benchmark_model_by_size(model_path, video_path, output=None, n_frames=10000,
228234
list of pixel image sizes (as ints), can only use one of resize or pixels, if both specified will use pixels, by default None
229235
print_rate : bool, optional
230236
flag to print frame by frame inference rate, by default False
231-
237+
232238
Example
233239
--------
234240
Linux/MacOs
@@ -252,21 +258,25 @@ def benchmark_model_by_size(model_path, video_path, output=None, n_frames=10000,
252258
print(resize)
253259
for i in range(len(resize)):
254260

255-
print("\nRun {:d} / {:d}\n".format(i+1, len(resize)))
261+
sys_info = get_system_info()
262+
#print("Your system info:", sys_info)
263+
datafilename=get_savebenchmarkfn(sys_info ,i, fn_ind, out_dir=out_dir)
256264

257-
inf_times, pixels_out, TFGPUinference = run_benchmark(model_path,
258-
video_path,
259-
resize=resize[i],
260-
pixels=pixels[i],
261-
n_frames=n_frames,
262-
print_rate=print_rate)
265+
#Check if a part has already been complted?
266+
if os.path.isfile(os.path.normpath(datafilename)):
267+
print("\nAlready ran {:d} / {:d}\n".format(i+1, len(resize)))
268+
else:
269+
print("\nRun {:d} / {:d}\n".format(i+1, len(resize)))
263270

264-
#TODO: check if a part has already been complted?
271+
inf_times, pixels_out, TFGPUinference = run_benchmark(model_path,
272+
video_path,
273+
resize=resize[i],
274+
pixels=pixels[i],
275+
n_frames=n_frames,
276+
print_rate=print_rate)
265277

266-
### saving results intermediately
267-
sys_info = get_system_info()
268-
#print("Your system info:", sys_info)
269-
save_benchmark(sys_info, inf_times, pixels_out, i, TFGPUinference, model=os.path.basename(model_path), out_dir=output)
278+
### saving results intermediately
279+
save_benchmark(sys_info, inf_times, pixels_out, i, fn_ind, TFGPUinference, model=os.path.basename(model_path), datafilename=datafilename)
270280

271281

272282
def main():

0 commit comments

Comments
 (0)