Create script/preprocess.py
Browse files- script/preprocess.py +80 -0
script/preprocess.py
ADDED
|
@@ -0,0 +1,80 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import json
|
| 2 |
+
import os
|
| 3 |
+
from datasets import Dataset, Image
|
| 4 |
+
from transformers import CLIPProcessor, CLIPModel, AutoTokenizer
|
| 5 |
+
from PIL import Image as PILImage
|
| 6 |
+
import torch
|
| 7 |
+
from tqdm import tqdm
|
| 8 |
+
|
| 9 |
+
def preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer_name, clip_model_name, output_dir):
|
| 10 |
+
"""
|
| 11 |
+
Preprocesses raw JSON and image data into a Hugging Face Dataset and saves it to a repository.
|
| 12 |
+
|
| 13 |
+
Args:
|
| 14 |
+
raw_json_path (str): Path to the raw JSON file.
|
| 15 |
+
image_dir (str): Directory containing the images.
|
| 16 |
+
dataset_name (str): Name of the Hugging Face Dataset.
|
| 17 |
+
tokenizer_name (str): Name of the tokenizer from Hugging Face Transformers.
|
| 18 |
+
clip_model_name (str): Name of the CLIP model from Hugging Face Transformers.
|
| 19 |
+
output_dir (str): Directory where the dataset repository will be saved.
|
| 20 |
+
"""
|
| 21 |
+
|
| 22 |
+
# Load data from JSON
|
| 23 |
+
with open(raw_json_path, 'r') as f:
|
| 24 |
+
data = json.load(f)
|
| 25 |
+
|
| 26 |
+
# Initialize lists to store data
|
| 27 |
+
images = []
|
| 28 |
+
questions = []
|
| 29 |
+
answers = []
|
| 30 |
+
ids = [] #add an id field.
|
| 31 |
+
|
| 32 |
+
# Load CLIP and Tokenizer
|
| 33 |
+
clip_processor = CLIPProcessor.from_pretrained(clip_model_name)
|
| 34 |
+
clip_model = CLIPModel.from_pretrained(clip_model_name).to("cuda")
|
| 35 |
+
tokenizer = AutoTokenizer.from_pretrained(tokenizer_name)
|
| 36 |
+
|
| 37 |
+
# Process each data point
|
| 38 |
+
for i, item in enumerate(tqdm(data, desc="Processing data")):
|
| 39 |
+
image_path = os.path.join(image_dir, item['image'])
|
| 40 |
+
question = item['question']
|
| 41 |
+
answer = item.get('answer', '') #handle cases where answer is missing.
|
| 42 |
+
id_field = str(i) #create id field.
|
| 43 |
+
|
| 44 |
+
try:
|
| 45 |
+
image = PILImage.open(image_path).convert("RGB") # Ensure RGB
|
| 46 |
+
images.append(image)
|
| 47 |
+
questions.append(question)
|
| 48 |
+
answers.append(answer)
|
| 49 |
+
ids.append(id_field)
|
| 50 |
+
|
| 51 |
+
except FileNotFoundError:
|
| 52 |
+
print(f"Warning: Image not found at {image_path}")
|
| 53 |
+
except Exception as e:
|
| 54 |
+
print(f"Warning: Error processing image {image_path}: {e}")
|
| 55 |
+
|
| 56 |
+
# Create Hugging Face Dataset
|
| 57 |
+
dataset_dict = {
|
| 58 |
+
'image': images,
|
| 59 |
+
'question': questions,
|
| 60 |
+
'answer': answers,
|
| 61 |
+
'id': ids,
|
| 62 |
+
}
|
| 63 |
+
|
| 64 |
+
dataset = Dataset.from_dict(dataset_dict)
|
| 65 |
+
dataset = dataset.cast_column("image", Image())
|
| 66 |
+
|
| 67 |
+
# Save to Hugging Face Dataset repository
|
| 68 |
+
dataset.save_to_disk(os.path.join(output_dir, dataset_name))
|
| 69 |
+
|
| 70 |
+
print(f"Dataset '{dataset_name}' saved to {os.path.join(output_dir, dataset_name)}")
|
| 71 |
+
|
| 72 |
+
# Example Usage:
|
| 73 |
+
raw_json_path = 'your_data.json' # replace with your json path.
|
| 74 |
+
image_dir = 'your_image_dir' # replace with your image directory.
|
| 75 |
+
dataset_name = 'my_multimodal_dataset'
|
| 76 |
+
tokenizer_name = 'deepseek-ai/deepseek-coder-1.3b-instruct'
|
| 77 |
+
clip_model_name = 'openai/clip-vit-base-patch32'
|
| 78 |
+
output_dir = 'my_datasets'
|
| 79 |
+
|
| 80 |
+
preprocess_multimodal_data(raw_json_path, image_dir, dataset_name, tokenizer_name, clip_model_name, output_dir)
|