class_docstr: Find duplicates using CNN and/or generate CNN encodings given a single image or a directory of images.
  
  The module can be used for 2 purposes: Encoding generation and duplicate detection.
  - Encodings generation:
  To propagate an image through a Convolutional Neural Network architecture and generate encodings. The generated
  encodings can be used at a later time for deduplication. Using the method 'encode_image', the CNN encodings for a
  single image can be obtained while the 'encode_images' method can be used to get encodings for all images in a
  directory.
  
  - Duplicate detection:
  Find duplicates either using the encoding mapping generated previously using 'encode_images' or using a Path to the
  directory that contains the images that need to be deduplicated. 'find_duplicates' and 'find_duplicates_to_remove'
  methods are provided to accomplish these tasks.
class_docstr_tok: ["Find","duplicates","using","CNN","and\/or","generate","CNN","encodings","given","a","single","image","or","a","directory","of","images",".","The","module","can","be","used","for","2","purposes",":","Encoding","generation","and","duplicate","detection",".","-","Encodings","generation",":","To","propagate","an","image","through","a","Convolutional","Neural","Network","architecture","and","generate","encodings",".","The","generated","encodings","can","be","used","at","a","later","time","for","deduplication",".","Using","the","method","'encode_image","'",",","the","CNN","encodings","for","a","single","image","can","be","obtained","while","the","'encode_images","'","method","can","be","used","to","get","encodings","for","all","images","in","a","directory",".","-","Duplicate","detection",":","Find","duplicates","either","using","the","encoding","mapping","generated","previously","using","'encode_images","'","or","using","a","Path","to","the","directory","that","contains","the","images","that","need","to","be","deduplicated",".","'find_duplicates","'","and","'find_duplicates_to_remove'","methods","are","provided","to","accomplish","these","tasks","."]
docstr: Generate CNN encoding for a single image.
  
  Args:
      image_file: Path to the image file.
      image_array: Optional, used instead of image_file. Image typecast to numpy array.
  
  Returns:
      encoding: Encodings for the image in the form of numpy array.
  
  Example:
  ```
  from imagededup.methods import CNN
  myencoder = CNN()
  encoding = myencoder.encode_image(image_file='path/to/image.jpg')
  OR
  encoding = myencoder.encode_image(image_array=<numpy array of image>)
  ```
docstr_tok: ["Generate","CNN","encoding","for","a","single","image",".","Args",":","image_file",":","Path","to","the","image","file",".","image_array",":","Optional",",","used","instead","of","image_file",".","Image","typecast","to","numpy","array",".","Returns",":","encoding",":","Encodings","for","the","image","in","the","form","of","numpy","array",".","Example",":","``","`","from","imagededup.methods","import","CNN","myencoder","=","CNN","(",")","encoding","=","myencoder.encode_image","(","image_file='path\/to\/image.jpg","'",")","OR","encoding","=","myencoder.encode_image","(","image_array=","<","numpy","array","of","image",">",")","``","`"]
code: def encode_image(
      self,
      image_file: Optional[Union[PurePath, str]] = None,
      image_array: Optional[np.ndarray] = None,
  ) -> np.ndarray:
      """
      Generate CNN encoding for a single image.
  
      Args:
          image_file: Path to the image file.
          image_array: Optional, used instead of image_file. Image typecast to numpy array.
  
      Returns:
          encoding: Encodings for the image in the form of numpy array.
  
      Example:
      ```
      from imagededup.methods import CNN
      myencoder = CNN()
      encoding = myencoder.encode_image(image_file='path/to/image.jpg')
      OR
      encoding = myencoder.encode_image(image_array=<numpy array of image>)
      ```
      """
      if isinstance(image_file, str):
          image_file = Path(image_file)
  
      if isinstance(image_file, PurePath):
          if not image_file.is_file():
              raise ValueError(
                  "Please provide either image file path or image array!"
              )
  
          image_pp = load_image(
              image_file=image_file, target_size=None, grayscale=False
          )
  
      elif isinstance(image_array, np.ndarray):
          image_array = expand_image_array_cnn(
              image_array
          )  # Add 3rd dimension if array is grayscale, do sanity checks
          image_pp = preprocess_image(
              image=image_array, target_size=None, grayscale=False
          )
      else:
          raise ValueError(
              "Please provide either image file path or image array!"
          )
  
      return (
          self._get_cnn_features_single(image_pp)
          if isinstance(image_pp, np.ndarray)
          else None
      )
  
imports: from pathlib import Path, PurePath
  import sys
  from typing import Dict, List, Optional, Union
  import warnings
  from multiprocessing import cpu_count
  import numpy
  from PIL import Image
  import torch
  from imagededup.handlers.search.retrieval import get_cosine_similarity
  from imagededup.utils.data_generator import img_dataloader
  from imagededup.utils.models import CustomModel, MobilenetV3, DEFAULT_MODEL_NAME
  from imagededup.utils.general_utils import generate_relative_names, get_files_to_remove, save_json
  from imagededup.utils.image_utils import expand_image_array_cnn, load_image, preprocess_image
  from imagededup.utils.logger import return_logger
label_desc: Use image node_id 6 for calling the CNN obj's underlying member method code with example usage: obj.encode_image(image_file, image_array) and returns: unknown