Views
No views yet
1# other transformers version may also work, but we have not tested
2pip install transformers==4.46 accelerate opencv-python torchvision einops pillow
3pip install git+https://github.com/bfshi/scaling_on_scales.git1from transformers import AutoConfig, AutoModel
2from termcolor import colored
3
4model_path = "Efficient-Large-Model/NVILA-Lite-2B-hf-preview"
5
6# you can use config
7config = AutoConfig.from_pretrained(model_path, trust_remote_code=True)
8model = AutoModel.from_config(config, trust_remote_code=True)
9# or directly from_pretrained
10model = AutoModel.from_pretrained(model_path, trust_remote_code=True, device_map="auto")
11
12# examples generate with raw text
13res = model.generate_content([
14 "how are you today?"
15])
16print(colored(res, "cyan", attrs=["bold"]))
17
18print("---" * 40)
19
20# examples generate with text + image
21import PIL.Image
22response = model.generate_content([
23 PIL.Image.open("inference_test/test_data/caption_meat.jpeg"),
24 "describe the image?"
25])
26print(colored(response, "cyan", attrs=["bold"]))AutoProcessor class to ease data preparation for training and finetuning.1from transformers import AutoProcessor, AutoModel
2
3model_path = "Efficient-Large-Model/NVILA-Lite-2B-hf-preview"
4processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
5model = AutoModel.from_pretrained(model_path, trust_remote_code=True, device_map="auto")
6# important: set model to eval mode, otherwise the model will be in training mode and will pad to right.
7model.eval()
8
9gpt_conv = [{
10 "role": "user",
11 "content": [
12 {"type": "image", "path": "https://nvlabs.github.io/VILA/asset/example.jpg"},
13 {"type": "text", "text": "Describe this image."}
14 ]
15}]
16text = processor.apply_chat_template(gpt_conv, tokenize=False, add_generation_prompt=True)
17inputs = processor([text])
18
19output_ids = model.generate(
20 input_ids=inputs.input_ids,
21 media=inputs.media,
22 media_config=inputs.media_config,
23 generation_config=model.generation_config,
24 max_new_tokens=256,
25)
26print(processor.tokenizer.batch_decode(output_ids, skip_special_tokens=True))
27
28##### the above code is equivalent to
29# response = model.generate_content([
30# PIL.Image.open("demo_images/demo_img_1.png"),
31# "describe the image?"
32# ])
33# print(colored(response, "cyan", attrs=["bold"]))1from transformers import AutoProcessor, AutoModel
2
3model_path = "Efficient-Large-Model/NVILA-Lite-2B-hf-preview"
4model_path = "./NVILA-Lite-2B-hf-preview"
5processor = AutoProcessor.from_pretrained(model_path, trust_remote_code=True)
6model = AutoModel.from_pretrained(model_path, trust_remote_code=True, device_map="auto")
7# important: set model to eval mode, otherwise the model will be in training mode and will pad to right.
8model.eval()
9
10gpt_conv1 = [{
11 "role": "user",
12 "content": [
13 {"type": "image", "path": "https://nvlabs.github.io/VILA/asset/example.jpg"},
14 {"type": "text", "text": "Describe this image."}
15 ]
16}]
17gpt_conv2 = [{
18 "role": "user",
19 "content": [
20 {"type": "image", "path": "https://nvlabs.github.io/VILA/asset/example_vqa.jpg"},
21 {"type": "text", "text": "Describe this image for me. Provide a detailed description of the image."}
22 ]
23}]
24
25messages = [gpt_conv1, gpt_conv2]
26texts = [
27 processor.apply_chat_template(msg, tokenize=False, add_generation_prompt=True)
28 for msg in messages
29]
30inputs = processor(texts)
31
32output_ids = model.generate(
33 input_ids=inputs.input_ids,
34 media=inputs.media,
35 media_config=inputs.media_config,
36 generation_config=model.generation_config,
37 max_new_tokens=256,
38)
39output_texts = processor.tokenizer.batch_decode(output_ids, skip_special_tokens=True)
40print(output_texts[0])
41print("---" * 40)
42print(output_texts[1])1import os, os.path as osp
2from transformers import AutoConfig, AutoModel, AutoProcessor, AutoTokenizer, AutoImageProcessor
3
4model_path = "Efficient-Large-Model/NVILA-Lite-2B"
5output_dir = "NVILA-Lite-2B-hf-preview"
6
7if osp.isdir(output_dir):
8 shutil.rmtree(output_dir)
9from llava.remote_code.modeling_vila import VILAForCasualLM
10VILAForCasualLM.convert_vila_dev_ckpt_to_remote(model_path, output_dir, copy=False)