CodeNewbie Community 🌱

ajayyadav
ajayyadav

Posted on

How to Automate Machine Learning with Images

MLers will already know that ML offers automation that can be used on the server-side thanks to the WhizzML language, designed especially for Machine Learning tasks. Still, it also offers client-side bindings for many programming languages. In this post, we’ll review the Python bindings approach. You can learn online and become masters in Machine Learning to understand Artificial Inteligence and Machine Learnnig concepts.

The File-Field Duality of Images

From the Machine Learning point of view, each image is a source for many fields, like the light levels in some regions, their color information, or the shapes it contains. In that sense, we need to think of an image as a composed field, just as we could think about other composed fields like text or date-time fields. At the same time, all this information is packed in an image file, and, as you probably know, uploading any file to ML generates a Source resource. This will also happen in this case for any uploaded image. But then, how do we manage to work with images as in any other field? Well, simply by allowing Sources to contain other sources as components. That’s why ML now offers Composite Sources.

Creating Composite Sources

The simplest way to create a Composite Source from a list of images is to pack them in a compressed file and upload them as you would upload any file to ML. Of course, if the file is located on your computer, you will need to use some client-side binding to help you automate that.

ML opens the compressed file and creates one Source per image plus a Composite Source containing one image per row. In addition to that, if the images in the compressed file are organized in folders, a new label field is added to each row to store the name of the folder the image was in. Thus, if you upload a compressed file containing two folders: cats and dogs with the corresponding images, the Composite Source will automatically handle labeling for you.

R to be able to use images as one more field in your Datasets, you need to assign other additional information to each image you upload to ML. Let’s start with the simple case of image classification.

To train an image classifier, you’ll need to provide the images and label you want to associate with each of them. The simplest way to do that is to upload a compressed file containing the images and a CSV or JSON file with the annotations. The annotations file should have at least a field that contains the path to the image file, as found in the compressed file, and another field where the label is stored.

Editing Annotations

Probably you’ve experienced the need to change or add new labels to an existing set of images. There are many reasons for that: you did not finish your labeling yet, or you redefined your labels to improve your results. In that case, you should change the contents of the labels associated with the images in your Composite Source. The table+image format will not allow you to alter its contents out of the box, but there’s a different way of creating a Composite Source for annotated images that will:

from ml.api import ML
api = ML()
composite_source = API.create_annotated_source("annotated_images.json")

From Immutability to Interpretability

On top of a good performance, today’s Machine Learning solutions need to provide accountability, interpretability, and reproducibility. The fact that resources in ML are immutable has always ensured those qualities. However, as shown in the previous section, Composite Sources allow some significant editing, like labeling our images. To maintain traceability of the Machine Learning models, we need to block that editing the minute we use the contents of the Source to produce other Machine Learning resources from them, that is when building a Dataset. Once a Dataset is built from it, the Source is automatically closed (its closed attribute is set to True) for editing and accepts no more modifications. If you need to modify a closed Source, you’ll need to clone it.

From ml.API import ML
API = ML()
closed_source = "source/61786d54520f9017b5000005" # this source is closed
open_cloned_source = API.clone_source(closed_source) # this one is open
The resulting Source is an open clone of the original one, ready to be changed again.

As you’ve seen, adding images to ML’s models is an easy task that starts by uploading them as Composite Sources. From then on, images can be analyzed by Deepnets and their Convolutional Neural Networks and by all kinds of Supervised and Unsupervised models, like Clusters or Anomaly Detectors; thanks to ML’s automated feature extraction.

Top comments (0)