Pipcook Documentation

repository·main·Indexed 25 days ago

https://github.com/alibaba/pipcook

A JavaScript application framework for machine learning engineering that allows Web engineers to train, serve, and optimize models. It features a CLI (@pipcook/cli) for a four-step workflow (install, train, test, deploy) and uses the Boa module to bridge the Node.js runtime with the Python ecosystem (including numpy, scikit-learn, and tensorflow) via N-API. The framework utilizes a script-based pipeline mechanism for datasource management, dataflow orchestration, and model training.

Tokens
28K
Snippets
80
Records
154
Agent score
82%

What's inside pipcook

  1. What is Pipcook Pipeline?

    main

    Pipcook Pipeline is a layer used to represent machine learning pipelines consisting of Pipcook scripts. It ensures system stability and scalability by using a Script mechanism. These scripts support various functions including:

    • Datasource management
    • Dataflow orchestration
    • Training
    • Validations

    A completed pipeline results in an output directory containing the trained model.

  2. What is a Pipcook Pipeline

    main

    In Pipcook, a Pipeline is a JSON-based description of a model's training process. It covers the entire lifecycle from sample collection to model evaluation.

    A Pipeline is composed of the following components:

    • datasource: A script (via URI) responsible for sample collection.
    • dataflow: An array of scripts (via URIs) that define data processing steps.
    • model: A script (via URI) that defines the model architecture and training logic.
    • artifact: A set of build plugins (e.g., pipcook-artifact-zip) called after training to transform, package, or deploy the output model.
    • options: Configuration for the execution environment, including the framework version and train parameters (like epochs).

    Scripts support http, https, and file protocols. Parameters for scripts are passed via URI query strings, while model-specific parameters can also be defined in options.train.

    {
      "specVersion": "2.0",
      "datasource": "https://example.com/datasource.js?param=value",
      "dataflow": [
        "https://example.com/dataflow.js?size=224"
      ],
      "model": "https://example.com/model.js",
      "artifact": [{
        "processor": "pipcook-artifact-zip@0.0.2",
        "target": "/tmp/mobilenet-model.zip"
      }],
      "options": {
        "framework": "tfjs@3.8",
        "train": {
          "epochs": 20,
          "validationRequired": true
        }
      }
    }
  3. How Python's 'with' statement works in Boa

    main

    In Python, the with statement is used for context management (similar to block scoping in JS, but specifically for managing resources). It relies on the __enter__ and __exit__ magic methods of the object being used.

    Boa provides a with(ctx, fn) method to replicate this behavior in Node.js, where ctx is the context manager and fn is the function to execute within that context.

    // Conceptual usage
    boa.with(localcontext, () => {
      // code executed within the context
    });
  4. How the Pipcook script ecosystem works

    main

    Pipcook distinguishes between different types of scripts based on their maintenance and accessibility:

    • Built-in scripts: Maintained by core collaborators and released bundled with Pipcook.
    • Community scripts: Maintained and released by individual authors. Pipcook can fetch these via http, https, or file protocols.
    • Private scripts: Maintained by private organizations or companies.

    To make community scripts discoverable by the Pipcook Web launcher, authors should:

    1. Add the GitHub topic pipcook-script to their repository.
    2. Create a pull request to add the script URI to the COMMUNITY_SCRIPTS.md file.
  5. Standard Image Dataset Format (PascalVOC)

    main

    When using the datasource script, image datasets must follow the PascalVOC format. The directory structure must separate annotations and images, with annotations further subdivided into train, test, and validation folders.

    Each image must have a corresponding .xml file in the annotations directory matching the image name.

    📂dataset
       ┣ 📂annotations
       ┃ ┣ 📂train
       ┃ ┃ ┣ 📜...
       ┃ ┃ ┗ 📜${image_name}.xml
       ┃ ┣ 📂test
       ┃ ┗ 📂validation
       ┗ 📂images
         ┣ 📜...
         ┗ 📜${image_name}.jpg
  6. Use WASM models for portable inference

    main

    Pipcook uses TVM to compile models into WASM format. This provides a portable solution that runs natively in both browsers and Node.js.

    Important Limitations:

    • CPU Only: Currently, WASM models only work for CPU execution. WebGPU support is not yet implemented.

    Folder Structure: A WASM model output includes:

    • browser.js (Entry point for browsers)
    • node.js (Entry point for Node.js)
    • model.wasi.js
    • model.wasi.wasm
    • modelDesc.json
    • modelParams.parmas
    • modelSpec.json
    • tvmjs.bundle.js
  7. Understand image classification prediction results and labelmaps

    main

    When an image classification model makes a prediction, it returns a two-dimensional array. Each inner array represents a single image and contains the probability (confidence) for each possible class.

    Labelmap: Because models process numbers rather than text, a labelmap is generated during training. This maps the serial number (index) used by the model back to the actual text category name.

    Example Mapping: If your labelmap is:

    {
      "column": 0,
      "pie": 1
    }

    And the model returns a prediction of [[0.1, 0.9]] for an image, it means:

    • Index 0 (column) has a confidence of 0.1.
    • Index 1 (pie) has a confidence of 0.9.

    The image is therefore predicted to be a pie chart.

  8. Understand Pipcook script categories

    main

    Pipcook uses specialized script categories to manage different stages of the machine learning lifecycle. Each category has a specific signature and purpose:

    1. datasource: Used to download data from external sources and provide data access interfaces. It returns a DatasetPool.
    2. dataflow: Used to process data. It takes a DatasetPool as input, performs transformations, and returns a new DatasetPool for the next stage.
    3. model: Used for training and inference. It consumes data from dataflow or datasource scripts to train, validate, and output model files, and provides a prediction interface.
  9. Understand the structure of Pipcook Scripts

    main

    In Pipcook, a Pipeline is defined using scripts. A Pipcook script is a JavaScript file that exports a specific method and is categorized into three distinct types that represent different phases of a machine learning workflow:

    1. datasource: Used to download sample data and provide the data access interface.
    2. dataflow: Used to convert the format of a downloaded dataset into a format acceptable for the subsequent model phase.
    3. model: Used to define the machine learning model, obtain training samples, and evaluate accuracy using the data interface.

    These script types act as user-defined plug-ins that compose to form a complete Pipeline.

  10. Understand the Machine Learning Workflow in Pipcook

    main

    Pipcook uses a Pipeline to describe a machine learning task. A pipeline is composed of different scripts (nodes) connected together. The typical workflow involves:

    1. Datasource: A script that collects samples and processes them into a format for feature learning (e.g., downloading and providing access to a dataset like MNIST).
    2. Dataflow: A script used to transform data, such as resizing images to specific dimensions (e.g., [224, 224]) required by a model.
    3. Model: A script that defines the machine learning model and its training parameters (e.g., using MobileNet with TensorFlow.js).
    4. Training: Running the pipeline to fit the model to the training set.
    5. Testing/Predicting: Using the trained model to evaluate effectiveness or make predictions on unseen data.
  11. Standard Text Dataset Format (CSV)

    main

    Text datasets must be provided as a CSV file without a header. The file must use a comma (,) as the delimiter. The structure requires exactly two columns:

    1. The first column contains the text content.
    2. The second column contains the category name.
    prod1, type1
    prod2, type2
    prod3, type2
    prod4, type1