1- #!/usr/bin/env python3
1+ #!/usr/bin/env -S uv run --script
22#
33# Copyright (C) 2024-2025 Intel Corporation
44# SPDX-License-Identifier: Apache-2.0
88PyTorch to OpenVINO Model Converter
99
1010Usage:
11- python model_converter.py config.json -o ./output_models
11+ uv run python model_converter.py config.json -o ./output_models
1212
1313"""
1414
@@ -74,7 +74,11 @@ def get_labels(self, label_set: str) -> Optional[str]:
7474
7575 return None
7676
77- def download_weights (self , url : str , filename : Optional [str ] = None ) -> Path :
77+ def download_weights (
78+ self ,
79+ url : str ,
80+ filename : Optional [str ] = None ,
81+ ) -> Path : # nosemgrep: python.lang.security.audit.dynamic-urllib-use-detected.dynamic-urllib-use-detected
7882 """
7983 Download model weights from URL with caching.
8084
@@ -101,14 +105,17 @@ def download_weights(self, url: str, filename: Optional[str] = None) -> Path:
101105 urllib .request .urlretrieve ( # noqa: S310
102106 url ,
103107 cached_file ,
104- ) # nosemgrep: python.lang.security.audit.dynamic-urllib-use-detected.dynamic-urllib-use-detected
108+ )
105109 self .logger .info ("✓ Download complete" )
106110 return cached_file
107111 except Exception as e :
108112 self .logger .error (f"Failed to download weights: { e } " )
109113 raise
110114
111- def load_model_class (self , class_path : str ) -> type :
115+ def load_model_class (
116+ self ,
117+ class_path : str ,
118+ ) -> type : # nosemgrep: python.lang.security.audit.non-literal-import.non-literal-import
112119 """
113120 Dynamically load a model class from a Python path.
114121
@@ -123,15 +130,18 @@ def load_model_class(self, class_path: str) -> type:
123130 self .logger .debug (f"Importing module: { module_path } " )
124131 module = importlib .import_module (
125132 module_path ,
126- ) # nosemgrep: python.lang.security.audit.non-literal-import.non-literal-import
133+ )
127134 model_class = getattr (module , class_name )
128135 self .logger .debug (f"Loaded class: { class_name } " )
129136 return model_class
130137 except Exception as e :
131138 self .logger .error (f"Failed to import module { module_path } : { e } " )
132139 raise
133140
134- def load_checkpoint (self , checkpoint_path : Path ) -> Dict [str , Any ]:
141+ def load_checkpoint (
142+ self ,
143+ checkpoint_path : Path ,
144+ ) -> Dict [str , Any ]: # nosemgrep: trailofbits.python.pickles-in-pytorch.pickles-in-pytorch
135145 """
136146 Load PyTorch checkpoint file.
137147
@@ -146,7 +156,7 @@ def load_checkpoint(self, checkpoint_path: Path) -> Dict[str, Any]:
146156 checkpoint_path ,
147157 map_location = "cpu" ,
148158 weights_only = True ,
149- ) # nosemgrep: trailofbits.python.pickles-in-pytorch.pickles-in-pytorch
159+ )
150160 self .logger .debug (f"Loaded checkpoint from: { checkpoint_path } " )
151161 return checkpoint
152162 except Exception as e :
@@ -480,16 +490,16 @@ def main():
480490 epilog = """
481491Examples:
482492 # Convert all models in config
483- python model_converter.py config.json -o ./models
493+ uv run python model_converter.py config.json -o ./models
484494
485495 # Convert a specific model
486- python model_converter.py config.json -o ./models --model resnet50
496+ uv run python model_converter.py config.json -o ./models --model resnet50
487497
488498 # List all models in config
489- python model_converter.py config.json --list
499+ uv run python model_converter.py config.json --list
490500
491501 # Enable verbose logging
492- python model_converter.py config.json -o ./models -v
502+ uv run python model_converter.py config.json -o ./models -v
493503 """ ,
494504 )
495505
0 commit comments