| import os | |
| import pandas as pd | |
| from datasets import Dataset, DatasetDict, load_dataset | |
| from PIL import Image | |
| def load_my_dataset(data_dir): | |
| # Load the CSV file | |
| csv_path = os.path.join(data_dir, 'Captions.csv') | |
| df = pd.read_csv(csv_path) | |
| # Define a function to load images | |
| def load_image(image_path): | |
| return Image.open(os.path.join(data_dir, image_path)) | |
| # Create a dataset from the DataFrame | |
| dataset = Dataset.from_pandas(df) | |
| # Map the image loading function to the dataset | |
| dataset = dataset.map(lambda example: {'image': load_image(example['image_path'])}) | |
| return dataset | |
| # Example usage | |
| # dataset = load_my_dataset('path_to_your_data_directory') |