From 67344f0f265811ee9df9463f2b19dc300f792b4b Mon Sep 17 00:00:00 2001 From: SolomonLake Date: Thu, 23 May 2024 17:54:00 -0500 Subject: [PATCH 1/4] Add support for paligemma deploy --- roboflow/core/version.py | 60 ++++++++++++++++++++++++++++++++++++++-- 1 file changed, 57 insertions(+), 3 deletions(-) diff --git a/roboflow/core/version.py b/roboflow/core/version.py index 89077674..2f75115d 100644 --- a/roboflow/core/version.py +++ b/roboflow/core/version.py @@ -432,11 +432,15 @@ def deploy(self, model_type: str, model_path: str, filename: str = "weights/best filename (str, optional): The name of the weights file. Defaults to "weights/best.pt". """ - supported_models = ["yolov5", "yolov7-seg", "yolov8", "yolov9", "yolonas"] + supported_models = ["yolov5", "yolov7-seg", "yolov8", "yolov9", "yolonas", "paligemma"] if not any(supported_model in model_type for supported_model in supported_models): raise (ValueError(f"Model type {model_type} not supported. Supported models are" f" {supported_models}")) + if "paligemma" in model_type: + self.deploy_paligemma(model_type, model_path, filename) + return + if "yolonas" in model_type: self.deploy_yolonas(model_type, model_path, filename) return @@ -548,6 +552,56 @@ def deploy(self, model_type: str, model_path: str, filename: str = "weights/best self.upload_zip(model_type, model_path) + def deploy_paligemma( + self, model_type: str, model_path: str, filename: str = "fine-tuned-paligemma-3b-pt-224.f16.npz" + ) -> None: + # Check if model_path exists + if not os.path.exists(model_path): + raise FileNotFoundError(f"Model path {model_path} does not exist.") + model_files = os.listdir(model_path) + print(f"Model files found in {model_path}: {model_files}") + + files_to_deploy = [] + + # Find first .npz file in model_path + npz_filename = next((file for file in model_files if file.endswith(".npz")), None) + if any([file.endswith(".safetensors") for file in model_files]): + print("Found .safetensors file in model path. Deploying PyTorch PaliGemma model.") + necessary_files = [ + "config.json", + "generation_config.json", + "model.safetensors.index.json", + "preprocessor_config.json", + "special_tokens_map.json", + "tokenizer_config.json", + "tokenizer.json", + ] + for file in necessary_files: + if file not in model_files: + print("Missing necessary file", file) + res = input("Do you want to continue? (y/n)") + if res.lower() != "y": + exit(1) + for file in model_files: + files_to_deploy.append(file) + elif npz_filename is not None: + print(f"Found .npz file {npz_filename} in model path. Deploying JAX PaliGemma model.") + files_to_deploy.append(npz_filename) + else: + raise FileNotFoundError(f"No .npz or .safetensors file found in model path {model_path}.") + + if len(files_to_deploy) == 0: + raise FileNotFoundError(f"No valid files found in model path {model_path}.") + + import tarfile + with tarfile.open(os.path.join(model_path, "roboflow_deploy.tar"), "w") as tar: + for file in files_to_deploy: + tar.add(os.path.join(model_path, file), arcname=file) + + + print("Uploading model to Roboflow... May take several minutes.") + self.upload_zip(model_type, model_path, "roboflow_deploy.tar") + def deploy_yolonas(self, model_type: str, model_path: str, filename: str = "weights/best.pt") -> None: try: import torch @@ -613,7 +667,7 @@ def deploy_yolonas(self, model_type: str, model_path: str, filename: str = "weig self.upload_zip(model_type, model_path) - def upload_zip(self, model_type: str, model_path: str): + def upload_zip(self, model_type: str, model_path: str, model_file_name: str = "roboflow_deploy.zip"): res = requests.get( f"{API_URL}/{self.workspace}/{self.project}/{self.version}" f"/uploadModel?api_key={self.__api_key}&modelType={model_type}&nocache=true" @@ -632,7 +686,7 @@ def upload_zip(self, model_type: str, model_path: str): res = requests.put( res.json()["url"], - data=open(os.path.join(model_path, "roboflow_deploy.zip"), "rb"), + data=open(os.path.join(model_path, model_file_name), "rb"), ) try: res.raise_for_status() From 9720bce60830e79fa48e1cf7c5c1290b249ee0eb Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Thu, 23 May 2024 22:55:22 +0000 Subject: [PATCH 2/4] =?UTF-8?q?fix(pre=5Fcommit):=20=F0=9F=8E=A8=20auto=20?= =?UTF-8?q?format=20pre-commit=20hooks?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- roboflow/core/version.py | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/roboflow/core/version.py b/roboflow/core/version.py index 2f75115d..b5b258b6 100644 --- a/roboflow/core/version.py +++ b/roboflow/core/version.py @@ -589,15 +589,15 @@ def deploy_paligemma( files_to_deploy.append(npz_filename) else: raise FileNotFoundError(f"No .npz or .safetensors file found in model path {model_path}.") - + if len(files_to_deploy) == 0: raise FileNotFoundError(f"No valid files found in model path {model_path}.") - + import tarfile with tarfile.open(os.path.join(model_path, "roboflow_deploy.tar"), "w") as tar: for file in files_to_deploy: tar.add(os.path.join(model_path, file), arcname=file) - + print("Uploading model to Roboflow... May take several minutes.") self.upload_zip(model_type, model_path, "roboflow_deploy.tar") From 8dfbea577033c08a21750b1d7b0d06801c517163 Mon Sep 17 00:00:00 2001 From: SolomonLake Date: Fri, 24 May 2024 10:29:39 -0500 Subject: [PATCH 3/4] make style --- roboflow/core/version.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/roboflow/core/version.py b/roboflow/core/version.py index b5b258b6..e3db9890 100644 --- a/roboflow/core/version.py +++ b/roboflow/core/version.py @@ -594,11 +594,11 @@ def deploy_paligemma( raise FileNotFoundError(f"No valid files found in model path {model_path}.") import tarfile + with tarfile.open(os.path.join(model_path, "roboflow_deploy.tar"), "w") as tar: for file in files_to_deploy: tar.add(os.path.join(model_path, file), arcname=file) - print("Uploading model to Roboflow... May take several minutes.") self.upload_zip(model_type, model_path, "roboflow_deploy.tar") From a4ad1daad54a461d0c5564693d5bc876e53caac6 Mon Sep 17 00:00:00 2001 From: SolomonLake Date: Fri, 24 May 2024 11:09:22 -0500 Subject: [PATCH 4/4] Add more logs for paligemma deploy --- roboflow/__init__.py | 2 +- roboflow/core/version.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/roboflow/__init__.py b/roboflow/__init__.py index b73d6f4f..85dac634 100644 --- a/roboflow/__init__.py +++ b/roboflow/__init__.py @@ -14,7 +14,7 @@ from roboflow.models import CLIPModel, GazeModel # noqa: F401 from roboflow.util.general import write_line -__version__ = "1.1.29" +__version__ = "1.1.30" def check_key(api_key, model, notebook, num_retries=0): diff --git a/roboflow/core/version.py b/roboflow/core/version.py index e3db9890..cae9b6d6 100644 --- a/roboflow/core/version.py +++ b/roboflow/core/version.py @@ -592,6 +592,7 @@ def deploy_paligemma( if len(files_to_deploy) == 0: raise FileNotFoundError(f"No valid files found in model path {model_path}.") + print(f"Zipping files for deploy: {files_to_deploy}") import tarfile @@ -599,7 +600,7 @@ def deploy_paligemma( for file in files_to_deploy: tar.add(os.path.join(model_path, file), arcname=file) - print("Uploading model to Roboflow... May take several minutes.") + print("Uploading to Roboflow... May take several minutes.") self.upload_zip(model_type, model_path, "roboflow_deploy.tar") def deploy_yolonas(self, model_type: str, model_path: str, filename: str = "weights/best.pt") -> None: