Skip to content

Commit e2ceac2

Browse files
committed
remove null values from saved preporcessor file for fast image processor
1 parent 875c36e commit e2ceac2

File tree

2 files changed

+21
-4
lines changed

2 files changed

+21
-4
lines changed

src/transformers/image_processing_utils_fast.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -936,6 +936,10 @@ def _preprocess(
936936

937937
def to_dict(self):
938938
encoder_dict = super().to_dict()
939+
940+
# Remove attributes with None values (potentially inherited from the base class but not used)
941+
encoder_dict = {k: v for k, v in encoder_dict.items() if v is not None}
942+
939943
encoder_dict.pop("_valid_processor_keys", None)
940944
encoder_dict.pop("_valid_kwargs_names", None)
941945
return encoder_dict

tests/test_image_processing_common.py

Lines changed: 17 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -341,9 +341,10 @@ def test_save_load_fast_slow(self):
341341
}
342342
dict_fast_0 = {key: dict_fast_0[key] for key in set(dict_fast_0) & set(dict_fast_1)}
343343
dict_fast_1 = {key: dict_fast_1[key] for key in set(dict_fast_0) & set(dict_fast_1)}
344-
# check that all additional keys are None, except for `default_to_square` and `data_format` which are only set in fast processors
344+
# Fast processors filter None values from to_dict(), so differences should only be special keys
345345
self.assertTrue(
346-
all(value is None for key, value in difference.items() if key not in ["default_to_square", "data_format"])
346+
all(key in ["default_to_square", "data_format"] for key in difference.keys()),
347+
f"Fast processors should only differ in special keys, found: {list(difference.keys())}",
347348
)
348349
# check that the remaining keys are the same
349350
self.assertEqual(dict_fast_0, dict_fast_1)
@@ -391,9 +392,10 @@ def test_save_load_fast_slow_auto(self):
391392
}
392393
dict_fast_0 = {key: dict_fast_0[key] for key in set(dict_fast_0) & set(dict_fast_1)}
393394
dict_fast_1 = {key: dict_fast_1[key] for key in set(dict_fast_0) & set(dict_fast_1)}
394-
# check that all additional keys are None, except for `default_to_square` and `data_format` which are only set in fast processors
395+
# Fast processors filter None values from to_dict(), so differences should only be special keys
395396
self.assertTrue(
396-
all(value is None for key, value in difference.items() if key not in ["default_to_square", "data_format"])
397+
all(key in ["default_to_square", "data_format"] for key in difference.keys()),
398+
f"Fast processors should only differ in special keys, found: {list(difference.keys())}",
397399
)
398400
# check that the remaining keys are the same
399401
self.assertEqual(dict_fast_0, dict_fast_1)
@@ -693,6 +695,17 @@ def _is_old_model_by_commit_date(model_type, date_cutoff=(2025, 9, 1)):
693695
f"a fast image processor implementation. Please implement the corresponding fast processor.",
694696
)
695697

698+
def test_fast_image_processor_to_dict_no_none_values(self):
699+
"""Test that fast image processors don't include None values in to_dict() output."""
700+
if self.fast_image_processing_class is None:
701+
self.skipTest("Skipping test as fast image processor is not defined")
702+
703+
image_processor = self.fast_image_processing_class(**self.image_processor_dict)
704+
processor_dict = image_processor.to_dict()
705+
706+
none_values = [k for k, v in processor_dict.items() if v is None]
707+
self.assertEqual(len(none_values), 0, f"Found None values in to_dict(): {none_values}")
708+
696709

697710
class AnnotationFormatTestMixin:
698711
# this mixin adds a test to assert that usages of the

0 commit comments

Comments
 (0)