Spaces:
Running
Running
added labels
Browse files
app.py
CHANGED
@@ -1,24 +1,47 @@
|
|
1 |
-
import
|
2 |
from io import BytesIO
|
3 |
-
from
|
|
|
|
|
4 |
from datasets import load_dataset
|
5 |
-
import
|
6 |
|
7 |
# Load the dataset
|
8 |
imagenet_hard_dataset = load_dataset("taesiri/imagenet-hard", split="validation")
|
9 |
-
|
10 |
os.makedirs("dataset", exist_ok=True)
|
11 |
|
12 |
-
list_of_images = []
|
13 |
|
14 |
-
|
15 |
image = imagenet_hard_dataset[i]["image"].convert("RGB")
|
16 |
-
|
17 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
18 |
|
19 |
|
20 |
if __name__ == "__main__":
|
21 |
-
#
|
22 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
23 |
session = fo.launch_app(dataset, port=7860, remote=True, address="0.0.0.0")
|
24 |
session.wait()
|
|
|
1 |
+
import os
|
2 |
from io import BytesIO
|
3 |
+
from multiprocessing import Pool, cpu_count
|
4 |
+
|
5 |
+
import fiftyone as fo
|
6 |
from datasets import load_dataset
|
7 |
+
from PIL import Image
|
8 |
|
9 |
# Load the dataset
|
10 |
imagenet_hard_dataset = load_dataset("taesiri/imagenet-hard", split="validation")
|
|
|
11 |
os.makedirs("dataset", exist_ok=True)
|
12 |
|
|
|
13 |
|
14 |
+
def process_image(i):
|
15 |
image = imagenet_hard_dataset[i]["image"].convert("RGB")
|
16 |
+
image_path = f"dataset/{i}.JPEG"
|
17 |
+
image.save(image_path, "JPEG", quality=80)
|
18 |
+
return {
|
19 |
+
"file_path": image_path,
|
20 |
+
"labels": imagenet_hard_dataset[i]["english_label"],
|
21 |
+
}
|
22 |
+
|
23 |
+
|
24 |
+
def create_fiftyone_sample(sample):
|
25 |
+
classifications = [
|
26 |
+
fo.Classification(label=str(label)) for label in sample["labels"]
|
27 |
+
]
|
28 |
+
return fo.Sample(
|
29 |
+
filepath=sample["file_path"],
|
30 |
+
labels=fo.Classifications(classifications=classifications),
|
31 |
+
)
|
32 |
|
33 |
|
34 |
if __name__ == "__main__":
|
35 |
+
# Process images in parallel and get the list of images with their labels
|
36 |
+
with Pool(cpu_count()) as pool:
|
37 |
+
samples_data = pool.map(process_image, range(len(imagenet_hard_dataset)))
|
38 |
+
|
39 |
+
# Create a FiftyOne dataset
|
40 |
+
dataset = fo.Dataset(name="imagenet-hard")
|
41 |
+
|
42 |
+
# Add images and labels to the FiftyOne dataset
|
43 |
+
samples = [create_fiftyone_sample(sample_data) for sample_data in samples_data]
|
44 |
+
dataset.add_samples(samples)
|
45 |
+
|
46 |
session = fo.launch_app(dataset, port=7860, remote=True, address="0.0.0.0")
|
47 |
session.wait()
|