Add app.py and examples
Browse files- README.md +5 -5
- SuSy.pt +3 -0
- app.py +95 -0
- config.json +8 -0
- example_authentic.jpg +0 -0
- example_dalle3.jpg +0 -0
- example_mjv5.jpg +0 -0
- example_sdxl.jpg +0 -0
- requirements.txt +5 -0
README.md
CHANGED
|
@@ -1,14 +1,14 @@
|
|
| 1 |
---
|
| 2 |
title: SuSy
|
| 3 |
-
emoji:
|
| 4 |
-
colorFrom:
|
| 5 |
-
colorTo:
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.44.1
|
| 8 |
app_file: app.py
|
| 9 |
-
pinned:
|
| 10 |
license: apache-2.0
|
| 11 |
short_description: Spot AI-Generated images with SuSy!
|
| 12 |
---
|
| 13 |
|
| 14 |
-
|
|
|
|
| 1 |
---
|
| 2 |
title: SuSy
|
| 3 |
+
emoji: 🔎
|
| 4 |
+
colorFrom: gray
|
| 5 |
+
colorTo: pink
|
| 6 |
sdk: gradio
|
| 7 |
sdk_version: 4.44.1
|
| 8 |
app_file: app.py
|
| 9 |
+
pinned: true
|
| 10 |
license: apache-2.0
|
| 11 |
short_description: Spot AI-Generated images with SuSy!
|
| 12 |
---
|
| 13 |
|
| 14 |
+
SuSy is a Spatial-Based Synthetic Image Detection and Recognition Model, designed and trained to detect synthetic images and attribute them to a generative model (i.e., two StableDiffusion models, two Midjourney versions and DALL·E 3)
|
SuSy.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:fa10fae300ee2742c7a373b6c3332c2595b461954b8f5616d2d382ef2751020e
|
| 3 |
+
size 50810392
|
app.py
ADDED
|
@@ -0,0 +1,95 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
import numpy as np
|
| 3 |
+
import torch
|
| 4 |
+
from PIL import Image
|
| 5 |
+
from skimage.feature import graycomatrix, graycoprops
|
| 6 |
+
from torchvision import transforms
|
| 7 |
+
|
| 8 |
+
# Load the model
|
| 9 |
+
model = torch.jit.load("SuSy.pt")
|
| 10 |
+
|
| 11 |
+
def process_image(image):
|
| 12 |
+
# Set Parameters
|
| 13 |
+
top_k_patches = 5
|
| 14 |
+
patch_size = 224
|
| 15 |
+
|
| 16 |
+
# Get the image dimensions
|
| 17 |
+
width, height = image.size
|
| 18 |
+
|
| 19 |
+
# Calculate the number of patches
|
| 20 |
+
num_patches_x = width // patch_size
|
| 21 |
+
num_patches_y = height // patch_size
|
| 22 |
+
|
| 23 |
+
# Divide the image in patches
|
| 24 |
+
patches = np.zeros((num_patches_x * num_patches_y, patch_size, patch_size, 3), dtype=np.uint8)
|
| 25 |
+
for i in range(num_patches_x):
|
| 26 |
+
for j in range(num_patches_y):
|
| 27 |
+
x = i * patch_size
|
| 28 |
+
y = j * patch_size
|
| 29 |
+
patch = image.crop((x, y, x + patch_size, y + patch_size))
|
| 30 |
+
patches[i * num_patches_y + j] = np.array(patch)
|
| 31 |
+
|
| 32 |
+
# Compute the most relevant patches (optional)
|
| 33 |
+
dissimilarity_scores = []
|
| 34 |
+
for patch in patches:
|
| 35 |
+
transform_patch = transforms.Compose([transforms.PILToTensor(), transforms.Grayscale()])
|
| 36 |
+
grayscale_patch = transform_patch(Image.fromarray(patch)).squeeze(0)
|
| 37 |
+
glcm = graycomatrix(grayscale_patch, [5], [0], 256, symmetric=True, normed=True)
|
| 38 |
+
dissimilarity_scores.append(graycoprops(glcm, "contrast")[0, 0])
|
| 39 |
+
|
| 40 |
+
# Sort patch indices by their dissimilarity score
|
| 41 |
+
sorted_indices = np.argsort(dissimilarity_scores)[::-1]
|
| 42 |
+
|
| 43 |
+
# Extract top k patches and convert them to tensor
|
| 44 |
+
top_patches = patches[sorted_indices[:top_k_patches]]
|
| 45 |
+
top_patches = torch.from_numpy(np.transpose(top_patches, (0, 3, 1, 2))) / 255.0
|
| 46 |
+
|
| 47 |
+
# Predict patches
|
| 48 |
+
model.eval()
|
| 49 |
+
with torch.no_grad():
|
| 50 |
+
preds = model(top_patches)
|
| 51 |
+
|
| 52 |
+
# Process results
|
| 53 |
+
classes = ['Authentic', 'DALL·E 3', 'Stable Diffusion 1.x', 'MJ V5/V6', 'MJ V1/V2', 'Stable Diffusion XL']
|
| 54 |
+
mean_probs = preds.mean(dim=0).numpy()
|
| 55 |
+
|
| 56 |
+
# Create a dictionary of class probabilities
|
| 57 |
+
class_probs = {cls: prob for cls, prob in zip(classes, mean_probs)}
|
| 58 |
+
|
| 59 |
+
# Sort probabilities in descending order
|
| 60 |
+
sorted_probs = dict(sorted(class_probs.items(), key=lambda item: item[1], reverse=True))
|
| 61 |
+
|
| 62 |
+
return sorted_probs
|
| 63 |
+
|
| 64 |
+
# Define Gradio interface
|
| 65 |
+
iface = gr.Interface(
|
| 66 |
+
fn=process_image,
|
| 67 |
+
inputs=gr.Image(type="pil"),
|
| 68 |
+
outputs=gr.Label(num_top_classes=6),
|
| 69 |
+
title="SuSy: Synthetic Image Detector",
|
| 70 |
+
description="Upload an image or select an example to classify it into different categories.",
|
| 71 |
+
examples=[
|
| 72 |
+
["example_authentic.jpg"],
|
| 73 |
+
["example_dalle3.jpg"],
|
| 74 |
+
["example_mjv5.jpg"],
|
| 75 |
+
["example_sdxl.jpg"]
|
| 76 |
+
],
|
| 77 |
+
article="""
|
| 78 |
+
<div style="text-align: center;">
|
| 79 |
+
<h3>About SuSy</h3>
|
| 80 |
+
<p>SuSy is an advanced synthetic image detector that can distinguish between authentic images and various types of AI-generated images. It analyzes patches of the input image to make its classification.</p>
|
| 81 |
+
<h4>Categories:</h4>
|
| 82 |
+
<ul style="list-style-type: none; padding: 0;">
|
| 83 |
+
<li>Authentic: Real, non-AI-generated images</li>
|
| 84 |
+
<li>DALL·E 3: Images generated by DALL-E 3</li>
|
| 85 |
+
<li>MJ V1/V2: Images generated by Midjourney versions 1 or 2</li>
|
| 86 |
+
<li>MJ V5/V6: Images generated by Midjourney versions 5 or 6</li>
|
| 87 |
+
<li>Stable Diffusion 1.x: Images generated by Stable Diffusion 1.x Models</li>
|
| 88 |
+
<li>Stable Diffusion XL: Images generated by Stable Diffusion XL</li>
|
| 89 |
+
</ul>
|
| 90 |
+
</div>
|
| 91 |
+
"""
|
| 92 |
+
)
|
| 93 |
+
|
| 94 |
+
# Launch the interface
|
| 95 |
+
iface.launch()
|
config.json
ADDED
|
@@ -0,0 +1,8 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
{
|
| 2 |
+
"model_architecture": "ResNet18",
|
| 3 |
+
"num_classes": 6,
|
| 4 |
+
"input_size": [224, 224],
|
| 5 |
+
"pretrained": true,
|
| 6 |
+
"learning_rate": 0.0001,
|
| 7 |
+
"batch_size": 256
|
| 8 |
+
}
|
example_authentic.jpg
ADDED
|
example_dalle3.jpg
ADDED
|
example_mjv5.jpg
ADDED
|
example_sdxl.jpg
ADDED
|
requirements.txt
ADDED
|
@@ -0,0 +1,5 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
torch
|
| 2 |
+
torchvision
|
| 3 |
+
pillow
|
| 4 |
+
scikit-image
|
| 5 |
+
gradio
|