Image Annotation with LLava & Ollama

Sam Witteveen · Beginner ·🧠 Large Language Models ·2y ago

Key Takeaways

The video demonstrates image annotation using LLava and Ollama with LLaMA 1.6 models, showcasing the ability to convert PNGs to bytes, customize prompts, and utilize 13 billion and 34 billion parameter models for improved image understanding. The tutorial also covers using LLaMA for image description, incorporating OCR for text detection, and processing images with streaming responses.

Full Transcript

okay in the last video I talked about some of the updates around llama and one of the things I talked about was that you can build a lot of little apps that actually do things on your local computer and so one of the ones I mentioned was about screenshots so people asked about that and in this video I'm going to go through show you how to do it I'm going to talk about the basics of what's going on it's very simple and at the end I'll talk about a different version that I'm using personally which is a more advanced version on this okay so the whole idea of this is that basically we've got a folder which we do screenshots for right most of us on our computers have a folder where we save automatically save screenshots and over time that folder gets very big or at least if you're me that gets very big and often I want a screenshot that I did a while back at a certain point in time and stuff like that but the only way to go through that is to actually go through and look at each of the screenshots and while that works one of the things you can do I'm going to show you here is that you can get one of the Vision language models to automatically annotate or write captions about the images which will make it easier for you to find the image later on so in this video I'm just going to show you a basic version of how to create the annotations using olama using the lava 1.6 model and then perhaps in another video I'll show you how to sort of integrate this with a rag system so that you can actually just use uh you know Q&A to basically query your screenshots and and get the the answers back that way all right so let's jump in and actually look at sort of a diagram of how this all works it's actually very simple I really we've got a folder where we've got our screenshots so the first code we're going to basically have is just something that goes to the folder and gets a list of the files out and then basically sorts those so that we've got them in some kind of order to basically use the next thing up is like the main logic of this and this is really just going to basically load up the file in my case it's loading up pngs you could set it to load up different things but in my case because it was pngs I I found that ama didn't seem to process the pgs it was more used to jpegs but what you can do is just convert the file to bytes and then it can handle bytes quite easily in here so basically I load up the file I convert it to bytes I then send it to the lava 1.6 model now here there's a few different models that you can use you can use the basic 7 billion parameter model and you'll get okay results out of that you can go up to the 13 billion parameter model which is the one I'm going to show you here and then you can also go up to the 34 billion parameter model so in my experimenting the thing that I found is that the 7 billion parameter model will often miss things that are really obvious and it can do a really good job on some images and not you know as great on other images the 13 billion paramet model is definitely better at sort of having a bit more understanding over the image and stuff like that and if you've looking for something specific in an image you could actually put that in your prompt that goes along with this and probably get decent results out of that obviously the 34 billion parameter model is going to be the best one uh for this but for a lot of people either you're not going to be able to run that or it's just going to be insanely slow for me on this machine that I'm using I've got uh 32 gig of RAM I can run that so it's not you know you don't need like a super computer to do this but it's definitely slower than the than the 13 billion parameter model that I'm going to be using here now when you send the image there you want to send it along with a prompt and you really want to customize the prompt for your particular use case so if you're just trying to index stuff maybe you have something about describing stuff I'll show you when I go through the code of the prompt that I'm using in here we then bring the results back from the lava one 1.6 model and then I basically just add that to a data frame so one of the things that I did early on was I basically check if there is a CSV file in the folder and if there is I just load it up and then we basically check to see okay has this file already been processed if not we'll process it if this file has been processed we just leave it and go to the next uh image in there but once we get the results back from lava 1.6 we basically put that into the data frame and then finally we just save the data frame out to a CSV file so I'm just showing you a really simple sort of Standalone version of this you could obviously save this to a database you could save this to a vector store which is one of the things I'll talk about at the end you've got a whole wide variety of things that you could do in here and then you end up finally with your CSV file which you could load into Excel or Google Sheets or use it wherever you want to use this kind of thing all right let's jump into the code and have a look at how this actually works okay so I'll go through the code it's pretty simple in here we've got our Imports up first so I'm just bringing in llama and then I'm going to bring in generate which we're going to use for actually generating the return where they going to use glob to basically get a list of the files I'm going to use pandas to make the data frame I'm going to use pill to bring in an image and then I'm going to convert it to bytes so that just shows you what I've got on here so first up we basically try to load a CSV file with the file name that we've got here so I'm calling it image descriptions. csb and if that exists we'll just convert that to a data frame so that we can then add anything that's new and then that will be saved at the end back to this if it doesn't exist we're just going to basically make a new pandas data frame we're going to give it two columns one is going to be the image file one is going to be description in here so when we run that now now we've got our data frame all right so we need to get a list of the files from where we're going to get them so basically here we're just doing a glob of the folder path in this case I'm going for uh PNG files just cuz I know that folder has nothing but PNG files in it but if you were using jpegs or something like that you could change this or you could use it star. star to get any and then put in a check to make sure that it's an image that kind of thing all right so we run that we've then basically got the list out I'm just going to sort the list in this case I'm just going to print out just some sort of debugging we're going to print out the first three images it gets and we're going to print out the head of the data frame if we've got one if we don't have one obviously we'll just see an empty data frame there right so now we come down to the main part of this so what we're going to do is we're going to have a loop where we're going to basically just go through each of the image files in this case I'm just taking the first five we're going to basically check if this image file is in the data frame and if it's in the data frame we're just going to skip it if it's not in the data frame we're going to process the image so this is the main function here the processing the images what this does we pass in the path name to the file and we're just going to print this out to the console so we can see it obviously you could turn these print statements off really easily and then we're going to basically load up that image and convert it to a bytes format so that we can just pass that in we're then going to basically pass that into generate you'll see this full response string I'm going to just set that up there but we're going to pass into generate so we're telling it which model we want now in this case I'm using the lava 13 billion 1.6 uh model but in my case I've got all three of the different sizes in here and if I was just running it as say a Cron job or or some sort of job that got run in the middle of the night or something I could just go for the really big model and I wouldn't worry too much about it taking that long and it doesn't take huge amount of time anyway it's more if your uh system has got enough RAM to actually run it or not and then I've got the prompt so the prompt that I'm passing in here is describe this image and make sure to include anything notable about it and then in Brackets I've got include text in the image so the idea here is that if it sees some text then that's probably going to be one of the most important things now the challenge is going to be that it often won't get that text especially with the smaller models so this is something that you want to be aware of but you can totally play Not only can you but you should totally play with the prompt here for your particular use case so sometimes if I've got it sort of looking for something in particular perhaps you've got it where you're trying to get it to do a not safe for work check or something like that play around with the prompt for those kind of things and be aware that the smaller models are just not going to be very good for certain things all right so that's my prompt I then pass in the image here and this is just taking in a list I'm just passing in the image bytes that we got in there and I'm going to stream the response out here and that is just so that I can see the text coming through and basically print out each response as we go through this and then basically just adding each of those streaming texts into the full response and then finally at the end of this function I'm basically just adding the path name or the image file name and the full response that we got back to the data frame so it's just adding it to a new row in there and then finally at the end of it we're just saving this out back to a CSV file with the same name as what we started with so if we've updated the CSV file we'll just be updating that file if there was no CSV file to start out with we're basically creating a CSV file here all right so let let's come in here I'm going to run it and let's see the sort of time frame that it actually takes to do this so you can see see that basically there was no CSV file in this case and we can see that at the start it is processing this image so it's actually loading up the lava model and then sure enough now we can see that okay it's generating text out here now this is what I mean by the smaller models will not always get the text right so that particular first image there is for Gemini right it's a screenshot of something about Gemini advanced in there and you can see that it's actually got genini so they won't always get perfect OCR or something like that obviously the bigger models will do better at some of this stuff you can get some things that will get quite nice results out here if we look at this one here about the Google logo it's done a quite nice job at sort of being able to interpret that so this is a stylized version of a Google logo it features an abstract Dragon Light creature with Chinese elements so the here has done a really nice job of being able to capture what's in there and actually on this one probably a lot of OCR systems wouldn't get the Google Google in there very well now looking at the results out we can see that okay it's basically done five different images here and and it's had no CSV file to load it's done the five images because I had that set to five there if I come out now we just saved that after changing it now to run through all the images now can actually see that okay already the CSV file has these images done in here so it doesn't need to do those images again and you'll notice that this time it was actually uh quicker to actually get the model because the model was already there and stuff as well in this case so you can run into some issues where if you're trying to load the model twice and you've got it half loaded or something like that in any of those cases you can just quit out of Al llama and come back in and it should work fine or you can actually go through and kill all the oama processes manually but it will often be in the middle of something and then restart a new process so you can see for a bunch of these it really has got the whole sort of IDE IDE the number of images in here of this sort of design and it really does understand that this is like a cad design going through and working that out which will make it quite easy for us to find this either just doing a keyword search in there or to actually use some kind of rag in there as well and you can see at this point it's generating pretty quickly like I've mentioned before I'm not using a super fast Mac here I'm just using a Mac Mini and it's going through and and being able to generate these images out just to show you some of these images one of them was a Walmart receipt that I got from online and I think it's done that this image appears to be a receipt from Walmart a large retail store the receipt list several items okay so that one has done a pretty good job what about the flying cat one this image shows an orange tabby cat in mid jump the front pores are extended out so you can see that it is actually getting some of these quite nicely and then like I mentioned it doesn't need to get them all perfectly for you to be able to then put this into some kind of search or some kind of rag for this how could you extend this and make an advanced version of this so one of the things that I've done to make it Advanced versions of this is where you get also things like the file modification or creation date and store that in the list and then when you put those into rag you can then basically use those as metadata to actually do searches and say want to search for this image it was from December 23 and it just be able to sort of hone down so especially if you've got a lot of images that are going to be very similar from time to time adding in anything that can get uh metadata in there as well can be really good so things like getting the modification date getting the owner or the username of the person who saved it those kind of things can be useful if it's not just for yourself as well but anyway this project gives you a simple example of how you could do this and hopefully you can see that this can be really useful okay so in one of the next videos I think I'll look at how to add in a custom rag with a fully open-source local model so that we can try that as well in here as always if you've got any questions or comments please put them in the comments below if you found the video useful please click and subscribe I'm going to be doing a bunch more things like this I'm currently working on a number of things to try and show people how to do the function calling with some of the open models and and looking at the different results that you get from different open models for doing things like that all right I'll see you in the next video bye for now

Original Description

LLava 1.6 models - https://huggingface.co/liuhaotian Code for this vid - https://github.com/samwit/ollama-tutorials/blob/main/ollama_python_lib/ollama_scshot_annotator.py For more tutorials on using LLMs and building Agents, check out my Patreon: Patreon: https://www.patreon.com/SamWitteveen Twitter: https://twitter.com/Sam_Witteveen 🕵️ Interested in building LLM Agents? Fill out the form below Building LLM Agents Form: https://drp.li/dIMes 👨‍💻Github: https://github.com/samwit/ollama-tutorials https://github.com/samwit/langchain-tutorials (updated) https://git hub.com/samwit/llm-tutorials ⏱️Time Stamps: 00:00 Intro 00:10 Image Captioning 00:00 Basic Idea of the Image Captioning ap 01:32 Image Captioning Diagram 01:41 Step 1: Get the file list from a folder 01:54 Step 2: Loading the files 02:24 Step 3: Send the file to LLaVA 1.6 via Ollama 03:56 Step 4: Saving the results back tothe DataFrame 04:24 Step 5: Save the DataFrame to CSV 04:59 Code Time
Watch on YouTube ↗ (saves to browser)
Sign in to unlock AI tutor explanation · ⚡30

Playlist

Uploads from Sam Witteveen · Sam Witteveen · 0 of 60

← Previous Next →
1 LangChain Basics Tutorial #1 - LLMs & PromptTemplates with Colab
LangChain Basics Tutorial #1 - LLMs & PromptTemplates with Colab
Sam Witteveen
2 LangChain Basics Tutorial #2 Tools and Chains
LangChain Basics Tutorial #2 Tools and Chains
Sam Witteveen
3 ChatGPT API Announcement & Code Walkthrough with LangChain
ChatGPT API Announcement & Code Walkthrough with LangChain
Sam Witteveen
4 Trying Out Flan 20B with UL2 - Working in Colab with 8Bit Inference
Trying Out Flan 20B with UL2 - Working in Colab with 8Bit Inference
Sam Witteveen
5 LangChain - Conversations with Memory (explanation & code walkthrough)
LangChain - Conversations with Memory (explanation & code walkthrough)
Sam Witteveen
6 LangChain Chat with Flan20B
LangChain Chat with Flan20B
Sam Witteveen
7 LangChain - Using Hugging Face Models locally (code walkthrough)
LangChain - Using Hugging Face Models locally (code walkthrough)
Sam Witteveen
8 PAL : Program-aided Language Models with LangChain code
PAL : Program-aided Language Models with LangChain code
Sam Witteveen
9 Building a Summarization System with LangChain and GPT-3 - Part 1
Building a Summarization System with LangChain and GPT-3 - Part 1
Sam Witteveen
10 Building a Summarization System with LangChain and GPT-3 - Part 2
Building a Summarization System with LangChain and GPT-3 - Part 2
Sam Witteveen
11 Microsoft's Visual ChatGPT using LangChain
Microsoft's Visual ChatGPT using LangChain
Sam Witteveen
12 Building a Summarization System with LangChain - Part 3 Using ChatGPT Turbo
Building a Summarization System with LangChain - Part 3 Using ChatGPT Turbo
Sam Witteveen
13 LangChain Agents - Joining Tools and Chains with Decisions
LangChain Agents - Joining Tools and Chains with Decisions
Sam Witteveen
14 Investigating Alpaca 7B - Finetuned LLaMa LLM
Investigating Alpaca 7B - Finetuned LLaMa LLM
Sam Witteveen
15 Comparing LLMs with LangChain
Comparing LLMs with LangChain
Sam Witteveen
16 Running Alpaca7B in Colab
Running Alpaca7B in Colab
Sam Witteveen
17 How to finetune your own Alpaca 7B
How to finetune your own Alpaca 7B
Sam Witteveen
18 How to make a custom dataset like Alpaca7B
How to make a custom dataset like Alpaca7B
Sam Witteveen
19 Understanding Constitutional AI - the paper and key concepts
Understanding Constitutional AI - the paper and key concepts
Sam Witteveen
20 Using Constitutional AI in LangChain
Using Constitutional AI in LangChain
Sam Witteveen
21 Talking to Alpaca with LangChain - Creating an Alpaca Chatbot
Talking to Alpaca with LangChain - Creating an Alpaca Chatbot
Sam Witteveen
22 Text-to-video-synthesis with Diffusers and Colab
Text-to-video-synthesis with Diffusers and Colab
Sam Witteveen
23 Meet Dolly the new Alpaca model
Meet Dolly the new Alpaca model
Sam Witteveen
24 Checking out the Cerebras-GPT family of models
Checking out the Cerebras-GPT family of models
Sam Witteveen
25 A Step-by-Step Guide to Fine-Tuning Your Dolly Model (tutorial)
A Step-by-Step Guide to Fine-Tuning Your Dolly Model (tutorial)
Sam Witteveen
26 Is GPT4All your new personal ChatGPT?
Is GPT4All your new personal ChatGPT?
Sam Witteveen
27 Raven - RWKV-7B RNN's LLM Strikes Back
Raven - RWKV-7B RNN's LLM Strikes Back
Sam Witteveen
28 Talk to your CSV & Excel with LangChain
Talk to your CSV & Excel with LangChain
Sam Witteveen
29 Vicuna - 90% of ChatGPT quality by using a new dataset?
Vicuna - 90% of ChatGPT quality by using a new dataset?
Sam Witteveen
30 Koala Revealed: The ChatGPT Alternative You Need to Know! 🔍
Koala Revealed: The ChatGPT Alternative You Need to Know! 🔍
Sam Witteveen
31 Running Koala for free in Colab. Your own personal ChatGPT? (tutorial)
Running Koala for free in Colab. Your own personal ChatGPT? (tutorial)
Sam Witteveen
32 BabyAGI: Discover the Power of Task-Driven Autonomous Agents!
BabyAGI: Discover the Power of Task-Driven Autonomous Agents!
Sam Witteveen
33 Auto-GPT - How to Automate a Task Based AI with GPT-4
Auto-GPT - How to Automate a Task Based AI with GPT-4
Sam Witteveen
34 Improve your BabyAGI with LangChain
Improve your BabyAGI with LangChain
Sam Witteveen
35 Generative Agents - Deep Dive and GPT-4 Recreation
Generative Agents - Deep Dive and GPT-4 Recreation
Sam Witteveen
36 GPT4ALLv2: The Improvements and Drawbacks You Need to Know!
GPT4ALLv2: The Improvements and Drawbacks You Need to Know!
Sam Witteveen
37 Dolly 2.0 by Databricks: Open for Business but is it  Ready to Impress!
Dolly 2.0 by Databricks: Open for Business but is it Ready to Impress!
Sam Witteveen
38 Red Pajama - Operation: Freeing LLaMA
Red Pajama - Operation: Freeing LLaMA
Sam Witteveen
39 Investigating Open Assistant - Models, Datasets and Addons
Investigating Open Assistant - Models, Datasets and Addons
Sam Witteveen
40 Investigating MiniGPT-4 - The Secret behind GPT-V?
Investigating MiniGPT-4 - The Secret behind GPT-V?
Sam Witteveen
41 Stable LM 3B - The new tiny kid on the block.
Stable LM 3B - The new tiny kid on the block.
Sam Witteveen
42 Bard can now code and put that code in Colab for you.
Bard can now code and put that code in Colab for you.
Sam Witteveen
43 Checking out Bark: a Text to Speech system by Suno AI
Checking out Bark: a Text to Speech system by Suno AI
Sam Witteveen
44 Fine-tuning LLMs with PEFT and LoRA
Fine-tuning LLMs with PEFT and LoRA
Sam Witteveen
45 Master PDF Chat with LangChain - Your essential guide to queries on documents
Master PDF Chat with LangChain - Your essential guide to queries on documents
Sam Witteveen
46 Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools
Using LangChain with DuckDuckGO Wikipedia & PythonREPL Tools
Sam Witteveen
47 Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)
Building Custom Tools and Agents with LangChain (gpt-3.5-turbo)
Sam Witteveen
48 StableVicuna: The New King of Open ChatGPTs?
StableVicuna: The New King of Open ChatGPTs?
Sam Witteveen
49 WizardLM: Evolving Instruction Datasets to Create a Better Model
WizardLM: Evolving Instruction Datasets to Create a Better Model
Sam Witteveen
50 LaMini-LM - Mini Models Maxi Data!
LaMini-LM - Mini Models Maxi Data!
Sam Witteveen
51 Finding the Best Free ChatGPT
Finding the Best Free ChatGPT
Sam Witteveen
52 MPT-7B - The First Commercially Usable Fully Trained LLaMA Style Model
MPT-7B - The First Commercially Usable Fully Trained LLaMA Style Model
Sam Witteveen
53 LangChain Retrieval QA Over Multiple Files with ChromaDB
LangChain Retrieval QA Over Multiple Files with ChromaDB
Sam Witteveen
54 LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs
LangChain Retrieval QA with Instructor Embeddings & ChromaDB for PDFs
Sam Witteveen
55 LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!!
LangChain + Retrieval Local LLMs for Retrieval QA - No OpenAI!!!
Sam Witteveen
56 Transformers Agent - Is this Hugging Face's LangChain Competitor?
Transformers Agent - Is this Hugging Face's LangChain Competitor?
Sam Witteveen
57 StarCoder - The LLM to make you a coding star?
StarCoder - The LLM to make you a coding star?
Sam Witteveen
58 Testing Starcoder for Reasoning with PAL
Testing Starcoder for Reasoning with PAL
Sam Witteveen
59 The New Wizards - Unfiltered & Unaligned
The New Wizards - Unfiltered & Unaligned
Sam Witteveen
60 Camel + LangChain for Synthetic Data & Market Research
Camel + LangChain for Synthetic Data & Market Research
Sam Witteveen

This video tutorial demonstrates how to use LLava and Ollama with LLaMA 1.6 models for image annotation, covering topics such as image description, OCR, and prompt engineering. The tutorial provides a comprehensive overview of the tools and techniques used in image annotation, including the use of different parameter models and customization of prompts. By following this tutorial, viewers can learn how to build effective image annotation models and develop their skills in LLM foundations, engine

Key Takeaways
  1. Build a folder for screenshots
  2. Get a list of files in the folder
  3. Sort files in order
  4. Load up a file in bytes
  5. Send file to LLaMA 1.6 model
  6. Load CSV file and convert to data frame
  7. Get list of image files using glob
  8. Check if image file is in data frame and process it if not
  9. Print image file path and process it
  10. Save data frame to CSV file
💡 The use of LLaMA 1.6 models with LLava and Ollama enables effective image annotation, and customization of prompts and incorporation of OCR can improve the accuracy of image descriptions.

Related Reads

📰
Hallucination vs Confabulation: Why LLMs Invent Answers Instead of Saying “I Don’t Know”
Learn why LLMs invent answers instead of saying 'I don't know' and understand the difference between hallucination and confabulation in AI models
Medium · AI
📰
Explaining how artificial intelligence works to a 10 year old
Learn how to explain AI to a 10-year-old and understand its basics
Medium · AI
📰
I Fed an Entire YouTube Channel Into an LLM (200 Videos, ~550k Tokens) 🥊
Learn how to feed an entire YouTube channel into an LLM and analyze the results, exploring the capabilities and limitations of AI models
Dev.to · David
📰
Building Production-Grade LLM Evaluation Pipelines: From Vibes to Metrics
Learn to build production-grade LLM evaluation pipelines, moving from subjective vibes to objective metrics
Dev.to · Imus

Chapters (10)

Intro
0:10 Image Captioning
Basic Idea of the Image Captioning ap
1:32 Image Captioning Diagram
1:41 Step 1: Get the file list from a folder
1:54 Step 2: Loading the files
2:24 Step 3: Send the file to LLaVA 1.6 via Ollama
3:56 Step 4: Saving the results back tothe DataFrame
4:24 Step 5: Save the DataFrame to CSV
4:59 Code Time
Up next
5 Levels of AI Agents - From Simple LLM Calls to Multi-Agent Systems
Dave Ebbelaar (LLM Eng)
Watch →