nhradek commited on
Commit
5d1e2f8
·
verified ·
1 Parent(s): d02f51e

Upload folder using huggingface_hub

Browse files
Files changed (1) hide show
  1. cgi_classification_app.py +4 -56
cgi_classification_app.py CHANGED
@@ -6,57 +6,6 @@ Automatically generated by Colab.
6
  Original file is located at
7
  https://colab.research.google.com/drive/1ckzOtXUiFW_NqlIandwoH07lnsLGKTLB
8
  """
9
- from scipy.spatial import distance
10
- import numpy as np
11
-
12
-
13
- class MeanClassifier:
14
- def fit(self, X, y):
15
- self.mean_0 = np.mean(X[y == 0], axis=0) if np.any(y == 0) else None
16
- self.mean_1 = np.mean(X[y == 1], axis=0) if np.any(y == 1) else None
17
-
18
- def predict(self, X):
19
- preds = []
20
- for x in X:
21
- dist_0 = (
22
- distance.euclidean(x, self.mean_0)
23
- if self.mean_0 is not None
24
- else np.inf
25
- )
26
- dist_1 = (
27
- distance.euclidean(x, self.mean_1)
28
- if self.mean_1 is not None
29
- else np.inf
30
- )
31
- preds.append(1 if dist_1 < dist_0 else 0)
32
- return np.array(preds)
33
-
34
- def predict_proba(self, X):
35
- # An implementation of probability prediction which uses a softmax function to determine the probability of each class based on the distance to the mean for each prototype
36
- preds = []
37
- for x in X:
38
- dist_0 = (
39
- distance.euclidean(x, self.mean_0) if self.mean_0 is not None else np
40
- )
41
- dist_1 = (
42
- distance.euclidean(x, self.mean_1)
43
- if self.mean_1 is not None
44
- else np.inf
45
- )
46
- prob_0 = np.exp(-dist_0) / (np.exp(-dist_0) + np.exp(-dist_1))
47
- prob_1 = np.exp(-dist_1) / (np.exp(-dist_0) + np.exp(-dist_1))
48
- preds.append([prob_0, prob_1])
49
- return np.array(preds)
50
-
51
- def mean_distance(self, x):
52
- dist_mean_0 = (
53
- distance.euclidean(x, self.mean_0) if self.mean_0 is not None else np.inf
54
- )
55
- dist_mean_1 = (
56
- distance.euclidean(x, self.mean_1) if self.mean_1 is not None else np.inf
57
- )
58
- return dist_mean_0, dist_mean_1
59
-
60
 
61
  import gradio as gr
62
  from PIL import Image
@@ -64,11 +13,10 @@ import numpy as np
64
  from PIL import Image
65
  from scipy.fftpack import fft2
66
  from tensorflow.keras.models import load_model, Model
67
- import pickle
68
 
69
- mean_clf = None
70
- with open("mean_clf.pkl", "rb") as f:
71
- mean_clf = pickle.load(f)
72
 
73
 
74
  # Function to apply Fourier transform
@@ -113,7 +61,7 @@ def calculate_embeddings(image, model_path="embedding_modelv2.keras"):
113
  def classify_image(image):
114
  embeddings = calculate_embeddings(image)
115
  # Convert to 2D array for model input
116
- probabilities = mean_clf.predict_proba(embeddings)[0]
117
  labels = ["Photo", "CGI"]
118
  return {f"{labels[i]}": prob for i, prob in enumerate(probabilities)}
119
 
 
6
  Original file is located at
7
  https://colab.research.google.com/drive/1ckzOtXUiFW_NqlIandwoH07lnsLGKTLB
8
  """
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
10
  import gradio as gr
11
  from PIL import Image
 
13
  from PIL import Image
14
  from scipy.fftpack import fft2
15
  from tensorflow.keras.models import load_model, Model
16
+ from xgboost import XGBClassifier
17
 
18
+ xgb_clf = XGBClassifier()
19
+ xgb_clf.load_model("xgb_cgi_classifier.json")
 
20
 
21
 
22
  # Function to apply Fourier transform
 
61
  def classify_image(image):
62
  embeddings = calculate_embeddings(image)
63
  # Convert to 2D array for model input
64
+ probabilities = xgb_clf.predict_proba(embeddings)[0]
65
  labels = ["Photo", "CGI"]
66
  return {f"{labels[i]}": prob for i, prob in enumerate(probabilities)}
67