[tests] remove TF tests (uses of require_tf) (#38944)
* remove uses of require_tf * remove redundant import guards * this class has no tests * nits * del tf rng comment
This commit is contained in:
@@ -17,15 +17,10 @@ import unittest
|
||||
|
||||
import numpy as np
|
||||
|
||||
from transformers.testing_utils import (
|
||||
require_tf,
|
||||
require_torch,
|
||||
require_torchvision,
|
||||
require_vision,
|
||||
)
|
||||
from transformers.utils import is_tf_available, is_torch_available, is_vision_available
|
||||
from transformers.testing_utils import require_torch, require_torchvision, require_vision
|
||||
from transformers.utils import is_torch_available, is_vision_available
|
||||
|
||||
from ...test_processing_common import ProcessorTesterMixin, prepare_image_inputs
|
||||
from ...test_processing_common import ProcessorTesterMixin
|
||||
|
||||
|
||||
if is_vision_available():
|
||||
@@ -38,11 +33,6 @@ if is_torch_available():
|
||||
|
||||
from transformers.models.sam.image_processing_sam import _mask_to_rle_pytorch
|
||||
|
||||
if is_tf_available():
|
||||
import tensorflow as tf
|
||||
|
||||
from transformers.models.sam.image_processing_sam import _mask_to_rle_tf
|
||||
|
||||
|
||||
@require_vision
|
||||
@require_torchvision
|
||||
@@ -202,143 +192,3 @@ class SamProcessorTest(ProcessorTesterMixin, unittest.TestCase):
|
||||
self.assertEqual(len(rle), 1)
|
||||
self.assertEqual(rle[0]["size"], [2, 2])
|
||||
self.assertEqual(rle[0]["counts"], [1, 3]) # 1 zero, followed by 3 ones
|
||||
|
||||
|
||||
@require_vision
|
||||
@require_tf
|
||||
class TFSamProcessorTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmpdirname = tempfile.mkdtemp()
|
||||
image_processor = SamImageProcessor()
|
||||
processor = SamProcessor(image_processor)
|
||||
processor.save_pretrained(self.tmpdirname)
|
||||
|
||||
def get_image_processor(self, **kwargs):
|
||||
return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmpdirname)
|
||||
|
||||
# This is to avoid repeating the skipping of the common tests
|
||||
def prepare_image_inputs(self):
|
||||
"""This function prepares a list of PIL images."""
|
||||
return prepare_image_inputs()
|
||||
|
||||
def test_save_load_pretrained_additional_features(self):
|
||||
processor = SamProcessor(image_processor=self.get_image_processor())
|
||||
processor.save_pretrained(self.tmpdirname)
|
||||
|
||||
image_processor_add_kwargs = self.get_image_processor(do_normalize=False, padding_value=1.0)
|
||||
|
||||
processor = SamProcessor.from_pretrained(self.tmpdirname, do_normalize=False, padding_value=1.0)
|
||||
|
||||
self.assertEqual(processor.image_processor.to_json_string(), image_processor_add_kwargs.to_json_string())
|
||||
self.assertIsInstance(processor.image_processor, SamImageProcessor)
|
||||
|
||||
def test_image_processor(self):
|
||||
image_processor = self.get_image_processor()
|
||||
|
||||
processor = SamProcessor(image_processor=image_processor)
|
||||
|
||||
image_input = self.prepare_image_inputs()
|
||||
|
||||
input_feat_extract = image_processor(image_input, return_tensors="np")
|
||||
input_processor = processor(images=image_input, return_tensors="np")
|
||||
|
||||
input_feat_extract.pop("original_sizes") # pop original_sizes as it is popped in the processor
|
||||
input_feat_extract.pop("reshaped_input_sizes") # pop reshaped_input_sizes as it is popped in the processor
|
||||
|
||||
for key in input_feat_extract.keys():
|
||||
self.assertAlmostEqual(input_feat_extract[key].sum(), input_processor[key].sum(), delta=1e-2)
|
||||
|
||||
@require_tf
|
||||
def test_post_process_masks(self):
|
||||
image_processor = self.get_image_processor()
|
||||
|
||||
processor = SamProcessor(image_processor=image_processor)
|
||||
dummy_masks = [tf.ones((1, 3, 5, 5))]
|
||||
|
||||
original_sizes = [[1764, 2646]]
|
||||
|
||||
reshaped_input_size = [[683, 1024]]
|
||||
masks = processor.post_process_masks(dummy_masks, original_sizes, reshaped_input_size, return_tensors="tf")
|
||||
self.assertEqual(masks[0].shape, (1, 3, 1764, 2646))
|
||||
|
||||
masks = processor.post_process_masks(
|
||||
dummy_masks,
|
||||
tf.convert_to_tensor(original_sizes),
|
||||
tf.convert_to_tensor(reshaped_input_size),
|
||||
return_tensors="tf",
|
||||
)
|
||||
self.assertEqual(masks[0].shape, (1, 3, 1764, 2646))
|
||||
|
||||
# should also work with np
|
||||
dummy_masks = [np.ones((1, 3, 5, 5))]
|
||||
masks = processor.post_process_masks(
|
||||
dummy_masks, np.array(original_sizes), np.array(reshaped_input_size), return_tensors="tf"
|
||||
)
|
||||
|
||||
self.assertEqual(masks[0].shape, (1, 3, 1764, 2646))
|
||||
|
||||
dummy_masks = [[1, 0], [0, 1]]
|
||||
with self.assertRaises(tf.errors.InvalidArgumentError):
|
||||
masks = processor.post_process_masks(
|
||||
dummy_masks, np.array(original_sizes), np.array(reshaped_input_size), return_tensors="tf"
|
||||
)
|
||||
|
||||
def test_rle_encoding(self):
|
||||
"""
|
||||
Test the run-length encoding function.
|
||||
"""
|
||||
# Test that a mask of all zeros returns a single run [height * width].
|
||||
input_mask = tf.zeros((1, 2, 2), dtype=tf.int64) # shape: 1 x 2 x 2
|
||||
rle = _mask_to_rle_tf(input_mask)
|
||||
|
||||
self.assertEqual(len(rle), 1)
|
||||
self.assertEqual(rle[0]["size"], [2, 2])
|
||||
# For a 2x2 all-zero mask, we expect a single run of length 4:
|
||||
self.assertEqual(rle[0]["counts"], [4])
|
||||
|
||||
# Test that a mask of all ones returns [0, height * width].
|
||||
input_mask = tf.ones((1, 2, 2), dtype=tf.int64) # shape: 1 x 2 x 2
|
||||
rle = _mask_to_rle_tf(input_mask)
|
||||
|
||||
self.assertEqual(len(rle), 1)
|
||||
self.assertEqual(rle[0]["size"], [2, 2])
|
||||
# For a 2x2 all-one mask, we expect two runs: [0, 4].
|
||||
self.assertEqual(rle[0]["counts"], [0, 4])
|
||||
|
||||
# Test a mask with mixed 0s and 1s to ensure the run-length encoding is correct.
|
||||
# Example mask:
|
||||
# Row 0: [0, 1]
|
||||
# Row 1: [1, 1]
|
||||
# This is shape (1, 2, 2).
|
||||
# Flattened in Fortran order -> [0, 1, 1, 1].
|
||||
# The RLE for [0,1,1,1] is [1, 3].
|
||||
input_mask = tf.constant([[[0, 1], [1, 1]]], dtype=tf.int64)
|
||||
rle = _mask_to_rle_tf(input_mask)
|
||||
|
||||
self.assertEqual(len(rle), 1)
|
||||
self.assertEqual(rle[0]["size"], [2, 2])
|
||||
self.assertEqual(rle[0]["counts"], [1, 3]) # 1 zero, followed by 3 ones
|
||||
|
||||
|
||||
@require_vision
|
||||
@require_torchvision
|
||||
class SamProcessorEquivalenceTest(unittest.TestCase):
|
||||
def setUp(self):
|
||||
self.tmpdirname = tempfile.mkdtemp()
|
||||
image_processor = SamImageProcessor()
|
||||
processor = SamProcessor(image_processor)
|
||||
processor.save_pretrained(self.tmpdirname)
|
||||
|
||||
def get_image_processor(self, **kwargs):
|
||||
return AutoProcessor.from_pretrained(self.tmpdirname, **kwargs).image_processor
|
||||
|
||||
def tearDown(self):
|
||||
shutil.rmtree(self.tmpdirname)
|
||||
|
||||
# This is to avoid repeating the skipping of the common tests
|
||||
def prepare_image_inputs(self):
|
||||
"""This function prepares a list of PIL images."""
|
||||
return prepare_image_inputs()
|
||||
|
||||
Reference in New Issue
Block a user