

'''simple docstring'''

import csv
import dataclasses
import json
from dataclasses import dataclass
from typing import List, Optional, Union

from ...utils import is_tf_available, is_torch_available, logging


UpperCAmelCase							=					logging.get_logger(__name__)


@dataclass
class 					lowerCAmelCase       :
       lowerCAmelCase_				=	42
       lowerCAmelCase_				=	42
       lowerCAmelCase_				=	None
       lowerCAmelCase_				=	None







       def      snake_case				(							self     :	int ):







            """simple docstring"""







            return json.dumps(dataclasses.asdict(self )  , indent=2 ) + "\n"






@dataclass(frozen=snake_case__				)
class 					lowerCAmelCase       :
       lowerCAmelCase_				=	42
       lowerCAmelCase_				=	None
       lowerCAmelCase_				=	None
       lowerCAmelCase_				=	None







       def      snake_case				(							self     :	Tuple ):







            """simple docstring"""







            return json.dumps(dataclasses.asdict(self ) ) + "\n"






class 					lowerCAmelCase       :







       def      snake_case				(							self     :	Optional[Any]  , __lowercase     :	Optional[Any] ):







            """simple docstring"""







            raise NotImplementedError()







       def      snake_case				(							self     :	str  , __lowercase     :	Dict ):







            """simple docstring"""







            raise NotImplementedError()







       def      snake_case				(							self     :	List[str]  , __lowercase     :	Dict ):







            """simple docstring"""







            raise NotImplementedError()







       def      snake_case				(							self     :	Dict  , __lowercase     :	Dict ):







            """simple docstring"""







            raise NotImplementedError()







       def      snake_case				(							self     :	List[Any] ):







            """simple docstring"""







            raise NotImplementedError()







       def      snake_case				(							self     :	Optional[Any]  , __lowercase     :	Dict ):







            """simple docstring"""







            if len(self.get_labels() ) > 1:
                 __lowercase     						=self.get_labels()[int(example.label )]
            return example







       @classmethod
       def      snake_case				(							cls     :	str  , __lowercase     :	Dict  , __lowercase     :	Union[str, Any]=None ):







            """simple docstring"""







            with open(__lowercase  , 'r'  , encoding='utf-8-sig' ) as f:
                 return list(csv.reader(__lowercase  , delimiter='\t'  , quotechar=__lowercase ) )






class 					lowerCAmelCase       ( snake_case__				):







       def __init__(							self     :	Union[str, Any]  , __lowercase     :	str=None  , __lowercase     :	int=None  , __lowercase     :	List[str]="classification"  , __lowercase     :	Tuple=False ):







            """simple docstring"""







            __lowercase     						=[] if labels is None else labels
            __lowercase     						=[] if examples is None else examples
            __lowercase     						=mode
            __lowercase     						=verbose







       def __len__(							self     :	Any ):







            """simple docstring"""







            return len(self.examples )







       def __getitem__(							self     :	Any  , __lowercase     :	str ):







            """simple docstring"""







            if isinstance(__lowercase  , __lowercase ):
                 return SingleSentenceClassificationProcessor(labels=self.labels  , examples=self.examples[idx] )
            return self.examples[idx]







       @classmethod
       def      snake_case				(							cls     :	Any  , __lowercase     :	List[Any]  , __lowercase     :	List[Any]=""  , __lowercase     :	List[Any]=0  , __lowercase     :	List[str]=1  , __lowercase     :	Any=None  , __lowercase     :	Optional[Any]=False  , **__lowercase     :	List[Any] ):







            """simple docstring"""







            __lowercase     						=cls(**__lowercase )
            processor.add_examples_from_csv(
                __lowercase  , split_name=__lowercase  , column_label=__lowercase  , column_text=__lowercase  , column_id=__lowercase  , skip_first_row=__lowercase  , overwrite_labels=__lowercase  , overwrite_examples=__lowercase  , )
            return processor







       @classmethod
       def      snake_case				(							cls     :	List[Any]  , __lowercase     :	List[str]  , __lowercase     :	List[str]=None  , **__lowercase     :	Dict ):







            """simple docstring"""







            __lowercase     						=cls(**__lowercase )
            processor.add_examples(__lowercase  , labels=__lowercase )
            return processor







       def      snake_case				(							self     :	Any  , __lowercase     :	List[Any]  , __lowercase     :	Any=""  , __lowercase     :	List[Any]=0  , __lowercase     :	Dict=1  , __lowercase     :	Optional[Any]=None  , __lowercase     :	Dict=False  , __lowercase     :	List[str]=False  , __lowercase     :	Any=False  , ):







            """simple docstring"""







            __lowercase     						=self._read_tsv(__lowercase )
            if skip_first_row:
                 __lowercase     						=lines[1:]
            __lowercase     						=[]
            __lowercase     						=[]
            __lowercase     						=[]
            for i, line in enumerate(__lowercase ):
                 texts.append(line[column_text] )
                 labels.append(line[column_label] )
                 if column_id is not None:
                      ids.append(line[column_id] )
                 else:
                      __lowercase     						=f'''{split_name}-{i}''' if split_name else str(__lowercase )
                      ids.append(__lowercase )

            return self.add_examples(
                __lowercase  , __lowercase  , __lowercase  , overwrite_labels=__lowercase  , overwrite_examples=__lowercase )







       def      snake_case				(							self     :	Dict  , __lowercase     :	Any  , __lowercase     :	Optional[Any]=None  , __lowercase     :	Optional[int]=None  , __lowercase     :	Union[str, Any]=False  , __lowercase     :	Optional[int]=False ):







            """simple docstring"""







            if labels is not None and len(__lowercase ) != len(__lowercase ):
                 raise ValueError(
                     f'''Text and labels have mismatched lengths {len(__lowercase )} and {len(__lowercase )}''' )
            if ids is not None and len(__lowercase ) != len(__lowercase ):
                 raise ValueError(f'''Text and ids have mismatched lengths {len(__lowercase )} and {len(__lowercase )}''' )
            if ids is None:
                 __lowercase     						=[None] * len(__lowercase )
            if labels is None:
                 __lowercase     						=[None] * len(__lowercase )
            __lowercase     						=[]
            __lowercase     						=set()
            for text_or_text_and_label, label, guid in zip(__lowercase  , __lowercase  , __lowercase ):
                 if isinstance(__lowercase  , (tuple, list) ) and label is None:
                      __lowercase     						=text_or_text_and_label
                 else:
                      __lowercase     						=text_or_text_and_label
                 added_labels.add(__lowercase )
                 examples.append(InputExample(guid=__lowercase  , text_a=__lowercase  , text_b=__lowercase  , label=__lowercase ) )

            # Update examples
            if overwrite_examples:
                 __lowercase     						=examples
            else:
                 self.examples.extend(__lowercase )

            # Update labels
            if overwrite_labels:
                 __lowercase     						=list(__lowercase )
            else:
                 __lowercase     						=list(set(self.labels ).union(__lowercase ) )

            return self.examples







       def      snake_case				(							self     :	Optional[int]  , __lowercase     :	Any  , __lowercase     :	List[Any]=None  , __lowercase     :	str=False  , __lowercase     :	List[Any]=0  , __lowercase     :	List[Any]=True  , __lowercase     :	Optional[int]=None  , ):







            """simple docstring"""







            if max_length is None:
                 __lowercase     						=tokenizer.max_len

            __lowercase     						={label: i for i, label in enumerate(self.labels )}

            __lowercase     						=[]
            for ex_index, example in enumerate(self.examples ):
                 if ex_index % 10000 == 0:
                      logger.info(f'''Tokenizing example {ex_index}''' )

                 __lowercase     						=tokenizer.encode(
                     example.text_a  , add_special_tokens=__lowercase  , max_length=min(__lowercase  , tokenizer.max_len )  , )
                 all_input_ids.append(__lowercase )

            __lowercase     						=max(len(__lowercase ) for input_ids in all_input_ids )

            __lowercase     						=[]
            for ex_index, (input_ids, example) in enumerate(zip(__lowercase  , self.examples ) ):
                 if ex_index % 10000 == 0:
                      logger.info(f'''Writing example {ex_index}/{len(self.examples )}''' )
                 # The mask has 1 for real tokens and 0 for padding tokens. Only real
                 # tokens are attended to.
                 __lowercase     						=[1 if mask_padding_with_zero else 0] * len(__lowercase )

                 # Zero-pad up to the sequence length.
                 __lowercase     						=batch_length - len(__lowercase )
                 if pad_on_left:
                      __lowercase     						=([pad_token] * padding_length) + input_ids
                      __lowercase     						=([0 if mask_padding_with_zero else 1] * padding_length) + attention_mask
                 else:
                      __lowercase     						=input_ids + ([pad_token] * padding_length)
                      __lowercase     						=attention_mask + ([0 if mask_padding_with_zero else 1] * padding_length)

                 if len(__lowercase ) != batch_length:
                      raise ValueError(f'''Error with input length {len(__lowercase )} vs {batch_length}''' )
                 if len(__lowercase ) != batch_length:
                      raise ValueError(f'''Error with input length {len(__lowercase )} vs {batch_length}''' )

                 if self.mode == "classification":
                      __lowercase     						=label_map[example.label]
                 elif self.mode == "regression":
                      __lowercase     						=float(example.label )
                 else:
                      raise ValueError(self.mode )

                 if ex_index < 5 and self.verbose:
                      logger.info('*** Example ***' )
                      logger.info(f'''guid: {example.guid}''' )
                      logger.info(f'''input_ids: {" ".join([str(__lowercase ) for x in input_ids] )}''' )
                      logger.info(f'''attention_mask: {" ".join([str(__lowercase ) for x in attention_mask] )}''' )
                      logger.info(f'''label: {example.label} (id = {label})''' )

                 features.append(InputFeatures(input_ids=__lowercase  , attention_mask=__lowercase  , label=__lowercase ) )

            if return_tensors is None:
                 return features
            elif return_tensors == "tf":
                 if not is_tf_available():
                      raise RuntimeError('return_tensors set to \'tf\' but TensorFlow 2.0 can\'t be imported' )
                 import tensorflow as tf

                 def gen():
                      for ex in features:
                           yield ({"input_ids": ex.input_ids, "attention_mask": ex.attention_mask}, ex.label)

                 __lowercase     						=tf.data.Dataset.from_generator(
                     __lowercase  , ({'input_ids': tf.intaa, 'attention_mask': tf.intaa}, tf.intaa)  , ({'input_ids': tf.TensorShape([None] ), 'attention_mask': tf.TensorShape([None] )}, tf.TensorShape([] ))  , )
                 return dataset
            elif return_tensors == "pt":
                 if not is_torch_available():
                      raise RuntimeError('return_tensors set to \'pt\' but PyTorch can\'t be imported' )
                 import torch
                 from torch.utils.data import TensorDataset

                 __lowercase     						=torch.tensor([f.input_ids for f in features]  , dtype=torch.long )
                 __lowercase     						=torch.tensor([f.attention_mask for f in features]  , dtype=torch.long )
                 if self.mode == "classification":
                      __lowercase     						=torch.tensor([f.label for f in features]  , dtype=torch.long )
                 elif self.mode == "regression":
                      __lowercase     						=torch.tensor([f.label for f in features]  , dtype=torch.float )

                 __lowercase     						=TensorDataset(__lowercase  , __lowercase  , __lowercase )
                 return dataset
            else:
                 raise ValueError('return_tensors should be one of \'tf\' or \'pt\'' )





'''simple docstring'''

from typing import Dict, List, Optional, Union

import numpy as np

from ...image_processing_utils import BaseImageProcessor, BatchFeature, get_size_dict
from ...image_transforms import normalize, rescale, resize, to_channel_dimension_format
from ...image_utils import (
    IMAGENET_STANDARD_MEAN,
    IMAGENET_STANDARD_STD,
    ChannelDimension,
    ImageInput,
    PILImageResampling,
    make_list_of_images,
    to_numpy_array,
    valid_images,
)
from ...utils import TensorType, logging


UpperCAmelCase							=					logging.get_logger(__name__)


class 					lowerCAmelCase       ( A				):
       lowerCAmelCase_				=	["pixel_values"]







       def __init__(							self     :	List[Any]  , __lowercase     :	bool = True  , __lowercase     :	Optional[Dict[str, int]] = None  , __lowercase     :	PILImageResampling = PILImageResampling.BILINEAR  , __lowercase     :	bool = True  , __lowercase     :	Union[int, float] = 1 / 255  , __lowercase     :	bool = True  , __lowercase     :	Optional[Union[float, List[float]]] = None  , __lowercase     :	Optional[Union[float, List[float]]] = None  , **__lowercase     :	List[Any]  , ):







            """simple docstring"""







            super().__init__(**__lowercase )
            __lowercase     						=size if size is not None else {'height': 224, 'width': 224}
            __lowercase     						=get_size_dict(__lowercase )
            __lowercase     						=do_resize
            __lowercase     						=do_rescale
            __lowercase     						=do_normalize
            __lowercase     						=size
            __lowercase     						=resample
            __lowercase     						=rescale_factor
            __lowercase     						=image_mean if image_mean is not None else IMAGENET_STANDARD_MEAN
            __lowercase     						=image_std if image_std is not None else IMAGENET_STANDARD_STD







       def      snake_case				(							self     :	Dict  , __lowercase     :	np.ndarray  , __lowercase     :	Dict[str, int]  , __lowercase     :	PILImageResampling = PILImageResampling.BILINEAR  , __lowercase     :	Optional[Union[str, ChannelDimension]] = None  , **__lowercase     :	Union[str, Any]  , ):







            """simple docstring"""







            __lowercase     						=get_size_dict(__lowercase )
            if "height" not in size or "width" not in size:
                 raise ValueError(f'''The `size` dictionary must contain the keys `height` and `width`. Got {size.keys()}''' )
            return resize(
                __lowercase  , size=(size['height'], size['width'])  , resample=__lowercase  , data_format=__lowercase  , **__lowercase )







       def      snake_case				(							self     :	str  , __lowercase     :	np.ndarray  , __lowercase     :	float  , __lowercase     :	Optional[Union[str, ChannelDimension]] = None  , **__lowercase     :	Optional[Any] ):







            """simple docstring"""







            return rescale(__lowercase  , scale=__lowercase  , data_format=__lowercase  , **__lowercase )







       def      snake_case				(							self     :	Any  , __lowercase     :	np.ndarray  , __lowercase     :	Union[float, List[float]]  , __lowercase     :	Union[float, List[float]]  , __lowercase     :	Optional[Union[str, ChannelDimension]] = None  , **__lowercase     :	str  , ):







            """simple docstring"""







            return normalize(__lowercase  , mean=__lowercase  , std=__lowercase  , data_format=__lowercase  , **__lowercase )







       def      snake_case				(							self     :	str  , __lowercase     :	ImageInput  , __lowercase     :	Optional[bool] = None  , __lowercase     :	Dict[str, int] = None  , __lowercase     :	PILImageResampling = None  , __lowercase     :	Optional[bool] = None  , __lowercase     :	Optional[float] = None  , __lowercase     :	Optional[bool] = None  , __lowercase     :	Optional[Union[float, List[float]]] = None  , __lowercase     :	Optional[Union[float, List[float]]] = None  , __lowercase     :	Optional[Union[str, TensorType]] = None  , __lowercase     :	Union[str, ChannelDimension] = ChannelDimension.FIRST  , **__lowercase     :	Tuple  , ):







            """simple docstring"""







            __lowercase     						=do_resize if do_resize is not None else self.do_resize
            __lowercase     						=do_rescale if do_rescale is not None else self.do_rescale
            __lowercase     						=do_normalize if do_normalize is not None else self.do_normalize
            __lowercase     						=resample if resample is not None else self.resample
            __lowercase     						=rescale_factor if rescale_factor is not None else self.rescale_factor
            __lowercase     						=image_mean if image_mean is not None else self.image_mean
            __lowercase     						=image_std if image_std is not None else self.image_std

            __lowercase     						=size if size is not None else self.size
            __lowercase     						=get_size_dict(__lowercase )

            __lowercase     						=make_list_of_images(__lowercase )

            if not valid_images(__lowercase ):
                 raise ValueError(
                     'Invalid image type. Must be of type PIL.Image.Image, numpy.ndarray, '
                     'torch.Tensor, tf.Tensor or jax.ndarray.' )

            if do_resize and size is None:
                 raise ValueError('Size must be specified if do_resize is True.' )

            if do_rescale and rescale_factor is None:
                 raise ValueError('Rescale factor must be specified if do_rescale is True.' )

            # All transformations expect numpy arrays.
            __lowercase     						=[to_numpy_array(__lowercase ) for image in images]

            if do_resize:
                 __lowercase     						=[self.resize(image=__lowercase  , size=__lowercase  , resample=__lowercase ) for image in images]

            if do_rescale:
                 __lowercase     						=[self.rescale(image=__lowercase  , scale=__lowercase ) for image in images]

            if do_normalize:
                 __lowercase     						=[self.normalize(image=__lowercase  , mean=__lowercase  , std=__lowercase ) for image in images]

            __lowercase     						=[to_channel_dimension_format(__lowercase  , __lowercase ) for image in images]

            __lowercase     						={'pixel_values': images}
            return BatchFeature(data=__lowercase  , tensor_type=__lowercase )


