Getting Started with ONNX-Scala: A Step-by-Step Guide

Oct 8, 2023 | Data Science

In the ever-evolving world of AI and machine learning, ONNX (Open Neural Network Exchange) provides a framework for interoperability among different AI models. This blog post aims to guide you through the setup and basic usage of the ONNX-Scala library, an intriguing blend of Scala 3 and the ONNX framework.

Setting Up Your Project

To kickstart your journey with ONNX-Scala, the first step is to integrate it into your Scala project. Simply add the following line to your project’s build.sbt file:

libraryDependencies += org.emergent-order %% onnx-scala-backends % 0.17.0

Downloading the Model File

Before you can make predictions using the SqueezeNet model, you need to download the model file. This can be achieved using the following link:

SqueezeNet Model File

Creating an Image Tensor

Once you have set everything up, we’ll delve into creating an image tensor. Think of the image tensor as a canvas where we fill it up with color values. In this case, we’re painting our canvas with the pixel value of 42.

import java.nio.file.Files, Paths
import org.emergentorder.onnx.Tensors._
import org.emergentorder.onnx.backends._

val squeezenetBytes = Files.readAllBytes(Paths.get("squeezenet1_1_Opset18.onnx"))
val squeezenet = new ORTModelBackend(squeezenetBytes)
val data = Array.fill(1 * 3 * 224 * 224)(42f) // Filling our canvas

val shape = 1 #: 3 #: 224 #: 224 #: SNil
val tensorShapeDenotation = Batch ##: Channel ##: Height ##: Width ##: TSNil

val imageTens = Tensor(data, tensorShapeDenotation, shape)

Here, the image tensor is like preparing a large canvas (224×224) where each pixel represents a color. In our case, we filled every pixel with a value of 42.

Running Inference

Next, we will run the SqueezeNet model to classify our image tensor. This is akin to asking a trained artist (the model) to identify what she sees on the canvas we provided.

val out = squeezenet.fullModel[Float, ImageNetClassification, Batch ##: Class ##: TSNil, 1 #: 1000 #: SNil](Tuple(imageTens))
val data = out.data.unsafeRunSync()
val res1: Int = data.indices.maxBy(data)

The output shape and predicted class unveil the result of our artist’s interpretation. The model predicts the class of the image, providing us with the highest scoring class, which in this case may just be a radiator!

Troubleshooting Common Issues

  • Model Not Found: Ensure that the SqueezeNet model file is correctly downloaded and the path provided is accurate.
  • Incorrect Configuration: Double-check that your Scala version is compatible with ONNX-Scala; ensure you’re using Scala 3 (Dotty).
  • Runtime Errors: If you encounter untyped input failures, it might be beneficial to wrap your tensor creation and model invocation with type-safe checks.

For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.

Conclusion

With this guide, you are well on your way to leveraging ONNX-Scala for deep learning applications. This journey through creating and running your first model showcases the potential of combining functional programming with AI.

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.

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

Tech News and Blog Highlights, Straight to Your Inbox