Skip to content

Commit e21c177

Browse files
Copilotanxiangsir
andauthored
Add test for CLIP Processor consistency with AutoImageProcessor (#45)
* Initial plan * Add test for CLIP Processor consistency with AutoImageProcessor Co-authored-by: anxiangsir <31175974+anxiangsir@users.noreply.github.com> * refactor: remove unnecessary skip markers from consistency tests --------- Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: anxiangsir <31175974+anxiangsir@users.noreply.github.com> Co-authored-by: xiangan <anxiangsir@outlook.com>
1 parent aa258a1 commit e21c177

1 file changed

Lines changed: 53 additions & 12 deletions

File tree

tests/test_consistency.py

Lines changed: 53 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -111,10 +111,7 @@ def _compute_file_hash(self, content: str) -> str:
111111
"""Compute SHA256 hash of file content."""
112112
return hashlib.sha256(content.encode("utf-8")).hexdigest()
113113

114-
@pytest.mark.skipif(
115-
True, # Skip by default since network may not be available
116-
reason="Network access required for HuggingFace download"
117-
)
114+
118115
def test_modeling_file_consistency(self, local_modeling_path):
119116
"""Test consistency between local and remote modeling file.
120117
@@ -143,10 +140,7 @@ def test_modeling_file_consistency(self, local_modeling_path):
143140
except Exception as e:
144141
pytest.skip(f"Could not download from HuggingFace: {e}")
145142

146-
@pytest.mark.skipif(
147-
True, # Skip by default since network may not be available
148-
reason="Network access required for HuggingFace download"
149-
)
143+
150144
def test_config_file_consistency(self, local_config_path):
151145
"""Test consistency between local and remote configuration file.
152146
@@ -261,10 +255,7 @@ def test_manual_preprocess_normalization(self, sample_image):
261255
assert not torch.isnan(tensor).any(), "Tensor contains NaN values"
262256
assert not torch.isinf(tensor).any(), "Tensor contains Inf values"
263257

264-
@pytest.mark.skipif(
265-
True, # Skip by default since network may not be available
266-
reason="Network access required for HuggingFace model download"
267-
)
258+
268259
def test_preprocessing_consistency_with_auto_processor(self, sample_image):
269260
"""Test consistency between manual preprocessing and AutoImageProcessor.
270261
@@ -307,6 +298,56 @@ def test_preprocessing_consistency_with_auto_processor(self, sample_image):
307298
except Exception as e:
308299
pytest.skip(f"Could not load AutoImageProcessor: {e}")
309300

301+
302+
def test_preprocessing_consistency_with_clip_processor(self, sample_image):
303+
"""Test consistency between CLIP Processor and AutoImageProcessor.
304+
305+
This test compares the output of CLIP CLIPImageProcessor with
306+
the AutoImageProcessor from HuggingFace onevision-encoder-large
307+
to ensure they produce identical results.
308+
"""
309+
try:
310+
from transformers import AutoImageProcessor, CLIPImageProcessor
311+
312+
# Load the AutoImageProcessor from HuggingFace onevision-encoder-large
313+
auto_preprocessor = AutoImageProcessor.from_pretrained(
314+
"lmms-lab-encoder/onevision-encoder-large",
315+
trust_remote_code=True
316+
)
317+
318+
# Load the CLIP CLIPImageProcessor with same parameters
319+
clip_preprocessor = CLIPImageProcessor.from_pretrained(
320+
"lmms-lab-encoder/onevision-encoder-large",
321+
trust_remote_code=True
322+
)
323+
324+
# Process with AutoImageProcessor
325+
auto_output = auto_preprocessor(images=sample_image, return_tensors="pt")
326+
auto_tensor = auto_output["pixel_values"]
327+
328+
# Process with CLIP CLIPImageProcessor
329+
clip_output = clip_preprocessor(images=sample_image, return_tensors="pt")
330+
clip_tensor = clip_output["pixel_values"]
331+
332+
# Compare outputs
333+
assert auto_tensor.shape == clip_tensor.shape, (
334+
f"Shape mismatch: auto={auto_tensor.shape}, clip={clip_tensor.shape}"
335+
)
336+
337+
# Check if values are close (allowing for small floating point differences)
338+
is_close = torch.allclose(auto_tensor, clip_tensor, rtol=1e-4, atol=1e-4)
339+
if not is_close:
340+
max_diff = (auto_tensor - clip_tensor).abs().max().item()
341+
pytest.fail(
342+
f"Preprocessing outputs differ between AutoImageProcessor and CLIPImageProcessor!\n"
343+
f"Max difference: {max_diff}\n"
344+
f"Auto tensor stats: min={auto_tensor.min()}, max={auto_tensor.max()}\n"
345+
f"CLIP tensor stats: min={clip_tensor.min()}, max={clip_tensor.max()}"
346+
)
347+
348+
except Exception as e:
349+
pytest.skip(f"Could not load processors: {e}")
350+
310351
def test_grayscale_image_conversion(self):
311352
"""Test that grayscale images are converted to RGB."""
312353
# Create a grayscale image

0 commit comments

Comments
 (0)