forked from pepisg/simple_segmentation_toolkit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathautolabel.py
More file actions
executable file
·104 lines (87 loc) · 2.99 KB
/
autolabel.py
File metadata and controls
executable file
·104 lines (87 loc) · 2.99 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
#!/usr/bin/env python3
"""Auto-labeling script for semantic segmentation."""
import argparse
import yaml
from pathlib import Path
from typing import Dict
import os
from sam_labeler import LabelingSession
def load_config(config_path: Path) -> Dict:
"""Load configuration from YAML file."""
with open(config_path, 'r') as f:
config = yaml.safe_load(f)
return config
def check_gpu(device: str) -> str:
"""Check GPU availability and return appropriate device."""
if device == "cuda":
try:
import torch
if torch.cuda.is_available():
print(f"✓ GPU available: {torch.cuda.get_device_name(0)}")
print(f" CUDA version: {torch.version.cuda}")
return "cuda"
else:
print("Warning: CUDA not available, falling back to CPU")
return "cpu"
except ImportError:
print("Warning: PyTorch not found, falling back to CPU")
return "cpu"
except Exception as e:
print(f"Warning: GPU check failed ({e}), falling back to CPU")
return "cpu"
return device
def main():
"""Main entry point."""
parser = argparse.ArgumentParser(
description="Auto-label images with segmentation masks using Grounding SAM",
formatter_class=argparse.RawDescriptionHelpFormatter,
epilog="""
Examples:
python autolabel.py --input_dir data/raw_images
python autolabel.py --input_dir data/raw_images --config configs/ontology.yaml --device cpu
Note: Accepted images will be moved to accepted/images, masks to accepted/masks.
Discarded images will be moved to discarded/.
The raw_images folder will be empty when finished.
"""
)
parser.add_argument(
"--input_dir",
type=str,
default="data/raw_images",
help="Directory containing input images"
)
parser.add_argument(
"--config",
type=str,
default="configs/ontology.yaml",
help="Path to configuration YAML file"
)
parser.add_argument(
"--device",
type=str,
default="cuda",
choices=["cuda", "cpu"],
help="Device to use (cuda for GPU, cpu for CPU)"
)
args = parser.parse_args()
os.environ['PYTORCH_CUDA_ALLOC_CONF'] = 'expandable_segments:True'
# Load configuration
config_path = Path(args.config)
if not config_path.exists():
raise FileNotFoundError(f"Configuration file not found: {config_path}")
config = load_config(config_path)
# Check GPU
device = check_gpu(args.device)
# Setup paths
script_dir = Path(__file__).parent
input_dir = script_dir / args.input_dir if not Path(args.input_dir).is_absolute() else Path(args.input_dir)
# Create and run labeling session
session = LabelingSession(
image_dir=input_dir,
config=config,
config_path=config_path,
device=device
)
session.run()
if __name__ == "__main__":
main()