Update Tuberculosis_Dataset.py
Browse files- Tuberculosis_Dataset.py +18 -16
Tuberculosis_Dataset.py
CHANGED
|
@@ -1,11 +1,11 @@
|
|
| 1 |
-
from datasets import GeneratorBasedBuilder, DownloadManager, DatasetInfo, BuilderConfig, SplitGenerator, Version
|
| 2 |
from datasets.features import Features, Value, Sequence
|
| 3 |
import datasets
|
| 4 |
import pandas as pd
|
| 5 |
import json
|
| 6 |
import zipfile
|
| 7 |
from PIL import Image
|
| 8 |
-
import
|
| 9 |
import io
|
| 10 |
|
| 11 |
_DESCRIPTION = """\
|
|
@@ -31,7 +31,7 @@ class TuberculosisDataset(GeneratorBasedBuilder):
|
|
| 31 |
"age": Value("int8"),
|
| 32 |
"case_text": Value("string"),
|
| 33 |
"keywords": Value("string"),
|
| 34 |
-
"
|
| 35 |
"caption": Value("string"),
|
| 36 |
}),
|
| 37 |
supervised_keys = None,
|
|
@@ -47,7 +47,7 @@ class TuberculosisDataset(GeneratorBasedBuilder):
|
|
| 47 |
"caption_json": f"{base_url}image_metadata.json",
|
| 48 |
"images_zip": "https://github.com/zhankai-ye/tuberculosis_dataset/raw/main/images/PMC.zip"
|
| 49 |
}
|
| 50 |
-
downloaded_files = dl_manager.
|
| 51 |
|
| 52 |
return [
|
| 53 |
SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs=downloaded_files),
|
|
@@ -76,10 +76,11 @@ class TuberculosisDataset(GeneratorBasedBuilder):
|
|
| 76 |
merged_df = pd.merge(merged_df, caption[['case_id', 'caption']], on='case_id', how='left')
|
| 77 |
merged_df = merged_df.where(pd.notnull(merged_df), None)
|
| 78 |
merged_df['age'] = merged_df['age'].astype('int8')
|
|
|
|
| 79 |
# Prepare images
|
| 80 |
-
|
| 81 |
|
| 82 |
-
# Yield examples
|
| 83 |
for idx, row in merged_df.iterrows():
|
| 84 |
yield idx, {
|
| 85 |
"case_id": row["case_id"],
|
|
@@ -87,19 +88,20 @@ class TuberculosisDataset(GeneratorBasedBuilder):
|
|
| 87 |
"age": int(row["age"]),
|
| 88 |
"case_text": row["case_text"],
|
| 89 |
"keywords": row["keywords"],
|
| 90 |
-
"
|
| 91 |
"caption": row["caption"],
|
| 92 |
}
|
| 93 |
|
| 94 |
-
def _prepare_images(self, images_zip_path
|
| 95 |
-
|
| 96 |
with zipfile.ZipFile(images_zip_path, 'r') as zip_ref:
|
| 97 |
-
zip_ref.extractall(extracted_path) # Extract images
|
| 98 |
for file_info in zip_ref.infolist():
|
| 99 |
if file_info.filename.endswith('.jpg') and not file_info.is_dir():
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
|
|
|
|
|
|
|
|
| 1 |
+
from datasets import GeneratorBasedBuilder, DownloadManager, DatasetInfo, Array3D, BuilderConfig, SplitGenerator, Version
|
| 2 |
from datasets.features import Features, Value, Sequence
|
| 3 |
import datasets
|
| 4 |
import pandas as pd
|
| 5 |
import json
|
| 6 |
import zipfile
|
| 7 |
from PIL import Image
|
| 8 |
+
import numpy as np
|
| 9 |
import io
|
| 10 |
|
| 11 |
_DESCRIPTION = """\
|
|
|
|
| 31 |
"age": Value("int8"),
|
| 32 |
"case_text": Value("string"),
|
| 33 |
"keywords": Value("string"),
|
| 34 |
+
"image_arrays": Sequence(Value(dtype="uint8")),
|
| 35 |
"caption": Value("string"),
|
| 36 |
}),
|
| 37 |
supervised_keys = None,
|
|
|
|
| 47 |
"caption_json": f"{base_url}image_metadata.json",
|
| 48 |
"images_zip": "https://github.com/zhankai-ye/tuberculosis_dataset/raw/main/images/PMC.zip"
|
| 49 |
}
|
| 50 |
+
downloaded_files = dl_manager.download(urls)
|
| 51 |
|
| 52 |
return [
|
| 53 |
SplitGenerator(name=datasets.Split.TRAIN, gen_kwargs=downloaded_files),
|
|
|
|
| 76 |
merged_df = pd.merge(merged_df, caption[['case_id', 'caption']], on='case_id', how='left')
|
| 77 |
merged_df = merged_df.where(pd.notnull(merged_df), None)
|
| 78 |
merged_df['age'] = merged_df['age'].astype('int8')
|
| 79 |
+
|
| 80 |
# Prepare images
|
| 81 |
+
image_arrays = self._prepare_images(images_zip)
|
| 82 |
|
| 83 |
+
# Yield examples
|
| 84 |
for idx, row in merged_df.iterrows():
|
| 85 |
yield idx, {
|
| 86 |
"case_id": row["case_id"],
|
|
|
|
| 88 |
"age": int(row["age"]),
|
| 89 |
"case_text": row["case_text"],
|
| 90 |
"keywords": row["keywords"],
|
| 91 |
+
"image_arrays": image_arrays.get(row["case_id"], []),
|
| 92 |
"caption": row["caption"],
|
| 93 |
}
|
| 94 |
|
| 95 |
+
def _prepare_images(self, images_zip_path):
|
| 96 |
+
image_arrays = {}
|
| 97 |
with zipfile.ZipFile(images_zip_path, 'r') as zip_ref:
|
|
|
|
| 98 |
for file_info in zip_ref.infolist():
|
| 99 |
if file_info.filename.endswith('.jpg') and not file_info.is_dir():
|
| 100 |
+
with zip_ref.open(file_info) as image_file:
|
| 101 |
+
img = Image.open(io.BytesIO(image_file.read()))
|
| 102 |
+
img_array = np.array(img)
|
| 103 |
+
key = file_info.filename.split('/')[-1].split('_')[0]
|
| 104 |
+
if key not in image_arrays:
|
| 105 |
+
image_arrays[key] = []
|
| 106 |
+
image_arrays[key].append(img_array)
|
| 107 |
+
return image_arrays
|