Image Classification Computer Vision with Hugging Face Transformers -Google ViT - Python ML Tutorial

1littlecoder · Beginner ·👁️ Computer Vision ·5y ago

Key Takeaways

Performs image classification using Hugging Face Transformers and Google ViT

Full Transcript

dear fellow coders welcome to one little coder if you ask any nlp developer who uses hugging face a lot and you ask them like what do you want from hugging face or what do you like about hugging face and that you know you don't have somewhere else that is usually hugging face transformers and computer vision so an intersection of hugging phase transformers and computer vision is something a lot of nlp developers actually are or i should say machine learning developers who use hugging face a lot is something that they dream about and that dream is true today or probably i should say yesterday because hugging face now has a computer vision model it's called vision transformer and then it's also integrated as part of your existing transformer the advantage is you use the same api you use the same syntax and then now you can actually build a very good computer vision model using hugging face so hugging phase the company that is known for nlp has got a vision transformer which is for computer vision and this video is going to be about that in this video we're going to see how to use hugging fish hosted inference api for computer vision problem which in this case is image classification it's going to basically find out what the image is and then we're going to move into google collab notebook to see how to write a python code for the same thing so first let's get started so this is the page where the hugging face model is listed so this to be honest this is actually the model from google and it's on hugging face model hub so this model which is uh vit it stands for vision transformer it's a pre-trained model on you know image net 21k like 14 million images um with eight hundred 21 forty three classes at a resolution of two hundred and twenty four by two hundred and twenty four and it's also fine tuned on uh another one million images with a thousand classes at a resolution of two to four by two to four so it was introduced in this paper that uh that is listed here and this model uh this this pre-trained model you can actually use it within transformers just uh like you know how you would use your nlp libraries like gpd2 um gpt neo or any other nlp library so we're going to use it in the same way and same steps so and we would actually can now use hugging phase transformer and image classification free trained model and actually predict what image is this so first let's start with the hosted inference api so i'm a big fan of hugging fish hosted inference api primarily for the reason because this works on phone and then you can test how something works before even you know you spend um uh gbs uh downloading a model like um if you're doing it on google cola maybe it's a different topic altogether but if you're going to do it on your local machine it takes a lot of time um so if you if you if you want to test a model so this is an amazing option and if you are somebody who uploads a lot of model to hugging face model hub my request to you is make sure that your model works with hosted inference api so let's get started so what i'm going to show you is i have an image like a car and then i'm going to upload that car here so let me drag and drop it here and then after i drag and drop you can see that it is computing and after it computes it says it's a sports car with 0.845 uh probability um or score i should say and it also calls it convertible and then probably like 0.03 car wheel but what what stands out is that it has got an 84 percent probability or score to say that this is a sports car which means it actually works very nice one thing that you should probably understand is um we don't know whether this image was actually part of the training image highly unlikely it was not what if maybe this model was not even available when uh this image was uh this pre-trained model was created and that is that is the beauty of the entire transfer learning um world right so you like even if this image was not available there so still it works fine if you want to fine tune it for a different problem still it works fine so yeah that is that is all the greatest thing about it now that we actually see that this model works fine and um you know we we have tested it with the hosted inference api from hugging face model hub let's quickly move on to the google collab and then see how to actually do it uh on your python environment so the first step is you need to install the latest version of uh hugging face model latest version of transformers from let me put actually hugging face model hub okay so that's the first step and your next step is uh import import required libraries so you need transformers for um actually creating uh the model download sorry downloading the model uh doing a training doing classification and then finally predicting so you need a transformers va feature extractor va v80 feature extractor and v80 for uh for image classification these two classes from there and then you need image from pll this is primarily to process image pll if you are not familiar with pl is the python 3 fork or i should say like pillow is the python 3 fork of pil and once you install pillow you would get pl and then finally you need a request because we are actually trying to download the image from internet which we are going to use for classification so because of that like if you only want somebody to upload an image and then do it you may not require a request but because we are going to take a url from a user and then download that image from internet and then figure out what image it is like using classification that's why we need request after you've got all these three libraries next is define um in download image from the given url so we given you we give a url and from that url we actually can you know download the image so this is like the first step is you don't give the url the second step is you actually download the image and store it in image and the next one is i'm sorry the next one is display the image display the image the i should say downloaded image i mean it doesn't really matter a lot because what i'm saying you're actually listening to me but in case if you or just reading if somebody you've shared this google color notebook please please feel free to share this google collab notebook which i would link in the youtube description with anybody you like the whole idea is uh everybody should enjoy the way hugging face uh you know it's developing uh their open source libraries amazing contribution from the team once again you know heartfelt thanks from a fan of hugging face for whatever they are doing whatever they have been doing with nlp and then what they're going to do for computer vision i'm truly amazed by the team so let's display the image okay it's a cat or cats cute cats okay so i don't know what type of cat it is probably like maybe machine learning will teach me but i don't have that much knowledge about cat so i'll just simply call it cat or cute cats once you have that thing now it's time for us to download pre-trained pre-trained models or model so we have a feature extractor uh it's very similar to any hugging face uh or transformer model uh that you have used so if you have seen my recent videos about gpt 2 gpt neo you would have seen a very similar fashion so you would have a tokenizer in nlp world and then here we have a feature extractor and then we have uh the actual model so feature extractor instead of tokenizer and then you have uh the model so once you download uh or assign these two into the respective objects then the next step that you're going to do is you're going to extract features from the input model that you have extract features which is what we're going to pass on to the model for prediction so now model i should say maybe like inference oops and from that you are going to actually calculate the logits or just take the logits outside and then pass on that logit uh that your model has given you to predict so like we saw initially that uh there are this this model is uh fine-tuned on thousand classes and we are going to pass it to one of those like we are going to we are trying to find out which of these thousand classes come very closer to the one that has been predicted so that's what we're going to do so uh yeah i'm not going to say it says like model predicts so text model predicts one of the thousand image net classes so i think i've ran this okay i didn't run this run it run it run it run it so let's see so it says it's an egyptian cat so i don't know if it is an egyptian cat but uh but it says it's an egyptian cat so let's try with something that i understand or probably like you also understand which is a beautiful laptop so what is in this url is instead of this url i'm going to give this url downloading the image successfully done now display the image and this is quite the high resolution image um i don't know why did i pick up a very high resolution image but yeah this is a laptop that all of us almost are familiar with um a macbook but again um it's a laptop right so the model download we don't have to do it again because we are going to just do inference and we have to do feature extraction again model inference get the logits and then try to find the class and it says it's a notebook or a notebook computer and that is exactly right because it's a notebook probably like uh i think in the future we would probably would love to have a class called a macbook because we are a different class altogether but uh this does its job so if you want to try something something else as well uh i don't know what what object we can try like maybe like white house let's see take white house white house white house okay um and then which image like i don't want a very high resolution image but all the images that i've got are very high resolution probably this looks okay copy image address and then go to the notebook and then paste it probably like this okay let's see if it works does it actually display the image i think it didn't download it properly maybe i might have to display the image oh it's uh i think that's something okay i did a mistake because this this image if you see the resolution of this image it is 4206 by 2200 so it's a huge image i don't know what's going to happen let's see let's extract some features from it model inference get the logit and then do the prediction and it says it's a palace okay probably american president lives in a palace so and that palace is called white house and we know that it is white house and that desert so in a very few lines of code that almost all nlp developers are already familiar with which is a very popular hugging face in tags now you can actually do a very good computer vision so either you can either either you can um create a streamled web application using this or you can have a cli tool or you can just you know like do hand like you you can use it for any any number of purpose um that that you would like to you know use it for so this video was supposed to tell you how to do computer vision using hugging face with google's vision transformer and the task that we did is image classification but what hacking face is saying is that you can actually use uh this for if if you have uh like this model fine tuned for some other task other than image classification you can use it for that purpose as well but if you want to try out with any image please try to use hugging face hosted inference api but um please do not kill their server so if you want to use it for experiment purpose try to download the model and test it out yourself so that you are not overloading the server and once again a huge respect and thanks to the hugging face team for uh for actively developing and then releasing open source product this means a lot for the open source community machine learning community and especially for somebody like me who's an applied uh i would say data scientist i'm not doing research i'm not you know going um going and you know creating state of the art model but if somebody creates a state-of-the-art model people like hugging fish get it to me which i can actually put it in good use in an industry setup and thanks to hacking face for that and if you have any questions in this video please let me know in the comment section otherwise please give us a shout out to hugging face team for the amazing work that they're doing and also please give me a thumbs up i would really appreciate that thank you so much for watching this video see you on the next one take care

Original Description

In this Python tutorial, You'll learn how to use the very latest Hugging Face model (on Model Hub)- Computer Vision Vision Transfomers (ViT Model from Google) to do Image Classification using Hugging Face Transformers (all in Transformers api). Hugging Face is quite popular for NLP but this time it's about Computer Vision and Image Classification. This video introduces the latest ViT Model from Hugging Face Model Hub Demonstrates ViT Image Classification with Hosted Inference API Google Colab Python Code Walk through for Image Classification with Hugging Face Transformers With Computer Vision from Hugging Face Transformers, I hope more things to come! Colab Code - https://colab.research.google.com/drive/1evEipae2Oh6Ogv_0sNWKJTAZ4LJBQ9y_?usp=sharing Hugging Face ViT - https://huggingface.co/google/vit-base-patch16-224 Vision Transformer (ViT) model pre-trained on ImageNet-21k (14 million images, 21,843 classes) at resolution 224x224, and fine-tuned on ImageNet 2012 (1 million images, 1,000 classes) at resolution 224x224. It was introduced in the paper An Image is Worth 16x16 Words: Transformers for Image Recognition at Scale by Dosovitskiy et al. and first released in this repository. However, the weights were converted from the timm repository by Ross Wightman, who already converted the weights from JAX to PyTorch. Credits go to him.
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from 1littlecoder · 1littlecoder · 0 of 60

← Previous Next →
1 How to create your Free Data Science Blog on Github with Fastpages from Fastai
How to create your Free Data Science Blog on Github with Fastpages from Fastai
1littlecoder
2 Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
Making Interactive Matplotlib Plots for Data Science Visualizations on Jupyter (Python)
1littlecoder
3 Create your first Data Science Web App using R Shiny
Create your first Data Science Web App using R Shiny
1littlecoder
4 How to create a Reproducible Example in R using reprex
How to create a Reproducible Example in R using reprex
1littlecoder
5 No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
No Code Visualization using esquisse with Tableau-like Drag and Drop GUI in R
1littlecoder
6 Scrape HTML Table using rvest and Process them for insights using tidyverse in R
Scrape HTML Table using rvest and Process them for insights using tidyverse in R
1littlecoder
7 Google Teachable Machine Learning Build No Code AI solution
Google Teachable Machine Learning Build No Code AI solution
1littlecoder
8 Create meaningful fake tidy datasets in R using fakir [#rstats Package]
Create meaningful fake tidy datasets in R using fakir [#rstats Package]
1littlecoder
9 How to enable using R Programming with Visual Studio VS Code
How to enable using R Programming with Visual Studio VS Code
1littlecoder
10 Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
Python, Community, Books - with Abhiram R - Bangpypers Co-organizers | 1littlecoder podcast
1littlecoder
11 Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
Growing a Tech Community across India - Anubha Maneshwar, Founder Girlscript | 1littlecoder Podcast
1littlecoder
12 Intro to Google Colab - How to use Colab
Intro to Google Colab - How to use Colab
1littlecoder
13 Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
Intro to Plotly Express - Complex Interactive Charts with One-Line of Python Code
1littlecoder
14 Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
Indic NLP Python Toolkit Open Source Development - iNLTK Creator Gaurav Arora | 1littlecoder Podcast
1littlecoder
15 Do you want a career in Data Science - Tamil Webinar
Do you want a career in Data Science - Tamil Webinar
1littlecoder
16 Android Smartphone Analysis in R [Live Coding Screencast]
Android Smartphone Analysis in R [Live Coding Screencast]
1littlecoder
17 Programmatically create Images, Memes, Watermarks using Python with imgmaker
Programmatically create Images, Memes, Watermarks using Python with imgmaker
1littlecoder
18 Kaggle Walkthrough to get you started with Data Science - Webinar
Kaggle Walkthrough to get you started with Data Science - Webinar
1littlecoder
19 Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
Community, Corporate Job, Coding - Gnana Lakshmi T C aka Gyan, WomenWhoCode Leadership Fellow
1littlecoder
20 Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
Easy ggplot2 Theme Customization with {ggeasy} | Data Visualization in R
1littlecoder
21 Excel to R - Pivot + Bar Chart in Excel  & R using tidyverse [Live Coding]
Excel to R - Pivot + Bar Chart in Excel & R using tidyverse [Live Coding]
1littlecoder
22 Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
Excel to R #2 - VLOOKUP in Excel to LEFT_JOIN, MERGE in R
1littlecoder
23 5 websites to get Free Real-World Datasets for Data Science/ML Projects
5 websites to get Free Real-World Datasets for Data Science/ML Projects
1littlecoder
24 Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
Excel to R #3 - APPROXIMATE VLOOKUP in Excel to FUZZY LEFT_JOIN in R
1littlecoder
25 Correlation-alternative PPS (Predictive Power Score) Python Package Demo
Correlation-alternative PPS (Predictive Power Score) Python Package Demo
1littlecoder
26 Automated Website Screenshots in R using {webshot}
Automated Website Screenshots in R using {webshot}
1littlecoder
27 Installing Custom RStudio Theme (Synthwave85)
Installing Custom RStudio Theme (Synthwave85)
1littlecoder
28 Analyse Google Trends Search Data in R using {gtrendsR}
Analyse Google Trends Search Data in R using {gtrendsR}
1littlecoder
29 3 Tips to ask question on Stack Overflow the right way to get answers
3 Tips to ask question on Stack Overflow the right way to get answers
1littlecoder
30 Learn Data Science with R - Mini Projects - Web Scraping Zomato
Learn Data Science with R - Mini Projects - Web Scraping Zomato
1littlecoder
31 Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
Easily make Dumbbell Chart using {ggcharts} | Data Visualization in R
1littlecoder
32 GET Hackernews Front Page Results using REST API in R
GET Hackernews Front Page Results using REST API in R
1littlecoder
33 Quickly deploy ML WebApps from Google Colab using ngrok
Quickly deploy ML WebApps from Google Colab using ngrok
1littlecoder
34 Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
Use Jupyter Notebooks within VSCode (Visual Studio Code) in 2020
1littlecoder
35 Plotly Interactive Plots as Pandas Plotting Backend df.plot()
Plotly Interactive Plots as Pandas Plotting Backend df.plot()
1littlecoder
36 Stack Overflow Developer Survey 2020 Highlights for New Programmers
Stack Overflow Developer Survey 2020 Highlights for New Programmers
1littlecoder
37 Matplotlib Animation Charts in Python using Celluloid
Matplotlib Animation Charts in Python using Celluloid
1littlecoder
38 Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
Coding, Postwoman, Passion Project Book - Liyas Thomas Open Source Developer - 1littlecoder podcast
1littlecoder
39 Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
Aspiring Data Scientist, Tips on How to learn Business Domain Knowledge
1littlecoder
40 Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
Bokeh Interactive Charts as Pandas Plotting Backend df.plot_bokeh()
1littlecoder
41 Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
Easy Fast Python Pandas Summary with Sidetable | Pandas Tips & Tricks
1littlecoder
42 Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
Inception, Content Ideas, Consistency - Srivatsan Srinivasan AIEngineering YouTube Content Creator
1littlecoder
43 ggplot2 Text Customization with ggtext | Data Visualization in R
ggplot2 Text Customization with ggtext | Data Visualization in R
1littlecoder
44 Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
Penguins Dataset Overview - iris alternative | EDA Data Visualization in R
1littlecoder
45 YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
YouTube Growth Tips, Content Creation - Bhavesh Bhatt, YouTuber (Data Science & Machine Learning) #7
1littlecoder
46 Matplotlib Animated Bar Chart Race in Python | Data Visualization
Matplotlib Animated Bar Chart Race in Python | Data Visualization
1littlecoder
47 Simple Python GUI Development using {guietta}
Simple Python GUI Development using {guietta}
1littlecoder
48 #8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
#8 Niche, Growth, Monetization - David Langer - YouTuber Dave on Data
1littlecoder
49 Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
Simple Fast 3-step Python OCR using Deep Learning 40+ Languages
1littlecoder
50 Github New Feature Profile Summary/Mini-Resume - Profile Views
Github New Feature Profile Summary/Mini-Resume - Profile Views
1littlecoder
51 Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
Otto ML Assistant, GPT-3 on Philosophers, Nvidia-ARM - 3 ML Tech News
1littlecoder
52 What is OpenAI GPT-3 - Hype, Examples, Worries
What is OpenAI GPT-3 - Hype, Examples, Worries
1littlecoder
53 Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
Julia 1.5, Datamuse API, Live HDR+ Pixel 4a - Machine Learning Tech News
1littlecoder
54 Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
Self-driving Car Engineer sentenced, arXiv Dataset, AI/ML Startup Idea - Machine Learning Tech News
1littlecoder
55 GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
GPT-3 Explorer, Ciphey (Automated Decryption), Py-Sudoku - ML Tech News
1littlecoder
56 How to use Advanced Google Search to extract Email Ids from Linkedin
How to use Advanced Google Search to extract Email Ids from Linkedin
1littlecoder
57 Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
Cartoonizer Toon-IT (AI Web App), GPT-3 Advice, Android Earthquake Detection - ML Tech News
1littlecoder
58 Flow - R Package to visualize code logic, functions as a Flow Diagram
Flow - R Package to visualize code logic, functions as a Flow Diagram
1littlecoder
59 Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
Build GPT-3-like Language Model on Google Colab with minGPT [PyTorch]
1littlecoder
60 Create a Pencil Sketch Portrait with Python OpenCV
Create a Pencil Sketch Portrait with Python OpenCV
1littlecoder

Related Reads

Up next
9-Phase Computer Vision Roadmap 2026 | AI & Deep Learning | #shorts
SCALER
Watch →