generated from allenai/python-package-template
-
Notifications
You must be signed in to change notification settings - Fork 397
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prepping data to be in a trainable format
- Loading branch information
1 parent
dc86a99
commit fcb67eb
Showing
2 changed files
with
92 additions
and
41 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,82 @@ | ||
import numpy as np | ||
from io import BytesIO | ||
from PIL import Image | ||
import base64 | ||
|
||
|
||
def prepare_data_for_qwen2_training(example, processor): | ||
# Prepare messages | ||
messages = [ | ||
{ | ||
"role": "user", | ||
"content": [ | ||
{ | ||
"type": "image", | ||
"image": example["input_prompt_image_base64"] # Placeholder | ||
}, | ||
{"type": "text", "text": example["input_prompt_text"]}, | ||
], | ||
} | ||
] | ||
# Apply chat template to get the text | ||
text = processor.apply_chat_template( | ||
messages, tokenize=False, add_generation_prompt=True | ||
) | ||
|
||
# Decode image from base64 | ||
main_image = Image.open(BytesIO(base64.b64decode(example["input_prompt_image_base64"]))) | ||
|
||
# Process inputs using processor | ||
inputs = processor( | ||
text=[text], | ||
images=[main_image], | ||
padding=True, | ||
return_tensors="np", | ||
) | ||
|
||
# Get labels by tokenizing the output text | ||
labels = processor( | ||
text=[example["response"]], | ||
padding=True, | ||
return_tensors="np" | ||
) | ||
|
||
# Concatenate input_ids and labels | ||
input_ids = np.concatenate([inputs.input_ids[0], labels.input_ids[0]], axis=0) | ||
attention_mask = np.concatenate([inputs.attention_mask[0], labels.attention_mask[0]], axis=0) | ||
|
||
# Create labels, masking the input portion with -100 | ||
labels_full = np.full_like(input_ids, fill_value=-100) | ||
labels_full[len(inputs.input_ids[0]):] = labels.input_ids[0] | ||
|
||
# Return as dict, including pixel_values | ||
return { | ||
"input_ids": input_ids.tolist(), | ||
"attention_mask": attention_mask.tolist(), | ||
"labels": labels_full.tolist(), | ||
"pixel_values": inputs.pixel_values[0] | ||
} | ||
|
||
|
||
# Define a custom data collator | ||
class DataCollatorForVisionLanguageModeling: | ||
def __init__(self, processor): | ||
self.processor = processor | ||
|
||
def __call__(self, features): | ||
input_ids = [f['input_ids'] for f in features] | ||
attention_mask = [f['attention_mask'] for f in features] | ||
labels = [f['labels'] for f in features] | ||
pixel_values = [f['pixel_values'] for f in features] | ||
|
||
# Pad input_ids, attention_mask, labels | ||
batch = self.processor.pad( | ||
{"input_ids": input_ids, "attention_mask": attention_mask, "labels": labels}, | ||
return_tensors="pt", | ||
padding=True, | ||
) | ||
|
||
# Stack pixel_values | ||
batch['pixel_values'] = torch.stack([torch.tensor(pv) for pv in pixel_values]) | ||
|
||
return batch |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters