comparison yolov8.py @ 2:158e6ce48345 draft

planemo upload for repository https://github.com/bgruening/galaxytools/tree/master/tools commit 739bcabf09bdb9c291121a6b1f889dabe1a98ea9
author bgruening
date Fri, 11 Jul 2025 06:49:20 +0000
parents dfda27273ead
children 97bc82ee2a61
comparison
equal deleted inserted replaced
1:dfda27273ead 2:158e6ce48345
173 173
174 174
175 # 175 #
176 # Functions 176 # Functions
177 # 177 #
178
179 def safe_rmtree(path):
180 try:
181 shutil.rmtree(path)
182 except OSError:
183 time.sleep(1)
184 shutil.rmtree(path, ignore_errors=True)
185
186
178 # Train a new model on the dataset mentioned in yaml file 187 # Train a new model on the dataset mentioned in yaml file
179 def trainModel(model_path, model_name, yaml_filepath, **kwargs): 188 def trainModel(model_path, model_name, yaml_filepath, **kwargs):
180 if "imgsz" in kwargs: 189 if "imgsz" in kwargs:
181 image_size = kwargs['imgsz'] 190 image_size = kwargs['imgsz']
182 else: 191 else:
262 else: 271 else:
263 init_lr = 1.0 272 init_lr = 1.0
264 273
265 train_save_path = os.path.expanduser('~/runs/' + args.mode + '/train/') 274 train_save_path = os.path.expanduser('~/runs/' + args.mode + '/train/')
266 if os.path.isdir(train_save_path): 275 if os.path.isdir(train_save_path):
267 shutil.rmtree(train_save_path) 276 safe_rmtree(train_save_path)
268 # Load a pretrained YOLO model (recommended for training) 277 # Load a pretrained YOLO model (recommended for training)
269 if args.model_format == 'pt': 278 if args.model_format == 'pt':
270 model = YOLO(os.path.join(model_path, model_name + "." + args.model_format)) 279 model = YOLO(os.path.join(model_path, model_name + "." + args.model_format))
271 else: 280 else:
272 model = YOLO(model_name + "." + args.model_format) 281 model = YOLO(model_name + "." + args.model_format)
283 # Validate the trained model 292 # Validate the trained model
284 def validateModel(model): 293 def validateModel(model):
285 # Remove prediction save path if already exists 294 # Remove prediction save path if already exists
286 val_save_path = os.path.expanduser('~/runs/' + args.mode + '/val/') 295 val_save_path = os.path.expanduser('~/runs/' + args.mode + '/val/')
287 if os.path.isdir(val_save_path): 296 if os.path.isdir(val_save_path):
288 shutil.rmtree(val_save_path) 297 safe_rmtree(val_save_path)
289 # Validate the model 298 # Validate the model
290 metrics = model.val() # no args needed, dataset & settings remembered 299 metrics = model.val() # no args needed, dataset & settings remembered
291 metrics.box.map # map50-95 300 metrics.box.map # map50-95
292 metrics.box.map50 # map50 301 metrics.box.map50 # map50
293 metrics.box.map75 # map75 302 metrics.box.map75 # map75
325 run_save_dir = kwargs['run_dir'] 334 run_save_dir = kwargs['run_dir']
326 else: 335 else:
327 # Remove prediction save path if already exists 336 # Remove prediction save path if already exists
328 pred_save_path = os.path.expanduser('~/runs/' + args.mode + '/predict/') 337 pred_save_path = os.path.expanduser('~/runs/' + args.mode + '/predict/')
329 if os.path.isdir(pred_save_path): 338 if os.path.isdir(pred_save_path):
330 shutil.rmtree(pred_save_path) 339 safe_rmtree(pred_save_path)
331 if "foldername" in kwargs: 340 if "foldername" in kwargs:
332 save_folder_name = kwargs['foldername'] 341 save_folder_name = kwargs['foldername']
333 # infer on a local image or directory containing images/videos 342 # infer on a local image or directory containing images/videos
334 prediction = model.predict(source=source_datapath, save=True, stream=True, 343 prediction = model.predict(source=source_datapath, save=True, stream=True,
335 conf=confidence, imgsz=image_size, 344 conf=confidence, imgsz=image_size,