Reader small image

You're reading from  Transformers for Natural Language Processing and Computer Vision - Third Edition

Product typeBook
Published inFeb 2024
Reading LevelN/a
PublisherPackt
ISBN-139781805128724
Edition3rd Edition
Languages
Tools
Right arrow
Author (1)
Denis Rothman
Denis Rothman
author image
Denis Rothman

Denis Rothman graduated from Sorbonne University and Paris-Diderot University, designing one of the very first word2matrix patented embedding and patented AI conversational agents. He began his career authoring one of the first AI cognitive Natural Language Processing (NLP) chatbots applied as an automated language teacher for Moet et Chandon and other companies. He authored an AI resource optimizer for IBM and apparel producers. He then authored an Advanced Planning and Scheduling (APS) solution used worldwide.
Read more about Denis Rothman

Right arrow

Hugging Face AutoTrain: Training Vision Models without Coding

Training a machine learning model doesn’t require a college degree anymore. Somebody with no AI or programming knowledge can train a transformer model.

Hugging Face’s AutoTrain requires no coding. You just need to upload your data. AutoTrain will then automatically process the data and choose and train one or several models for your project. You can then deploy the trained model in a few clicks.

You can find similar services on Google AI Platform AutoML, Amazon SageMaker Autopilot, Microsoft Azure Machine Learning, IBM Watson Studio, and a growing number of other platforms.

In less than a few years, anybody can compete with somebody with a Ph.D. in AI who spent years in college. With automated machine learning platforms, a powerful copilot such as ChatGPT, and a credit card, anybody can compete with an AI engineer.

This chapter will show how anybody can train a vision transformer with absolutely...

Goal and scope of this chapter

AI systems have become more automated but also more complex. This chapter aims to point out the expertise a non-AI user may require in some cases from an AI expert to get the job done.

This chapter describes the main steps of how to implement Hugging Fae AutoTrain for an image classification task on CIFAR-10 transportation images.

The scope remains to implement an automated training process on a cloud platform.

While going through the process, we will point out some, not all, of the issues that may come up to show the role of an AI professional in the ever-changing workplace.

Before we begin, we will go through some key things to note:

  • Through hard work and creativity, Hugging Face managed to make transformers accessible. They also deserve credit for anticipating transformer technology’s huge success years before it reached mainstream users with ChatGPT.
  • The goal of this chapter is not to oppose college graduate...

Getting started

The Hugging Face platform is continuously upgraded and modified to adapt to the AI market. The interfaces are constantly evolving through competition and user feedback. As such, this section describes the process and provides the links to get the job done. Focus on the processes. The interface will continuously evolve. Modern AI requires adaptability and continuous learning.

Make sure to read Hugging Face’s auto-training conditions and the cost of this service before proceeding to activate anything on the platform.

To get started, first, go to Hugging Face’s AutoTrain platform: https://huggingface.co/autotrain.

You will see that Hugging Face insists on creating “powerful models without code.”

Make sure to follow the instructions that will also evolve but are worth the time it takes. Make sure to obtain your Hugging Face token.

The AI professional sees the opportunity to save time and energy. The non-AI professional...

Uploading the dataset

First, click on Upload Training File(s) as shown in Figure 18.2:

A close-up of a cloud  Description automatically generated

Figure 18.2: Uploading the training dataset

The interface may evolve, but you will need to upload data. You will need to read the documentation on the Hugging Face platform carefully to prepare your data. The interface evolves constantly to follow the cutting-edge AI market. Choose your method but remain focused on the task: loading data.

You will need to follow Hugging Face’s procedures for data formatting: https://huggingface.co/docs/autotrain/image_classification.

Make sure to read the upgrades regularly. Again, it is worthwhile!

In this case, we are loading CIFAR-10 images as shown in the Figure 18.3 excerpt:

A collage of different images of airplanes and cars  Description automatically generated

Figure 18.3: Excerpt of CIFAR-10 transportation images

The CIFAR-10 images in this chapter are from Learning Multiple Layers of Features from Tiny Images, Alex Krizhevsky, 2009:https://www.cs.toronto.edu/~kriz/learning-features-2009-TR...

Training models with AutoTrain

When we activate the training process, Hugging Face first downloads our data from the Hugging Face Hub, where we uploaded our dataset.

Hugging Face’s AutoTrain interface provides a rapid, seamless, and intuitive approach to training text and vision models. Just click on Start Training when you are ready, as shown in Figure 18.8:

A blue rectangle with white text  Description automatically generated

Figure 18.8: AutoTrain: an intuitive approach

Hugging Face AutoTrain’s module acts as an overall controller for your training process. For more, read Hugging Face’s documentation, which evolves constantly as the platform makes progress: https://huggingface.co/docs/autotrain/main/en/index#who-should-use-autotrain.

Once a model is trained, we can train other ones to create a trained model set of potential candidates for our project.

Reminder: Evaluate the cost of training before running a training process. Budget management remains a critical factor in training AI models.

...

Deploying a model

Make sure to read the latest continuously evolving documentation and explore the latest Hugging Face interfaces. The interface may change, but you will have to create a model card for your model.

We can go to our profile and choose the model card of one of our trained models to access the settings of the model and decide how to deploy it, as shown in Figure 18.11:

A screenshot of a computer  Description automatically generated

Figure 18.11: Model card of a trained model

We can choose a deployment method: deploy on a platform such as AWS, for example, to create an endpoint or run the model directly in transformers. In this chapter, we will run the models by using transformers directly.

We first need to make each model public by going to the model card, then Settings, making the model public, as shown in Figure 18.12:

A screenshot of a computer  Description automatically generated

Figure 18.12: Making a Hugging Face model public

Clicking on Make public will make it public (Figure 18.13):

A close up of a text  Description automatically generated

Figure 18.13: Model visibility status

We will now use the...

Running our models for inference

The trained models can now perform image classification with validation images. In this section, we will run several trained models.

Open the Hugging_Face_AutoTrain.ipynb that we will use in this section to:

  • Retrieve a relatively easy image and a challenging one.
  • Classify the validation images.
  • Analyze the difficulty of image classification.
  • Investigate the configuration of the trained models.

We will begin by retrieving the validation images.

Retrieving validation images

The notebook first imports IPython for media rendering:

from IPython.display import Image     #This is used for rendering images in the notebook

The first image is relatively easy to classify: generate_an_image_of_a_car_in_space.jpg. This image, which we will now download, was classified in Chapter 16, Beyond Text: Vision Transformers in the Dawn of Revolutionary AI, by a vision transformer:

#Development access to delete...

Summary

This chapter brought us to the frontier of AI, where automation rages. Competitors around the world are struggling to make AI accessible to mainstream users. OpenAI’s ChatGPT opened the door to a flood of automated tasks.

Hugging Face has successfully deployed numerous automated functions on their platform, made transformers easy to run, and provided many other productive functions. Hugging Face AutoTrain provides a no-coding service to train and deploy AI models.

We began by creating an image classification project with a Hugging Face Space. We uploaded a CIFAR-10 dataset with transportation images.

We then continued by running the models with a difficult and an easy image of cars. We first built an API function to query the models and an output processing function to display the scores produced.

The models explored were ViT, Swin, BEiT, ConvNext, and ResNet. The chapter notebook displayed each model’s configuration, accompanied by a brief model...

Questions

  1. Hugging Face AutoTrain can train every vision transformer on the market. (True/False)
  2. Datasets are always easy to create. (True/False)
  3. A no-coding AI system requires no machine learning knowledge. (True/False)
  4. Hugging Face AutoTrain can classify any image submitted. (True/False)
  5. Even a well-prepared dataset can be insufficient. (True/False)
  6. Creating a validation set of images to test a vision transformer is useful. (True/False)
  7. Even a well-trained model can lead to overfitting. (True/False)
  8. It may take months to optimize a vision transformer. (True/False)
  9. An automated service can sometimes work well with no coding. (True/False)
  10. An AI professional will always be necessary for complex AI issues. (True/False)

Further reading

Join our community on Discord

Join our community’s Discord space for discussions with the authors and other readers:

https://www.packt.link/Transformers

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Transformers for Natural Language Processing and Computer Vision - Third Edition
Published in: Feb 2024Publisher: PacktISBN-13: 9781805128724
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Denis Rothman

Denis Rothman graduated from Sorbonne University and Paris-Diderot University, designing one of the very first word2matrix patented embedding and patented AI conversational agents. He began his career authoring one of the first AI cognitive Natural Language Processing (NLP) chatbots applied as an automated language teacher for Moet et Chandon and other companies. He authored an AI resource optimizer for IBM and apparel producers. He then authored an Advanced Planning and Scheduling (APS) solution used worldwide.
Read more about Denis Rothman