17
17
API_URL ,
18
18
APP_URL ,
19
19
DEMO_KEYS ,
20
+ TQDM_DISABLE ,
20
21
TYPE_CLASSICATION ,
21
22
TYPE_INSTANCE_SEGMENTATION ,
22
23
TYPE_KEYPOINT_DETECTION ,
@@ -432,11 +433,15 @@ def deploy(self, model_type: str, model_path: str, filename: str = "weights/best
432
433
filename (str, optional): The name of the weights file. Defaults to "weights/best.pt".
433
434
"""
434
435
435
- supported_models = ["yolov5" , "yolov7-seg" , "yolov8" , "yolov9" , "yolonas" ]
436
+ supported_models = ["yolov5" , "yolov7-seg" , "yolov8" , "yolov9" , "yolonas" , "paligemma" , "yolov10" ]
436
437
437
438
if not any (supported_model in model_type for supported_model in supported_models ):
438
439
raise (ValueError (f"Model type { model_type } not supported. Supported models are" f" { supported_models } " ))
439
440
441
+ if "paligemma" in model_type :
442
+ self .deploy_paligemma (model_type , model_path , filename )
443
+ return
444
+
440
445
if "yolonas" in model_type :
441
446
self .deploy_yolonas (model_type , model_path , filename )
442
447
return
@@ -454,6 +459,17 @@ def deploy(self, model_type: str, model_path: str, filename: str = "weights/best
454
459
455
460
print_warn_for_wrong_dependencies_versions ([("ultralytics" , "==" , "8.0.196" )], ask_to_continue = True )
456
461
462
+ elif "yolov10" in model_type :
463
+ try :
464
+ import torch
465
+ import ultralytics
466
+
467
+ except ImportError :
468
+ raise (
469
+ "The ultralytics python package is required to deploy yolov10"
470
+ " models. Please install it with `pip install ultralytics`"
471
+ )
472
+
457
473
elif "yolov5" in model_type or "yolov7" in model_type or "yolov9" in model_type :
458
474
try :
459
475
import torch
@@ -474,7 +490,7 @@ def deploy(self, model_type: str, model_path: str, filename: str = "weights/best
474
490
class_names .sort (key = lambda x : x [0 ])
475
491
class_names = [x [1 ] for x in class_names ]
476
492
477
- if "yolov8" in model_type :
493
+ if "yolov8" in model_type or "yolov10" in model_type :
478
494
# try except for backwards compatibility with older versions of ultralytics
479
495
if "-cls" in model_type :
480
496
nc = model ["model" ].yaml ["nc" ]
@@ -548,6 +564,57 @@ def deploy(self, model_type: str, model_path: str, filename: str = "weights/best
548
564
549
565
self .upload_zip (model_type , model_path )
550
566
567
+ def deploy_paligemma (
568
+ self , model_type : str , model_path : str , filename : str = "fine-tuned-paligemma-3b-pt-224.f16.npz"
569
+ ) -> None :
570
+ # Check if model_path exists
571
+ if not os .path .exists (model_path ):
572
+ raise FileNotFoundError (f"Model path { model_path } does not exist." )
573
+ model_files = os .listdir (model_path )
574
+ print (f"Model files found in { model_path } : { model_files } " )
575
+
576
+ files_to_deploy = []
577
+
578
+ # Find first .npz file in model_path
579
+ npz_filename = next ((file for file in model_files if file .endswith (".npz" )), None )
580
+ if any ([file .endswith (".safetensors" ) for file in model_files ]):
581
+ print ("Found .safetensors file in model path. Deploying PyTorch PaliGemma model." )
582
+ necessary_files = [
583
+ "config.json" ,
584
+ "generation_config.json" ,
585
+ "model.safetensors.index.json" ,
586
+ "preprocessor_config.json" ,
587
+ "special_tokens_map.json" ,
588
+ "tokenizer_config.json" ,
589
+ "tokenizer.json" ,
590
+ ]
591
+ for file in necessary_files :
592
+ if file not in model_files :
593
+ print ("Missing necessary file" , file )
594
+ res = input ("Do you want to continue? (y/n)" )
595
+ if res .lower () != "y" :
596
+ exit (1 )
597
+ for file in model_files :
598
+ files_to_deploy .append (file )
599
+ elif npz_filename is not None :
600
+ print (f"Found .npz file { npz_filename } in model path. Deploying JAX PaliGemma model." )
601
+ files_to_deploy .append (npz_filename )
602
+ else :
603
+ raise FileNotFoundError (f"No .npz or .safetensors file found in model path { model_path } ." )
604
+
605
+ if len (files_to_deploy ) == 0 :
606
+ raise FileNotFoundError (f"No valid files found in model path { model_path } ." )
607
+ print (f"Zipping files for deploy: { files_to_deploy } " )
608
+
609
+ import tarfile
610
+
611
+ with tarfile .open (os .path .join (model_path , "roboflow_deploy.tar" ), "w" ) as tar :
612
+ for file in files_to_deploy :
613
+ tar .add (os .path .join (model_path , file ), arcname = file )
614
+
615
+ print ("Uploading to Roboflow... May take several minutes." )
616
+ self .upload_zip (model_type , model_path , "roboflow_deploy.tar" )
617
+
551
618
def deploy_yolonas (self , model_type : str , model_path : str , filename : str = "weights/best.pt" ) -> None :
552
619
try :
553
620
import torch
@@ -613,7 +680,7 @@ def deploy_yolonas(self, model_type: str, model_path: str, filename: str = "weig
613
680
614
681
self .upload_zip (model_type , model_path )
615
682
616
- def upload_zip (self , model_type : str , model_path : str ):
683
+ def upload_zip (self , model_type : str , model_path : str , model_file_name : str = "roboflow_deploy.zip" ):
617
684
res = requests .get (
618
685
f"{ API_URL } /{ self .workspace } /{ self .project } /{ self .version } "
619
686
f"/uploadModel?api_key={ self .__api_key } &modelType={ model_type } &nocache=true"
@@ -632,7 +699,7 @@ def upload_zip(self, model_type: str, model_path: str):
632
699
633
700
res = requests .put (
634
701
res .json ()["url" ],
635
- data = open (os .path .join (model_path , "roboflow_deploy.zip" ), "rb" ),
702
+ data = open (os .path .join (model_path , model_file_name ), "rb" ),
636
703
)
637
704
try :
638
705
res .raise_for_status ()
@@ -685,9 +752,10 @@ def bar_progress(current, total, width=80):
685
752
# write the zip file to the desired location
686
753
with open (location + "/roboflow.zip" , "wb" ) as f :
687
754
total_length = int (response .headers .get ("content-length" ))
755
+ desc = None if TQDM_DISABLE else f"Downloading Dataset Version Zip in { location } to { format } :"
688
756
for chunk in tqdm (
689
757
response .iter_content (chunk_size = 1024 ),
690
- desc = f"Downloading Dataset Version Zip in { location } to { format } :" ,
758
+ desc = desc ,
691
759
total = int (total_length / 1024 ) + 1 ,
692
760
):
693
761
if chunk :
@@ -711,10 +779,11 @@ def __extract_zip(self, location, format):
711
779
Raises:
712
780
RuntimeError: If there is an error unzipping the file
713
781
""" # noqa: E501 // docs
782
+ desc = None if TQDM_DISABLE else f"Extracting Dataset Version Zip to { location } in { format } :"
714
783
with zipfile .ZipFile (location + "/roboflow.zip" , "r" ) as zip_ref :
715
784
for member in tqdm (
716
785
zip_ref .infolist (),
717
- desc = f"Extracting Dataset Version Zip to { location } in { format } :" ,
786
+ desc = desc ,
718
787
):
719
788
try :
720
789
zip_ref .extract (member , location )
0 commit comments