Spaces:
Paused
Paused
File size: 12,991 Bytes
ed8c5ad 106ce32 ed8c5ad 106ce32 ed8c5ad 6d7c8da 106ce32 ed8c5ad 106ce32 ed8c5ad 6d7c8da ed8c5ad 262ca90 ed8c5ad |
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 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 |
import csv
import json
import os
import pickle
import random
import string
import sys
import time
from glob import glob
import datasets
import gdown
import gradio as gr
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import torch
import torchvision
from huggingface_hub import HfApi, login, snapshot_download
from PIL import Image
import re
from fnmatch import translate
session_token = os.environ.get("SessionToken")
login(token=session_token)
csv.field_size_limit(sys.maxsize)
np.random.seed(int(time.time()))
with open("./imagenet_hard_nearest_indices.pkl", "rb") as f:
knn_results = pickle.load(f)
with open("imagenet-labels.json") as f:
wnid_to_label = json.load(f)
with open("id_to_label.json", "r") as f:
id_to_labels = json.load(f)
imagenet_training_samples_path = "imagenet_samples"
bad_items = open("./ex2.txt", "r").read().split("\n")
bad_items = [x.split(".")[0] for x in bad_items]
bad_items = [int(x) for x in bad_items if x != ""]
NUMBER_OF_IMAGES = len(bad_items)
gdown.cached_download(
url="https://huggingface.co/datasets/taesiri/imagenet_hard_review_samples/resolve/main/data.zip",
path="./data.zip",
quiet=False,
md5="ece2720fed664e71799f316a881d4324",
)
# EXTRACT if needed
if not os.path.exists("./imagenet_samples") or not os.path.exists(
"./knn_cache_for_imagenet_hard"
):
torchvision.datasets.utils.extract_archive(
from_path="data.zip",
to_path="./",
remove_finished=False,
)
imagenet_hard = datasets.load_dataset("taesiri/imagenet-hard", split="validation")
def update_snapshot(username):
escaped_username = re.escape(username)
pattern = f"*{escaped_username}*.json"
output_dir = snapshot_download(
repo_id="taesiri/imagenet_hard_review_data_r2",
allow_patterns=translate(pattern),
repo_type="dataset",
)
files = glob(f"{output_dir}/*.json")
df = pd.DataFrame()
columns = ["id", "user_id", "time", "decision"]
rows = []
for file in files:
with open(file) as f:
data = json.load(f)
tdf = [data[x] for x in columns]
rows.append(tdf)
df = pd.DataFrame(rows, columns=columns)
# download and append all CSV files
output_dir = snapshot_download(
repo_id="taesiri/imagenet_hard_review_data_r3",
allow_patterns="*.csv",
repo_type="dataset",
)
files = glob(f"{output_dir}/*.csv")
if len(files) > 0:
csv_dataframes = [pd.read_csv(file) for file in files]
csv_dataframes = pd.concat(csv_dataframes, ignore_index=True)
df = pd.concat([df, csv_dataframes], ignore_index=True)
# remove duplicate rows
df = df.drop_duplicates(subset=["id", "user_id"], keep="last")
df = df[df["user_id"] == username]
return df
def generate_dataset(username):
global NUMBER_OF_IMAGES
df = update_snapshot(username)
all_images = set(bad_items)
answered = set(df.id)
remaining = list(all_images - answered)
# shuffle remaining
random.shuffle(remaining)
NUMBER_OF_IMAGES = len(bad_items)
print(f"NUMBER_OF_IMAGES: {NUMBER_OF_IMAGES}")
print(f"Remaining: {len(remaining)}")
if NUMBER_OF_IMAGES == 0:
return []
data = []
for i, image in enumerate(remaining):
data.append(
{
"id": remaining[i],
}
)
return data
def string_to_image(text):
text = text.replace("_", " ").lower().replace(", ", "\n")
# Create a blank white square image
img = np.ones((220, 75, 3))
fig, ax = plt.subplots(figsize=(6, 2.25))
ax.imshow(img, extent=[0, 1, 0, 1])
ax.text(0.5, 0.75, text, fontsize=18, ha="center", va="center")
ax.set_xticks([])
ax.set_yticks([])
ax.set_xticklabels([])
ax.set_yticklabels([])
for spine in ax.spines.values():
spine.set_visible(False)
return fig
all_samples = glob("./imagenet_samples/*.JPEG")
qid_to_sample = {
int(x.split("/")[-1].split(".")[0].split("_")[0]): x for x in all_samples
}
def get_training_samples(qid):
labels_id = imagenet_hard[int(qid)]["label"]
samples = [qid_to_sample[x] for x in labels_id]
return samples
def load_sample(data, current_index):
image_id = data[current_index]["id"]
qimage = imagenet_hard[int(image_id)]["image"]
# labels = data[current_index]["correct_label"]
labels = imagenet_hard[int(image_id)]["english_label"]
# print(f"Image ID: {image_id}")
# print(f"Labels: {labels}")
return qimage, labels
def preprocessing(data, current_index, history, username):
data = generate_dataset(username)
remaining_images = len(data)
labeled_images = len(bad_items) - remaining_images
if remaining_images == 0:
fake_plot = string_to_image("No more images to review")
empty_image = Image.new("RGB", (224, 224))
return (
empty_image,
fake_plot,
current_index,
history,
data,
None,
labeled_images,
)
current_index = 0
qimage, labels = load_sample(data, current_index)
image_id = data[current_index]["id"]
training_samples_image = get_training_samples(image_id)
training_samples_image = [
Image.open(x).convert("RGB") for x in training_samples_image
]
# labels is a list of labels, conver it to a string
labels = ", ".join(labels)
label_plot = string_to_image(labels)
return (
qimage,
label_plot,
current_index,
history,
data,
training_samples_image,
labeled_images,
)
def update_app(decision, data, current_index, history, username):
global NUMBER_OF_IMAGES
if current_index == -1:
fake_plot = string_to_image("Please Enter your username and load samples")
empty_image = Image.new("RGB", (224, 224))
return empty_image, fake_plot, current_index, history, data, None, 0
if current_index == NUMBER_OF_IMAGES - 1:
time_stamp = int(time.time())
image_id = data[current_index]["id"]
# convert to percentage
dicision_dict = {
"id": int(image_id),
"user_id": username,
"time": time_stamp,
"decision": decision,
}
# upload the decision to the server
temp_filename = f"results_{username}_{time_stamp}.json"
# convert decision_dict to json and save it on the disk
with open(temp_filename, "w") as f:
json.dump(dicision_dict, f)
api = HfApi()
api.upload_file(
path_or_fileobj=temp_filename,
path_in_repo=temp_filename,
repo_id="taesiri/imagenet_hard_review_data_r2",
repo_type="dataset",
)
os.remove(temp_filename)
fake_plot = string_to_image("Thank you for your time!")
empty_image = Image.new("RGB", (224, 224))
remaining_images = len(data)
labeled_images = (len(bad_items) - remaining_images) + current_index
return (
empty_image,
fake_plot,
current_index,
history,
data,
None,
labeled_images + 1,
)
if current_index >= 0 and current_index < NUMBER_OF_IMAGES - 1:
time_stamp = int(time.time())
image_id = data[current_index]["id"]
# convert to percentage
dicision_dict = {
"id": int(image_id),
"user_id": username,
"time": time_stamp,
"decision": decision,
}
# upload the decision to the server
temp_filename = f"results_{username}_{time_stamp}.json"
# convert decision_dict to json and save it on the disk
with open(temp_filename, "w") as f:
json.dump(dicision_dict, f)
api = HfApi()
api.upload_file(
path_or_fileobj=temp_filename,
path_in_repo=temp_filename,
repo_id="taesiri/imagenet_hard_review_data_r2",
repo_type="dataset",
)
os.remove(temp_filename)
# Load the Next Image
current_index += 1
qimage, labels = load_sample(data, current_index)
image_id = data[current_index]["id"]
training_samples_image = get_training_samples(image_id)
training_samples_image = [
Image.open(x).convert("RGB") for x in training_samples_image
]
# labels is a list of labels, conver it to a string
labels = ", ".join(labels)
label_plot = string_to_image(labels)
remaining_images = len(data)
labeled_images = (len(bad_items) - remaining_images) + current_index
return (
qimage,
label_plot,
current_index,
history,
data,
training_samples_image,
labeled_images,
)
newcss = """
#query_image{
}
#nn_gallery {
height: auto !important;
}
#sample_gallery {
height: auto !important;
}
/* Set display to flex for the parent element */
.svelte-parentrowclass {
display: flex;
}
/* Set the flex-grow property for the children elements */
.svelte-parentrowclass > #query_image {
min-width: min(400px, 40%);
flex : 1;
flex-grow: 0; !important;
border-style: solid;
height: auto !important;
}
.svelte-parentrowclass > .svelte-rightcolumn {
flex: 2;
flex-grow: 0; !important;
min-width: min(600px, 60%);
}
"""
with gr.Blocks(css=newcss, theme=gr.themes.Soft()) as demo:
data_gr = gr.State({})
current_index = gr.State(-1)
history = gr.State({})
gr.Markdown("# Help Us to Clean `ImageNet-Hard`!")
gr.Markdown("## Instructions")
gr.Markdown(
"Please enter your username and press `Load Samples`. The loading process might take up to a minute. Once the loading is done, you can start reviewing the samples."
)
gr.Markdown(
"""For each image, please select one of the following options: `Accept`, `Not Sure!`, `Reject`.
- If you think any of the labels are correct, please select `Accept`.
- If you think none of the labels matching the image, please select `Reject`.
- If you are not sure about the label, please select `Not Sure!`.
You can refer to `Training samples` if you are not sure about the target label.
"""
)
random_str = "".join(
random.choice(string.ascii_lowercase + string.digits) for _ in range(5)
)
with gr.Column():
with gr.Row():
username = gr.Textbox(label="Username", value=f"user-{random_str}")
labeled_images = gr.Textbox(label="Labeled Images", value="0")
total_images = gr.Textbox(label="Total Images", value=len(bad_items))
prepare_btn = gr.Button(value="Load Samples")
with gr.Column():
with gr.Row():
accept_btn = gr.Button(value="Accept")
myabe_btn = gr.Button(value="Not Sure!")
reject_btn = gr.Button(value="Reject")
with gr.Row(elem_id="parent_row", elem_classes="svelte-parentrowclass"):
query_image = gr.Image(type="pil", label="Query", elem_id="query_image")
with gr.Column(
elem_id="samples_col",
elem_classes="svelte-rightcolumn",
):
label_plot = gr.Plot(
label="Is this a correct label for this image?", type="fig"
)
training_samples = gr.Gallery(
type="pil", label="Training samples", elem_id="sample_gallery"
)
accept_btn.click(
update_app,
inputs=[accept_btn, data_gr, current_index, history, username],
outputs=[
query_image,
label_plot,
current_index,
history,
data_gr,
training_samples,
labeled_images,
],
)
myabe_btn.click(
update_app,
inputs=[myabe_btn, data_gr, current_index, history, username],
outputs=[
query_image,
label_plot,
current_index,
history,
data_gr,
training_samples,
labeled_images,
],
)
reject_btn.click(
update_app,
inputs=[reject_btn, data_gr, current_index, history, username],
outputs=[
query_image,
label_plot,
current_index,
history,
data_gr,
training_samples,
labeled_images,
],
)
prepare_btn.click(
preprocessing,
inputs=[data_gr, current_index, history, username],
outputs=[
query_image,
label_plot,
current_index,
history,
data_gr,
training_samples,
labeled_images,
],
)
demo.launch(debug=False)
|