


import argparse
import json

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import ConvNextConfig, SegformerImageProcessor, UperNetConfig, UperNetForSemanticSegmentation



def        UpperCamelCase_(   lowerCamelCase_				)					->		List[Any]:
    _lowercase :							List[str]   	=	384
    if "tiny" in model_name:
        _lowercase :							Union[str, Any]   	=	[3, 3, 9, 3]
        _lowercase :							int   	=	[96, 192, 384, 768]
    if "small" in model_name:
        _lowercase :							List[str]   	=	[3, 3, 27, 3]
        _lowercase :							Union[str, Any]   	=	[96, 192, 384, 768]
    if "base" in model_name:
        _lowercase :							Union[str, Any]   	=	[3, 3, 27, 3]
        _lowercase :							Optional[int]   	=	[128, 256, 512, 1024]
        _lowercase :							int   	=	512
    if "large" in model_name:
        _lowercase :							Optional[int]   	=	[3, 3, 27, 3]
        _lowercase :							Dict   	=	[192, 384, 768, 1536]
        _lowercase :							Tuple   	=	768
    if "xlarge" in model_name:
        _lowercase :							List[str]   	=	[3, 3, 27, 3]
        _lowercase :							Dict   	=	[256, 512, 1024, 2048]
        _lowercase :							str   	=	1024

    # set label information
    _lowercase :							Any   	=	150
    _lowercase :							List[Any]   	=	'huggingface/label-files'
    _lowercase :							Tuple   	=	'ade20k-id2label.json'
    _lowercase :							List[str]   	=	json.load(open(hf_hub_download(lowerCamelCase_							,				lowerCamelCase_							,				repo_type='dataset'				)							,				'r'				)				)
    _lowercase :							Any   	=	{int(lowerCamelCase_				): v for k, v in idalabel.items()}
    _lowercase :							Any   	=	{v: k for k, v in idalabel.items()}

    _lowercase :							List[str]   	=	ConvNextConfig(
        depths=lowerCamelCase_							,				hidden_sizes=lowerCamelCase_							,				out_features=['stage1', 'stage2', 'stage3', 'stage4']				)
    _lowercase :							str   	=	UperNetConfig(
        backbone_config=lowerCamelCase_							,				auxiliary_in_channels=lowerCamelCase_							,				num_labels=lowerCamelCase_							,				idalabel=lowerCamelCase_							,				labelaid=lowerCamelCase_							,				)

    return config



def        UpperCamelCase_(   lowerCamelCase_				)					->		Any:
    _lowercase :							Any   	=	[]

    # fmt: off
    # stem
    rename_keys.append(('backbone.downsample_layers.0.0.weight', 'backbone.embeddings.patch_embeddings.weight')				)
    rename_keys.append(('backbone.downsample_layers.0.0.bias', 'backbone.embeddings.patch_embeddings.bias')				)
    rename_keys.append(('backbone.downsample_layers.0.1.weight', 'backbone.embeddings.layernorm.weight')				)
    rename_keys.append(('backbone.downsample_layers.0.1.bias', 'backbone.embeddings.layernorm.bias')				)
    # stages
    for i in range(len(config.backbone_config.depths				)				):
        for j in range(config.backbone_config.depths[i]				):
            rename_keys.append((F'''backbone.stages.{i}.{j}.gamma''', F'''backbone.encoder.stages.{i}.layers.{j}.layer_scale_parameter''')				)
            rename_keys.append((F'''backbone.stages.{i}.{j}.depthwise_conv.weight''', F'''backbone.encoder.stages.{i}.layers.{j}.dwconv.weight''')				)
            rename_keys.append((F'''backbone.stages.{i}.{j}.depthwise_conv.bias''', F'''backbone.encoder.stages.{i}.layers.{j}.dwconv.bias''')				)
            rename_keys.append((F'''backbone.stages.{i}.{j}.norm.weight''', F'''backbone.encoder.stages.{i}.layers.{j}.layernorm.weight''')				)
            rename_keys.append((F'''backbone.stages.{i}.{j}.norm.bias''', F'''backbone.encoder.stages.{i}.layers.{j}.layernorm.bias''')				)
            rename_keys.append((F'''backbone.stages.{i}.{j}.pointwise_conv1.weight''', F'''backbone.encoder.stages.{i}.layers.{j}.pwconv1.weight''')				)
            rename_keys.append((F'''backbone.stages.{i}.{j}.pointwise_conv1.bias''', F'''backbone.encoder.stages.{i}.layers.{j}.pwconv1.bias''')				)
            rename_keys.append((F'''backbone.stages.{i}.{j}.pointwise_conv2.weight''', F'''backbone.encoder.stages.{i}.layers.{j}.pwconv2.weight''')				)
            rename_keys.append((F'''backbone.stages.{i}.{j}.pointwise_conv2.bias''', F'''backbone.encoder.stages.{i}.layers.{j}.pwconv2.bias''')				)
        if i > 0:
            rename_keys.append((F'''backbone.downsample_layers.{i}.0.weight''', F'''backbone.encoder.stages.{i}.downsampling_layer.0.weight''')				)
            rename_keys.append((F'''backbone.downsample_layers.{i}.0.bias''', F'''backbone.encoder.stages.{i}.downsampling_layer.0.bias''')				)
            rename_keys.append((F'''backbone.downsample_layers.{i}.1.weight''', F'''backbone.encoder.stages.{i}.downsampling_layer.1.weight''')				)
            rename_keys.append((F'''backbone.downsample_layers.{i}.1.bias''', F'''backbone.encoder.stages.{i}.downsampling_layer.1.bias''')				)

        rename_keys.append((F'''backbone.norm{i}.weight''', F'''backbone.hidden_states_norms.stage{i+1}.weight''')				)
        rename_keys.append((F'''backbone.norm{i}.bias''', F'''backbone.hidden_states_norms.stage{i+1}.bias''')				)

    # decode head
    rename_keys.extend(
        [
            ('decode_head.conv_seg.weight', 'decode_head.classifier.weight'),
            ('decode_head.conv_seg.bias', 'decode_head.classifier.bias'),
            ('auxiliary_head.conv_seg.weight', 'auxiliary_head.classifier.weight'),
            ('auxiliary_head.conv_seg.bias', 'auxiliary_head.classifier.bias'),
        ]				)
    # fmt: on

    return rename_keys



def        UpperCamelCase_(   lowerCamelCase_							,				lowerCamelCase_							,				lowerCamelCase_				)					->		Optional[int]:
    _lowercase :							int   	=	dct.pop(lowerCamelCase_				)
    _lowercase :							List[str]   	=	val



def        UpperCamelCase_(   lowerCamelCase_							,				lowerCamelCase_							,				lowerCamelCase_				)					->		Any:
    _lowercase :							List[Any]   	=	{
        'upernet-convnext-tiny': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_tiny_fp16_512x512_160k_ade20k/upernet_convnext_tiny_fp16_512x512_160k_ade20k_20220227_124553-cad485de.pth',
        'upernet-convnext-small': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_small_fp16_512x512_160k_ade20k/upernet_convnext_small_fp16_512x512_160k_ade20k_20220227_131208-1b1e394f.pth',
        'upernet-convnext-base': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_base_fp16_512x512_160k_ade20k/upernet_convnext_base_fp16_512x512_160k_ade20k_20220227_181227-02a24fc6.pth',
        'upernet-convnext-large': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_large_fp16_640x640_160k_ade20k/upernet_convnext_large_fp16_640x640_160k_ade20k_20220226_040532-e57aa54d.pth',
        'upernet-convnext-xlarge': 'https://download.openmmlab.com/mmsegmentation/v0.5/convnext/upernet_convnext_xlarge_fp16_640x640_160k_ade20k/upernet_convnext_xlarge_fp16_640x640_160k_ade20k_20220226_080344-95fc38c2.pth',
    }
    _lowercase :							str   	=	model_name_to_url[model_name]
    _lowercase :							Optional[Any]   	=	torch.hub.load_state_dict_from_url(lowerCamelCase_							,				map_location='cpu'				)['state_dict']

    _lowercase :							Tuple   	=	get_upernet_config(lowerCamelCase_				)
    _lowercase :							Optional[int]   	=	UperNetForSemanticSegmentation(lowerCamelCase_				)
    model.eval()

    # replace "bn" => "batch_norm"
    for key in state_dict.copy().keys():
        _lowercase :							List[str]   	=	state_dict.pop(lowerCamelCase_				)
        if "bn" in key:
            _lowercase :							Optional[int]   	=	key.replace('bn'							,				'batch_norm'				)
        _lowercase :							Union[str, Any]   	=	val

    # rename keys
    _lowercase :							List[str]   	=	create_rename_keys(lowerCamelCase_				)
    for src, dest in rename_keys:
        rename_key(lowerCamelCase_							,				lowerCamelCase_							,				lowerCamelCase_				)

    model.load_state_dict(lowerCamelCase_				)

    # verify on image
    _lowercase :							Dict   	=	'https://huggingface.co/datasets/hf-internal-testing/fixtures_ade20k/resolve/main/ADE_val_00000001.jpg'
    _lowercase :							Any   	=	Image.open(requests.get(lowerCamelCase_							,				stream=lowerCamelCase_				).raw				).convert('RGB'				)

    _lowercase :							Optional[int]   	=	SegformerImageProcessor()
    _lowercase :							Union[str, Any]   	=	processor(lowerCamelCase_							,				return_tensors='pt'				).pixel_values

    with torch.no_grad():
        _lowercase :							List[Any]   	=	model(lowerCamelCase_				)

    if model_name == "upernet-convnext-tiny":
        _lowercase :							Optional[Any]   	=	torch.tensor(
            [[-8.81_10, -8.81_10, -8.65_21], [-8.81_10, -8.81_10, -8.65_21], [-8.77_46, -8.77_46, -8.61_30]]				)
    elif model_name == "upernet-convnext-small":
        _lowercase :							Optional[Any]   	=	torch.tensor(
            [[-8.82_36, -8.82_36, -8.67_71], [-8.82_36, -8.82_36, -8.67_71], [-8.76_38, -8.76_38, -8.62_40]]				)
    elif model_name == "upernet-convnext-base":
        _lowercase :							Optional[int]   	=	torch.tensor(
            [[-8.85_58, -8.85_58, -8.69_05], [-8.85_58, -8.85_58, -8.69_05], [-8.76_69, -8.76_69, -8.60_21]]				)
    elif model_name == "upernet-convnext-large":
        _lowercase :							Optional[Any]   	=	torch.tensor(
            [[-8.66_60, -8.66_60, -8.62_10], [-8.66_60, -8.66_60, -8.62_10], [-8.63_10, -8.63_10, -8.59_64]]				)
    elif model_name == "upernet-convnext-xlarge":
        _lowercase :							Optional[int]   	=	torch.tensor(
            [[-8.49_80, -8.49_80, -8.39_77], [-8.49_80, -8.49_80, -8.39_77], [-8.43_79, -8.43_79, -8.34_12]]				)
    print('Logits:'							,				outputs.logits[0, 0, :3, :3]				)
    assert torch.allclose(outputs.logits[0, 0, :3, :3]							,				lowerCamelCase_							,				atol=1e-4				)
    print('Looks ok!'				)

    if pytorch_dump_folder_path is not None:
        print(F'''Saving model {model_name} to {pytorch_dump_folder_path}'''				)
        model.save_pretrained(lowerCamelCase_				)
        print(F'''Saving processor to {pytorch_dump_folder_path}'''				)
        processor.save_pretrained(lowerCamelCase_				)

    if push_to_hub:
        print(F'''Pushing model and processor for {model_name} to hub'''				)
        model.push_to_hub(F'''openmmlab/{model_name}'''				)
        processor.push_to_hub(F'''openmmlab/{model_name}'''				)


if __name__ == "__main__":
 SCREAMING_SNAKE_CASE :   Optional[Any]							      =				argparse.ArgumentParser()
 # Required parameters
 parser.add_argument(
     "--model_name",
     default="upernet-convnext-tiny",
     type=str,
     choices=[F"upernet-convnext-{size}" for size in ["tiny", "small", "base", "large", "xlarge"]],
     help="Name of the ConvNext UperNet model you'd like to convert.",
 )
 parser.add_argument(
     "--pytorch_dump_folder_path", default=None, type=str, help="Path to the output PyTorch model directory."
 )
 parser.add_argument(
     "--push_to_hub", action="store_true", help="Whether or not to push the converted model to the 🤗 hub."
 )

 SCREAMING_SNAKE_CASE :   Optional[Any]							      =				parser.parse_args()
 convert_upernet_checkpoint(args.model_name, args.pytorch_dump_folder_path, args.push_to_hub)






import os

import pytest
import yaml

from datasets.features.features import Features, Value
from datasets.info import DatasetInfo, DatasetInfosDict



@pytest.mark.parametrize(
    'files'							,				[
        ['full:README.md', 'dataset_infos.json'],
        ['empty:README.md', 'dataset_infos.json'],
        ['dataset_infos.json'],
        ['full:README.md'],
    ]							,				)
def        UpperCamelCase_(   lowerCamelCase_							,				lowerCamelCase_				)					->		List[str]:
    _lowercase :							Any   	=	tmp_path_factory.mktemp('dset_infos_dir'				)
    if "full:README.md" in files:
        with open(dataset_infos_dir / 'README.md'							,				'w'				) as f:
            f.write('---\ndataset_info:\n  dataset_size: 42\n---'				)
    if "empty:README.md" in files:
        with open(dataset_infos_dir / 'README.md'							,				'w'				) as f:
            f.write(''				)
    # we want to support dataset_infos.json for backward compatibility
    if "dataset_infos.json" in files:
        with open(dataset_infos_dir / 'dataset_infos.json'							,				'w'				) as f:
            f.write('{"default": {"dataset_size": 42}}'				)
    _lowercase :							Any   	=	DatasetInfosDict.from_directory(lowerCamelCase_				)
    assert dataset_infos
    assert dataset_infos["default"].dataset_size == 42



@pytest.mark.parametrize(
    'dataset_info'							,				[
        DatasetInfo(),
        DatasetInfo(
            description='foo'							,				features=Features({'a': Value('int32'				)}				)							,				builder_name='builder'							,				config_name='config'							,				version='1.0.0'							,				splits=[{'name': 'train'}]							,				download_size=42							,				),
    ]							,				)
def        UpperCamelCase_(   lowerCamelCase_							,				lowerCamelCase_				)					->		Optional[int]:
    _lowercase :							Union[str, Any]   	=	str(lowerCamelCase_				)
    dataset_info.write_to_directory(lowerCamelCase_				)
    _lowercase :							List[str]   	=	DatasetInfo.from_directory(lowerCamelCase_				)
    assert dataset_info == reloaded
    assert os.path.exists(os.path.join(lowerCamelCase_							,				'dataset_info.json'				)				)



def        UpperCamelCase_(   )					->		int:
    _lowercase :							Tuple   	=	DatasetInfo(
        description='foo'							,				citation='bar'							,				homepage='https://foo.bar'							,				license='CC0'							,				features=Features({'a': Value('int32'				)}				)							,				post_processed={}							,				supervised_keys=()							,				task_templates=[]							,				builder_name='builder'							,				config_name='config'							,				version='1.0.0'							,				splits=[{'name': 'train', 'num_examples': 42}]							,				download_checksums={}							,				download_size=1337							,				post_processing_size=442							,				dataset_size=1234							,				size_in_bytes=1337 + 442 + 1234							,				)
    _lowercase :							Optional[int]   	=	dataset_info._to_yaml_dict()
    assert sorted(lowerCamelCase_				) == sorted(DatasetInfo._INCLUDED_INFO_IN_YAML				)
    for key in DatasetInfo._INCLUDED_INFO_IN_YAML:
        assert key in dataset_info_yaml_dict
        assert isinstance(dataset_info_yaml_dict[key]							,				(list, dict, int, str)				)
    _lowercase :							str   	=	yaml.safe_dump(lowerCamelCase_				)
    _lowercase :							str   	=	yaml.safe_load(lowerCamelCase_				)
    assert dataset_info_yaml_dict == reloaded



def        UpperCamelCase_(   )					->		int:
    _lowercase :							Tuple   	=	DatasetInfo()
    _lowercase :							Tuple   	=	dataset_info._to_yaml_dict()
    assert dataset_info_yaml_dict == {}



@pytest.mark.parametrize(
    'dataset_infos_dict'							,				[
        DatasetInfosDict(),
        DatasetInfosDict({'default': DatasetInfo()}				),
        DatasetInfosDict({'my_config_name': DatasetInfo()}				),
        DatasetInfosDict(
            {
                'default': DatasetInfo(
                    description='foo'							,				features=Features({'a': Value('int32'				)}				)							,				builder_name='builder'							,				config_name='config'							,				version='1.0.0'							,				splits=[{'name': 'train'}]							,				download_size=42							,				)
            }				),
        DatasetInfosDict(
            {
                'v1': DatasetInfo(dataset_size=42				),
                'v2': DatasetInfo(dataset_size=1337				),
            }				),
    ]							,				)
def        UpperCamelCase_(   lowerCamelCase_							,				lowerCamelCase_				)					->		Dict:
    _lowercase :							Tuple   	=	str(lowerCamelCase_				)
    dataset_infos_dict.write_to_directory(lowerCamelCase_				)
    _lowercase :							Tuple   	=	DatasetInfosDict.from_directory(lowerCamelCase_				)

    # the config_name of the dataset_infos_dict take over the attribute
    for config_name, dataset_info in dataset_infos_dict.items():
        _lowercase :							Any   	=	config_name
        # the yaml representation doesn't include fields like description or citation
        # so we just test that we can recover what we can from the yaml
        _lowercase :							str   	=	DatasetInfo._from_yaml_dict(dataset_info._to_yaml_dict()				)
    assert dataset_infos_dict == reloaded

    if dataset_infos_dict:
        assert os.path.exists(os.path.join(lowerCamelCase_							,				'README.md'				)				)


