-
Notifications
You must be signed in to change notification settings - Fork 1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
187fc8f
commit 1de6d91
Showing
33 changed files
with
699 additions
and
880 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
from typing import Optional, Any | ||
|
||
import torch | ||
|
||
from surya.settings import settings | ||
|
||
|
||
class ModelLoader: | ||
def __init__(self, checkpoint: Optional[str] = None): | ||
self.checkpoint = checkpoint | ||
|
||
def model( | ||
self, | ||
device: torch.device | str | None = settings.TORCH_DEVICE_MODEL, | ||
dtype: Optional[torch.dtype | str] = settings.MODEL_DTYPE) -> Any: | ||
raise NotImplementedError() | ||
|
||
def processor( | ||
self | ||
) -> Any: | ||
raise NotImplementedError() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,43 @@ | ||
from typing import Optional | ||
|
||
import torch | ||
|
||
from surya.common.load import ModelLoader | ||
from surya.detection.processor import SegformerImageProcessor | ||
|
||
from surya.detection.model.config import EfficientViTConfig | ||
from surya.detection.model.encoderdecoder import EfficientViTForSemanticSegmentation | ||
from surya.settings import settings | ||
|
||
|
||
class DetectionModelLoader(ModelLoader): | ||
def __init__(self, checkpoint: Optional[str] = None): | ||
super().__init__(checkpoint) | ||
|
||
if self.checkpoint is None: | ||
self.checkpoint = settings.DETECTOR_MODEL_CHECKPOINT | ||
|
||
def model( | ||
self, | ||
device: Optional[torch.device | str] = None, | ||
dtype: Optional[torch.dtype | str] = None | ||
): | ||
config = EfficientViTConfig.from_pretrained(self.checkpoint) | ||
model = EfficientViTForSemanticSegmentation.from_pretrained(self.checkpoint, torch_dtype=dtype, config=config, | ||
ignore_mismatched_sizes=True) | ||
model = model.to(device) | ||
model = model.eval() | ||
|
||
if settings.DETECTOR_STATIC_CACHE: | ||
torch.set_float32_matmul_precision('high') | ||
torch._dynamo.config.cache_size_limit = 1 | ||
torch._dynamo.config.suppress_errors = False | ||
|
||
print(f"Compiling detection model {self.checkpoint} on device {device} with dtype {dtype}") | ||
model = torch.compile(model) | ||
|
||
print(f"Loaded detection model {self.checkpoint} on device {device} with dtype {dtype}") | ||
return model | ||
|
||
def processor(self): | ||
return SegformerImageProcessor.from_pretrained(self.checkpoint) |
File renamed without changes.
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.