2525from roboflow .models .instance_segmentation import InstanceSegmentationModel
2626from roboflow .models .object_detection import ObjectDetectionModel
2727from roboflow .models .semantic_segmentation import SemanticSegmentationModel
28+ from roboflow .util .versions import (
29+ print_warn_for_wrong_dependencies_versions ,
30+ warn_for_wrong_dependencies_versions ,
31+ )
2832
2933load_dotenv ()
3034
@@ -153,6 +157,12 @@ def download(self, model_format=None, location=None, overwrite: bool = True):
153157
154158 self .__wait_if_generating ()
155159
160+ if model_format == "yolov8" :
161+ # we assume the user will want to use yolov8, for now we only support ultralytics=="8.11.0"
162+ print_warn_for_wrong_dependencies_versions (
163+ [("ultralytics" , "<=" , "8.0.20" )]
164+ )
165+
156166 model_format = self .__get_format_identifier (model_format )
157167
158168 if model_format not in self .exports :
@@ -284,14 +294,15 @@ def train(self, speed=None, checkpoint=None) -> bool:
284294
285295 return True
286296
297+ # @warn_for_wrong_dependencies_versions([("ultralytics", "<=", "8.0.20")])
287298 def deploy (self , model_type : str , model_path : str ) -> None :
288299 """Uploads provided weights file to Roboflow
289300
290301 Args:
291302 model_path (str): File path to model weights to be uploaded
292303 """
293304
294- supported_models = ["yolov8" ]
305+ supported_models = ["yolov8" , "yolov5" ]
295306
296307 if model_type not in supported_models :
297308 raise (
@@ -300,24 +311,38 @@ def deploy(self, model_type: str, model_path: str) -> None:
300311 )
301312 )
302313
303- try :
304- import torch
305- import ultralytics
306- except ImportError as e :
307- raise (
308- "The ultralytics python package is required to deploy yolov8 models. Please install it with `pip install ultralytics`"
314+ if model_type == "yolov8" :
315+ try :
316+ import torch
317+ import ultralytics
318+
319+ except ImportError as e :
320+ raise (
321+ "The ultralytics python package is required to deploy yolov8 models. Please install it with `pip install ultralytics`"
322+ )
323+
324+ print_warn_for_wrong_dependencies_versions (
325+ [("ultralytics" , "<=" , "8.0.20" )]
309326 )
310327
311- # add logic to save torch state dict safely
312- if model_type == "yolov8" :
313- model = torch .load (os .path .join (model_path + "weights/best.pt" ))
328+ elif model_type == "yolov5" :
329+ try :
330+ import torch
331+ except ImportError as e :
332+ raise (
333+ "The torch python package is required to deploy yolov5 models. Please install it with `pip install torch`"
334+ )
335+
336+ model = torch .load (os .path .join (model_path , "weights/best.pt" ))
314337
315- class_names = []
316- for i , val in enumerate (model ["model" ].names ):
317- class_names .append ((val , model ["model" ].names [val ]))
318- class_names .sort (key = lambda x : x [0 ])
319- class_names = [x [1 ] for x in class_names ]
338+ class_names = []
339+ for i , val in enumerate (model ["model" ].names ):
340+ class_names .append ((val , model ["model" ].names [val ]))
341+ class_names .sort (key = lambda x : x [0 ])
342+ class_names = [x [1 ] for x in class_names ]
320343
344+ if model_type == "yolov8" :
345+ # try except for backwards compatibility with older versions of ultralytics
321346 try :
322347 model_artifacts = {
323348 "names" : class_names ,
@@ -344,29 +369,39 @@ def deploy(self, model_type: str, model_path: str) -> None:
344369 "ultralytics_version" : ultralytics .__version__ ,
345370 "model_type" : model_type ,
346371 }
347-
348- with open (os .path .join (model_path + "model_artifacts.json" , "w" )) as fp :
349- json .dump (model_artifacts , fp )
350-
351- torch .save (
352- model ["model" ].state_dict (), os .path .join (model_path + "state_dict.pt" )
353- )
354-
355- lista_files = [
356- "results.csv" ,
357- "results.png" ,
358- "model_artifacts.json" ,
359- "state_dict.pt" ,
360- ]
361- with zipfile .ZipFile (
362- os .path .join (model_path + "roboflow_deploy.zip" , "w" )
363- ) as zipMe :
364- for file in lista_files :
365- zipMe .write (
366- os .path .join (model_path + file ),
367- arcname = file ,
368- compress_type = zipfile .ZIP_DEFLATED ,
369- )
372+ elif model_type == "yolov5" :
373+ # parse from yaml for yolov5
374+
375+ with open (os .path .join (model_path , "opt.yaml" ), "r" ) as stream :
376+ opts = yaml .safe_load (stream )
377+
378+ model_artifacts = {
379+ "names" : class_names ,
380+ "yaml" : model ["model" ].yaml ,
381+ "nc" : model ["model" ].nc ,
382+ "args" : {"imgsz" : opts ["imgsz" ], "batch" : opts ["batch_size" ]},
383+ "model_type" : model_type ,
384+ }
385+
386+ with open (model_path + "model_artifacts.json" , "w" ) as fp :
387+ json .dump (model_artifacts , fp )
388+
389+ torch .save (model ["model" ].state_dict (), model_path + "state_dict.pt" )
390+
391+ lista_files = [
392+ "results.csv" ,
393+ "results.png" ,
394+ "model_artifacts.json" ,
395+ "state_dict.pt" ,
396+ ]
397+
398+ with zipfile .ZipFile (model_path + "roboflow_deploy.zip" , "w" ) as zipMe :
399+ for file in lista_files :
400+ zipMe .write (
401+ model_path + file ,
402+ arcname = file ,
403+ compress_type = zipfile .ZIP_DEFLATED ,
404+ )
370405
371406 res = requests .get (
372407 f"{ API_URL } /{ self .workspace } /{ self .project } /{ self .version } /uploadModel?api_key={ self .__api_key } "
@@ -384,7 +419,7 @@ def deploy(self, model_type: str, model_path: str) -> None:
384419
385420 res = requests .put (
386421 res .json ()["url" ],
387- data = open (os .path .join (model_path + "roboflow_deploy.zip" , "rb" ) ),
422+ data = open (os .path .join (model_path + "roboflow_deploy.zip" ) , "rb" ),
388423 )
389424 try :
390425 res .raise_for_status ()
@@ -404,8 +439,6 @@ def deploy(self, model_type: str, model_path: str) -> None:
404439 except Exception as e :
405440 print (f"An error occured when uploading the model: { e } " )
406441
407- # torch.load("state_dict.pt", weights_only=True)
408-
409442 def __download_zip (self , link , location , format ):
410443 """
411444 Download a dataset's zip file from the given URL and save it in the desired location
0 commit comments