imagededup Documentation

repository·master·Indexed 26 days ago

https://github.com/idealo/imagededup

A Python package for finding exact and near duplicates in image collections. It supports multiple deduplication algorithms, including hashing methods (PHash, DHash, AHash, WHash) and Convolutional Neural Networks (CNN). The library allows for custom PyTorch model integration via CustomModel and provides tools for encoding generation, duplicate visualization, and quality evaluation using metrics like MAP, NDCG, and F1-score.

Tokens
5.6K
Snippets
14
Records
29
Agent score
91%

What's inside imagededup

  1. Available deduplication algorithms in imagededup

    master

    The package supports several algorithms for finding exact and near duplicates:

    • Convolutional Neural Network (CNN): Best for near duplicates and datasets containing transformations. Supports prepackaged models or custom models.
    • Perceptual hashing (PHash)
    • Difference hashing (DHash): Noted as the fastest method for exact duplicates.
    • Wavelet hashing (WHash)
    • Average hashing (AHash)
  2. Compare performance of Hashing vs CNN methods

    master

    When choosing between hashing and CNN methods, consider the following trade-offs:

    • Speed: Hashing methods (dhash, phash, etc.) are significantly faster than the cnn method. dhash is typically the fastest.
    • Accuracy: The cnn method is much more robust for near-duplicates and transformed images, whereas hashing methods are primarily effective for exact or near-exact matches.
    • Hardware: The cnn method is computationally intensive and will run much faster if a GPU is available. Benchmarks provided are based on CPU-only execution.
  3. Use a user-defined custom PyTorch model with CNN

    master

    To use your own neural network architecture, wrap your PyTorch model in a CustomModel object. Your model must be a subclass of torch.nn.Module and implement a forward method that returns a tensor of shape (batch_size, features).

    You must provide:

    1. name: A string identifier for the model.
    2. model: The PyTorch model instance.
    3. transform: A function (e.g., torchvision.transforms.Compose) that converts a PIL.Image into a PyTorch tensor compatible with your model's preprocessing requirements.

    Note: name and transform do not have to be attributes of the model class itself; they can be passed separately to the CustomModel constructor.

    from imagededup.methods import CNN
    from imagededup.utils import CustomModel
    import torch
    from torchvision.transforms import transforms
    
    # Define your custom model
    class MyModel(torch.nn.Module):
        transform = transforms.Compose(
            [
                transforms.ToTensor()
            ]
        )
        name = 'my_custom_model'
    
        def __init__(self):
            super().__init__()
            # Define the layers of the model here
    
        def forward(self, x):
            # Do something with x
            return x
    
    # Wrap in CustomModel
    custom_config = CustomModel(name=MyModel.name,
                                model=MyModel(),
                                transform=MyModel.transform)
    
    # Initialize CNN with your custom model
    cnn = CNN(model_config=custom_config)
    
    # Use the model as usual
    # ...
  4. Use prepackaged CNN models with CustomModel

    master

    You can use high-performance prepackaged models (MobileNetV3, ViT, or EfficientNet) by wrapping them in a CustomModel construct and passing them to the CNN class via the model_config argument.

    Available prepackaged models:

    • MobilenetV3 (Default)
    • ViT (Vision Transformer)
    • EfficientNet (EfficientNet B4)
    from imagededup.methods import CNN
    from imagededup.utils import CustomModel
    from imagededup.utils.models import ViT, MobilenetV3, EfficientNet
    
    # Declare a custom config with a prepackaged model
    custom_config = CustomModel(name=EfficientNet.name,
                                model=EfficientNet(), 
                                transform=EfficientNet.transform)
    
    # Pass the config to the CNN object
    cnn = CNN(model_config=custom_config)
    
    # Use the model as usual
    # ...
  5. Install imagededup

    master

    You can install imagededup via PyPI (recommended) or directly from the GitHub source.

    From PyPI:

    pip install imagededup

    From GitHub source:

    git clone https://github.com/idealo/imagededup.git
    cd imagededup
    pip install .
    pip install imagededup
  6. Plot duplicates of an image using plot_duplicates

    master

    After generating a duplicate dictionary for an image directory (e.g., using find_duplicates), you can visualize the duplicates associated with a specific file using the plot_duplicates method from imagededup.utils.

    This function generates a plot showing the target image alongside its identified duplicates. You can pass either a simple duplicate map (filenames as keys, list of duplicates as values) or a map containing similarity scores.

  7. Difference between Information Retrieval and Classification metrics

    master

    When evaluating duplicates, imagededup treats symmetric relationships differently depending on the metric type:

    1. Information Retrieval Metrics (map, ndcg, jaccard): These treat each key in the map as an independent 'query'. If a symmetric relationship is missing (e.g., A lists B as a duplicate, but B does not list A), the error is counted twice (once for query A and once for query B).

    2. Classification Metrics (precision, recall, f1-score): These form unique pairs of images and label them as 0 (non-duplicate) or 1 (duplicate). In this mode, a missing symmetric relationship is only accounted for once as a single failed pair.

  8. Emulate native compiler built-ins using PSNIP_BUILTIN_EMULATE_NATIVE

    master

    You can use native-style built-in names (like __builtin_ffs) regardless of which compiler is in use (e.g., in MSVC or older GCC versions) by defining the PSNIP_BUILTIN_EMULATE_NATIVE macro before including builtin.h.

    When this macro is defined, the header provides definitions for missing native built-ins, allowing you to write code that is portable across different compilers without changing the function names.

  9. Choose the best method for your duplicate detection use case

    master

    Based on benchmark results, use the following strategies depending on your goal:

    Finding Near Duplicates

    Use the cnn method with a min_similarity_threshold between 0.5 and 0.9. Hashing methods generally perform poorly for near-duplicate detection.

    Finding Transformed Duplicates (crops, flips, rotations)

    Use the cnn method with the default min_similarity_threshold of 0.9. Hashing methods are not effective for most transformations (except resizing).

    Finding Exact Duplicates

    • Hashing: Use dhash with a max_distance_threshold of 0 for the fastest performance.
    • CNN: Use the cnn method with a high min_similarity_threshold (e.g., 0.9).
  10. Build and serve the documentation locally

    master

    To build and view the project documentation locally, install mkdocs and mkdocs-material via pip, then use the mkdocs serve command. The documentation will be available at http://127.0.0.1:8000/.

    pip install mkdocs mkdocs-material
    mkdocs serve
  11. Configure dependencies for builtin.h

    master

    To ensure maximum portability when using builtin.h, you should #include the exact-int module before including the header.

    If you prefer not to add the exact-int module to your project, you can omit it; the module will then rely on <stdint.h>. Alternatively, you can manually define the following macros to appropriate types:

    • psnip_int8_t
    • psnip_uint8_t
    • psnip_int16_t
    • psnip_uint16_t
    • psnip_int32_t
    • psnip_uint32_t
    • psnip_int64_t
    • psnip_uint64_t