Views
No views yet
git clone https://github.com/BigData-KSU/RS-LLaVA.git
cd RS-LLaVAconda create -n rs-llava python=3.10 -y
conda activate rs-llava
pip install --upgrade pip # enable PEP 660 supportpip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/cu118
pip install transformers==4.35
pip install einops
pip inastall SentencePiece
pip install accelerate
pip install peft1
2import torch
3import os
4from llava.constants import IMAGE_TOKEN_INDEX, DEFAULT_IMAGE_TOKEN, DEFAULT_IM_START_TOKEN, DEFAULT_IM_END_TOKEN
5from llava.conversation import conv_templates, SeparatorStyle
6from llava.model.builder import load_pretrained_model
7from llava.utils import disable_torch_init
8from llava.mm_utils import tokenizer_image_token, get_model_name_from_path, KeywordsStoppingCriteria
9from PIL import Image
10import math
11
12######## model here.................
13model_path = 'BigData-KSU/RS-llava-v1.5-7b-LoRA'
14
15model_base = 'Intel/neural-chat-7b-v3-3'
16
17#### Further instrcutions here..........
18conv_mode = 'llava_v1'
19disable_torch_init()
20
21model_name = get_model_name_from_path(model_path)
22print('model name', model_name)
23print('model base', model_base)
24
25
26tokenizer, model, image_processor, context_len = load_pretrained_model(model_path, model_base, model_name)
27
28
29def chat_with_RS_LLaVA(cur_prompt,image_name):
30 # Prepare the input text, adding image-related tokens if needed
31 image_mem = Image.open(image_name)
32 image_tensor = image_processor.preprocess(image_mem, return_tensors='pt')['pixel_values'][0]
33
34 if model.config.mm_use_im_start_end:
35 cur_prompt = f"{DEFAULT_IM_START_TOKEN} {DEFAULT_IMAGE_TOKEN} {DEFAULT_IM_END_TOKEN}\n{cur_prompt}"
36 else:
37 cur_prompt = f"{DEFAULT_IMAGE_TOKEN}\n{cur_prompt}"
38
39 # Create a copy of the conversation template
40 conv = conv_templates[conv_mode].copy()
41 conv.append_message(conv.roles[0], cur_prompt)
42 conv.append_message(conv.roles[1], None)
43 prompt = conv.get_prompt()
44
45 # Process image inputs if provided
46 input_ids = tokenizer_image_token(prompt, tokenizer, IMAGE_TOKEN_INDEX, return_tensors='pt').unsqueeze(0) .cuda()
47 stop_str = conv.sep if conv.sep_style != SeparatorStyle.TWO else conv.sep2
48 keywords = [stop_str]
49 stopping_criteria = KeywordsStoppingCriteria(keywords, tokenizer, input_ids)
50
51 with torch.inference_mode():
52 output_ids = model.generate(
53 input_ids,
54 images=image_tensor.unsqueeze(0).half().cuda(),
55 do_sample=True,
56 temperature=0.2,
57 top_p=None,
58 num_beams=1,
59 no_repeat_ngram_size=3,
60 max_new_tokens=2048,
61 use_cache=True)
62
63 input_token_len = input_ids.shape[1]
64 n_diff_input_output = (input_ids != output_ids[:, :input_token_len]).sum().item()
65 if n_diff_input_output > 0:
66 print(f'[Warning] {n_diff_input_output} output_ids are not the same as the input_ids')
67 outputs = tokenizer.batch_decode(output_ids[:, input_token_len:], skip_special_tokens=True)[0]
68 outputs = outputs.strip()
69
70 return outputs
71
72
73if __name__ == "__main__":
74
75
76 print('Model input...............')
77 cur_prompt='Generate three questions and answers about the content of this image. Then, compile a summary.'
78 image_name='assets/example_images/parking_lot_010.jpg'
79
80
81 outputs=chat_with_RS_LLaVA(cur_prompt,image_name)
82 print('Model Response.....')
83 print(outputs)
84
851@Article{rs16091477,
2AUTHOR = {Bazi, Yakoub and Bashmal, Laila and Al Rahhal, Mohamad Mahmoud and Ricci, Riccardo and Melgani, Farid},
3TITLE = {RS-LLaVA: A Large Vision-Language Model for Joint Captioning and Question Answering in Remote Sensing Imagery},
4JOURNAL = {Remote Sensing},
5VOLUME = {16},
6YEAR = {2024},
7NUMBER = {9},
8ARTICLE-NUMBER = {1477},
9URL = {https://www.mdpi.com/2072-4292/16/9/1477},
10ISSN = {2072-4292},
11DOI = {10.3390/rs16091477}
12}
13