






'''simple docstring'''

from typing import Any, Callable, Dict, List, Optional, Union

import torch
from transformers import CLIPImageProcessor, CLIPTextModel, CLIPTokenizer

from diffusers import (
    AutoencoderKL,
    DDIMScheduler,
    DiffusionPipeline,
    LMSDiscreteScheduler,
    PNDMScheduler,
    StableDiffusionPipeline,
    UNetaDConditionModel,
)
from diffusers.pipelines.stable_diffusion import StableDiffusionPipelineOutput
from diffusers.pipelines.stable_diffusion.safety_checker import StableDiffusionSafetyChecker


UpperCamelCase      					="CompVis/stable-diffusion-v1-1"
UpperCamelCase      					="CompVis/stable-diffusion-v1-2"
UpperCamelCase      					="CompVis/stable-diffusion-v1-3"
UpperCamelCase      					="CompVis/stable-diffusion-v1-4"




class      A     (   SCREAMING_SNAKE_CASE__    ):






 """simple docstring"""





 def __init__(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase      ,  __lowerCAmelCase = True      ,  ):
       super()._init_()

       UpperCamelCase_					: List[str]            =				StableDiffusionPipeline.from_pretrained(__lowerCAmelCase			)
       UpperCamelCase_					: str            =				StableDiffusionPipeline.from_pretrained(__lowerCAmelCase			)
       UpperCamelCase_					: Optional[Any]            =				StableDiffusionPipeline.from_pretrained(__lowerCAmelCase			)
       UpperCamelCase_					: str            =				StableDiffusionPipeline(
           vae=__lowerCAmelCase      ,  text_encoder=__lowerCAmelCase      ,  tokenizer=__lowerCAmelCase      ,  unet=__lowerCAmelCase      ,  scheduler=__lowerCAmelCase      ,  safety_checker=__lowerCAmelCase      ,  feature_extractor=__lowerCAmelCase      ,  requires_safety_checker=__lowerCAmelCase      ,  )

       self.register_modules(pipelinea=self.pipea      ,  pipelinea=self.pipea      ,  pipelinea=self.pipea      ,  pipelinea=self.pipea			)
 @property
 def 	_UpperCAmelCase							(				self			):
       return {k: getattr(self      ,  __lowerCAmelCase			) for k in self.config.keys() if not k.startswith("""_"""			)}
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase = "auto"			):
       if slice_size == "auto":
             # half the attention head size is usually a good trade-off between
             # speed and memory
             UpperCamelCase_					: List[Any]            =				self.unet.config.attention_head_dim // 2
       self.unet.set_attention_slice(__lowerCAmelCase			)
 def 	_UpperCAmelCase							(				self			):
       self.enable_attention_slicing(__lowerCAmelCase			)
 @torch.no_grad()
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 50      ,  __lowerCAmelCase = 7.5      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  __lowerCAmelCase = 0.0      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = "pil"      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  **__lowerCAmelCase      ,  ):
       return self.pipea(
           prompt=__lowerCAmelCase      ,  height=__lowerCAmelCase      ,  width=__lowerCAmelCase      ,  num_inference_steps=__lowerCAmelCase      ,  guidance_scale=__lowerCAmelCase      ,  negative_prompt=__lowerCAmelCase      ,  num_images_per_prompt=__lowerCAmelCase      ,  eta=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  latents=__lowerCAmelCase      ,  output_type=__lowerCAmelCase      ,  return_dict=__lowerCAmelCase      ,  callback=__lowerCAmelCase      ,  callback_steps=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )
 @torch.no_grad()
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 50      ,  __lowerCAmelCase = 7.5      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  __lowerCAmelCase = 0.0      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = "pil"      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  **__lowerCAmelCase      ,  ):
       return self.pipea(
           prompt=__lowerCAmelCase      ,  height=__lowerCAmelCase      ,  width=__lowerCAmelCase      ,  num_inference_steps=__lowerCAmelCase      ,  guidance_scale=__lowerCAmelCase      ,  negative_prompt=__lowerCAmelCase      ,  num_images_per_prompt=__lowerCAmelCase      ,  eta=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  latents=__lowerCAmelCase      ,  output_type=__lowerCAmelCase      ,  return_dict=__lowerCAmelCase      ,  callback=__lowerCAmelCase      ,  callback_steps=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )
 @torch.no_grad()
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 50      ,  __lowerCAmelCase = 7.5      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  __lowerCAmelCase = 0.0      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = "pil"      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  **__lowerCAmelCase      ,  ):
       return self.pipea(
           prompt=__lowerCAmelCase      ,  height=__lowerCAmelCase      ,  width=__lowerCAmelCase      ,  num_inference_steps=__lowerCAmelCase      ,  guidance_scale=__lowerCAmelCase      ,  negative_prompt=__lowerCAmelCase      ,  num_images_per_prompt=__lowerCAmelCase      ,  eta=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  latents=__lowerCAmelCase      ,  output_type=__lowerCAmelCase      ,  return_dict=__lowerCAmelCase      ,  callback=__lowerCAmelCase      ,  callback_steps=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )
 @torch.no_grad()
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 50      ,  __lowerCAmelCase = 7.5      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  __lowerCAmelCase = 0.0      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = "pil"      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  **__lowerCAmelCase      ,  ):
       return self.pipea(
           prompt=__lowerCAmelCase      ,  height=__lowerCAmelCase      ,  width=__lowerCAmelCase      ,  num_inference_steps=__lowerCAmelCase      ,  guidance_scale=__lowerCAmelCase      ,  negative_prompt=__lowerCAmelCase      ,  num_images_per_prompt=__lowerCAmelCase      ,  eta=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  latents=__lowerCAmelCase      ,  output_type=__lowerCAmelCase      ,  return_dict=__lowerCAmelCase      ,  callback=__lowerCAmelCase      ,  callback_steps=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )





 @torch.no_grad()
 def 	_UpperCAmelCase							(				self      ,  __lowerCAmelCase      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 5_12      ,  __lowerCAmelCase = 50      ,  __lowerCAmelCase = 7.5      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  __lowerCAmelCase = 0.0      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = "pil"      ,  __lowerCAmelCase = True      ,  __lowerCAmelCase = None      ,  __lowerCAmelCase = 1      ,  **__lowerCAmelCase      ,  ):
       UpperCamelCase_					: List[Any]            =				"""cuda""" if torch.cuda.is_available() else """cpu"""
       self.to(__lowerCAmelCase			)

       # Checks if the height and width are divisible by 8 or not
       if height % 8 != 0 or width % 8 != 0:
             raise ValueError(F"`height` and `width` must be divisible by 8 but are {height} and {width}."			)

       # Get first result from Stable Diffusion Checkpoint v1.1
       UpperCamelCase_					: str            =				self.textaimg_sda_a(
           prompt=__lowerCAmelCase      ,  height=__lowerCAmelCase      ,  width=__lowerCAmelCase      ,  num_inference_steps=__lowerCAmelCase      ,  guidance_scale=__lowerCAmelCase      ,  negative_prompt=__lowerCAmelCase      ,  num_images_per_prompt=__lowerCAmelCase      ,  eta=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  latents=__lowerCAmelCase      ,  output_type=__lowerCAmelCase      ,  return_dict=__lowerCAmelCase      ,  callback=__lowerCAmelCase      ,  callback_steps=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )

       # Get first result from Stable Diffusion Checkpoint v1.2
       UpperCamelCase_					: int            =				self.textaimg_sda_a(
           prompt=__lowerCAmelCase      ,  height=__lowerCAmelCase      ,  width=__lowerCAmelCase      ,  num_inference_steps=__lowerCAmelCase      ,  guidance_scale=__lowerCAmelCase      ,  negative_prompt=__lowerCAmelCase      ,  num_images_per_prompt=__lowerCAmelCase      ,  eta=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  latents=__lowerCAmelCase      ,  output_type=__lowerCAmelCase      ,  return_dict=__lowerCAmelCase      ,  callback=__lowerCAmelCase      ,  callback_steps=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )

       # Get first result from Stable Diffusion Checkpoint v1.3
       UpperCamelCase_					: Tuple            =				self.textaimg_sda_a(
           prompt=__lowerCAmelCase      ,  height=__lowerCAmelCase      ,  width=__lowerCAmelCase      ,  num_inference_steps=__lowerCAmelCase      ,  guidance_scale=__lowerCAmelCase      ,  negative_prompt=__lowerCAmelCase      ,  num_images_per_prompt=__lowerCAmelCase      ,  eta=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  latents=__lowerCAmelCase      ,  output_type=__lowerCAmelCase      ,  return_dict=__lowerCAmelCase      ,  callback=__lowerCAmelCase      ,  callback_steps=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )

       # Get first result from Stable Diffusion Checkpoint v1.4
       UpperCamelCase_					: Dict            =				self.textaimg_sda_a(
           prompt=__lowerCAmelCase      ,  height=__lowerCAmelCase      ,  width=__lowerCAmelCase      ,  num_inference_steps=__lowerCAmelCase      ,  guidance_scale=__lowerCAmelCase      ,  negative_prompt=__lowerCAmelCase      ,  num_images_per_prompt=__lowerCAmelCase      ,  eta=__lowerCAmelCase      ,  generator=__lowerCAmelCase      ,  latents=__lowerCAmelCase      ,  output_type=__lowerCAmelCase      ,  return_dict=__lowerCAmelCase      ,  callback=__lowerCAmelCase      ,  callback_steps=__lowerCAmelCase      ,  **__lowerCAmelCase      ,  )

       # Get all result images into a single list and pass it via StableDiffusionPipelineOutput for final result
       return StableDiffusionPipelineOutput([resa[0], resa[0], resa[0], resa[0]]			)














'''simple docstring'''

import numpy as np

# Importing the Keras libraries and packages
import tensorflow as tf
from tensorflow.keras import layers, models

if __name__ == "__main__":
      # Initialising the CNN
      # (Sequential- Building the model layer by layer)
      UpperCamelCase      					=models.Sequential()

      # Step 1 - Convolution
      # Here 64,64 is the length & breadth of dataset images and 3 is for the RGB channel
      # (3,3) is the kernel size (filter matrix)
      classifier.add(
          layers.ConvaD(32, (3, 3), input_shape=(64, 64, 3), activation="relu")
      )

      # Step 2 - Pooling
      classifier.add(layers.MaxPoolingaD(pool_size=(2, 2)))

      # Adding a second convolutional layer
      classifier.add(layers.ConvaD(32, (3, 3), activation="relu"))
      classifier.add(layers.MaxPoolingaD(pool_size=(2, 2)))

      # Step 3 - Flattening
      classifier.add(layers.Flatten())

      # Step 4 - Full connection
      classifier.add(layers.Dense(units=128, activation="relu"))
      classifier.add(layers.Dense(units=1, activation="sigmoid"))

      # Compiling the CNN
      classifier.compile(
          optimizer="adam", loss="binary_crossentropy", metrics=["accuracy"]
      )

      # Part 2 - Fitting the CNN to the images

      # Load Trained model weights

      # from keras.models import load_model
      # regressor=load_model('cnn.h5')

      UpperCamelCase      					=tf.keras.preprocessing.image.ImageDataGenerator(
          rescale=1.0 / 255, shear_range=0.2, zoom_range=0.2, horizontal_flip=True
      )

      UpperCamelCase      					=tf.keras.preprocessing.image.ImageDataGenerator(rescale=1.0 / 255)

      UpperCamelCase      					=train_datagen.flow_from_directory(
          "dataset/training_set", target_size=(64, 64), batch_size=32, class_mode="binary"
      )

      UpperCamelCase      					=test_datagen.flow_from_directory(
          "dataset/test_set", target_size=(64, 64), batch_size=32, class_mode="binary"
      )

      classifier.fit_generator(
          training_set, steps_per_epoch=5, epochs=30, validation_data=test_set
      )

      classifier.save("cnn.h5")

      # Part 3 - Making new predictions

      UpperCamelCase      					=tf.keras.preprocessing.image.load_img(
          "dataset/single_prediction/image.png", target_size=(64, 64)
      )
      UpperCamelCase      					=tf.keras.preprocessing.image.img_to_array(test_image)
      UpperCamelCase      					=np.expand_dims(test_image, axis=0)
      UpperCamelCase      					=classifier.predict(test_image)
      # training_set.class_indices
      if result[0][0] == 0:
            UpperCamelCase      					="Normal"
      if result[0][0] == 1:
            UpperCamelCase      					="Abnormality detected"






