codingutf8 2021 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache orglicenseslicense2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license testing suite for the pytorch segformer model import unittest from transformers import segformerconfig istorchavailable isvisionavailable from transformers models auto import getvalues from transformers testingutils import requiretorch slow torchdevice from testconfigurationcommon import configtester from testmodelingcommon import modeltestermixin floatstensor idstensor from testpipelinemixin import pipelinetestermixin if istorchavailable import torch from transformers import modelmapping segformerforimageclassification segformerforsemanticsegmentation segformermodel from transformers models segformer modelingsegformer import segformerpretrainedmodelarchivelist if isvisionavailable from pil import image from transformers import segformerimageprocessor class segformerconfigtesterconfigtester def createandtestconfigcommonpropertiesself config self configclassself inputsdict self parent asserttruehasattrconfig hiddensizes self parent asserttruehasattrconfig numattentionheads self parent asserttruehasattrconfig numencoderblocks class segformermodeltester def init self parent batchsize13 imagesize64 numchannels3 numencoderblocks4 depths1 1 1 1 srratios8 4 2 1 hiddensizes8 8 16 16 downsamplingrates1 4 8 16 numattentionheads1 1 2 2 istrainingtrue uselabelstrue hiddenactgelu hiddendropoutprob0 1 attentionprobsdropoutprob0 1 initializerrange0 02 numlabels3 scopenone self parent parent self batchsize batchsize self imagesize imagesize self numchannels numchannels self numencoderblocks numencoderblocks self srratios srratios self depths depths self hiddensizes hiddensizes self downsamplingrates downsamplingrates self numattentionheads numattentionheads self istraining istraining self uselabels uselabels self hiddenact hiddenact self hiddendropoutprob hiddendropoutprob self attentionprobsdropoutprob attentionprobsdropoutprob self initializerrange initializerrange self numlabels numlabels self scope scope def prepareconfigandinputsself pixelvalues floatstensorself batchsize self numchannels self imagesize self imagesize labels none if self uselabels labels idstensorself batchsize self imagesize self imagesize self numlabels config self getconfig return config pixelvalues labels def getconfigself return segformerconfig imagesizeself imagesize numchannelsself numchannels numencoderblocksself numencoderblocks depthsself depths hiddensizesself hiddensizes numattentionheadsself numattentionheads hiddenactself hiddenact hiddendropoutprobself hiddendropoutprob attentionprobsdropoutprobself attentionprobsdropoutprob initializerrangeself initializerrange def createandcheckmodelself config pixelvalues labels model segformermodelconfigconfig model totorchdevice model eval result modelpixelvalues expectedheight expectedwidth self imagesize self downsamplingrates1 2 self parent assertequal result lasthiddenstate shape self batchsize self hiddensizes1 expectedheight expectedwidth def createandcheckforimagesegmentationself config pixelvalues labels config numlabels self numlabels model segformerforsemanticsegmentationconfig model totorchdevice model eval result modelpixelvalues self parent assertequal result logits shape self batchsize self numlabels self imagesize 4 self imagesize 4 result modelpixelvalues labelslabels self parent assertequal result logits shape self batchsize self numlabels self imagesize 4 self imagesize 4 self parent assertgreaterresult loss 0 0 def createandcheckforbinaryimagesegmentationself config pixelvalues labels config numlabels 1 model segformerforsemanticsegmentationconfigconfig model totorchdevice model eval labels torch randint0 1 self batchsize self imagesize self imagesize totorchdevice result modelpixelvalues labelslabels self parent assertgreaterresult loss 0 0 def prepareconfigandinputsforcommonself configandinputs self prepareconfigandinputs config pixelvalues labels configandinputs inputsdict pixelvalues pixelvalues return config inputsdict requiretorch class segformermodeltestmodeltestermixin pipelinetestermixin unittest testcase allmodelclasses segformermodel segformerforsemanticsegmentation segformerforimageclassification if istorchavailable else pipelinemodelmapping featureextraction segformermodel imageclassification segformerforimageclassification imagesegmentation segformerforsemanticsegmentation if istorchavailable else fxcompatible true testheadmasking false testpruning false testresizeembeddings false def setupself self modeltester segformermodeltesterself self configtester segformerconfigtesterself configclasssegformerconfig def testconfigself self configtester runcommontests def testmodelself configandinputs self modeltester prepareconfigandinputs self modeltester createandcheckmodelconfigandinputs def testforbinaryimagesegmentationself configandinputs self modeltester prepareconfigandinputs self modeltester createandcheckforbinaryimagesegmentationconfigandinputs def testforimagesegmentationself configandinputs self modeltester prepareconfigandinputs self modeltester createandcheckforimagesegmentationconfigandinputs unittest skipsegformer does not use inputsembeds def testinputsembedsself pass unittest skipsegformer does not have getinputembeddings method and getoutputembeddings methods def testmodelcommonattributesself pass def testattentionoutputsself config inputsdict self modeltester prepareconfigandinputsforcommon config returndict true for modelclass in self allmodelclasses inputsdictoutputattentions true inputsdictoutputhiddenstates false config returndict true model modelclassconfig model totorchdevice model eval with torch nograd outputs modelself prepareforclassinputsdict modelclass attentions outputs attentions expectednumattentions sumself modeltester depths self assertequallenattentions expectednumattentions check that outputattentions also work using config del inputsdictoutputattentions config outputattentions true model modelclassconfig model totorchdevice model eval with torch nograd outputs modelself prepareforclassinputsdict modelclass attentions outputs attentions self assertequallenattentions expectednumattentions verify the first attentions first block first layer expectedseqlen self modeltester imagesize 4 2 expectedreducedseqlen self modeltester imagesize 4 self modeltester srratios0 2 self assertlistequal listattentions0 shape3 self modeltester numattentionheads0 expectedseqlen expectedreducedseqlen verify the last attentions last block last layer expectedseqlen self modeltester imagesize 32 2 expectedreducedseqlen self modeltester imagesize 32 self modeltester srratios1 2 self assertlistequal listattentions1 shape3 self modeltester numattentionheads1 expectedseqlen expectedreducedseqlen outlen lenoutputs check attention is always last and order is fine inputsdictoutputattentions true inputsdictoutputhiddenstates true model modelclassconfig model totorchdevice model eval with torch nograd outputs modelself prepareforclassinputsdict modelclass self assertequaloutlen 1 lenoutputs selfattentions outputs attentions self assertequallenselfattentions expectednumattentions verify the first attentions first block first layer expectedseqlen self modeltester imagesize 4 2 expectedreducedseqlen self modeltester imagesize 4 self modeltester srratios0 2 self assertlistequal listselfattentions0 shape3 self modeltester numattentionheads0 expectedseqlen expectedreducedseqlen def testhiddenstatesoutputself def checkhiddenstatesoutputinputsdict config modelclass model modelclassconfig model totorchdevice model eval with torch nograd outputs modelself prepareforclassinputsdict modelclass hiddenstates outputs hiddenstates expectednumlayers self modeltester numencoderblocks self assertequallenhiddenstates expectednumlayers verify the first hidden states first block self assertlistequal listhiddenstates0 shape3 self modeltester hiddensizes0 self modeltester imagesize 4 self modeltester imagesize 4 config inputsdict self modeltester prepareconfigandinputsforcommon for modelclass in self allmodelclasses inputsdictoutputhiddenstates true checkhiddenstatesoutputinputsdict config modelclass check that outputhiddenstates also work using config del inputsdictoutputhiddenstates config outputhiddenstates true checkhiddenstatesoutputinputsdict config modelclass def testtrainingself if not self modeltester istraining return config inputsdict self modeltester prepareconfigandinputsforcommon config returndict true for modelclass in self allmodelclasses if modelclass in getvaluesmodelmapping continue model modelclassconfig model totorchdevice model train inputs self prepareforclassinputsdict modelclass returnlabelstrue loss modelinputs loss loss backward slow def testmodelfrompretrainedself for modelname in segformerpretrainedmodelarchivelist 1 model segformermodel frompretrainedmodelname self assertisnotnonemodel we will verify our results on an image of cute cats def prepareimg image image open testsfixturestestssamplescoco000000039769 png return image requiretorch class segformermodelintegrationtestunittest testcase slow def testinferenceimagesegmentationadeself only resize normalize imageprocessor segformerimageprocessor imagescale512 512 keepratiofalse alignfalse dorandomcropfalse model segformerforsemanticsegmentation frompretrainednvidiasegformerb0finetunedade512512 to torchdevice image prepareimg encodedinputs imageprocessorimagesimage returntensorspt pixelvalues encodedinputs pixelvalues totorchdevice with torch nograd outputs modelpixelvalues expectedshape torch size1 model config numlabels 128 128 self assertequaloutputs logits shape expectedshape expectedslice torch tensor 4 6310 5 5232 6 2356 5 1921 6 1444 6 5996 5 4424 6 2790 6 7574 12 1391 13 3122 13 9554 12 8732 13 9352 14 3563 12 9438 13 8226 14 2513 12 5134 13 4686 14 4915 12 8669 14 4343 14 7758 13 2523 14 5819 15 0694 totorchdevice self asserttruetorch allcloseoutputs logits0 3 3 3 expectedslice atol1e4 slow def testinferenceimagesegmentationcityself only resize normalize imageprocessor segformerimageprocessor imagescale512 512 keepratiofalse alignfalse dorandomcropfalse model segformerforsemanticsegmentation frompretrained nvidiasegformerb1finetunedcityscapes10241024 totorchdevice image prepareimg encodedinputs imageprocessorimagesimage returntensorspt pixelvalues encodedinputs pixelvalues totorchdevice with torch nograd outputs modelpixelvalues expectedshape torch size1 model config numlabels 128 128 self assertequaloutputs logits shape expectedshape expectedslice torch tensor 13 5748 13 9111 12 6500 14 3500 15 3683 14 2328 14 7532 16 0424 15 6087 17 1651 15 8725 12 9653 17 2580 17 3718 14 8223 16 6058 16 8783 16 7452 3 6456 3 0209 1 4203 3 0797 3 1959 2 0000 1 8757 1 9217 1 6997 totorchdevice self asserttruetorch allcloseoutputs logits0 3 3 3 expectedslice atol1e1 slow def testpostprocessingsemanticsegmentationself only resize normalize imageprocessor segformerimageprocessor imagescale512 512 keepratiofalse alignfalse dorandomcropfalse model segformerforsemanticsegmentation frompretrainednvidiasegformerb0finetunedade512512 to torchdevice image prepareimg encodedinputs imageprocessorimagesimage returntensorspt pixelvalues encodedinputs pixelvalues totorchdevice with torch nograd outputs modelpixelvalues outputs logits outputs logits detach cpu segmentation imageprocessor postprocesssemanticsegmentationoutputsoutputs targetsizes500 300 expectedshape torch size500 300 self assertequalsegmentation0 shape expectedshape segmentation imageprocessor postprocesssemanticsegmentationoutputsoutputs expectedshape torch size128 128 self assertequalsegmentation0 shape expectedshape coding utf 8 2021 the huggingface inc team licensed under the apache license version 2 0 the license you may not use this file except in compliance with the license you may obtain a copy of the license at http www apache org licenses license 2 0 unless required by applicable law or agreed to in writing software distributed under the license is distributed on an as is basis without warranties or conditions of any kind either express or implied see the license for the specific language governing permissions and limitations under the license testing suite for the pytorch segformer model check that output_attentions also work using config verify the first attentions first block first layer verify the last attentions last block last layer check attention is always last and order is fine verify the first attentions first block first layer verify the first hidden states first block check that output_hidden_states also work using config we will verify our results on an image of cute cats only resize normalize only resize normalize only resize normalize
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
    import torch

    from transformers import (
        MODEL_MAPPING,
        SegformerForImageClassification,
        SegformerForSemanticSegmentation,
        SegformerModel,
    )
    from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
    from PIL import Image

    from transformers import SegformerImageProcessor


class SegformerConfigTester(ConfigTester):
    def create_and_test_config_common_properties(self):
        config = self.config_class(**self.inputs_dict)
        self.parent.assertTrue(hasattr(config, "hidden_sizes"))
        self.parent.assertTrue(hasattr(config, "num_attention_heads"))
        self.parent.assertTrue(hasattr(config, "num_encoder_blocks"))


class SegformerModelTester:
    def __init__(
        self,
        parent,
        batch_size=13,
        image_size=64,
        num_channels=3,
        num_encoder_blocks=4,
        depths=[1, 1, 1, 1],
        sr_ratios=[8, 4, 2, 1],
        hidden_sizes=[8, 8, 16, 16],
        downsampling_rates=[1, 4, 8, 16],
        num_attention_heads=[1, 1, 2, 2],
        is_training=True,
        use_labels=True,
        hidden_act="gelu",
        hidden_dropout_prob=0.1,
        attention_probs_dropout_prob=0.1,
        initializer_range=0.02,
        num_labels=3,
        scope=None,
    ):
        self.parent = parent
        self.batch_size = batch_size
        self.image_size = image_size
        self.num_channels = num_channels
        self.num_encoder_blocks = num_encoder_blocks
        self.sr_ratios = sr_ratios
        self.depths = depths
        self.hidden_sizes = hidden_sizes
        self.downsampling_rates = downsampling_rates
        self.num_attention_heads = num_attention_heads
        self.is_training = is_training
        self.use_labels = use_labels
        self.hidden_act = hidden_act
        self.hidden_dropout_prob = hidden_dropout_prob
        self.attention_probs_dropout_prob = attention_probs_dropout_prob
        self.initializer_range = initializer_range
        self.num_labels = num_labels
        self.scope = scope

    def prepare_config_and_inputs(self):
        pixel_values = floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size])

        labels = None
        if self.use_labels:
            labels = ids_tensor([self.batch_size, self.image_size, self.image_size], self.num_labels)

        config = self.get_config()
        return config, pixel_values, labels

    def get_config(self):
        return SegformerConfig(
            image_size=self.image_size,
            num_channels=self.num_channels,
            num_encoder_blocks=self.num_encoder_blocks,
            depths=self.depths,
            hidden_sizes=self.hidden_sizes,
            num_attention_heads=self.num_attention_heads,
            hidden_act=self.hidden_act,
            hidden_dropout_prob=self.hidden_dropout_prob,
            attention_probs_dropout_prob=self.attention_probs_dropout_prob,
            initializer_range=self.initializer_range,
        )

    def create_and_check_model(self, config, pixel_values, labels):
        model = SegformerModel(config=config)
        model.to(torch_device)
        model.eval()
        result = model(pixel_values)
        expected_height = expected_width = self.image_size // (self.downsampling_rates[-1] * 2)
        self.parent.assertEqual(
            result.last_hidden_state.shape, (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)
        )

    def create_and_check_for_image_segmentation(self, config, pixel_values, labels):
        config.num_labels = self.num_labels
        model = SegformerForSemanticSegmentation(config)
        model.to(torch_device)
        model.eval()
        result = model(pixel_values)
        self.parent.assertEqual(
            result.logits.shape, (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)
        )
        result = model(pixel_values, labels=labels)
        self.parent.assertEqual(
            result.logits.shape, (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)
        )
        self.parent.assertGreater(result.loss, 0.0)

    def create_and_check_for_binary_image_segmentation(self, config, pixel_values, labels):
        config.num_labels = 1
        model = SegformerForSemanticSegmentation(config=config)
        model.to(torch_device)
        model.eval()
        labels = torch.randint(0, 1, (self.batch_size, self.image_size, self.image_size)).to(torch_device)
        result = model(pixel_values, labels=labels)
        self.parent.assertGreater(result.loss, 0.0)

    def prepare_config_and_inputs_for_common(self):
        config_and_inputs = self.prepare_config_and_inputs()
        config, pixel_values, labels = config_and_inputs
        inputs_dict = {"pixel_values": pixel_values}
        return config, inputs_dict


@require_torch
class SegformerModelTest(ModelTesterMixin, PipelineTesterMixin, unittest.TestCase):
    all_model_classes = (
        (
            SegformerModel,
            SegformerForSemanticSegmentation,
            SegformerForImageClassification,
        )
        if is_torch_available()
        else ()
    )
    pipeline_model_mapping = (
        {
            "feature-extraction": SegformerModel,
            "image-classification": SegformerForImageClassification,
            "image-segmentation": SegformerForSemanticSegmentation,
        }
        if is_torch_available()
        else {}
    )

    fx_compatible = True
    test_head_masking = False
    test_pruning = False
    test_resize_embeddings = False

    def setUp(self):
        self.model_tester = SegformerModelTester(self)
        self.config_tester = SegformerConfigTester(self, config_class=SegformerConfig)

    def test_config(self):
        self.config_tester.run_common_tests()

    def test_model(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*config_and_inputs)

    def test_for_binary_image_segmentation(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_binary_image_segmentation(*config_and_inputs)

    def test_for_image_segmentation(self):
        config_and_inputs = self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_segmentation(*config_and_inputs)

    @unittest.skip("SegFormer does not use inputs_embeds")
    def test_inputs_embeds(self):
        pass

    @unittest.skip("SegFormer does not have get_input_embeddings method and get_output_embeddings methods")
    def test_model_common_attributes(self):
        pass

    def test_attention_outputs(self):
        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        config.return_dict = True

        for model_class in self.all_model_classes:
            inputs_dict["output_attentions"] = True
            inputs_dict["output_hidden_states"] = False
            config.return_dict = True
            model = model_class(config)
            model.to(torch_device)
            model.eval()
            with torch.no_grad():
                outputs = model(**self._prepare_for_class(inputs_dict, model_class))
            attentions = outputs.attentions

            expected_num_attentions = sum(self.model_tester.depths)
            self.assertEqual(len(attentions), expected_num_attentions)

            
            del inputs_dict["output_attentions"]
            config.output_attentions = True
            model = model_class(config)
            model.to(torch_device)
            model.eval()
            with torch.no_grad():
                outputs = model(**self._prepare_for_class(inputs_dict, model_class))
            attentions = outputs.attentions

            self.assertEqual(len(attentions), expected_num_attentions)

            
            expected_seq_len = (self.model_tester.image_size // 4) ** 2
            expected_reduced_seq_len = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
            self.assertListEqual(
                list(attentions[0].shape[-3:]),
                [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len],
            )

            
            expected_seq_len = (self.model_tester.image_size // 32) ** 2
            expected_reduced_seq_len = (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
            self.assertListEqual(
                list(attentions[-1].shape[-3:]),
                [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len],
            )
            out_len = len(outputs)

            
            inputs_dict["output_attentions"] = True
            inputs_dict["output_hidden_states"] = True
            model = model_class(config)
            model.to(torch_device)
            model.eval()
            with torch.no_grad():
                outputs = model(**self._prepare_for_class(inputs_dict, model_class))

            self.assertEqual(out_len + 1, len(outputs))

            self_attentions = outputs.attentions

            self.assertEqual(len(self_attentions), expected_num_attentions)
            
            expected_seq_len = (self.model_tester.image_size // 4) ** 2
            expected_reduced_seq_len = (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
            self.assertListEqual(
                list(self_attentions[0].shape[-3:]),
                [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len],
            )

    def test_hidden_states_output(self):
        def check_hidden_states_output(inputs_dict, config, model_class):
            model = model_class(config)
            model.to(torch_device)
            model.eval()

            with torch.no_grad():
                outputs = model(**self._prepare_for_class(inputs_dict, model_class))

            hidden_states = outputs.hidden_states

            expected_num_layers = self.model_tester.num_encoder_blocks
            self.assertEqual(len(hidden_states), expected_num_layers)

            
            self.assertListEqual(
                list(hidden_states[0].shape[-3:]),
                [
                    self.model_tester.hidden_sizes[0],
                    self.model_tester.image_size // 4,
                    self.model_tester.image_size // 4,
                ],
            )

        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
            inputs_dict["output_hidden_states"] = True
            check_hidden_states_output(inputs_dict, config, model_class)

            
            del inputs_dict["output_hidden_states"]
            config.output_hidden_states = True

            check_hidden_states_output(inputs_dict, config, model_class)

    def test_training(self):
        if not self.model_tester.is_training:
            return

        config, inputs_dict = self.model_tester.prepare_config_and_inputs_for_common()
        config.return_dict = True

        for model_class in self.all_model_classes:
            if model_class in get_values(MODEL_MAPPING):
                continue

            model = model_class(config)
            model.to(torch_device)
            model.train()
            inputs = self._prepare_for_class(inputs_dict, model_class, return_labels=True)
            loss = model(**inputs).loss
            loss.backward()

    @slow
    def test_model_from_pretrained(self):
        for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
            model = SegformerModel.from_pretrained(model_name)
            self.assertIsNotNone(model)



def prepare_img():
    image = Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png")
    return image


@require_torch
class SegformerModelIntegrationTest(unittest.TestCase):
    @slow
    def test_inference_image_segmentation_ade(self):
        
        image_processor = SegformerImageProcessor(
            image_scale=(512, 512), keep_ratio=False, align=False, do_random_crop=False
        )
        model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512").to(
            torch_device
        )

        image = prepare_img()
        encoded_inputs = image_processor(images=image, return_tensors="pt")
        pixel_values = encoded_inputs.pixel_values.to(torch_device)

        with torch.no_grad():
            outputs = model(pixel_values)

        expected_shape = torch.Size((1, model.config.num_labels, 128, 128))
        self.assertEqual(outputs.logits.shape, expected_shape)

        expected_slice = torch.tensor(
            [
                [[-4.6310, -5.5232, -6.2356], [-5.1921, -6.1444, -6.5996], [-5.4424, -6.2790, -6.7574]],
                [[-12.1391, -13.3122, -13.9554], [-12.8732, -13.9352, -14.3563], [-12.9438, -13.8226, -14.2513]],
                [[-12.5134, -13.4686, -14.4915], [-12.8669, -14.4343, -14.7758], [-13.2523, -14.5819, -15.0694]],
            ]
        ).to(torch_device)
        self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3], expected_slice, atol=1e-4))

    @slow
    def test_inference_image_segmentation_city(self):
        
        image_processor = SegformerImageProcessor(
            image_scale=(512, 512), keep_ratio=False, align=False, do_random_crop=False
        )
        model = SegformerForSemanticSegmentation.from_pretrained(
            "nvidia/segformer-b1-finetuned-cityscapes-1024-1024"
        ).to(torch_device)

        image = prepare_img()
        encoded_inputs = image_processor(images=image, return_tensors="pt")
        pixel_values = encoded_inputs.pixel_values.to(torch_device)

        with torch.no_grad():
            outputs = model(pixel_values)

        expected_shape = torch.Size((1, model.config.num_labels, 128, 128))
        self.assertEqual(outputs.logits.shape, expected_shape)

        expected_slice = torch.tensor(
            [
                [[-13.5748, -13.9111, -12.6500], [-14.3500, -15.3683, -14.2328], [-14.7532, -16.0424, -15.6087]],
                [[-17.1651, -15.8725, -12.9653], [-17.2580, -17.3718, -14.8223], [-16.6058, -16.8783, -16.7452]],
                [[-3.6456, -3.0209, -1.4203], [-3.0797, -3.1959, -2.0000], [-1.8757, -1.9217, -1.6997]],
            ]
        ).to(torch_device)
        self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3], expected_slice, atol=1e-1))

    @slow
    def test_post_processing_semantic_segmentation(self):
        
        image_processor = SegformerImageProcessor(
            image_scale=(512, 512), keep_ratio=False, align=False, do_random_crop=False
        )
        model = SegformerForSemanticSegmentation.from_pretrained("nvidia/segformer-b0-finetuned-ade-512-512").to(
            torch_device
        )

        image = prepare_img()
        encoded_inputs = image_processor(images=image, return_tensors="pt")
        pixel_values = encoded_inputs.pixel_values.to(torch_device)

        with torch.no_grad():
            outputs = model(pixel_values)

        outputs.logits = outputs.logits.detach().cpu()

        segmentation = image_processor.post_process_semantic_segmentation(outputs=outputs, target_sizes=[(500, 300)])
        expected_shape = torch.Size((500, 300))
        self.assertEqual(segmentation[0].shape, expected_shape)

        segmentation = image_processor.post_process_semantic_segmentation(outputs=outputs)
        expected_shape = torch.Size((128, 128))
        self.assertEqual(segmentation[0].shape, expected_shape)