Spaces:
Runtime error
Runtime error
Upload folder using huggingface_hub
Browse files- cgi_classification_app.py +51 -32
cgi_classification_app.py
CHANGED
|
@@ -6,12 +6,10 @@ Automatically generated by Colab.
|
|
| 6 |
Original file is located at
|
| 7 |
https://colab.research.google.com/drive/1ckzOtXUiFW_NqlIandwoH07lnsLGKTLB
|
| 8 |
"""
|
| 9 |
-
|
| 10 |
-
!pip install gradio
|
| 11 |
-
|
| 12 |
from scipy.spatial import distance
|
| 13 |
import numpy as np
|
| 14 |
|
|
|
|
| 15 |
class MeanClassifier:
|
| 16 |
def fit(self, X, y):
|
| 17 |
self.mean_0 = np.mean(X[y == 0], axis=0) if np.any(y == 0) else None
|
|
@@ -20,26 +18,45 @@ class MeanClassifier:
|
|
| 20 |
def predict(self, X):
|
| 21 |
preds = []
|
| 22 |
for x in X:
|
| 23 |
-
dist_0 =
|
| 24 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 25 |
preds.append(1 if dist_1 < dist_0 else 0)
|
| 26 |
return np.array(preds)
|
| 27 |
|
| 28 |
def predict_proba(self, X):
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
|
| 39 |
def mean_distance(self, x):
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
import gradio as gr
|
| 45 |
from PIL import Image
|
|
@@ -50,8 +67,8 @@ from tensorflow.keras.models import load_model, Model
|
|
| 50 |
import pickle
|
| 51 |
|
| 52 |
mean_clf = None
|
| 53 |
-
with open(
|
| 54 |
-
|
| 55 |
|
| 56 |
|
| 57 |
# Function to apply Fourier transform
|
|
@@ -60,21 +77,25 @@ def apply_fourier_transform(image):
|
|
| 60 |
fft_image = fft2(image)
|
| 61 |
return np.abs(fft_image)
|
| 62 |
|
|
|
|
| 63 |
def preprocess_image(image):
|
| 64 |
try:
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
|
|
|
|
|
|
| 72 |
except Exception as e:
|
| 73 |
print(f"Error processing image: {e}")
|
| 74 |
return None
|
| 75 |
|
|
|
|
| 76 |
# Function to load embedding model and calculate embeddings
|
| 77 |
-
def calculate_embeddings(image, model_path=
|
| 78 |
# Load the trained model
|
| 79 |
model = load_model(model_path)
|
| 80 |
|
|
@@ -91,16 +112,14 @@ def calculate_embeddings(image, model_path='embedding_modelv2.keras'):
|
|
| 91 |
|
| 92 |
def classify_image(image):
|
| 93 |
embeddings = calculate_embeddings(image)
|
| 94 |
-
|
| 95 |
probabilities = mean_clf.predict_proba(embeddings)[0]
|
| 96 |
labels = ["Photo", "CGI"]
|
| 97 |
return {f"{labels[i]}": prob for i, prob in enumerate(probabilities)}
|
| 98 |
|
|
|
|
| 99 |
interface = gr.Interface(
|
| 100 |
-
fn=classify_image,
|
| 101 |
-
inputs=["image"],
|
| 102 |
-
outputs=gr.Label(num_top_classes=2)
|
| 103 |
)
|
| 104 |
|
| 105 |
interface.launch(share=True)
|
| 106 |
-
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 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
|
|
|
|
| 77 |
fft_image = fft2(image)
|
| 78 |
return np.abs(fft_image)
|
| 79 |
|
| 80 |
+
|
| 81 |
def preprocess_image(image):
|
| 82 |
try:
|
| 83 |
+
image = Image.fromarray(image)
|
| 84 |
+
image = image.convert("L")
|
| 85 |
+
image = image.resize((256, 256))
|
| 86 |
+
image = apply_fourier_transform(image)
|
| 87 |
+
image = np.expand_dims(
|
| 88 |
+
image, axis=-1
|
| 89 |
+
) # Expand dimensions to match model input shape
|
| 90 |
+
image = np.expand_dims(image, axis=0) # Expand to add batch dimension
|
| 91 |
+
return image
|
| 92 |
except Exception as e:
|
| 93 |
print(f"Error processing image: {e}")
|
| 94 |
return None
|
| 95 |
|
| 96 |
+
|
| 97 |
# Function to load embedding model and calculate embeddings
|
| 98 |
+
def calculate_embeddings(image, model_path="embedding_modelv2.keras"):
|
| 99 |
# Load the trained model
|
| 100 |
model = load_model(model_path)
|
| 101 |
|
|
|
|
| 112 |
|
| 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 |
|
| 120 |
+
|
| 121 |
interface = gr.Interface(
|
| 122 |
+
fn=classify_image, inputs=["image"], outputs=gr.Label(num_top_classes=2)
|
|
|
|
|
|
|
| 123 |
)
|
| 124 |
|
| 125 |
interface.launch(share=True)
|
|
|