Metadata-Version: 2.1
Name: out-of-domain-library
Version: 0.2
Summary: A library for out-of-domain tasks.
Home-page: https://github.com/suraj385/out_of_domain_library
Author: Suraj Gautam
Author-email: beoingsuraz3385@gmail.com
Classifier: Programming Language :: Python :: 3
Classifier: License :: OSI Approved :: MIT License
Classifier: Operating System :: OS Independent
Requires-Python: >=3.6
Description-Content-Type: text/markdown
Requires-Dist: torch >=1.7.1
Requires-Dist: torchvision >=0.8.2
Requires-Dist: Pillow >=8.0.1
Requires-Dist: numpy >=1.19.4
Requires-Dist: scikit-learn >=0.23.2

# Out of Domain Library

This library provides tools for feature extraction and similarity checking using various pre-trained models. It can be used to determine whether an image is in-domain or out-of-domain based on cosine similarity with a set of gallery features.

## Installation

To use this library, simply clone the repository and install the required packages.


bash


git clone https://github.com/suraj385/out_of_domain_library.git

cd out_of_domain_library

pip install -r requirements.txt

or 

pip install out-of-domain-library

Note

The library achieved 99.77% accuracy with a threshold of 0.85 with resnet50

Available Models

The following pre-trained models are available for feature extraction:

resnet18,

resnet34,

resnet50,

resnet101,

resnet152,

vgg16,

vgg19,

inception_v3,

densenet121,

densenet169,

efficientnet_b0,

efficientnet_b7,

Usage


# Extract features using ResNet-50 and save to a file

import torch

from torchvision import models, transforms

import pandas as pd

import numpy as np

from out_of_domain.feature_extraction import save_gallery_features

t_image_folder = "test_images" #path to your folder 

model_name = "resnet50"

output_file = f"{model_name}.npy"

save_gallery_features(t_image_folder, model_name)

print("Test passed: save_gallery_features function works as expected.")

# Check if the test image and folder  is in-domain using ResNet-50, create a csv , Evaluate the Model on Multiple Folders and Test Accuracy !

import torch

import numpy as np

import pandas as pd

from out_of_domain.similarity_check import is_in_domain, evaluate_model, eval_image_folder

def test_similarity_check():
    
    test_image_folder = "test_images"
    
    model_name = "resnet50"
    
    threshold = 0.85
    
    output_file = f"{model_name}.npy"
    
   
    
    gallery_features = np.load(output_file)
    
   
    print("Checking if a single image is in-domain...")
    
    test_image_path = "/Users/surajgautam/out_of_domain_library/test_images/image_0.jpg"
    
    result = is_in_domain(test_image_path, gallery_features, model_name, threshold)
    
    print(f"Test image is {'in-domain' if result else 'out-of-domain'}")
    
    # Evaluate the model on multiple folders
    
    test_folder_in_domain = "test_folder_in_domain"
    
    test_folder_out_of_domain = "test_folder_out_of_domain"
    
    
    test_folders = [
        (test_folder_in_domain, True), #true for indomain
        (test_folder_out_of_domain, False) #false for out-of-domain
    ]
    
    # Evaluate the model
    
    print("Evaluating the model on multiple folders...")
    
    results = evaluate_model(gallery_features, test_folders, model_name, threshold)
   
    for folder, (correct, total, accuracy) in results.items():
        
        print(f"Folder: {folder}, Correct: {correct}, Total: {total}, Accuracy: {accuracy:.4f}")
    
    # Evaluate the image folder
   
    print("Evaluating the image folder...")
   
    eval_image_folder(test_image_folder, gallery_features, model_name, threshold)
    
    # Load and print the results from the CSV file
   
    results_df = pd.read_csv("image_domain_evaluation.csv")
    
    print("Results from image_domain_evaluation.csv:")
    
    print(results_df)
    
  

# Run the test function
if __name__ == "__main__":
    
    test_similarity_check()
    
    
    
    
    
