Image safety (NSFW) detector—supports multi-source input and batch inference
Input column name | Description |
|---|---|
images | An array containing the input images, supporting URL, Base64, or binary format. |
Returns an array containing the detection results, where each element is the NSFW (Not Safe for Work) confidence score (floating-point value) for the corresponding image. If detection fails, the element is None.
If a parameter does not have a default value, it is required
Parameter name | Type | Default value | Description |
|---|---|---|---|
image_src_type | str | "image_url" | The format type of the input image. Optional values are ["image_url", "image_base64", "image_binary"]. |
model_path | str | "/home/ray/workdir/models" | The root directory path of the pre-trained model on the local machine. |
model_name | str | "Falconsai/nsfw_image_detection" | The specific model directory name under model_path. Optional values are ["Falconsai/nsfw_image_detection"]. |
dtype | str | "float16" | Model inference precision selection. float16 is faster, while float32 offers higher precision but also higher memory usage. Optional values are ["float16", "float32"]. |
batch_size | int | 16 | The number of images sent to the model for inference at one time. The larger the batch, the higher the throughput, but also the higher the memory usage. |
rank | int | 0 | The GPU index used for inference. When using CPU for inference, this parameter can remain 0. |
The following code demonstrates how to use Daft and LAS UDF to perform NSFW detection on images.
from __future__ import annotations import os import daft from daft import col from daft.las.functions.image.image_nsfw_detect import ImageNsfwDetect from daft.las.functions.udf import las_udf if __name__ == "__main__": if os.getenv("DAFT_RUNNER", "native") == "ray": import logging import ray def configure_logging(): logging.basicConfig( level=logging.INFO, format="%(asctime)s - %(name)s - %(levelname)s - %(message)s", datefmt="%Y-%m-%d %H:%M:%S.%s".format(), ) logging.getLogger("tracing.span").setLevel(logging.WARNING) logging.getLogger("daft_io.stats").setLevel(logging.WARNING) logging.getLogger("DaftStatisticsManager").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaScheduler").setLevel(logging.WARNING) logging.getLogger("DaftFlotillaDispatcher").setLevel(logging.WARNING) ray.init(dashboard_host="0.0.0.0", runtime_env={"worker_process_setup_hook": configure_logging}) daft.set_runner_ray() daft.set_execution_config(actor_udf_ready_timeout=600) daft.set_execution_config(min_cpu_per_task=0) tos_dir_url = os.getenv("TOS_DIR_URL", "las-cn-beijing-public-online.tos-cn-beijing.volces.com") samples = { "image": [ f"https://{tos_dir_url}/public/shared_image_dataset/cat_ip_adapter.jpeg" ] } image_src_type = "image_url" model_path = os.getenv("MODEL_PATH", "/opt/las/models") model_name = "Falconsai/nsfw_image_detection" rank = 0 num_gpus = 0 batch_size = 1 ds = daft.from_pydict(samples) ds = ds.with_column( "nsfw_detect", las_udf( ImageNsfwDetect, construct_args={ "image_src_type": image_src_type, "batch_size": batch_size, "model_path": model_path, "model_name": model_name, "rank": rank, }, num_gpus=num_gpus, batch_size=1, )(col("image")), ) ds.show() # ╭────────────────────────────────┬────────────────────────╮ # │ image ┆ nsfw_detect │ # │ --- ┆ --- │ # │ Utf8 ┆ Float64 │ # ╞════════════════════════════════╪════════════════════════╡ # │ https://las-cn-beijing-public… ┆ 0.000114 │ # ╰────────────────────────────────┴────────────────────────╯