File size: 12,195 Bytes
9313bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bf0e4cf
 
 
 
 
 
 
 
 
 
 
 
9313bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a942e66
 
 
 
 
 
 
 
 
9313bc1
a942e66
 
9313bc1
 
 
a942e66
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9313bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d901742
 
 
 
9313bc1
 
 
 
 
 
d901742
 
9313bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d901742
9313bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
d901742
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9313bc1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
import json
import os
import pickle
import random
import time
from collections import Counter
from datetime import datetime
from glob import glob

import gdown
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import seaborn as sns
import streamlit as st
from PIL import Image

import SessionState
from download_utils import *
from image_utils import *

random.seed(datetime.now())
np.random.seed(int(time.time()))

NUMBER_OF_TRIALS = 20
CLASSIFIER_TAG = "CHM"
explaination_functions = [load_chm_nns, load_knn_nns]
selected_xai_tool = None

# Config
folder_to_name = {}
class_descriptions = {}
classifier_predictions = {}
selected_dataset = "Final"

root_visualization_dir = "./visualizations/"
viz_url = "https://static.taesiri.com/xai/Final.zip"
viz_archivefile = "Final.zip"

demonstration_url = "https://static.taesiri.com/xai/demonstrations.zip"
demonst_zipfile = "demonstrations.zip"

picklefile_url = "https://static.taesiri.com/xai/Task1_Results_CHM_and_EMD.pickle"
prediction_root = "./predictions/"
prediction_pickle = f"{prediction_root}predictions.pickle"


# Get the Data
download_files(
    root_visualization_dir,
    viz_url,
    viz_archivefile,
    demonstration_url,
    demonst_zipfile,
    picklefile_url,
    prediction_root,
    prediction_pickle,
)
################################################
# GLOBAL VARIABLES
app_mode = ""

## Shared/Global Information
with open("imagenet-labels.json", "rb") as f:
    folder_to_name = json.load(f)

with open("gloss.txt", "r") as f:
    description_file = f.readlines()

class_descriptions = {l.split("\t")[0]: l.split("\t")[1] for l in description_file}
################################################

with open(prediction_pickle, "rb") as f:
    classifier_predictions = pickle.load(f)

# SESSION STATE
session_state = SessionState.get(
    page=1,
    first_run=1,
    user_feedback={},
    queries=[],
    is_classifier_correct={},
    XAI_tool="Unselected",
)
################################################


def resmaple_queries():
    if session_state.first_run == 1:
        both_correct = glob(
            root_visualization_dir + selected_dataset + "/Both_correct/*.JPEG"
        )
        both_wrong = glob(
            root_visualization_dir + selected_dataset + "/Both_wrong/*.JPEG"
        )

        correct_samples = list(
            np.random.choice(a=both_correct, size=NUMBER_OF_TRIALS // 2, replace=False)
        )
        wrong_samples = list(
            np.random.choice(a=both_wrong, size=NUMBER_OF_TRIALS // 2, replace=False)
        )

        all_images = correct_samples + wrong_samples
        random.shuffle(all_images)
        session_state.queries = all_images
        session_state.first_run = -1
        # RESET INTERACTIONS
        session_state.user_feedback = {}
        session_state.is_classifier_correct = {}


def render_experiment(query):
    current_query = session_state.queries[query]
    query_id = os.path.basename(current_query)

    predicted_wnid = classifier_predictions[query_id][f"{CLASSIFIER_TAG}-predictions"]
    prediction_confidence = classifier_predictions[query_id][
        f"{CLASSIFIER_TAG}-confidence"
    ]
    prediction_label = folder_to_name[predicted_wnid]
    class_def = class_descriptions[predicted_wnid]

    session_state.is_classifier_correct[query_id] = classifier_predictions[query_id][
        f"{CLASSIFIER_TAG}-Output"
    ]

    ################################### SHOW QUERY and PREDICTION

    col1, col2 = st.columns(2)
    with col1:
        st.image(load_query(current_query), caption=f"Query ID: {query_id}")
    with col2:
        ################################### SHOW DESCRIPTION OF CLASS
        with st.expander("Show Class Description"):
            st.write(f"**Name**: {prediction_label}")
            st.write("**Class Definition**:")
            st.markdown("`" + class_def + "`")
            st.image(
                Image.open(f"demonstrations/{predicted_wnid}.jpeg"),
                caption=f"Class Explanation",
                use_column_width=True,
            )

        
        default_value = 0
        if query_id in session_state.user_feedback.keys():
            if session_state.user_feedback[query_id] == "Correct":
                default_value = 1
            elif session_state.user_feedback[query_id] == "Wrong":
                default_value = 2

        session_state.user_feedback[query_id] = st.radio(
            "What do you think about model's prediction?",
            ("-", "Correct", "Wrong"),
            key=query_id,
            index=default_value,
        )
        st.write(f"**Model Prediction**: {prediction_label}")
        st.write(f"**Model Confidence**: {prediction_confidence}")

    ################################### SHOW Model Explanation
    if selected_xai_tool is not None:
        st.image(
            selected_xai_tool(current_query),
            caption=f"Explaination",
            use_column_width=True,
        )

    ################################### SHOW DEBUG INFO

    if st.button("Debug: Show Everything"):
        st.image(Image.open(current_query))


def render_results():
    user_correct_guess = 0
    for q in session_state.user_feedback.keys():
        if session_state.is_classifier_correct[q] == session_state.user_feedback[q]:
            user_correct_guess += 1

    st.write(
        f"User performance on {CLASSIFIER_TAG}: {user_correct_guess} out of {len( session_state.user_feedback)} Correct"
    )
    st.markdown("##  User Performance Breakdown")

    categories = [
        "Correct",
        "Wrong",
    ]  # set(session_state.is_classifier_correct.values())
    breakdown_stats_correct = {c: 0 for c in categories}
    breakdown_stats_wrong = {c: 0 for c in categories}

    experiment_summary = []

    for q in session_state.user_feedback.keys():
        category = "Correct" if session_state.is_classifier_correct[q] else "Wrong"
        is_user_correct = category == session_state.user_feedback[q]

        if is_user_correct:
            breakdown_stats_correct[category] += 1
        else:
            breakdown_stats_wrong[category] += 1

        experiment_summary.append(
            [
                q,
                classifier_predictions[q]["real-gts"],
                folder_to_name[
                    classifier_predictions[q][f"{CLASSIFIER_TAG}-predictions"]
                ],
                category,
                session_state.user_feedback[q],
                is_user_correct,
            ]
        )
    ################################### Summary Table
    experiment_summary_df = pd.DataFrame.from_records(
        experiment_summary,
        columns=[
            "Query",
            "GT Labels",
            f"{CLASSIFIER_TAG} Prediction",
            "Category",
            "User Prediction",
            "Is User Prediction Correct",
        ],
    )
    st.write("Summary", experiment_summary_df)

    csv = convert_df(experiment_summary_df)
    st.download_button(
        "Press to Download", csv, "summary.csv", "text/csv", key="download-records"
    )
    ################################### SHOW BREAKDOWN
    user_pf_by_model_pred = experiment_summary_df.groupby("Category").agg(
        {"Is User Prediction Correct": ["count", "sum", "mean"]}
    )
    # rename columns
    user_pf_by_model_pred.columns = user_pf_by_model_pred.columns.droplevel(0)
    user_pf_by_model_pred.columns = [
        "Count",
        "Correct User Guess",
        "Mean User Performance",
    ]
    user_pf_by_model_pred.index.name = "Model Prediction"
    st.write("User performance break down by Model prediction:", user_pf_by_model_pred)
    csv = convert_df(user_pf_by_model_pred)
    st.download_button(
        "Press to Download",
        csv,
        "user-performance-by-model-prediction.csv",
        "text/csv",
        key="download-performance-by-model-prediction",
    )
    ################################### CONFUSION MATRIX

    confusion_matrix = pd.crosstab(
        experiment_summary_df["Category"],
        experiment_summary_df["User Prediction"],
        rownames=["Actual"],
        colnames=["Predicted"],
    )
    st.write("Confusion Matrix", confusion_matrix)
    csv = convert_df(confusion_matrix)
    st.download_button(
        "Press to Download",
        csv,
        "confusion-matrix.csv",
        "text/csv",
        key="download-confusiion-matrix",
    )


def render_menu():
    # Render the readme as markdown using st.markdown.
    readme_text = st.markdown(
        """
  # Instructions
  ```
  When testing this study, you should first see the class definition, then hide the expander and see the query.
  ```
  """
    )

    app_mode = st.selectbox(
        "Choose the page to show:",
        ["Experiment Instruction", "Start Experiment", "See the Results"],
    )

    if app_mode == "Experiment Instruction":
        st.success("To continue select an option in the dropdown menu.")
    elif app_mode == "Start Experiment":
        # Clear Canvas
        readme_text.empty()

        page_id = session_state.page
        col1, col4, col2, col3 = st.columns(4)
        prev_page = col1.button("Previous Image")

        if prev_page:
            page_id -= 1
            if page_id < 1:
                page_id = 1

        next_page = col2.button("Next Image")

        if next_page:
            page_id += 1
            if page_id > NUMBER_OF_TRIALS:
                page_id = NUMBER_OF_TRIALS

        if page_id == NUMBER_OF_TRIALS:
            st.success(
                'You have reached the last image. Please go to the "Results" page to see your performance.'
            )
            if st.button("View"):
                app_mode = "See the Results"

        if col3.button("Resample"):
            st.write("Restarting ...")
            page_id = 1
            session_state.first_run = 1
            resmaple_queries()

        session_state.page = page_id
        st.write(f"Render Experiment: {session_state.page}")
        render_experiment(session_state.page - 1)
    elif app_mode == "See the Results":
        readme_text.empty()
        st.write("Results Summary")
        render_results()


def main():
    global app_mode
    global session_state
    global selected_xai_tool

    # Set the session state
    # State Management and General Setup
    st.set_page_config(layout="wide")
    st.title("TASK - 1 - ImageNetREAL")

    options = [
        "Unselected",
        "NOXAI",
        "KNN",
        "EMD Nearest Neighbors",
        "EMD Correspondence",
        "CHM Nearest Neighbors",
        "CHM Correspondence",
    ]

    st.markdown(
        """ <style>
              div[role="radiogroup"] >  :first-child{
                  display: none !important;
              }
          </style>
          """,
        unsafe_allow_html=True,
    )

    if session_state.XAI_tool == "Unselected":
        default = options.index(session_state.XAI_tool)
        session_state.XAI_tool = st.radio(
            "What explaination tool do you want to evaluate?",
            options,
            key="which_xai",
            index=default,
        )
        # print(session_state.XAI_tool)

    if session_state.XAI_tool != "Unselected":
        st.markdown(f"## SELECTED METHOD ``{session_state.XAI_tool}``")

        if session_state.XAI_tool == "NOXAI":
            selected_xai_tool = None
            CLASSIFIER_TAG = "KNN"
        elif session_state.XAI_tool == "KNN":
            selected_xai_tool = load_knn_nns
            CLASSIFIER_TAG = "KNN"
        elif session_state.XAI_tool == "CHM Nearest Neighbors":
            selected_xai_tool = load_chm_nns
            CLASSIFIER_TAG = "CHM"
        elif session_state.XAI_tool == "CHM Correspondence":
            selected_xai_tool = load_chm_corrs
            CLASSIFIER_TAG = "CHM"
        elif session_state.XAI_tool == "EMD Nearest Neighbors":
            selected_xai_tool = load_emd_nns
            CLASSIFIER_TAG = "EMD"
        elif session_state.XAI_tool == "EMD Correspondence":
            selected_xai_tool = load_emd_corrs
            CLASSIFIER_TAG = "EMD"

        resmaple_queries()
        render_menu()


if __name__ == "__main__":
    main()