Welcome to the exciting world of nanoLLaVA, a small but extremely powerful 1B vision-language model designed specifically to run efficiently on edge devices. In this blog, we will guide you through how to set up and utilize this fantastic model.
What is nanoLLaVA?
nanoLLaVA is a vision-language model that combines the capability of understanding images and generating text. It effectively bridges visual information with linguistic understanding, making it versatile for various applications such as image captioning and answering questions based on pictures.
Getting Started with nanoLLaVA
To use nanoLLaVA, you will need to install a few libraries and set up a script. Below is a step-by-step guide to get you started:
Step 1: Install Dependencies
Before implementing the model, ensure you have the necessary packages installed. You can do this by running:
bash
pip install -U transformers accelerate flash_attn
Step 2: Import Libraries
Your Python script will need to import several libraries. Here’s how to do it:
python
import torch
import transformers
from transformers import AutoModelForCausalLM, AutoTokenizer
from PIL import Image
import warnings
# Disable some warnings
transformers.logging.set_verbosity_error()
transformers.logging.disable_progress_bar()
warnings.filterwarnings("ignore")
Step 3: Device Configuration
Ensure you are using your device optimally, either utilizing a GPU or the CPU. Here’s how:
python
# Set device
torch.set_default_device("cuda") # or "cpu"
Step 4: Load the Model and Tokenizer
Next, load the model and tokenizer used for generating texts based on images:
python
# Create model
model = AutoModelForCausalLM.from_pretrained(
"qnguyen3/nanoLLaVA",
torch_dtype=torch.float16,
device_map="auto",
trust_remote_code=True
)
tokenizer = AutoTokenizer.from_pretrained(
"qnguyen3/nanoLLaVA",
trust_remote_code=True
)
Step 5: Prepare Your Input
Set up your image and text prompt for the model. For example:
python
# Text prompt
prompt = "Describe this image in detail"
messages = [{"role": "user", "content": prompt}]
text = tokenizer.apply_chat_template(messages, tokenize=False, add_generation_prompt=True)
# Prepare image
image = Image.open("path/to/image.png")
image_tensor = model.process_images([image], model.config).to(dtype=model.dtype)
Step 6: Generate Output
Finally, generate the output based on the processed image and prompt:
python
# Generate output
output_ids = model.generate(
input_ids,
images=image_tensor,
max_new_tokens=2048,
use_cache=True
)[0]
print(tokenizer.decode(output_ids[input_ids.shape[1]:], skip_special_tokens=True).strip())
Understanding the Code: An Analogy
Think of using the nanoLLaVA model like preparing a dish with various ingredients:
- Ingredients (Libraries): Just like you need specific ingredients for your dish, you need to import various libraries like PyTorch and transformers.
- Prep Work (Device Setup): Before cooking, you set up your kitchen (CPU or GPU) to ensure you have the right environment to work in.
- Cooking (Model Loading): Loading the model and tokenizer is akin to preparing your main ingredient for cooking.
- Combining Elements (Input Preparation): Just as you combine your ingredients, you prepare your image and prompt to interact with the model.
- Serving (Generate Output): Finally, generating the output is like serving the dish you’ve created for others to enjoy!
Troubleshooting
If you encounter issues while setting up or using nanoLLaVA, here are some tips:
- Common Errors: If you run into import errors, ensure all required libraries are installed.
- Device Issues: If your device settings cause problems, verify whether you have access to a GPU or if your device configuration is correct.
- Model Doesn’t Generate Output: Check to ensure that the input image path is correct and that the model and tokenizer are loaded properly.
For more insights, updates, or to collaborate on AI development projects, stay connected with fxis.ai.
Conclusion
By following these instructions, you will now be well-equipped to deploy and explore the capabilities of nanoLLaVA! 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.

