File size: 8,439 Bytes
57f7624
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
import gradio as gr

import os
import glob
import json
import re
from pathlib import Path

import json


def group_images_by_index(image_paths, is_audio=False):
    # Regular expression pattern to extract the key from each image path
    if is_audio:
        pattern = r"audio_(\d+).png"
    else:
        pattern = r"img_(\d+).png"
    # Dictionary to store the grouped images
    grouped_images = {}
    # Iterate over each image path
    for image_path in image_paths:
        # Extract the key using the regular expression pattern
        match = re.search(pattern, image_path)
        if match:
            key = int(match.group(1))

            # Add the image path to the corresponding group in the dictionary
            if key not in grouped_images:
                grouped_images[key] = []
            grouped_images[key].append(image_path)
    # Sort the dictionary by keys
    sorted_grouped_images = dict(sorted(grouped_images.items()))
    return sorted_grouped_images


def build_image_description(i, data_none, data_attack):
    if i == 0:
        fake_det_score = float(data_none["fake_det_score"])

        return f"det_score: {fake_det_score:.2f}"
    elif i == 1:
        psnr = float(data_none["psnr"])
        ssim = float(data_none["ssim"])
        lpips = float(data_none["lpips"])

        det_score = float(data_none["watermark_det_score"])
        p_value = data_none["p_value"]
        bit_acc = data_none["bit_acc"]

        return f"psnr: {psnr:.2f} ssim: {ssim:.2f} lpips: {lpips:.2f} det_score: {det_score:.2f} p_value: {p_value} bit_acc: {bit_acc}"
    elif i == 2:
        fake_det_score = float(data_attack["fake_det_score"])

        return f"det_score: {fake_det_score:.2f}"
    elif i == 3:
        det_score = float(data_attack["watermark_det_score"])
        p_value = data_attack["p_value"]
        word_acc = data_attack["word_acc"]
        bit_acc = data_attack["bit_acc"]

        return f"word_acc: {word_acc:.2f} det_score: {det_score:.2f} p_value: {p_value} bit_acc: {bit_acc}"


def build_audio_description(i, data_none, data_attack):
    if i == 0:
        tn_detect_prob = float(data_none["tn_detect_prob"])

        return f"det_score: {tn_detect_prob:.2f}"
    elif i == 1:
        snr = float(data_none["snr"])
        sisnr = float(data_none["sisnr"])
        stoi = float(data_none["stoi"])
        pesq = float(data_none["pesq"])

        det_score = float(data_none["detect_prob"])
        bit_acc = data_none["ba"]

        return f"snr: {snr:.2f} sisnr: {sisnr:.2f} stoi: {stoi:.2f} pesq: {pesq:.2f} det_score: {det_score:.2f} bit_acc: {bit_acc}"
    elif i == 2:
        tn_detect_prob = float(data_attack["tn_detect_prob"])

        return f"det_score: {tn_detect_prob:.2f}"
    elif i == 3:
        det_score = float(data_attack["detect_prob"])
        bit_acc = data_attack["ba"]

        return f"det_score: {det_score:.2f} bit_acc: {bit_acc}"


def build_image_infos(abs_path: Path):

    with (abs_path / "data/image_eval_results.json").open("r") as f:
        image_data = json.loads(f.read())

    examples_dir = Path("./examples/image")
    image_infos = {}

    model_image_infos = {}
    for model_name in os.listdir(examples_dir):
        model_attacks_dir = examples_dir / model_name

        for attack_name in os.listdir(model_attacks_dir):
            attack_dir = model_attacks_dir / attack_name
            image_paths = glob.glob(f"{attack_dir}/*.png")

            all_files = []

            for i, files in group_images_by_index(image_paths).items():
                data_none = image_data["eval"]["val2014"]["wam"]["none"][i]
                data_attack = image_data["eval"]["val2014"]["wam"][attack_name][i]

                files = sorted([(f, Path(f).stem) for f in files], key=lambda x: x[1])
                files = files[2:] + files[:2]

                files = [
                    (f, f"{n}\n{build_image_description(i, data_none, data_attack)}")
                    for i, (f, n) in enumerate(files)
                ]

                all_files.extend(files)

            model_image_infos[attack_name] = all_files

        image_infos[model_name] = model_image_infos

    return image_infos


def build_audio_infos(abs_path: Path):

    with (abs_path / "data/audio_eval_results.json").open("r") as f:
        audio_data = json.loads(f.read())

    examples_dir = Path("./examples/audio")
    audio_infos = {}

    model_audio_infos = {}
    for model_name in os.listdir(examples_dir):
        model_attacks_dir = examples_dir / model_name

        for attack_name in os.listdir(model_attacks_dir):
            attack_dir = model_attacks_dir / attack_name
            image_paths = glob.glob(f"{attack_dir}/*.png")

            all_files = []

            for i, files in group_images_by_index(image_paths, is_audio=True).items():
                data_none = audio_data["eval"]["ravdess"][model_name]["identity"][i]
                data_attack = audio_data["eval"]["ravdess"][model_name][attack_name][i]

                files = sorted([(f, Path(f).stem) for f in files], key=lambda x: x[1])
                files = files[2:] + files[:2]

                files = [
                    (f, f"{n}\n{build_audio_description(i, data_none, data_attack)}")
                    for i, (f, n) in enumerate(files)
                ]

                all_files.extend(files)

            model_audio_infos[attack_name] = all_files

        audio_infos[model_name] = model_audio_infos

    return audio_infos


def examples_tab(abs_path: Path):
    image_infos = build_image_infos(abs_path)

    # First combo box (category selection)
    model_choice = gr.Dropdown(
        choices=list(image_infos.keys()),
        label="Select a Model",
        value=None,
    )
    # Second combo box (subcategory selection)
    # Initialize with options from the first category by default
    attack_choice = gr.Dropdown(
        choices=list(image_infos["wam"].keys()),
        label="Select an Attack",
        value=None,
    )

    # Gallery component to display images
    gallery = gr.Gallery(
        label="Image Gallery",
        columns=4,
        rows=1,
    )

    # Update options for the second combo box when the first one changes
    def update_subcategories(selected_category):
        values = list(image_infos[selected_category].keys())
        values = [(v, v) for v in values]
        attack_choice.choices = values
        # return gr.Dropdown.update(choices=list(image_infos[selected_category].keys()))

    # Function to load images based on selections from both combo boxes
    def load_images(category, subcategory):
        return image_infos.get(category, {}).get(subcategory, [])

    # Update gallery based on both combo box selections
    model_choice.change(
        fn=update_subcategories, inputs=model_choice, outputs=attack_choice
    )
    attack_choice.change(
        fn=load_images, inputs=[model_choice, attack_choice], outputs=gallery
    )


def audio_examples_tab(abs_path: Path):
    audio_infos = build_audio_infos(abs_path)

    # First combo box (category selection)
    model_choice = gr.Dropdown(
        choices=list(audio_infos.keys()),
        label="Select a Model",
        value=None,
    )
    # Second combo box (subcategory selection)
    # Initialize with options from the first category by default
    attack_choice = gr.Dropdown(
        choices=list(audio_infos["audioseal"].keys()),
        label="Select an Attack",
        value=None,
    )

    # Gallery component to display images
    gallery = gr.Gallery(
        label="Image Gallery", columns=4, rows=1, object_fit="scale-down"
    )

    # Update options for the second combo box when the first one changes
    def update_subcategories(selected_category):
        values = list(audio_infos[selected_category].keys())
        values = [(v, v) for v in values]
        attack_choice.choices = values
        # return gr.Dropdown.update(choices=list(image_infos[selected_category].keys()))

    # Function to load images based on selections from both combo boxes
    def load_audios(category, subcategory):
        return audio_infos.get(category, {}).get(subcategory, [])

    # Update gallery based on both combo box selections
    model_choice.change(
        fn=update_subcategories, inputs=model_choice, outputs=attack_choice
    )
    attack_choice.change(
        fn=load_audios, inputs=[model_choice, attack_choice], outputs=gallery
    )