How to Run Diffusion Models with diffusers.js

Dec 13, 2020 | Data Science

Are you excited about leveraging the power of diffusion models on your GPU using diffusers.js? This article will guide you through the installation, usage, and troubleshooting of this cutting-edge library, ensuring you’re well-equipped to create stunning images with ease!

Installation

To get started, you need to install the library. Open your terminal and run the following command:

bash
npm i @aislamov/diffusers.js

Usage

Now that you’ve installed the library, let’s see how to use it in both browser and Node.js environments.

Usage in the Browser (React)

Follow these steps to use diffusers.js in the browser:

  1. Import the `DiffusionPipeline` from the library:
  2. js
    import DiffusionPipeline from '@aislamov/diffusers.js';
    
  3. Load the pre-trained model:
  4. js
    const pipe = DiffusionPipeline.fromPretrained('aislamov/stable-diffusion-2-1-base-onnx');
    
  5. Run the pipeline with your chosen prompt:
  6. js
    const images = pipe.run({
      prompt: 'an astronaut running a horse',
      numInferenceSteps: 30,
    });
    
  7. Render the image on a canvas:
  8. js
    const canvas = document.getElementById('canvas');
    const data = await images[0].toImageData({
      tensorLayout: 'NCWH',
      format: 'RGB',
    });
    canvas.getContext(2d).putImageData(data, 0, 0);
    

Usage in Node.js

For Node.js, follow a similar approach:

  1. Import the required libraries:
  2. js
    import DiffusionPipeline from '@aislamov/diffusers.js';
    import { PNG } from 'pngjs';
    
  3. Load the model and run the pipeline:
  4. js
    const pipe = DiffusionPipeline.fromPretrained('aislamov/stable-diffusion-2-1-base-onnx');
    const images = pipe.run({
      prompt: 'an astronaut running a horse',
      numInferenceSteps: 30,
    });
    
  5. Process the resulting image data and save it:
  6. js
    const data = await images[0].mul(255).round().clipByValue(0, 255).transpose(0, 2, 3, 1);
    const p = new PNG({ width: 512, height: 512, inputColorType: 2 });
    p.data = Buffer.from(data.data);
    p.pack().pipe(fs.createWriteStream('output.png')).on('finish', () => {
      console.log('Image saved as output.png');
    });
    

Understanding the Code with an Analogy

Imagine you’re a chef cooking a delightful dish. The DiffusionPipeline is like your cooking pot, where you’ll combine various ingredients (model configurations) to prepare a meal (generate images). First, you gather your ingredients:

  • The model (like a recipe) guides how the dish will taste (how the image appears).
  • Your prompt is like the flavor you want to add–in this case, “an astronaut running a horse.”

By running the pipeline, you’re essentially cooking your dish in the pot, and when it’s done, you serve it on a canvas (the plate), displaying your masterpiece for everyone to enjoy!

Troubleshooting

While running diffusion models, you may encounter a few common issues. Here are some troubleshooting tips:

  • If you receive GPU-related errors, ensure you have the required CUDA or DML WebGPU support. You will need the cpu revision on machines without GPU support:
  • js
    const pipe = DiffusionPipeline.fromPretrained(
      'aislamov/stable-diffusion-2-1-base-onnx',
      { revision: 'cpu' }
    );
    
  • Double-check that all dependencies are correctly installed in your project.
  • For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

If you encounter any issues related to the configuration or need guidance on GPU settings, consider looking into the ONNX runtime GitHub for updates or relevant fixes.

Conclusion

At fxis.ai, we believe that such advancements are crucial for the future of AI, as they enable more comprehensive and effective solutions. Our team is continually exploring new methodologies to push the envelope in artificial intelligence, ensuring that our clients benefit from the latest technological innovations.

Now you’re set to create your own stunning images with diffusion models using the diffusers.js library! Happy coding!

Stay Informed with the Newest F(x) Insights and Blogs

Tech News and Blog Highlights, Straight to Your Inbox