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()
Metadata
Release files for out-of-domain-library 0.2
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| out_of_domain_library-0.2.tar.gz | 4.6 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| out_of_domain_library-0.2-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.4 kB
Release files / out_of_domain_library-0.2.tar.gz
| Download URL | out_of_domain_library-0.2.tar.gz |
|---|---|
| Size | 4.6 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
7541f4141abcc1b142dc46f678658103a4e76ea721edabe968227d560d75376b
|
|
BLAKE2b-256 checksum How to use checksums |
dd93f75ca3f84f87aef64ab54754df9873437f61286b51c14c4604947c7c614b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.10.13
|
Release files / out_of_domain_library-0.2-py3-none-any.whl
| Download URL | out_of_domain_library-0.2-py3-none-any.whl |
|---|---|
| Size | 5.8 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
7883ccfaf2654eca5425562217c4c3175c6e436f6d2c6cf03c3bd6a173d165b5
|
|
BLAKE2b-256 checksum How to use checksums |
98c5a7e1c9270f3479be703f65842f7a00051b3000f0ea8d3c97907da573a169
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
twine/5.1.0 CPython/3.10.13
|