Create load_dataset.py
Browse files- load_dataset.py +24 -0
load_dataset.py
ADDED
|
@@ -0,0 +1,24 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import pandas as pd
|
| 3 |
+
from datasets import Dataset, DatasetDict, load_dataset
|
| 4 |
+
from PIL import Image
|
| 5 |
+
|
| 6 |
+
def load_my_dataset(data_dir):
|
| 7 |
+
# Load the CSV file
|
| 8 |
+
csv_path = os.path.join(data_dir, 'Captions.csv')
|
| 9 |
+
df = pd.read_csv(csv_path)
|
| 10 |
+
|
| 11 |
+
# Define a function to load images
|
| 12 |
+
def load_image(image_path):
|
| 13 |
+
return Image.open(os.path.join(data_dir, image_path))
|
| 14 |
+
|
| 15 |
+
# Create a dataset from the DataFrame
|
| 16 |
+
dataset = Dataset.from_pandas(df)
|
| 17 |
+
|
| 18 |
+
# Map the image loading function to the dataset
|
| 19 |
+
dataset = dataset.map(lambda example: {'image': load_image(example['image_path'])})
|
| 20 |
+
|
| 21 |
+
return dataset
|
| 22 |
+
|
| 23 |
+
# Example usage
|
| 24 |
+
# dataset = load_my_dataset('path_to_your_data_directory')
|