How to Use Mask2Former for Universal Image Segmentation

A beginner's guide to utilizing AI for accurate image segmentation, and finding similar models on Replicate Codex

How to Use Mask2Former for Universal Image Segmentation
Using AI to find out what's in an image with mask2former!

There's a world of possibilities when it comes to image segmentation, and Mask2Former is here to help you unlock them. In this guide, I'll walk you through using this amazing AI model for universal image segmentation. I'll show you how to understand its inputs and outputs, and how to interact with it using code. The model is ranked highly on Replicate Codex, and we'll also see how we can use this platform to find similar models and decide which one we like. Let's begin.

About the Mask2Former Model

Mask2Former, developed by Facebook Research, is an AI model designed for universal image segmentation. It helps you segment images with great precision, and its applications range from object detection to image editing. You can learn more about the model's specifications on its detail page.

Understanding the Inputs and Outputs of the Mask2Former Model

Before we dive into using Mask2Former, let's take a moment to understand its inputs and outputs.


Mask2Former requires only one input:

  • image file: This is the input image for segmentation. The output will be the concatenation of Panoptic segmentation (top), instance segmentation (middle), and semantic segmentation (bottom).


The output schema of the Mask2Former model is as follows:

  "type": "array",
  "items": {
    "type": "object",
    "properties": {
      "file": {
        "type": "string",
        "format": "uri",
        "x-order": 0
      "text": {
        "type": "string",
        "x-order": 1
  "x-cog-array-type": "iterator"

Now that we have a better understanding of the inputs and outputs, let's move on to actually using the model.

A Step-by-Step Guide to Using the Mask2Former Model

Interact with the model's demo on Replicate

If you're not up for coding, you can interact directly with the model's "demo" on Replicate via their UI. You can use this link to interact directly with the interface and try it out! This is a nice way to play with the model's parameters and get some quick feedback and validation. If you do want to use coding, this guide will walk you through how to interact with the model's Replicate API.

Step 1: Install the Node.js client

First, you'll need to install the Node.js client:

npm install replicate

Step 2: Authenticate with your API token

Next, copy your API token and authenticate by setting it as an environment variable:

export REPLICATE_API_TOKEN=[token]

Step 3: Run the model

Now, you can run the model using the following code:

import Replicate from "replicate";

const replicate = new Replicate({
  auth: process.env.REPLICATE_API_TOKEN,

const output = await
    input: {
      image: "your_input_image_here"

Step 4: Set up a webhook (optional)

You can specify a webhook URL to be called when the prediction is complete. This can be useful if you want to receive updates asynchronously. Here's an example of how to set up a webhook:

const prediction = await replicate.predictions.create({
  version: "97c0c2edeeb7c120c2859dca4fdee58d185131f79c857ba519e3a5cb7cdd7c66",
  input: {
    image: "your_input_image_here"
  webhook: "",
  webhook_events_filter: ["completed"]

For more information, take a look at the webhook docs on Replicate.

Taking it Further - Finding Other Image Segmentation Models with Replicate Codex

Replicate Codex is a fantastic resource for discovering AI models that cater to various creative needs, including image segmentation. It's a fully searchable, filterable, tagged database of all the models on Replicate, and also allows you to compare models and sort by price or explore by creator. It's free, and it also has a digest email that will alert you when new models come out so you can try them.

If you're interested in finding similar models to Mask2Former...

Step 1: Visit Replicate Codex

Head over to Replicate Codex to begin your search for similar models.

Use the search bar at the top of the page to search for models with specific keywords, such as "image segmentation" or "object detection." This will show you a list of models related to your search query.

Example image segmentation options on ReplicateCodex.

Step 3: Filter the Results

On the left side of the search results page, you'll find several filters that can help you narrow down the list of models. You can filter and sort by models by type (Image-to-Image, Text-to-Image, etc.), cost, popularity, or even specific creators.

By applying these filters, you can find the models that best suit your specific needs and preferences. For example, if you're looking for an image segmentation model that's the most popular, you can just search and then sort by popularity.


In this guide, we explored the power of the Mask2Former model for universal image segmentation and how to interact with it using code. We also discussed how to leverage the search and filter features in Replicate Codex to find similar models and compare their outputs, allowing us to broaden our horizons in the world of AI-powered image segmentation.

I hope this guide has inspired you to explore the creative possibilities of AI and bring your imagination to life. Don't forget to subscribe for more tutorials, updates on new and improved AI models, and a wealth of inspiration for your next creative project. You can also follow me on Twitter for more AI-related content. Happy image segmenting and exploring the world of AI with Replicate Codex!

Subscribe or follow me on Twitter for more content like this!