taesiri commited on
Commit
9066d0b
1 Parent(s): 52c9d8a

added labels

Browse files
Files changed (1) hide show
  1. app.py +33 -10
app.py CHANGED
@@ -1,24 +1,47 @@
1
- import fiftyone as fo
2
  from io import BytesIO
3
- from PIL import Image
 
 
4
  from datasets import load_dataset
5
- import os
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
- for i in range(len(imagenet_hard_dataset)):
15
  image = imagenet_hard_dataset[i]["image"].convert("RGB")
16
- image.save(f"dataset/{i}.JPEG", "JPEG", quality=100)
17
- list_of_images.append(f"imagenet_hard_images/{i}.jpg")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
 
19
 
20
  if __name__ == "__main__":
21
- # Ensures that the App processes are safely launched on Windows
22
- dataset = fo.Dataset.from_images_dir("./dataset/")
 
 
 
 
 
 
 
 
 
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()