File size: 2,971 Bytes
c842c34
2b32ac3
87fdd37
0125f5b
c925413
1a3a4f4
4682354
1ca75e4
dfeefe7
bd20549
0125f5b
44b0f69
0125f5b
 
c925413
0125f5b
 
 
 
 
 
 
 
 
 
4682354
 
 
 
 
 
 
 
0125f5b
da35ee1
67038d7
da35ee1
 
44b0f69
87fdd37
0125f5b
b0939cc
0125f5b
3ed07ae
 
0125f5b
b0939cc
 
 
 
 
3d7e9e1
 
b0939cc
 
3d13539
b0939cc
0125f5b
b0939cc
 
 
 
e33579e
 
3ed07ae
 
a1fdc76
 
 
1fbf7f2
f1eea5a
4682354
87fdd37
11ac6ab
3d13539
ee691f1
3d7e9e1
87fdd37
 
c206780
4682354
0125f5b
f1eea5a
 
1383ffa
4682354
a7878a1
 
4682354
0125f5b
da35ee1
 
3faaf02
1fbf7f2
d7232b3
 
1fbf7f2
 
 
 
d7232b3
6c333b6
92f6098
1fbf7f2
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
import keras
import numpy as np
import streamlit as st
import random
import os
from PIL import Image, ImageOps
from io import BytesIO
from huggingface_hub import snapshot_download

def random_crop(img, min_size=160, max_size=2048, ratio=5/8):
    width, height = img.size

    crop_width = random.randint(min_size, min(max_size, width))
    crop_height = int(crop_width * ratio)

    if crop_height > height:
        crop_height = height
        crop_width = int(crop_height / ratio)
    
    left = random.randint(0, width - crop_width)
    top = random.randint(0, height - crop_height)
    right = left + crop_width
    bottom = top + crop_height

    return img.crop((left, top, right, bottom))

def jpg_compression(img):
    quality = random.randint(65, 100)
    jpeg_image = BytesIO()
    img.convert("RGB").save(jpeg_image, 'JPEG', quality=quality)
    jpeg_image.seek(0)
    compressed_img = Image.open(jpeg_image)
    return compressed_img
    
def get_prediction(img):
    x = np.array(img)
    x = np.expand_dims(x, axis=0)
    predictions = model.predict(x)
    return predictions[0,:]

models = ['DDPM', 'Glide', 'Latent Diffusion', 'Palette', 'Stable Diffusion', 'VQ Diffusion', 'real', 'unseen_fake']


st.title("Fake Detection")
st.divider()

st.subheader("Modelvariant")
variant = st.selectbox(
    "Choose the model",
    ("ResNet50v2-Basemodel", "ResNet50v2-Finetuned"),
    index=None,
    placeholder="Choose a model",
    label_visibility="hidden"
)
st.write("You selected model: ", variant)
binary = st.toggle("Activate binary classification")
st.divider()

if variant == "ResNet50v2-Basemodel":
    local_model_path = snapshot_download(repo_id="ElBeh/ma_basemodel")
else:
    local_model_path = snapshot_download(repo_id="ElBeh/ma_finetuned_model")


model = keras.models.load_model(local_model_path)

st.subheader("Image Preprocessing")
crop = st.toggle("random crop")
compress = st.toggle("jpeg compression")
st.divider()

file_name = st.file_uploader("Choose an image...")

#st.button("execute classification", type="primary")


if file_name is not None:
    col1, col2 = st.columns(2)
    image = Image.open(file_name)
    image = ImageOps.exif_transpose(image)
    
    if image.size != (200, 200) or image.mode != 'RGB':
        if crop:
            image = random_crop(image)
        image = image.resize((200, 200), Image.LANCZOS)     

    #if image.format != "JPEG" and compress :
    if  compress :
        image = jpg_compression(image)
        
    col1.image(image, use_column_width=True)  
    predictions = get_prediction(image)
    
    if binary:
        col2.header("Prediction")
        if predictions[6] > 0.5:
            col2.markdown(":green[real image!]")
        else:
            col2.markdown(":red[fake image!]")
    else:
        col2.header("Probabilities")
        if crop:
            st.button("re-crop")
        for idx,p in enumerate(predictions):
            col2.text(f"{ models[idx] }: { round(p * 100, 2)}%")