

'''simple docstring'''





import json
import os
import tempfile

from transformers.testing_utils import check_json_file_has_correct_format


class  UpperCAmelCase :

		'''simple docstring'''






		SCREAMING_SNAKE_CASE_           =					None
		def 			UpperCamelCase(					self		)  ->     int:






							'''simple docstring'''



							lowerCamelCase_  				=      self.feature_extraction_class(**self.feat_extract_dict		)
							lowerCamelCase_  				=      json.loads(feat_extract.to_json_string()		)
							for key, value in self.feat_extract_dict.items():
												self.assertEqual(obj[key]   ,			SCREAMING_SNAKE_CASE_		)
		def 			UpperCamelCase(					self		)  ->     str:






							'''simple docstring'''



							lowerCamelCase_  				=      self.feature_extraction_class(**self.feat_extract_dict		)

							with tempfile.TemporaryDirectory() as tmpdirname:
												lowerCamelCase_  				=      os.path.join(SCREAMING_SNAKE_CASE_   ,			'feat_extract.json'		)
												feat_extract_first.to_json_file(SCREAMING_SNAKE_CASE_		)
												lowerCamelCase_  				=      self.feature_extraction_class.from_json_file(SCREAMING_SNAKE_CASE_		)

							self.assertEqual(feat_extract_second.to_dict()   ,			feat_extract_first.to_dict()		)
		def 			UpperCamelCase(					self		)  ->     List[str]:






							'''simple docstring'''



							lowerCamelCase_  				=      self.feature_extraction_class(**self.feat_extract_dict		)

							with tempfile.TemporaryDirectory() as tmpdirname:
												lowerCamelCase_  				=      feat_extract_first.save_pretrained(SCREAMING_SNAKE_CASE_		)[0]
												check_json_file_has_correct_format(SCREAMING_SNAKE_CASE_		)
												lowerCamelCase_  				=      self.feature_extraction_class.from_pretrained(SCREAMING_SNAKE_CASE_		)

							self.assertEqual(feat_extract_second.to_dict()   ,			feat_extract_first.to_dict()		)






		def 			UpperCamelCase(					self		)  ->     str:






							'''simple docstring'''



							lowerCamelCase_  				=      self.feature_extraction_class()
							self.assertIsNotNone(SCREAMING_SNAKE_CASE_		)










"""simple docstring"""





import unittest

from transformers import is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device


if is_torch_available():
       import torch

       from transformers import AutoModelForImageClassification

if is_vision_available():
       from transformers import AutoImageProcessor

@require_torch
@require_vision
class 						lowercase   (   unittest.TestCase   ):





      @slow
      def        _snake_case     (			self					)						->	Any:
            lowerCAmelCase    		=			AutoImageProcessor.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""					)
            lowerCAmelCase    		=			AutoModelForImageClassification.from_pretrained("""microsoft/dit-base-finetuned-rvlcdip"""					)
            model.to(lowercase					)

            from datasets import load_dataset

            lowerCAmelCase    		=			load_dataset("""nielsr/rvlcdip-demo"""					)

            lowerCAmelCase    		=			dataset["""train"""][0]["""image"""].convert("""RGB"""					)

            lowerCAmelCase    		=			image_processor(lowercase ,		return_tensors="""pt"""					).to(lowercase					)

            # forward pass
            with torch.no_grad():
                  lowerCAmelCase    		=			model(**lowercase					)
                  lowerCAmelCase    		=			outputs.logits

            lowerCAmelCase    		=			torch.Size((1, 16)					)
            self.assertEqual(logits.shape ,		lowercase					)

            lowerCAmelCase    		=			torch.tensor(
                [-0.4_158, -0.4_092, -0.4_347] ,		device=lowercase ,		dtype=torch.float ,		)
            self.assertTrue(torch.allclose(logits[0, :3] ,		lowercase ,		atol=1e-4					)					)






