jays009 commited on
Commit
d2b2f0e
·
verified ·
1 Parent(s): ab59081

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -12
app.py CHANGED
@@ -1,7 +1,6 @@
1
  import gradio as gr
2
  import torch
3
  import torch.nn as nn
4
- import torch.serialization # For add_safe_globals
5
  from torchvision import models, transforms
6
  from huggingface_hub import hf_hub_download
7
  from PIL import Image
@@ -25,14 +24,6 @@ CONFIDENCE_THRESHOLD = 0.8 # 80%
25
  # Mahalanobis distance threshold for OOD detection
26
  MAHALANOBIS_THRESHOLD = 100.0 # Calibrate this using a validation set
27
 
28
- # Allowlist NumPy globals for torch.load
29
- torch.serialization.add_safe_globals([
30
- np.ndarray,
31
- np._core.multiarray._reconstruct,
32
- np.dtype,
33
- _codecs.encode
34
- ])
35
-
36
  # Download model from Hugging Face
37
  def download_model():
38
  model_path = hf_hub_download(repo_id="jays009/Resnet3", filename="model.pth")
@@ -84,9 +75,13 @@ transform = transforms.Compose([
84
 
85
  # Compute Mahalanobis distance for OOD detection
86
  def compute_mahalanobis_distance(features, mean, cov):
 
 
 
 
87
  # Compute the inverse covariance matrix
88
- cov_inv = np.linalg.inv(cov + np.eye(cov.shape[0]) * 1e-6) # Add small epsilon for numerical stability
89
- return mahalanobis(features, mean, cov_inv)
90
 
91
  # OOD detection using Mahalanobis distance
92
  def is_in_distribution(features):
@@ -119,7 +114,7 @@ def predict_from_image_url(image_url):
119
  main_model.fc = nn.Identity()
120
  features = main_model(image_tensor) # Shape: [1, 2048]
121
  main_model.fc = original_fc # Restore the final layer
122
- features = features[0].numpy() # Convert to numpy
123
 
124
  # Stage 1: OOD Detection using Mahalanobis distance
125
  if not is_in_distribution(features):
 
1
  import gradio as gr
2
  import torch
3
  import torch.nn as nn
 
4
  from torchvision import models, transforms
5
  from huggingface_hub import hf_hub_download
6
  from PIL import Image
 
24
  # Mahalanobis distance threshold for OOD detection
25
  MAHALANOBIS_THRESHOLD = 100.0 # Calibrate this using a validation set
26
 
 
 
 
 
 
 
 
 
27
  # Download model from Hugging Face
28
  def download_model():
29
  model_path = hf_hub_download(repo_id="jays009/Resnet3", filename="model.pth")
 
75
 
76
  # Compute Mahalanobis distance for OOD detection
77
  def compute_mahalanobis_distance(features, mean, cov):
78
+ # Convert PyTorch tensors to NumPy arrays for scipy
79
+ features_np = features
80
+ mean_np = mean.cpu().numpy()
81
+ cov_np = cov.cpu().numpy()
82
  # Compute the inverse covariance matrix
83
+ cov_inv = np.linalg.inv(cov_np + np.eye(cov_np.shape[0]) * 1e-6) # Add small epsilon for numerical stability
84
+ return mahalanobis(features_np, mean_np, cov_inv)
85
 
86
  # OOD detection using Mahalanobis distance
87
  def is_in_distribution(features):
 
114
  main_model.fc = nn.Identity()
115
  features = main_model(image_tensor) # Shape: [1, 2048]
116
  main_model.fc = original_fc # Restore the final layer
117
+ features = features[0].cpu().numpy() # Convert to numpy for scipy
118
 
119
  # Stage 1: OOD Detection using Mahalanobis distance
120
  if not is_in_distribution(features):