Skip to main content

📝 AI Pipelines

Description​

< What is it? >​

  • Definition: an AI pipeline is a sequence of connected processing steps. Each step passes its output to a subsequent step as input. A step can perform ordinary application logic, transform data, or run a model.

  • Example: a voice assistant connects speech recognition, response generation, and speech synthesis:

    Speech audio
    ↓
    Speech-to-Text model
    ↓
    Transcribed question
    ↓
    Language model
    ↓
    Answer text
    ↓
    Text-to-Speech model
    ↓
    Spoken answer

Key points​

< Common pipeline types >​

  • Purpose and data flow: pipelines organize the steps needed to produce a particular result, such as a trained model, a prediction, or retrieved content.

    PipelineTypical stepsResult
    TrainingCollect data → Split data → Preprocess → Train → Evaluate → Save modelTrained model and evaluation results
    Prediction / inferenceReceive input → Preprocess → Run model → Format outputPrediction or generated response
    Retrieval-Augmented Generation (RAG)Receive question → Retrieve documents → Build prompt → Generate answerAnswer informed by retrieved content
    Image searchReceive query → Generate embedding → Search indexed image vectors → Return matchesRanked existing images
  • Training and inference consistency: preprocessing learned during training, such as feature scaling, must be reused appropriately during inference. Fit such transformations on training data rather than the held-out evaluation data. Image search similarly requires compatible query and image embeddings.

< Connecting the steps >​

  • Input and output contracts: adjacent steps must agree on data formats. A speech recognition model outputs text that a language model can consume; a speech synthesis model then converts the answer text into audio.
  • Execution: a pipeline can run within one application or across multiple services. More complex workflows can branch, run independent steps in parallel, or repeat steps. A separate microservice is not required for every stage.

Comparison​

< Model vs. pipeline >​

  • Different responsibilities: a model performs a learned computation; a pipeline organizes the processing steps around one or more models. Google's Machine Learning Crash Course describes why a production ML system includes substantial data processing and infrastructure beyond the model itself.

    ConceptRoleVoice assistant example
    ModelPerforms a learned taskA speech recognition model transcribes audio
    PipelineConnects processing steps into a workflowSpeech recognition → Response generation → Speech synthesis

Implementation​

< Remove an image background with a Hugging Face pipeline >​

  • Purpose: Hugging Face Transformers provides a pipeline() helper that connects preprocessing, model inference, and postprocessing. This example uses briaai/RMBG-1.4 to separate the foreground from the background and return an image with transparency. See the RMBG-1.4 model card.

  • Setup: install the model's documented dependencies and Matplotlib for displaying the result:

    pip install -r https://huggingface.co/briaai/RMBG-1.4/resolve/main/requirements.txt
    pip install matplotlib
  • Sample input and code: the URL below is the sample image used in the model card. It can also be replaced with a local image path.

    import matplotlib.pyplot as plt
    from transformers import pipeline
    from transformers.image_utils import load_image

    image = load_image(
    "https://farm5.staticflickr.com/4007/4322154488_997e69e4cf_z.jpg"
    )

    # Load the image-segmentation pipeline for background removal.
    # trust_remote_code=True loads the model repository's custom Python code.
    pipe = pipeline(
    "image-segmentation",
    model="briaai/RMBG-1.4",
    trust_remote_code=True,
    )

    # Predict a foreground mask and apply it as the image's alpha channel.
    outputs = pipe(image)

    outputs.save("foreground.png")
    print("Output mode:", outputs.mode)
    print("Same size as input:", outputs.size == image.size)

    plt.imshow(outputs)
    plt.axis("off")
    plt.show()
  • Expected output (not executed here): this custom pipeline returns a Pillow image in RGBA mode, with the original dimensions and an alpha channel controlling transparency. The script displays the result and saves it as foreground.png; the quality of the foreground boundary depends on the model's prediction.

    Output mode: RGBA
    Same size as input: True
  • What the pipeline connects: image resizing and normalization → foreground prediction → mask resizing and application to the original image. For this checkpoint, pipe(image, return_mask=True) returns only the mask. Output formats depend on the selected task and model; the behavior here comes from RMBG's custom pipeline implementation.

Reference​