Skip to main content

A library for out-of-domain tasks.

Project description

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()

Project details


Release history Release notifications | RSS feed

This version

0.2

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

out_of_domain_library-0.2.tar.gz (4.6 kB view details)

Uploaded Source

Built Distribution

out_of_domain_library-0.2-py3-none-any.whl (5.8 kB view details)

Uploaded Python 3

File details

Details for the file out_of_domain_library-0.2.tar.gz.

File metadata

  • Download URL: out_of_domain_library-0.2.tar.gz
  • Upload date:
  • Size: 4.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.0 CPython/3.10.13

File hashes

Hashes for out_of_domain_library-0.2.tar.gz
Algorithm Hash digest
SHA256 7541f4141abcc1b142dc46f678658103a4e76ea721edabe968227d560d75376b
MD5 d4f0260e6c708c5baa7fa22800551ff0
BLAKE2b-256 dd93f75ca3f84f87aef64ab54754df9873437f61286b51c14c4604947c7c614b

See more details on using hashes here.

File details

Details for the file out_of_domain_library-0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for out_of_domain_library-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 7883ccfaf2654eca5425562217c4c3175c6e436f6d2c6cf03c3bd6a173d165b5
MD5 05b6b62653291bb60f59515e1d628475
BLAKE2b-256 98c5a7e1c9270f3479be703f65842f7a00051b3000f0ea8d3c97907da573a169

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page