Deep SVDD for images anomaly detection

140 Views Asked by At

Good evening to everyone! I'm pretty new in the world of anomaly detection. I’m currently trying to create an anomaly detection model using Deep SVDD with Python, which can find anomalies in a set of images. My dataset consists of 1000 images of cats and 1000 images of pandas, all of different sizes.

I plan to train the model only on the cat images, specifically on 800 cat images. For the test set, I would like to use 200 cat images and 2 panda images.

To prepare my data, I’m thinking of loading the images, resizing them all to the same size, transforming them into arrays, and normalizing them so that the pixel values are between 0 and 1. But by doing this, creating the model and then testing it, I get terrible results. I show you my code. Thanks to everyone who will respond.

import numpy as np
import os
from PIL import Image
from sklearn.metrics import accuracy_score, precision_score, recall_score, roc_auc_score 
import torch
import pandas as pd
from deepod.models.tabular import DeepSVDD
def load_images_from_folder(folder, size=(128, 128)):
    images = []
    for filename in os.listdir(folder):
        img = Image.open(os.path.join(folder, filename))
        if img is not None:
            img = img.resize(size, Image.ANTIALIAS)
            img_array = np.array(img).flatten()  # Flatten the image
            img_array = img_array / 255.0  # Normalize to [0, 1]
            images.append(img_array)
    return images

# Load and resize images from the 'cat' folder
cat_images_array = load_images_from_folder("cat_folder_path)

# Load and resize images from the 'panda' folder
panda_images_array = load_images_from_folder("panda_folder_path")

# split cat images into training and validation sets
train_size = int(0.8 * len(cat_images_array))  # 80% of the cat images for training
cat_images_train = cat_images_array[:train_size]
cat_images_val = cat_images_array[train_size:]           
panda_images_test = panda_images_array[:10]
# combine cat and dog images to create the validation set
val_images = cat_images_val + panda_images_test

cat_images_train = torch.tensor(cat_images_train)
val_images = torch.tensor(val_images)
# Initialize DeepSVDD model
deep_svdd = DeepSVDD(epochs = 100, batch_size = 30, hidden_dims= '100',lr=0.0001) 
# Train model
deep_svdd.fit(cat_images_train , y = None)

# Test model
scores = deep_svdd.decision_function(val_images)
print(scores)

threshold = 0.6
# Compute binary labels for your validation set
# Assume that cat_images_val is a list of your actual cat images in the validation set
y_true = [1 if i < len(cat_images_val) else 0 for i in range(len(val_images))]

# Compute predicted labels based on the anomaly scores and threshold
y_pred = [1 if score < threshold else 0 for score in scores]

# Calculate metrics
accuracy = accuracy_score(y_true, y_pred)
precision = precision_score(y_true, y_pred)
recall = recall_score(y_true, y_pred)
roc_auc = roc_auc_score(y_true, scores)  # Note: roc_auc_score uses scores, not labels

print(f'Accuracy: {accuracy}')
print(f'Precision: {precision}')
print(f'Recall: {recall}')
print(f'ROC AUC: {roc_auc}')
from deepod.metrics import tabular_metrics
auc, ap, f1 = tabular_metrics(val_images, scores)
from sklearn.metrics import roc_curve
0

There are 0 best solutions below