VIOLIN / scripts /to_parquet.py
Perkzi's picture
Update dataset to version 2
c83afe9
# import os
# import pandas as pd
# from datasets import Dataset, Image, Features, Value
# df = pd.read_json("data/test.jsonl", lines=True)
# def get_full_path(file_name):
# return os.path.join(os.getcwd(), "data", file_name)
# df['image_path'] = df['file_name'].apply(get_full_path)
# # 3. binary
# def __force_embed_image(example):
# with open(example["image_path"], "rb") as f:
# image_bytes = f.read()
# return {"image": {"bytes": image_bytes}}
# ds = Dataset.from_pandas(df)
# ds = ds.map(__force_embed_image, remove_columns=["image_path"], num_proc=1)
# ds = ds.cast_column("image", Image())
# ds.to_parquet("violin-test.parquet")
import os
import pandas as pd
from datasets import Dataset, Image, Features, Value
# 1. Load Data
df = pd.read_json("data/test.jsonl", lines=True)
# 2. Embedding Function
def __force_embed_images(example):
image_cols = ["ground_truth", "image1_path", "image2_path"]
data_root = os.path.abspath("data")
for col in image_cols:
val = example.get(col)
if val and isinstance(val, str):
full_path = os.path.join(data_root, val.replace('\\', '/'))
if os.path.exists(full_path):
with open(full_path, "rb") as f:
img_bytes = f.read()
example[col] = {"bytes": img_bytes}
else:
example[col] = None
else:
example[col] = None
return example
# 3. Define the TARGET Features
target_features = Features({
"id": Value("string"),
"prompt": Value("string"),
"task": Value("int64"),
"ground_truth": Image(),
"color_1": Value("string"),
"color_2": Value("string"),
"hex_val_1": Value("string"),
"hex_val_2": Value("string"),
"direction": Value("string"),
"shape": Value("string"),
"position": Value("string"),
"size_ratio": Value("string"),
"center_x": Value("string"),
"center_y": Value("string"),
"mask_type": Value("string"),
"image_id": Value("string"),
"image1_path": Image(),
"image2_path": Image(),
})
# 4. Create Dataset without initial features
ds = Dataset.from_pandas(df)
# 5. EXECUTE MAP WITH FEATURES
ds = ds.map(
__force_embed_images,
features=target_features,
num_proc=1
)
# 6. Save to Parquet
ds.to_parquet("violin-test.parquet")
print("Success! All images manually embedded into parquet.")