Gharaee commited on
Commit
79052be
·
verified ·
1 Parent(s): 6cab2ed

Upload usage_demo_bioscan5m.py

Browse files
Files changed (1) hide show
  1. usage_demo_bioscan5m.py +75 -0
usage_demo_bioscan5m.py ADDED
@@ -0,0 +1,75 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ """
2
+ BIOSCAN-5M Dataset Loader
3
+
4
+ Author: Zahra Gharaee (https://github.com/zahrag)
5
+ License: MIT License
6
+ Description:
7
+ This script serves as a usage demo for loading and accessing the BIOSCAN-5M dataset,
8
+ which includes millions of annotated insect images along with associated metadata for machine learning and biodiversity research.
9
+ It demonstrates how to use the dataset loader to access multiple image resolutions (e.g., cropped and original)
10
+ and predefined splits (e.g., training, validation, pretraining).
11
+ The demo integrates with the Hugging Face `datasets` library, showcasing how to load
12
+ the dataset locally or from the Hugging Face Hub for seamless data preparation and machine learning workflows.
13
+ """
14
+
15
+ import matplotlib.pyplot as plt
16
+ from datasets import load_dataset
17
+
18
+
19
+ def plot_image_with_metadata(ex):
20
+
21
+ image = ex["image"]
22
+
23
+ # Define the metadata fields to show
24
+ fields_to_show = [
25
+ "processid", "sampleid", "phylum", "class", "order", "family", "subfamily", "genus", "species",
26
+ "dna_bin", "dna_barcode", "country", "province_state", "coord-lat", "coord-lon",
27
+ "image_measurement_value", "area_fraction", "scale_factor", "split"
28
+ ]
29
+
30
+ # Prepare metadata as formatted strings
31
+ metadata_lines = []
32
+ for cnt, field in enumerate(fields_to_show):
33
+ value = ex.get(field, "N/A")
34
+ if field == "dna_barcode" and value not in ("N/A", None, ""):
35
+ value = value[:10] + " ... " + f"({len(value)} bp)" # bp: base pairs
36
+ if field == "image_measurement_value" and value not in (None, "", "N/A"):
37
+ value = int(value)
38
+ metadata_lines.append(f"{cnt + 1}- {field}: {value}")
39
+
40
+ fig, axs = plt.subplots(1, 2, figsize=(12, 6), gridspec_kw={'width_ratios': [1.2, 2]})
41
+ plt.subplots_adjust(wspace=0.1)
42
+ fig.suptitle(f"Image and Metadata: {ex.get('processid', '')}", fontsize=14)
43
+
44
+ # Left: metadata
45
+ axs[0].axis("off")
46
+ metadata_text = "\n".join(metadata_lines)
47
+ axs[0].text(0, 0.9, metadata_text, fontsize=14, va='top', ha='left', transform=axs[0].transAxes, wrap=True)
48
+
49
+ # Right: image
50
+ axs[1].imshow(image)
51
+ axs[1].axis("off")
52
+
53
+ plt.tight_layout()
54
+ plt.show()
55
+
56
+
57
+ def main():
58
+
59
+ ds_val = load_dataset("bioscan5m.py", name="cropped_256_eval", split="validation", trust_remote_code=True)
60
+ print(f"{ds_val.description}{ds_val.license}{ds_val.citation}")
61
+
62
+ # Print and visualize a few examples
63
+ samples_to_show = 10
64
+ cnt = 1
65
+ for i, sp in enumerate(ds_val):
66
+ plot_image_with_metadata(sp)
67
+ if cnt == samples_to_show:
68
+ break
69
+ cnt += 1
70
+
71
+ if __name__ == '__main__':
72
+ main()
73
+
74
+
75
+