Spaces:
Running on T4
Running on T4
Add cpu option for segmentation model
Browse files- segment_image.py +9 -3
segment_image.py
CHANGED
|
@@ -52,7 +52,8 @@ class SegmentImage:
|
|
| 52 |
region_overlap_threshold: float = 0.5,
|
| 53 |
class_id_region: int = 1,
|
| 54 |
class_id_line: int = 2,
|
| 55 |
-
min_polygon_points: int = 3
|
|
|
|
| 56 |
|
| 57 |
self.model_path = model_path
|
| 58 |
self.max_size = max_size
|
|
@@ -66,6 +67,7 @@ class SegmentImage:
|
|
| 66 |
self.class_id_region = class_id_region
|
| 67 |
self.class_id_line = class_id_line
|
| 68 |
self.min_polygon_points = min_polygon_points
|
|
|
|
| 69 |
|
| 70 |
# Validate model path
|
| 71 |
if not os.path.exists(self.model_path):
|
|
@@ -81,8 +83,12 @@ class SegmentImage:
|
|
| 81 |
Exception: If model initialization fails
|
| 82 |
"""
|
| 83 |
try:
|
| 84 |
-
self.model = RFDETRSeg2XLarge(
|
| 85 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 86 |
print(f"✓ Segmentation model initialized successfully")
|
| 87 |
except Exception as e:
|
| 88 |
raise RuntimeError(f'Failed to initialize segmentation model: {e}')
|
|
|
|
| 52 |
region_overlap_threshold: float = 0.5,
|
| 53 |
class_id_region: int = 1,
|
| 54 |
class_id_line: int = 2,
|
| 55 |
+
min_polygon_points: int = 3,
|
| 56 |
+
device: str = 'cuda:0'):
|
| 57 |
|
| 58 |
self.model_path = model_path
|
| 59 |
self.max_size = max_size
|
|
|
|
| 67 |
self.class_id_region = class_id_region
|
| 68 |
self.class_id_line = class_id_line
|
| 69 |
self.min_polygon_points = min_polygon_points
|
| 70 |
+
self.device = device
|
| 71 |
|
| 72 |
# Validate model path
|
| 73 |
if not os.path.exists(self.model_path):
|
|
|
|
| 83 |
Exception: If model initialization fails
|
| 84 |
"""
|
| 85 |
try:
|
| 86 |
+
self.model = RFDETRSeg2XLarge(
|
| 87 |
+
pretrain_weights=self.model_path,
|
| 88 |
+
device=self.device
|
| 89 |
+
)
|
| 90 |
+
if self.device != 'cpu':
|
| 91 |
+
self.model.optimize_for_inference()
|
| 92 |
print(f"✓ Segmentation model initialized successfully")
|
| 93 |
except Exception as e:
|
| 94 |
raise RuntimeError(f'Failed to initialize segmentation model: {e}')
|