Skip to content

Commit 1262336

Browse files
committed
style
1 parent 96ecfa0 commit 1262336

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

tools/model_converter/README.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ uv pip install torch torchvision openvino
3030
### Basic Usage
3131

3232
```bash
33-
python model_converter.py config.json -o ./output_models
33+
uv run python model_converter.py config.json -o ./output_models
3434
```
3535

3636
### Command-Line Options
@@ -55,31 +55,31 @@ options:
5555
**List all models in configuration:**
5656

5757
```bash
58-
python model_converter.py example_config.json --list
58+
uv run python model_converter.py example_config.json --list
5959
```
6060

6161
**Convert all models:**
6262

6363
```bash
64-
python model_converter.py example_config.json -o ./converted_models
64+
uv run python model_converter.py example_config.json -o ./converted_models
6565
```
6666

6767
**Convert a specific model:**
6868

6969
```bash
70-
python model_converter.py example_config.json -o ./converted_models --model resnet50
70+
uv run python model_converter.py example_config.json -o ./converted_models --model resnet50
7171
```
7272

7373
**Use custom cache directory:**
7474

7575
```bash
76-
python model_converter.py example_config.json -o ./output -c ./my_cache
76+
uv run python model_converter.py example_config.json -o ./output -c ./my_cache
7777
```
7878

7979
**Enable verbose logging:**
8080

8181
```bash
82-
python model_converter.py example_config.json -o ./output -v
82+
uv run python model_converter.py example_config.json -o ./output -v
8383
```
8484

8585
## Configuration File Format

tools/model_converter/model_converter.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
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
@@ -8,7 +8,7 @@
88
PyTorch to OpenVINO Model Converter
99
1010
Usage:
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="""
481491
Examples:
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

Comments
 (0)