@@ -174,14 +174,16 @@ def run_benchmark(model_path, video_path, resize=None, pixels=None, n_frames=100
174174 resize = resize if resize is not None else 1
175175 return inf_times , resize * im_size [0 ] * resize * im_size [1 ], TFGPUinference
176176
177+ def get_savebenchmarkfn (sys_info ,i , fn_ind , out_dir = None ):
178+ ''' get filename to save data (definitions see save_benchmark)'''
179+ out_dir = out_dir if out_dir is not None else os .getcwd ()
180+ host_name , op_sys , host_python , dev = sys_info
181+
182+ base_name = "benchmark_{}_{}_{}_{}.pickle" .format (host_name , sys_info ['device_type' ], fn_ind , i )
183+ datafilename = out_dir + '/' + base_name
184+ return datafilename
177185
178- def save_benchmark (sys_info : dict ,
179- inf_times : np .ndarray ,
180- pixels : typing .Union [np .ndarray , float ],
181- iter : int ,
182- TFGPUinference : bool ,
183- model : str = None ,
184- out_dir : str = None ):
186+ def save_benchmark (sys_info , inf_times , pixels , i , fn_ind , TFGPUinference , model = None , out_dir = None ,datafilename = None ):
185187 """ Save benchmarking data with system information to a pickle file
186188
187189 Parameters
@@ -192,7 +194,7 @@ def save_benchmark(sys_info: dict,
192194 array of inference times generated by :func:`run_benchmark`
193195 pixels : float or :class:`numpy.ndarray`
194196 number of pixels for each benchmark run. If an array, each index corresponds to a row in inf_times
195- iter: int
197+ i: integer
196198 number of the specific instance of experiment (so every part is saved individually)
197199 TFGPUinference: bool
198200 flag if using tensorflow inference or numpy inference DLC model
@@ -208,9 +210,12 @@ def save_benchmark(sys_info: dict,
208210 flag indicating successful save
209211 """
210212
211- out_dir = out_dir if out_dir is not None else os .getcwd ()
212- # host_name, op_sys, host_python, dev = sys_info
213+ if datafilename is None :
214+ #out_dir = out_dir if out_dir is not None else os.getcwd()
215+ datafilename = get_savebenchmarkfn (sys_info ,iter , fn_ind , out_dir = out_dir )
213216
217+ host_name , op_sys , host_python , dev = sys_info
218+ host_name = host_name .replace (" " , "" )
214219
215220 model_type = None
216221 if model is not None :
@@ -221,22 +226,15 @@ def save_benchmark(sys_info: dict,
221226 else :
222227 model_type = None
223228
224- fn_ind = 0
225- base_name = "benchmark_{}_{}_{}_{}.pickle" .format (sys_info ['host_name' ], sys_info ['device_type' ], fn_ind , iter )
226- while os .path .isfile (os .path .normpath (out_dir + '/' + base_name )):
227- fn_ind += 1
228- base_name = "benchmark_{}_{}_{}_{}.pickle" .format (sys_info ['host_name' ], sys_info ['device_type' ], fn_ind ,iter )
229-
230- fn = os .path .normpath (out_dir )
231-
232229 data = {'model' : model ,
233230 'model_type' : model_type ,
234231 'TFGPUinference' : TFGPUinference ,
235232 'pixels' : pixels ,
236233 'inference_times' : inf_times }
234+
237235 data .update (sys_info )
238236
239- pickle .dump (data , open (os .path .normpath (out_dir + '/' + base_name ), 'wb' ))
237+ pickle .dump (data , open (os .path .normpath (datafilename ), 'wb' ))
240238
241239 return True
242240
@@ -245,7 +243,7 @@ def read_pickle(filename):
245243 with open (filename , "rb" ) as handle :
246244 return pickle .load (handle )
247245
248- def benchmark_model_by_size (model_path , video_path , output = None , n_frames = 10000 , resize = None , pixels = None , print_rate = False ):
246+ def benchmark_model_by_size (model_path , video_path , fn_ind , out_dir = None , n_frames = 10000 , resize = None , pixels = None , print_rate = False ):
249247 """Benchmark DLC model by image size
250248
251249 Parameters
@@ -254,7 +252,9 @@ def benchmark_model_by_size(model_path, video_path, output=None, n_frames=10000,
254252 path to exported DLC model
255253 video_path : str
256254 path to video file
257- output : str, optional
255+ fn_ind : integer
256+ auxiliary variable for creating a unique identifier for saving the models
257+ out_dir : str, optional
258258 directory to save data, will not save data if None, by default None
259259 n_frames : int, optional
260260 number of frames to run, by default 10000
@@ -264,7 +264,7 @@ def benchmark_model_by_size(model_path, video_path, output=None, n_frames=10000,
264264 list of pixel image sizes (as ints), can only use one of resize or pixels, if both specified will use pixels, by default None
265265 print_rate : bool, optional
266266 flag to print frame by frame inference rate, by default False
267-
267+
268268 Example
269269 --------
270270 Linux/MacOs
@@ -292,21 +292,24 @@ def benchmark_model_by_size(model_path, video_path, output=None, n_frames=10000,
292292
293293 for i in range (len (resize )):
294294
295- print ("\n Run {:d} / {:d}\n " .format (i + 1 , len (resize )))
296-
297- inf_times , pixels_out , TFGPUinference = run_benchmark (model_path ,
298- video_path ,
299- resize = resize [i ],
300- pixels = pixels [i ],
301- n_frames = n_frames ,
302- print_rate = print_rate )
303-
304- #TODO: check if a part has already been complted?
305-
306- ### saving results intermediately
307-
295+ sys_info = get_system_info ()
308296 #print("Your system info:", sys_info)
309- save_benchmark (sys_info , inf_times , pixels_out , i , TFGPUinference , model = os .path .basename (model_path ), out_dir = output )
297+ datafilename = get_savebenchmarkfn (sys_info ,i , fn_ind , out_dir = out_dir )
298+
299+ #Check if a subset was already been completed?
300+ if os .path .isfile (os .path .normpath (datafilename )):
301+ print ("\n Already ran {:d} / {:d}\n " .format (i + 1 , len (resize )))
302+ else :
303+ print ("\n Run {:d} / {:d}\n " .format (i + 1 , len (resize )))
304+ inf_times , pixels_out , TFGPUinference = run_benchmark (model_path ,
305+ video_path ,
306+ resize = resize [i ],
307+ pixels = pixels [i ],
308+ n_frames = n_frames ,
309+ print_rate = print_rate )
310+
311+ ### saving results intermediately
312+ save_benchmark (sys_info , inf_times , pixels_out , i , fn_ind , TFGPUinference , model = os .path .basename (model_path ), datafilename = datafilename )
310313
311314
312315def main ():
0 commit comments