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: Take in dictionary {filename: encoded image}, detects duplicates above the given cosine similarity threshold
  and returns a dictionary containing key as filename and value as a list of duplicate filenames. Optionally,
  the cosine distances could be returned instead of just duplicate filenames for each query file.
  
  Args:
      encoding_map: Dictionary with keys as file names and values as encoded images.
      min_similarity_threshold: Cosine similarity above which retrieved duplicates are valid.
      scores: Boolean indicating whether similarity scores are to be returned along with retrieved duplicates.
      num_sim_workers: Optional, number of cpu cores to use for multiprocessing similarity computation, set to number of CPUs in the system by default. 0 disables multiprocessing.
  
  Returns:
      if scores is True, then a dictionary of the form {'image1.jpg': [('image1_duplicate1.jpg',
      score), ('image1_duplicate2.jpg', score)], 'image2.jpg': [] ..}
      if scores is False, then a dictionary of the form {'image1.jpg': ['image1_duplicate1.jpg',
      'image1_duplicate2.jpg'], 'image2.jpg':['image1_duplicate1.jpg',..], ..}
docstr_tok: ["Take","in","dictionary","{","filename",":","encoded","image","}",",","detects","duplicates","above","the","given","cosine","similarity","threshold","and","returns","a","dictionary","containing","key","as","filename","and","value","as","a","list","of","duplicate","filenames",".","Optionally",",","the","cosine","distances","could","be","returned","instead","of","just","duplicate","filenames","for","each","query","file",".","Args",":","encoding_map",":","Dictionary","with","keys","as","file","names","and","values","as","encoded","images",".","min_similarity_threshold",":","Cosine","similarity","above","which","retrieved","duplicates","are","valid",".","scores",":","Boolean","indicating","whether","similarity","scores","are","to","be","returned","along","with","retrieved","duplicates",".","num_sim_workers",":","Optional",",","number","of","cpu","cores","to","use","for","multiprocessing","similarity","computation",",","set","to","number","of","CPUs","in","the","system","by","default",".","0","disables","multiprocessing",".","Returns",":","if","scores","is","True",",","then","a","dictionary","of","the","form","{","'image1.jpg","'",":","[","(","'image1_duplicate1.jpg","'",",","score",")",",","(","'image1_duplicate2.jpg","'",",","score",")","]",",","'image2.jpg","'",":","[","]","..","}","if","scores","is","False",",","then","a","dictionary","of","the","form","{","'image1.jpg","'",":","[","'image1_duplicate1.jpg","'",",","'image1_duplicate2.jpg","'","]",",","'image2.jpg","'",":","[","'image1_duplicate1.jpg","'",",","..","]",",","..","}"]
code: def _find_duplicates_dict(
      self,
      encoding_map: Dict[str, list],
      min_similarity_threshold: float,
      scores: bool,
      outfile: Optional[str] = None,
      num_sim_workers: int = cpu_count(),
  ) -> Dict:
      """
      Take in dictionary {filename: encoded image}, detects duplicates above the given cosine similarity threshold
      and returns a dictionary containing key as filename and value as a list of duplicate filenames. Optionally,
      the cosine distances could be returned instead of just duplicate filenames for each query file.
  
      Args:
          encoding_map: Dictionary with keys as file names and values as encoded images.
          min_similarity_threshold: Cosine similarity above which retrieved duplicates are valid.
          scores: Boolean indicating whether similarity scores are to be returned along with retrieved duplicates.
          num_sim_workers: Optional, number of cpu cores to use for multiprocessing similarity computation, set to number of CPUs in the system by default. 0 disables multiprocessing.
  
      Returns:
          if scores is True, then a dictionary of the form {'image1.jpg': [('image1_duplicate1.jpg',
          score), ('image1_duplicate2.jpg', score)], 'image2.jpg': [] ..}
          if scores is False, then a dictionary of the form {'image1.jpg': ['image1_duplicate1.jpg',
          'image1_duplicate2.jpg'], 'image2.jpg':['image1_duplicate1.jpg',..], ..}
      """
  
      # get all image ids
      # we rely on dictionaries preserving insertion order in Python >=3.6
      image_ids = np.array([*encoding_map.keys()])
  
      # put image encodings into feature matrix
      features = np.array([*encoding_map.values()])
  
      self.logger.info("Start: Calculating cosine similarities...")
  
      self.cosine_scores = get_cosine_similarity(
          features, self.verbose, num_workers=num_sim_workers
      )
  
      np.fill_diagonal(
          self.cosine_scores, 2.0
      )  # allows to filter diagonal in results, 2 is a placeholder value
  
      self.logger.info("End: Calculating cosine similarities.")
  
      self.results = {}
      for i, j in enumerate(self.cosine_scores):
          duplicates_bool = (j >= min_similarity_threshold) & (j < 2)
  
          if scores:
              tmp = np.array([*zip(image_ids, j)], dtype=object)
              duplicates = list(map(tuple, tmp[duplicates_bool]))
  
          else:
              duplicates = list(image_ids[duplicates_bool])
  
          self.results[image_ids[i]] = duplicates
  
      if outfile and scores:
          save_json(
              results=self.results, filename=outfile, float_scores=True
          )
      elif outfile:
          save_json(results=self.results, filename=outfile)
      return self.results
  
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 9 for calling the CNN obj's underlying member method code with example usage: obj._find_duplicates_dict(encoding_map, min_similarity_threshold, scores, outfile, num_sim_workers) and returns: self