Skip to content

Commit

Permalink
[FIX] silencing of huggingface transformer warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
sophiamaedler committed Feb 2, 2025
1 parent f5bb1bf commit 3a9a946
Showing 1 changed file with 27 additions and 10 deletions.
37 changes: 27 additions & 10 deletions src/scportrait/pipeline/classification.py
Original file line number Diff line number Diff line change
Expand Up @@ -1201,23 +1201,39 @@ def _load_model(self):

# silence warnings from transformers that are not relevant here
# we do actually just want to load some of the weights to access the convnext features
import warnings

warnings.filterwarnings(
"ignore",
message="Some weights of the model checkpoint at facebook/convnext-xlarge-224-22k were not used.*",
)
warnings.filterwarnings(
"ignore",
message="Could not find image processor class in the image processor config.*",
)

model = ConvNextModel.from_pretrained("facebook/convnext-xlarge-224-22k")
model.eval()
model.to(self.inference_device)

return model

def _silence_warnings(self):
import logging
from transformers import logging as hf_logging

# Create a custom filter class to suppress specific warnings from huggingfaces transformers
class SpecificMessageFilter(logging.Filter):
def __init__(self, suppressed_keywords):
super().__init__()
self.suppressed_keywords = suppressed_keywords

def filter(self, record):
return not any(
keyword in record.getMessage()
for keyword in self.suppressed_keywords
)

# Keywords to suppress
suppressed_keywords = [
"Some weights of the model checkpoint at facebook",
"Could not find image processor class in the image processor config",
]

transformers_logger = hf_logging.get_logger()
for handler in transformers_logger.handlers:
handler.addFilter(SpecificMessageFilter(suppressed_keywords))

def _setup_transform(self):
# lazy imports
from transformers import AutoImageProcessor
Expand Down Expand Up @@ -1314,6 +1330,7 @@ class based on the previous single-cell extraction. Therefore, only the second a
inference_device: "cuda"
"""

self._silence_warnings()
self.create_temp_dir() # setup directory for memory mapped temp file generation using alphabase.io

self.extraction_dir = extraction_dir
Expand Down

0 comments on commit 3a9a946

Please sign in to comment.