Update new_dataset_script.py
Browse files- new_dataset_script.py +93 -36
new_dataset_script.py
CHANGED
|
@@ -121,52 +121,109 @@ class NewDataset(datasets.GeneratorBasedBuilder):
|
|
| 121 |
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 122 |
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
| 123 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 124 |
-
|
| 125 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
return [
|
| 127 |
-
|
| 128 |
-
name=
|
| 129 |
-
# These kwargs will be passed to _generate_examples
|
| 130 |
gen_kwargs={
|
| 131 |
-
"
|
|
|
|
|
|
|
| 132 |
"split": "train",
|
| 133 |
},
|
| 134 |
),
|
| 135 |
-
|
| 136 |
-
name=
|
| 137 |
-
# These kwargs will be passed to _generate_examples
|
| 138 |
gen_kwargs={
|
| 139 |
-
"
|
| 140 |
-
"
|
|
|
|
|
|
|
| 141 |
},
|
| 142 |
),
|
| 143 |
-
|
| 144 |
-
name=
|
| 145 |
-
# These kwargs will be passed to _generate_examples
|
| 146 |
gen_kwargs={
|
| 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 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 121 |
# dl_manager is a datasets.download.DownloadManager that can be used to download and extract URLS
|
| 122 |
# It can accept any type or nested list/dict and will give back the same structure with the url replaced with path to local files.
|
| 123 |
# By default the archives will be extracted and a path to a cached folder where they are extracted is returned instead of the archive
|
| 124 |
+
|
| 125 |
+
# Download and extract the dataset using Hugging Face's datasets library
|
| 126 |
+
data_files = dl_manager.download_and_extract({
|
| 127 |
+
"csv": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/Labeled Stomatal Images.csv",
|
| 128 |
+
"zip": "https://huggingface.co/datasets/XintongHe/Populus_Stomatal_Images_Datasets/resolve/main/Labeled Stomatal Images.zip"
|
| 129 |
+
})
|
| 130 |
+
|
| 131 |
+
# Load the CSV file containing species and scientific names
|
| 132 |
+
species_info = pd.read_csv(data_files["csv"])
|
| 133 |
+
|
| 134 |
+
# Get the list of all image files from the CSV
|
| 135 |
+
all_files = species_info['FileName'].tolist()
|
| 136 |
+
|
| 137 |
+
# Shuffle the list for random split
|
| 138 |
+
random.shuffle(all_files)
|
| 139 |
+
|
| 140 |
+
# Split the files into train/validation/test
|
| 141 |
+
num_files = len(all_files)
|
| 142 |
+
train_split_end = int(num_files * 0.7)
|
| 143 |
+
val_split_end = train_split_end + int(num_files * 0.15)
|
| 144 |
+
|
| 145 |
+
train_files = all_files[:train_split_end]
|
| 146 |
+
val_files = all_files[train_split_end:val_split_end]
|
| 147 |
+
test_files = all_files[val_split_end:]
|
| 148 |
+
|
| 149 |
return [
|
| 150 |
+
SplitGenerator(
|
| 151 |
+
name=Split.TRAIN,
|
|
|
|
| 152 |
gen_kwargs={
|
| 153 |
+
"filepaths": train_files,
|
| 154 |
+
"species_info": species_info,
|
| 155 |
+
"data_dir": extracted_path,
|
| 156 |
"split": "train",
|
| 157 |
},
|
| 158 |
),
|
| 159 |
+
SplitGenerator(
|
| 160 |
+
name=Split.VALIDATION,
|
|
|
|
| 161 |
gen_kwargs={
|
| 162 |
+
"filepaths": val_files,
|
| 163 |
+
"species_info": species_info,
|
| 164 |
+
"data_dir": extracted_path,
|
| 165 |
+
"split": "validation",
|
| 166 |
},
|
| 167 |
),
|
| 168 |
+
SplitGenerator(
|
| 169 |
+
name=Split.TEST,
|
|
|
|
| 170 |
gen_kwargs={
|
| 171 |
+
"filepaths": test_files,
|
| 172 |
+
"species_info": species_info,
|
| 173 |
+
"data_dir": extracted_path,
|
| 174 |
+
"split": "test",
|
| 175 |
},
|
| 176 |
),
|
| 177 |
]
|
| 178 |
|
| 179 |
+
def _generate_examples(self, filepaths, species_info, data_dir, split):
|
| 180 |
+
"""Yields examples as (key, example) tuples."""
|
| 181 |
+
for file_name in filepaths:
|
| 182 |
+
# Extract the base name without the file extension
|
| 183 |
+
image_id = os.path.splitext(file_name)[0]
|
| 184 |
+
|
| 185 |
+
# Construct the full image file path
|
| 186 |
+
image_path = os.path.join(data_dir, f"{image_id}.jpg")
|
| 187 |
+
# Construct the full label file path
|
| 188 |
+
label_path = os.path.join(data_dir, f"{image_id}.txt")
|
| 189 |
+
|
| 190 |
+
# Open image and convert to array
|
| 191 |
+
with Image.open(image_path) as img:
|
| 192 |
+
pics_array = np.array(img)
|
| 193 |
+
width, height = img.size
|
| 194 |
+
|
| 195 |
+
# Extract species and scientific name from CSV file
|
| 196 |
+
species_row = species_info.loc[species_info['FileName'] == image_id]
|
| 197 |
+
species = species_row['Species'].values[0]
|
| 198 |
+
scientific_name = species_row['ScientificName'].values[0]
|
| 199 |
+
|
| 200 |
+
# Parse YOLO annotations
|
| 201 |
+
annotations = self._parse_yolo_labels(label_path, width, height)
|
| 202 |
+
|
| 203 |
+
# Yield the final structured example
|
| 204 |
+
yield image_id, {
|
| 205 |
+
"image_id": image_id,
|
| 206 |
+
"species": species,
|
| 207 |
+
"scientific_name": scientific_name,
|
| 208 |
+
"pics_array": pics_array,
|
| 209 |
+
"image_resolution": {"width": width, "height": height},
|
| 210 |
+
"annotations": annotations
|
| 211 |
+
}
|
| 212 |
+
|
| 213 |
+
def _parse_yolo_labels(self, label_path, width, height):
|
| 214 |
+
annotations = []
|
| 215 |
+
with open(label_path, 'r') as file:
|
| 216 |
+
yolo_data = file.readlines()
|
| 217 |
+
|
| 218 |
+
for line in yolo_data:
|
| 219 |
+
class_id, x_center_rel, y_center_rel, width_rel, height_rel = map(float, line.split())
|
| 220 |
+
annotations.append({
|
| 221 |
+
"category_id": int(class_id),
|
| 222 |
+
"bounding_box": {
|
| 223 |
+
"x_center": x_center_rel * width,
|
| 224 |
+
"y_center": y_center_rel * height,
|
| 225 |
+
"width": width_rel * width,
|
| 226 |
+
"height": height_rel * height
|
| 227 |
+
}
|
| 228 |
+
})
|
| 229 |
+
return annotatio
|