For massive datasets like Laion5B, use img2dataset in distributed mode via PySpark.
Infrastructure Recommendation:
- Use 1 master node and ~10 worker nodes (e.g.,
c6i.4xlarge). - Ensure the
img2dataset.pex file is located at a path accessible to all worker nodes.
Setup Steps:
- Start the Spark master.
- Start Spark workers on your compute nodes.
- Run the
download() function with distributor="pyspark" within a Spark environment.
Key Configuration Requirements:
- Set
os.environ['PYSPARK_PYTHON'] to the path of your .pex file. - Configure
spark.executorEnv.PEX_ROOT to a local directory (e.g., ./.pex). - Ensure
spark.driver.host and spark.driver.bindAddress are set to the master node's IP so workers can communicate back.
from img2dataset import download
import os
from pyspark.sql import SparkSession
def create_spark_session():
# This path must be available on all worker nodes
pex_file = "/home/ubuntu/img2dataset.pex"
os.environ['PYSPARK_PYTHON'] = pex_file
spark = (
SparkSession.builder
.config("spark.submit.deployMode", "client") \
.config("spark.executorEnv.PEX_ROOT", "./.pex")
.config("spark.driver.port", "5678")
.config("spark.driver.blockManager.port", "6678")
.config("spark.driver.host", "172.31.44.42")
.config("spark.driver.bindAddress", "172.31.44.42")
.config("spark.executor.memory", "16G")
.config("spark.executor.memoryOverhead", "8G")
.config("spark.task.maxFailures", "100")
.master("spark://172.31.44.42:7077")
.appName("spark-stats")
.getOrCreate()
)
return spark
spark = create_spark_session()
url_list = "s3://laion-us-east-1/laion-metadata/laion2B-en/"
output_dir = "s3://laion-us-east-1/laion-data/laion2B-data"
download(
processes_count=1,
thread_count=64,
url_list = url_list,
image_size=384,
resize_only_if_bigger=True,
resize_mode="keep_ratio",
skip_reencode=True,
output_folder=output_dir,
output_format="webdataset",
input_format="parquet",
url_col="URL",
caption_col="TEXT",
enable_wandb=True,
number_sample_per_shard=10000,
distributor="pyspark",
save_additional_columns=["NSFW","similarity","LICENSE"],
oom_shard_count=6,
)