-
Notifications
You must be signed in to change notification settings - Fork 0
/
renew_weight.py
56 lines (43 loc) · 1.89 KB
/
renew_weight.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
from transformers import LlavaNextProcessor, LlavaNextForConditionalGeneration,GenerationConfig
import torch
from PIL import Image
import requests
processor = LlavaNextProcessor.from_pretrained('/root/model_playground/cache/qwen2_5vl_8B')
# print(processor.chat_template)
model = LlavaNextForConditionalGeneration.from_pretrained(r'C:\Users\lenov\PycharmProjects\DRL\llm\cache\checkpoints\SFT\checkpoint-750', torch_dtype=torch.bfloat16, low_cpu_mem_usage=True)
model.to("cuda:0")
# prepare image and text prompt, using the appropriate prompt template
# url = "http://images.cocodataset.org/val2017/000000039769.jpg"
# image = Image.open(requests.get(url, stream=True).raw)
from datasets import load_dataset
_dataset=load_dataset(path=r"C:\Users\lenov\.cache\modelscope\hub\datasets\coco_2014_caption\coco_2014_caption",split='train[:20]')
example=_dataset[0]
image=example['image']
conversation = [
{
"role": "system",
"content": [
# {"type": "image"},
{"type": "text", "text": "please recognise the information in the picture"},
],
},
{
"role": "user",
"content": [
{"type": "image"},
{"type": "text", "text": "What is shown in this image?"},
],
},
]
prompt = processor.apply_chat_template(conversation, add_generation_prompt=True)
print(prompt)
inputs = processor(image, prompt, return_tensors="pt").to("cuda:0")
# print(inputs['attention_mask'],inputs['input_ids'])
# # autoregressively complete prompt
output = model.generate(inputs['input_ids'],
# attention_mask=inputs['attention_mask'],
max_new_tokens=100)
# print(output[0])
print(processor.decode(output[0], skip_special_tokens=False))
model.save_pretrained(r'C:\Users\lenov\PycharmProjects\DRL\llm\cache\qwen2_5vl_8B')
processor.save_pretrained(r'C:\Users\lenov\PycharmProjects\DRL\llm\cache\qwen2_5vl_8B')