


import gc
import random
import unittest

import numpy as np
import torch
from transformers import (
    CLIPImageProcessor,
    CLIPTextConfig,
    CLIPTextModel,
    CLIPTokenizer,
    CLIPVisionConfig,
    CLIPVisionModelWithProjection,
)

from diffusers import AutoencoderKL, DDIMScheduler, DDPMScheduler, StableUnCLIPImgaImgPipeline, UNetaDConditionModel
from diffusers.pipelines.pipeline_utils import DiffusionPipeline
from diffusers.pipelines.stable_diffusion.stable_unclip_image_normalizer import StableUnCLIPImageNormalizer
from diffusers.utils.import_utils import is_xformers_available
from diffusers.utils.testing_utils import (
    enable_full_determinism,
    floats_tensor,
    load_image,
    load_numpy,
    require_torch_gpu,
    skip_mps,
    slow,
    torch_device,
)

from ..pipeline_params import TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS, TEXT_GUIDED_IMAGE_VARIATION_PARAMS
from ..test_pipelines_common import (
    PipelineKarrasSchedulerTesterMixin,
    PipelineLatentTesterMixin,
    PipelineTesterMixin,
    assert_mean_pixel_difference,
)


enable_full_determinism()







class       __lowerCAmelCase   (      _UpperCamelCase    ,						_UpperCamelCase    ,						_UpperCamelCase    ,						unittest.TestCase						):
			UpperCamelCase        =					StableUnCLIPImgaImgPipeline
			UpperCamelCase        =					TEXT_GUIDED_IMAGE_VARIATION_PARAMS
			UpperCamelCase        =					TEXT_GUIDED_IMAGE_VARIATION_BATCH_PARAMS
			UpperCamelCase        =					frozenset(
			    []						)  # TO-DO: update image_params once pipeline is refactored with VaeImageProcessor.preprocess
			UpperCamelCase        =					frozenset([]						)



			def    _lowerCamelCase	(						self       :  Any)       ->   Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       32
						_UpperCAmelCase			=       embedder_hidden_size

						# image encoding components

						_UpperCAmelCase			=       CLIPImageProcessor(crop_size=32			,		size=32)

						torch.manual_seed(0)
						_UpperCAmelCase			=       CLIPVisionModelWithProjection(
						    CLIPVisionConfig(
						        hidden_size=_SCREAMING_SNAKE_CASE			,		projection_dim=_SCREAMING_SNAKE_CASE			,		num_hidden_layers=5			,		num_attention_heads=4			,		image_size=32			,		intermediate_size=37			,		patch_size=1			,		))

						# regular denoising components

						torch.manual_seed(0)
						_UpperCAmelCase			=       StableUnCLIPImageNormalizer(embedding_dim=_SCREAMING_SNAKE_CASE)
						_UpperCAmelCase			=       DDPMScheduler(beta_schedule='squaredcos_cap_v2')

						torch.manual_seed(0)
						_UpperCAmelCase			=       CLIPTokenizer.from_pretrained('hf-internal-testing/tiny-random-clip')

						torch.manual_seed(0)
						_UpperCAmelCase			=       CLIPTextModel(
						    CLIPTextConfig(
						        bos_token_id=0			,		eos_token_id=2			,		hidden_size=_SCREAMING_SNAKE_CASE			,		projection_dim=32			,		intermediate_size=37			,		layer_norm_eps=1E-05			,		num_attention_heads=4			,		num_hidden_layers=5			,		pad_token_id=1			,		vocab_size=10_00			,		))

						torch.manual_seed(0)
						_UpperCAmelCase			=       UNetaDConditionModel(
						    sample_size=32			,		in_channels=4			,		out_channels=4			,		down_block_types=('CrossAttnDownBlock2D', 'DownBlock2D')			,		up_block_types=('UpBlock2D', 'CrossAttnUpBlock2D')			,		block_out_channels=(32, 64)			,		attention_head_dim=(2, 4)			,		class_embed_type='projection'			,		projection_class_embeddings_input_dim=embedder_projection_dim * 2			,		cross_attention_dim=_SCREAMING_SNAKE_CASE			,		layers_per_block=1			,		upcast_attention=_SCREAMING_SNAKE_CASE			,		use_linear_projection=_SCREAMING_SNAKE_CASE			,		)

						torch.manual_seed(0)
						_UpperCAmelCase			=       DDIMScheduler(
						    beta_schedule='scaled_linear'			,		beta_start=0.0_0_0_8_5			,		beta_end=0.0_1_2			,		prediction_type='v_prediction'			,		set_alpha_to_one=_SCREAMING_SNAKE_CASE			,		steps_offset=1			,		)

						torch.manual_seed(0)
						_UpperCAmelCase			=       AutoencoderKL()

						_UpperCAmelCase			=       {
						    # image encoding components
						    'feature_extractor': feature_extractor,
						    'image_encoder': image_encoder.eval(),
						    # image noising components
						    'image_normalizer': image_normalizer.eval(),
						    'image_noising_scheduler': image_noising_scheduler,
						    # regular denoising components
						    'tokenizer': tokenizer,
						    'text_encoder': text_encoder.eval(),
						    'unet': unet.eval(),
						    'scheduler': scheduler,
						    'vae': vae.eval(),
						}

						return components



			def    _lowerCamelCase	(						self       :  Tuple			,		A       :  List[Any]			,		A       :  Any=0			,		A       :  List[str]=True)       ->   Dict:



						"""simple docstring"""






						if str(_SCREAMING_SNAKE_CASE).startswith('mps'):
									_UpperCAmelCase			=       torch.manual_seed(_SCREAMING_SNAKE_CASE)
						else:
									_UpperCAmelCase			=       torch.Generator(device=_SCREAMING_SNAKE_CASE).manual_seed(_SCREAMING_SNAKE_CASE)

						_UpperCAmelCase			=       floats_tensor((1, 3, 32, 32)			,		rng=random.Random(_SCREAMING_SNAKE_CASE)).to(_SCREAMING_SNAKE_CASE)

						if pil_image:
									_UpperCAmelCase			=       input_image * 0.5 + 0.5
									_UpperCAmelCase			=       input_image.clamp(0			,		1)
									_UpperCAmelCase			=       input_image.cpu().permute(0			,		2			,		3			,		1).float().numpy()
									_UpperCAmelCase			=       DiffusionPipeline.numpy_to_pil(_SCREAMING_SNAKE_CASE)[0]

						return {
						    "prompt": "An anime racoon running a marathon",
						    "image": input_image,
						    "generator": generator,
						    "num_inference_steps": 2,
						    "output_type": "np",
						}



			@skip_mps
			def    _lowerCamelCase	(						self       :  Dict)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       'cpu'  # ensure determinism for the device-dependent torch.Generator
						_UpperCAmelCase			=       self.get_dummy_components()
						_UpperCAmelCase			=       StableUnCLIPImgaImgPipeline(**_SCREAMING_SNAKE_CASE)
						_UpperCAmelCase			=       sd_pipe.to(_SCREAMING_SNAKE_CASE)
						sd_pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE)

						_UpperCAmelCase			=       self.get_dummy_inputs(_SCREAMING_SNAKE_CASE)
						inputs.update({'image_embeds': None})
						_UpperCAmelCase			=       sd_pipe(**_SCREAMING_SNAKE_CASE).images
						_UpperCAmelCase			=       image[0, -3:, -3:, -1]

						assert image.shape == (1, 32, 32, 3)
						_UpperCAmelCase			=       np.array([0.3_8_7_2, 0.7_2_2_4, 0.5_6_0_1, 0.4_7_4_1, 0.6_8_7_2, 0.5_8_1_4, 0.4_6_3_6, 0.3_8_6_7, 0.5_0_7_8])

						assert np.abs(image_slice.flatten() - expected_slice).max() < 1E-3



			def    _lowerCamelCase	(						self       :  List[Any])       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       torch_device in ['cpu', 'mps']

						self._test_attention_slicing_forward_pass(test_max_difference=_SCREAMING_SNAKE_CASE)



			def    _lowerCamelCase	(						self       :  Tuple)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       torch_device in ['cpu', 'mps']

						self._test_inference_batch_single_identical(test_max_difference=_SCREAMING_SNAKE_CASE)



			@unittest.skipIf(
			    torch_device != 'cuda' or not is_xformers_available()			,		reason='XFormers attention is only available with CUDA and `xformers` installed'			,		)
			def    _lowerCamelCase	(						self       :  Any)       ->   Optional[int]:



						"""simple docstring"""






						self._test_xformers_attention_forwardGenerator_pass(test_max_difference=_SCREAMING_SNAKE_CASE)







@slow
@require_torch_gpu
class       __lowerCAmelCase   (      unittest.TestCase						):



			def    _lowerCamelCase	(						self       :  List[Any])       ->   List[Any]:



						"""simple docstring"""






						super().tearDown()
						gc.collect()
						torch.cuda.empty_cache()



			def    _lowerCamelCase	(						self       :  Optional[int])       ->   List[str]:



						"""simple docstring"""






						_UpperCAmelCase			=       load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png')

						_UpperCAmelCase			=       load_numpy(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_l_img2img_anime_turtle_fp16.npy')

						_UpperCAmelCase			=       StableUnCLIPImgaImgPipeline.from_pretrained(
						    'fusing/stable-unclip-2-1-l-img2img'			,		torch_dtype=torch.floataa)
						pipe.to(_SCREAMING_SNAKE_CASE)
						pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE)
						# stable unclip will oom when integration tests are run on a V100,
						# so turn on memory savings
						pipe.enable_attention_slicing()
						pipe.enable_sequential_cpu_offload()

						_UpperCAmelCase			=       torch.Generator(device='cpu').manual_seed(0)
						_UpperCAmelCase			=       pipe(_SCREAMING_SNAKE_CASE			,		'anime turle'			,		generator=_SCREAMING_SNAKE_CASE			,		output_type='np')

						_UpperCAmelCase			=       output.images[0]

						assert image.shape == (7_68, 7_68, 3)

						assert_mean_pixel_difference(_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE)



			def    _lowerCamelCase	(						self       :  Optional[int])       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png')

						_UpperCAmelCase			=       load_numpy(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/stable_unclip_2_1_h_img2img_anime_turtle_fp16.npy')

						_UpperCAmelCase			=       StableUnCLIPImgaImgPipeline.from_pretrained(
						    'fusing/stable-unclip-2-1-h-img2img'			,		torch_dtype=torch.floataa)
						pipe.to(_SCREAMING_SNAKE_CASE)
						pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE)
						# stable unclip will oom when integration tests are run on a V100,
						# so turn on memory savings
						pipe.enable_attention_slicing()
						pipe.enable_sequential_cpu_offload()

						_UpperCAmelCase			=       torch.Generator(device='cpu').manual_seed(0)
						_UpperCAmelCase			=       pipe(_SCREAMING_SNAKE_CASE			,		'anime turle'			,		generator=_SCREAMING_SNAKE_CASE			,		output_type='np')

						_UpperCAmelCase			=       output.images[0]

						assert image.shape == (7_68, 7_68, 3)

						assert_mean_pixel_difference(_SCREAMING_SNAKE_CASE			,		_SCREAMING_SNAKE_CASE)



			def    _lowerCamelCase	(						self       :  str)       ->   Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       load_image(
						    'https://huggingface.co/datasets/hf-internal-testing/diffusers-images/resolve/main/stable_unclip/turtle.png')

						torch.cuda.empty_cache()
						torch.cuda.reset_max_memory_allocated()
						torch.cuda.reset_peak_memory_stats()

						_UpperCAmelCase			=       StableUnCLIPImgaImgPipeline.from_pretrained(
						    'fusing/stable-unclip-2-1-h-img2img'			,		torch_dtype=torch.floataa)
						_UpperCAmelCase			=       pipe.to(_SCREAMING_SNAKE_CASE)
						pipe.set_progress_bar_config(disable=_SCREAMING_SNAKE_CASE)
						pipe.enable_attention_slicing()
						pipe.enable_sequential_cpu_offload()

						_UpperCAmelCase			=       pipe(
						    _SCREAMING_SNAKE_CASE			,		'anime turtle'			,		num_inference_steps=2			,		output_type='np'			,		)

						_UpperCAmelCase			=       torch.cuda.max_memory_allocated()
						# make sure that less than 7 GB is allocated
						assert mem_bytes < 7 * 10**9





import os
from glob import glob

import imageio
import torch
import torchvision
import wandb
from img_processing import custom_to_pil, loop_post_process, preprocess, preprocess_vqgan
from loaders import load_vqgan
from PIL import Image
from torch import nn

from transformers import CLIPModel, CLIPTokenizerFast
from utils import get_device, get_timestamp, show_pil







class       __lowerCAmelCase   :



			def __init__(						self       :  Any			,		A       :  str = "cpu"			,		A       :  str = "openai/clip-vit-large-patch14")       ->   None:



						"""simple docstring"""






						_UpperCAmelCase			=       device
						_UpperCAmelCase			=       CLIPTokenizerFast.from_pretrained(A)
						_UpperCAmelCase			=       [0.4_8_1_4_5_4_6_6, 0.4_5_7_8_2_7_5, 0.4_0_8_2_1_0_7_3]
						_UpperCAmelCase			=       [0.2_6_8_6_2_9_5_4, 0.2_6_1_3_0_2_5_8, 0.2_7_5_7_7_7_1_1]
						_UpperCAmelCase			=       torchvision.transforms.Normalize(self.image_mean			,		self.image_std)
						_UpperCAmelCase			=       torchvision.transforms.Resize(2_24)
						_UpperCAmelCase			=       torchvision.transforms.CenterCrop(2_24)



			def    _lowerCamelCase	(						self       :  str			,		A       :  Any)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       self.resize(A)
						_UpperCAmelCase			=       self.center_crop(A)
						_UpperCAmelCase			=       self.normalize(A)
						return images



			def __call__(						self       :  Any			,		A       :  Dict=None			,		A       :  Dict=None			,		**A       :  List[Any])       ->   Dict:



						"""simple docstring"""






						_UpperCAmelCase			=       self.tokenizer(text=A			,		**A)
						_UpperCAmelCase			=       self.preprocess_img(A)
						_UpperCAmelCase			=       {key: value.to(self.device) for (key, value) in encoding.items()}
						return encoding







class       __lowerCAmelCase   (      nn.Module						):



			def __init__(						self       :  List[Any]			,		A       :  Any=10			,		A       :  List[Any]=0.0_1			,		A       :  Optional[int]=None			,		A       :  int=None			,		A       :  Dict=None			,		A       :  Tuple=None			,		A       :  str=None			,		A       :  Dict=None			,		A       :  Union[str, Any]=False			,		A       :  Any=True			,		A       :  Any="image"			,		A       :  Tuple=True			,		A       :  List[Any]=False			,		A       :  int=False			,		A       :  int=False			,		)       ->   None:



						"""simple docstring"""






						super().__init__()
						_UpperCAmelCase			=       None
						_UpperCAmelCase			=       device if device else get_device()
						if vqgan:
									_UpperCAmelCase			=       vqgan
						else:
									_UpperCAmelCase			=       load_vqgan(self.device			,		conf_path=A			,		ckpt_path=A)
						self.vqgan.eval()
						if clip:
									_UpperCAmelCase			=       clip
						else:
									_UpperCAmelCase			=       CLIPModel.from_pretrained('openai/clip-vit-base-patch32')
						self.clip.to(self.device)
						_UpperCAmelCase			=       ProcessorGradientFlow(device=self.device)

						_UpperCAmelCase			=       iterations
						_UpperCAmelCase			=       lr
						_UpperCAmelCase			=       log
						_UpperCAmelCase			=       make_grid
						_UpperCAmelCase			=       return_val
						_UpperCAmelCase			=       quantize
						_UpperCAmelCase			=       self.vqgan.decoder.z_shape



			def    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  int=None			,		A       :  Union[str, Any]=None			,		A       :  Dict=5			,		A       :  Optional[Any]=True)       ->   Optional[Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       []
						if output_path is None:
									_UpperCAmelCase			=       './animation.gif'
						if input_path is None:
									_UpperCAmelCase			=       self.save_path
						_UpperCAmelCase			=       sorted(glob(input_path + '/*'))
						if not len(A):
									raise ValueError(
									    'No images found in save path, aborting (did you pass save_intermediate=True to the generate'
									    ' function?)')
						if len(A) == 1:
									print('Only one image found in save path, (did you pass save_intermediate=True to the generate function?)')
						_UpperCAmelCase			=       total_duration / len(A)
						_UpperCAmelCase			=       [frame_duration] * len(A)
						if extend_frames:
									_UpperCAmelCase			=       1.5
									_UpperCAmelCase			=       3
						for file_name in paths:
									if file_name.endswith('.png'):
												images.append(imageio.imread(A))
						imageio.mimsave(A			,		A			,		duration=A)
						print(F"gif saved to {output_path}")



			def    _lowerCamelCase	(						self       :  List[str]			,		A       :  Optional[Any]=None			,		A       :  Optional[int]=None)       ->   int:



						"""simple docstring"""






						if not (path or img):
									raise ValueError('Input either path or tensor')
						if img is not None:
									raise NotImplementedError
						_UpperCAmelCase			=       preprocess(Image.open(A)			,		target_image_size=2_56).to(self.device)
						_UpperCAmelCase			=       preprocess_vqgan(A)
						_UpperCAmelCase	,					*_UpperCAmelCase			=       self.vqgan.encode(A)
						return z



			def    _lowerCamelCase	(						self       :  List[str]			,		A       :  int)       ->   Dict:



						"""simple docstring"""






						_UpperCAmelCase			=       self.latent.detach().requires_grad_()
						_UpperCAmelCase			=       base_latent + transform_vector
						if self.quantize:
									_UpperCAmelCase	,					*_UpperCAmelCase			=       self.vqgan.quantize(A)
						else:
									_UpperCAmelCase			=       trans_latent
						return self.vqgan.decode(A)



			def    _lowerCamelCase	(						self       :  Any			,		A       :  Dict			,		A       :  Dict			,		A       :  Optional[Any]=None)       ->   Any:



						"""simple docstring"""






						_UpperCAmelCase			=       self.clip_preprocessor(text=A			,		images=A			,		return_tensors='pt'			,		padding=A)
						_UpperCAmelCase			=       self.clip(**A)
						_UpperCAmelCase			=       clip_outputs.logits_per_image
						if weights is not None:
									_UpperCAmelCase			=       similarity_logits * weights
						return similarity_logits.sum()



			def    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  Dict			,		A       :  int			,		A       :  Tuple)       ->   str:



						"""simple docstring"""






						_UpperCAmelCase			=       self._get_clip_similarity(pos_prompts['prompts']			,		A			,		weights=(1 / pos_prompts['weights']))
						if neg_prompts:
									_UpperCAmelCase			=       self._get_clip_similarity(neg_prompts['prompts']			,		A			,		weights=neg_prompts['weights'])
						else:
									_UpperCAmelCase			=       torch.tensor([1]			,		device=self.device)
						_UpperCAmelCase			=       -torch.log(A) + torch.log(A)
						return loss



			def    _lowerCamelCase	(						self       :  Tuple			,		A       :  Optional[int]			,		A       :  List[Any]			,		A       :  Optional[int])       ->   Union[str, Any]:



						"""simple docstring"""






						_UpperCAmelCase			=       torch.randn_like(self.latent			,		requires_grad=A			,		device=self.device)
						_UpperCAmelCase			=       torch.optim.Adam([vector]			,		lr=self.lr)

						for i in range(self.iterations):
									optim.zero_grad()
									_UpperCAmelCase			=       self._add_vector(A)
									_UpperCAmelCase			=       loop_post_process(A)
									_UpperCAmelCase			=       self._get_CLIP_loss(A			,		A			,		A)
									print('CLIP loss'			,		A)
									if self.log:
												wandb.log({'CLIP Loss': clip_loss})
									clip_loss.backward(retain_graph=A)
									optim.step()
									if self.return_val == "image":
												yield custom_to_pil(transformed_img[0])
									else:
												yield vector



			def    _lowerCamelCase	(						self       :  Dict			,		A       :  Any			,		A       :  Optional[int]			,		A       :  str)       ->   Any:



						"""simple docstring"""






						wandb.init(reinit=A			,		project='face-editor')
						wandb.config.update({'Positive Prompts': positive_prompts})
						wandb.config.update({'Negative Prompts': negative_prompts})
						wandb.config.update({'lr': self.lr, 'iterations': self.iterations})
						if image_path:
									_UpperCAmelCase			=       Image.open(A)
									_UpperCAmelCase			=       image.resize((2_56, 2_56))
									wandb.log('Original Image'			,		wandb.Image(A))



			def    _lowerCamelCase	(						self       :  Dict			,		A       :  int)       ->   Dict:



						"""simple docstring"""






						if not prompts:
									return []
						_UpperCAmelCase			=       []
						_UpperCAmelCase			=       []
						if isinstance(A			,		A):
									_UpperCAmelCase			=       [prompt.strip() for prompt in prompts.split('|')]
						for prompt in prompts:
									if isinstance(A			,		(tuple, list)):
												_UpperCAmelCase			=       prompt[0]
												_UpperCAmelCase			=       float(prompt[1])
									elif ":" in prompt:
												_UpperCAmelCase	,					_UpperCAmelCase			=       prompt.split(':')
												_UpperCAmelCase			=       float(A)
									else:
												_UpperCAmelCase			=       prompt
												_UpperCAmelCase			=       1.0
									processed_prompts.append(A)
									weights.append(A)
						return {
						    "prompts": processed_prompts,
						    "weights": torch.tensor(A			,		device=self.device),
						}



			def    _lowerCamelCase	(						self       :  Optional[int]			,		A       :  Union[str, Any]			,		A       :  Union[str, Any]=None			,		A       :  int=None			,		A       :  Optional[Any]=True			,		A       :  Dict=False			,		A       :  Union[str, Any]=True			,		A       :  Any=True			,		A       :  Any=None			,		)       ->   Dict:



						"""simple docstring"""






						if image_path:
									_UpperCAmelCase			=       self._get_latent(A)
						else:
									_UpperCAmelCase			=       torch.randn(self.latent_dim			,		device=self.device)
						if self.log:
									self._init_logging(A			,		A			,		A)

						assert pos_prompts, "You must provide at least one positive prompt."
						_UpperCAmelCase			=       self.process_prompts(A)
						_UpperCAmelCase			=       self.process_prompts(A)
						if save_final and save_path is None:
									_UpperCAmelCase			=       os.path.join('./outputs/'			,		'_'.join(pos_prompts['prompts']))
						if not os.path.exists(A):
									os.makedirs(A)
						else:
									_UpperCAmelCase			=       save_path + '_' + get_timestamp()
									os.makedirs(A)
						_UpperCAmelCase			=       save_path

						_UpperCAmelCase			=       self.vqgan.decode(self.latent)[0]
						if show_intermediate:
									print('Original Image')
									show_pil(custom_to_pil(A))

						_UpperCAmelCase			=       loop_post_process(A)
						for iter, transformed_img in enumerate(self._optimize_CLIP(A			,		A			,		A)):
									if show_intermediate:
												show_pil(A)
									if save_intermediate:
												transformed_img.save(os.path.join(self.save_path			,		F"iter_{iter:03d}.png"))
									if self.log:
												wandb.log({'Image': wandb.Image(A)})
						if show_final:
									show_pil(A)
						if save_final:
									transformed_img.save(os.path.join(self.save_path			,		F"iter_{iter:03d}_final.png"))
